解题思路: 本题是一道很简单的结构体基本操作的问题,需要注意实现各个功能的代码逻辑问题
自定义函数:
①创建链表(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 人评分
这可能是一个假的冒泡法浏览:1071 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:783 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:563 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:603 |
WU-整除问题 (C++代码)浏览:648 |
C语言程序设计教程(第三版)课后习题9.2 (C语言代码)浏览:573 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:541 |
企业奖金发放 (C语言代码)浏览:2459 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:456 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:1287 |