原题链接:数据结构-链表的基本操作
解题思路: 本题是一道很简单的结构体基本操作的问题,需要注意实现各个功能的代码逻辑问题
自定义函数:
①创建链表(createList)
②创建节点(createNode)
③插入节点(注意输入的顺序与列表中的顺序相反,所以使用头插法)
void insertNodeByhead(Node* headNode, int num) { Node* newNode = createNode(num); newNode -> next = headNode -> next; headNode -> next = newNode; }
④自定义get函数
void get(Node* headNode, int a) //获得第a个元素 { Node* p = headNode -> next; for(int i = 1; i < a; i ++) //将头节点的下一个节点的元素为第一个元素(因为头节点无数据) { p = p -> next; if(p == NULL) //当想要获取的节点越界,则获取失败 { printf("get fail\n"); return; } } if(p != NULL && a >= 1) printf("%d\n", p -> num); else printf("get fail\n"); //a<=0时,无法获取到元素 return; }
⑤自定义delete函数
void deleteNode(Node* headNode, int a)//删除第a个元素 { Node* p = headNode -> next; Node* pFront = headNode; for(int i = 1; i < a; i ++) { pFront = p; if(p == NULL) //想要删除的目的节点越界,则删除失败 { printf("delete fail\n"); return; } p = p -> next; } if(p != NULL) { pFront -> next = p -> next; free(p); printf("delete OK\n"); } else printf("delete fail\n"); return; }
⑥自定义insert函数
void insert(Node* headNode, int a, int b) //第a个位置前面插入b { Node* newNode = createNode(b); Node* p = headNode; int n = 0; //链表长度计数器 while(p && n < a - 1) { p = p -> next; ++ n; } if(!p || n > a - 1) //n小于1或大于表长,插入失败 { printf("insert fail\n"); } else { newNode -> next = p -> next; p -> next = newNode; printf("insert OK\n"); } return; }
⑦自定义show函数
void show(Node* headNode)//输出链表 { Node* p = headNode -> next; if (headNode -> next != NULL) { while(p != NULL) { printf("%d ", p -> num); p = p -> next; } printf("\n"); } else printf("Link list is empty\n"); //return; }
注意事项:
①插入节点(insertNodeByhand函数):注意输入的顺序与列表中的顺序相反,所以使用头插法
②在某节点前插入元素(insert函数):判断插入位置是否超过链表长度,这里就用到了计数器
参考代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Node{ int num; struct Node* next; }Node; Node* createList() { Node* headNode = (Node*)malloc(sizeof(Node)); if(headNode == NULL) { printf("申请内存失败!\n"); exit(0); } headNode -> next = NULL; return headNode; } Node* createNode(int num) { Node* newNode = (Node*)malloc(sizeof(Node)); if(newNode == NULL) { printf("申请内存失败!\n"); exit(0); } newNode -> num = num; newNode -> next = NULL; return newNode; } void insertNodeByhead(Node* headNode, int num) { Node* newNode = createNode(num); newNode -> next = headNode -> next; headNode -> next = newNode; } void get(Node* headNode, int a) //获得第a个元素 { Node* p = headNode -> next; for(int i = 1; i < a; i ++) { p = p -> next; if(p == NULL) { printf("get fail\n"); return; } } if(p != NULL && a >= 1) printf("%d\n", p -> num); else printf("get fail\n"); return; } void deleteNode(Node* headNode, int a)//删除第a个元素 { Node* p = headNode -> next; Node* pFront = headNode; for(int i = 1; i < a; i ++) { pFront = p; if(p == NULL) { printf("delete fail\n"); return; } p = p -> next; } if(p != NULL) { pFront -> next = p -> next; free(p); printf("delete OK\n"); } else printf("delete fail\n"); return; } void insert(Node* headNode, int a, int b)//第a个位置前面插入b { Node* newNode = createNode(b); Node* p = headNode; int n = 0; //链表长度计数器 while(p && n < a - 1) { p = p -> next; ++ n; } if(!p || n > a - 1) //n小于1或大于表长,插入失败 { printf("insert fail\n"); } else { newNode -> next = p -> next; p -> next = newNode; printf("insert OK\n"); } return; } void show(Node* headNode)//输出链表 { Node* p = headNode -> next; if (headNode -> next != NULL) { while(p != NULL) { printf("%d ", p -> num); p = p -> next; } printf("\n"); } else printf("Link list is empty\n"); //return; } int main() { Node* list = createList(); int n = 0, num = 0; scanf("%d", &n); for(int i = 0; i < n; i ++) { scanf("%d", &num); insertNodeByhead(list, num); } int count = 0; //执行语句的行数 scanf("%d", &count); while(count --) { int a = 0, b = 0; char str[10]; scanf("%s", str); if(!strcmp(str, "insert")) { scanf("%d %d", &a, &b); insert(list, a, b); } else if(!strcmp(str, "get")) { scanf("%d", &a); get(list, a); } else if(!strcmp(str, "delete")) { scanf("%d", &a); deleteNode(list, a); } else if(!strcmp(str, "show")) { show(list); } } return 0; }
0.0分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复