首页 > 代码库 > C语言链表
C语言链表
#define _CRT_SECURE_NO_WARNINGS #include "stdio.h" #include "stdlib.h" typedef struct _Teacher { int age; struct _Teacher *next; }Teacher; Teacher* create_teacher(); int print_teacher(Teacher * pHeader); int add_teacher(Teacher * pHeader, int x, int y); int del_teacher(Teacher *pHeader, int x); int reverse_teacher(Teacher *pHeader); int free_teacher(Teacher *pHeader); //创建链表 Teacher* create_teacher() { Teacher* pHeader = (Teacher*)malloc(sizeof(Teacher)); if (pHeader == NULL) { printf("err:malloc pheader err"); return NULL; } Teacher* pCur = NULL; pCur = pHeader; pHeader->age = 0; pHeader->next = NULL; int i = 0; while (1) { printf("please enter your age:"); scanf("%d", &i); if (i == -1) { break; } Teacher* temp = (Teacher*)malloc(sizeof(Teacher)); if (temp == NULL) { free_teacher(pHeader); return -1; } temp->age = i; temp->next = NULL; pCur->next = temp; pCur = temp; } return pHeader; } //读取链表 int print_teacher(Teacher * pHeader) { if (pHeader == NULL) { printf("err print_teacher:pHeader=NULL"); return -1; } Teacher * pCur = pHeader->next; while (pCur) { printf("%d ", pCur->age); pCur = pCur->next; } } //在age=x前面插入y,没有则插入到最后 int add_teacher(Teacher * pHeader, int x, int y) { if (pHeader == NULL) { printf("err add_teacher:pHeader=NULL"); return -1; } Teacher * pM = (Teacher *)malloc(sizeof(Teacher)); if (pM == NULL) { free_teacher(pHeader); return -1; } pM->age = y; Teacher *pPre = NULL, *pCur = NULL; pPre = pHeader; pCur = pPre->next; while (pCur != NULL) { if (pCur->age == x) { break; } pPre = pCur; pCur = pCur->next; } pM->next = pCur; pPre->next = pM; } //删除链表节点 int del_teacher(Teacher *pHeader, int x) { if (pHeader == NULL) { printf("err del_teacher:pheader==NULL"); return -1; } Teacher *delT = NULL; Teacher *pPre = pHeader; Teacher *pCur = pHeader->next; while (pCur != NULL) { if (pCur->age == x) { delT = pCur; break; } pPre = pCur; pCur = pCur->next; } if (delT == NULL) { printf("\nnot found age=%d\n", x); return 0; } pPre->next = pCur->next; free(delT); return 0; } //释放内存 int free_teacher(Teacher *pHeader) { Teacher *pCur = pHeader; Teacher *next = pCur->next; while (pCur != NULL) { next = pCur->next; free(pCur); pCur = next; } return 0; } //逆向链表 int reverse_teacher(Teacher *pHeader) { if (pHeader == NULL) { printf("err del_teacher:pheader==NULL"); return -1; } if (pHeader->next == NULL || pHeader->next->next == NULL) { return 0; } Teacher * pPre = pHeader->next, *pCur = pHeader->next->next,*temp =NULL; pPre->next = NULL; while (pCur != NULL) { temp = pCur->next; pCur->next = pPre; pPre = pCur; pCur = temp; } pHeader->next = pPre; return 0; } void main() { Teacher * pHeader = NULL; pHeader = create_teacher(); int res = 0; if (pHeader == NULL) { printf("err create_teacher"); return; } //res = print_teacher(pHeader); //res = add_teacher(pHeader, 20, 19); //res = print_teacher(pHeader); //res = del_teacher(pHeader, 20); //res = print_teacher(pHeader); res =reverse_teacher(pHeader); res = print_teacher(pHeader); res = free_teacher(pHeader); system("pause"); }
C语言链表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。