#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"); }
时间: 2024-11-05 14:55:46