#include<stdio.h> #include<stdlib.h> typedef int ElemType; struct Node{ Node *next; ElemType data; }; void CreateList(Node*&L) { L = (Node*)malloc(sizeof(Node)); if(!L) exit(-2); L->next = NULL; L->data = 0; } void InsertList(Node*&L,ElemType e) { Node *p = L; for(int i=0;i<L->data;++i) { p=p->next; } Node *s = NULL; s=(Node*)malloc(sizeof(Node)); s->data = e; s->next = p->next; p->next = s; ++(L->data); } void DeleteList(Node*&L,int n) { Node*p = L; if(n<1||n>L->data) exit(-2); for(int i = 0;i<n-1;i++) { p=p->next; } Node*temp = p->next; p->next = p->next->next; free(temp); } void ShowList(Node*&L) { Node *p = L; while(p){ printf(">%d\t",(p)->data); p=p->next; } printf("\n"); } int main() { Node *N1; CreateList(N1); InsertList(N1,1); ShowList(N1); InsertList(N1,2); ShowList(N1); InsertList(N1,3); ShowList(N1); InsertList(N1,4); ShowList(N1); InsertList(N1,5); ShowList(N1); DeleteList(N1,4); ShowList(N1); DeleteList(N1,2); ShowList(N1); }
时间: 2024-12-28 20:58:11