#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Node) struct Node { int data; struct Node* Next; }; typedef struct Node *PtrToNode; typedef PtrToNode List;//表 typedef PtrToNode Position;//位置 List ReCreatTable(int n);//逆序建立单链表 void PrintTable(Position head);//遍历单链表 void Insert5th(Position head);//在单链表第五个元素前插入一个值为999的元素 void Delete5th(Position head);//删除单链表第五个元素 List ReCreatTable(int n) { int i; Position p, head; head = (Position)malloc(LEN); head->Next = NULL; for (i = 1; i <= n; i++) { p = (Position)malloc(LEN); printf("请输入元素:"); scanf("%d", &p->data); p->Next = head->Next; head->Next = p; } return (head); } void PrintTable(Position head) { Position s; s = head->Next; printf("链表:"); while (s != NULL) { if (s->Next == NULL) printf("%d\n", s->data); else printf("%d ", s->data); s = s->Next; } } void Insert5th(Position head) { Position P, TmpCell; TmpCell = (Position)malloc(LEN); P = head; int i; for (i = 0; i < 4; i++) { P = P->Next; } if (TmpCell == NULL) printf("Out of space!!!"); TmpCell->data = 999; TmpCell->Next = P->Next; P->Next = TmpCell; } void Delete5th(Position head) { Position P, TmpCell;//TemCell是删除元素 P = head; int i; for (i = 0; i < 4; i++) { P = P->Next; TmpCell = P->Next; } P->Next = TmpCell->Next; free(TmpCell); } int main() { int n; Position head; printf("请输入链表元素个数:"); scanf("%d", &n); head = ReCreatTable(n); PrintTable(head); printf("在第五个元素前插入999后"); Insert5th(head); PrintTable(head); printf("删除第五个元素后"); Delete5th(head); PrintTable(head); return 0; }