21000929ad


私信TA

用户名:21000929ad

访问量:728

签 名:

等  级
排  名 6270
经  验 1381
参赛次数 0
文章发表 16
年  龄 0
在职情况 学生
学  校 广东工业大学
专  业

  自我简介:

TA的其他文章

解题思路:

注意事项:

参考代码:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct Node

{

int data;

struct Node* next;

};


struct Node* creatlist()

{

struct Node* headnode = (struct Node*)malloc(sizeof(struct Node));

headnode->next = NULL;

return headnode;

}


struct Node* creatnode(int data)

{

struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));

newnode->data = data;

newnode->next = NULL;

return newnode;

}

void printlist(struct Node* head)

{

struct Node* pmove = head->next;

if (pmove == NULL)

printf("Link list is empty\n");

while (pmove)

{

printf("%d ", pmove->data);

pmove = pmove->next;

if(!pmove)

printf("\n");

}

}


void insertneed(struct Node* head, int a, int b)

{

int cnt = 1,i;

struct Node* newnode = creatnode(b);

struct Node* postnodeFront = head;

struct Node* posnode = head->next;

if (!posnode&&a>1)

{

printf("insert fail\n");

return;

}

if (a < 1)

{

printf("insert fail\n");

return;

}

while (cnt != a)

{

postnodeFront = posnode;

if (posnode == NULL)

return;

posnode = posnode->next;

cnt++;

}

if (a > cnt)

{

printf("insert fail\n");

return;

}

postnodeFront->next = newnode;

newnode->next = posnode;

printf("insert OK\n");

}


void deletenode(struct Node* head, int data)

{

int cnt = 1;

struct Node* postnodeFront = head;

struct Node* postnode = head->next;

if (postnode == NULL)

{

printf("delete fail\n");

return;

}

while (cnt != data)

{

postnodeFront = postnode;

postnode = postnodeFront->next;

cnt++;

}

if (cnt == data)

{

postnodeFront->next = postnode->next;

printf("delete OK\n");

}

else

{

printf("delete fail\n");

return;

}

}


void getlist(struct Node* head1, int data)

{

struct Node* posnode = head1->next;

int cnt = 1;

if (posnode == NULL)

{

printf("get fail\n");

}

else if (data == 1)

printf("%d\n", posnode->data);

while (cnt!=data)

{

cnt++;

posnode = posnode->next;

if (posnode == NULL)

{

printf("get fail\n");

break;

}

if (cnt == data)

printf("%d\n", posnode->data);

}

}



void insertheadby(struct Node* head, int data)

{

struct Node* newNode = creatnode(data);

if (head->next ==NULL)

head->next = newNode;

else 

{

newNode->next = head->next;

head->next = newNode;

}

}

int main()

{

struct Node* head1 = creatlist();

int n,i,data,num1,num2;

scanf("%d", &n);

for (i = 0; i < n; i++)

{

scanf("%d", &data);

insertheadby(head1, data);

}

scanf("%d", &n);

char str[100];

for (int i = 0; i < n; i++)

{

scanf("%s", &str);

if (!strcmp(str, "show"))

{

printlist(head1);

continue;

}

if (!strcmp(str, "delete"))

{

scanf("%d", &num1);

deletenode(head1, num1);

continue;

}

if (!strcmp(str, "insert"))

{

scanf("%d%d", &num1, &num2);

insertneed(head1, num1, num2);

continue;

}

if (!strcmp(str, "get"))

{

scanf("%d", &num1);

getlist(head1, num1);

continue;

}

}

return 0;

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区