1 #include<stdio.h> 2 #include<stdlib.h> 3 typedef char datetype;/*定义新的数据类型名*/ 4 typedef struct node 5 { 6 datetype date; 7 struct node *next; 8 }listnode; 9 typedef listnode *linklist; 10 int delete(linklist h,int num)/*删除结点*/ 11 { 12 linklist p=h; 13 listnode *q=NULL; 14 int i=num; 15 int n=1; 16 while(n<i)/*寻找删除位置*/ 17 { 18 q=p;/*该结点和前后两个结点*/ 19 p=p->next; 20 n++; 21 } 22 if(p==NULL)/*该结点不存在*/ 23 printf("No Found Node!"); 24 else 25 { 26 q->next=p->next; 27 free(p); 28 } 29 } 30 31 void output(linklist head)/*链表遍历*/ 32 { 33 linklist p=head; 34 while(p!=NULL) 35 { 36 printf("%c",p->date); 37 p=p->next; 38 } 39 } 40 int rear_creat(linklist head,int index0,int m)/*插入新结点,链表头,结点位置,结点date*/ 41 { 42 linklist k,g; 43 int i=index0,n; 44 k=head; 45 n=1; 46 while(n<i)/*寻找结点位置*/ 47 { 48 n++; 49 k=k->next; 50 } 51 g=(listnode *)malloc(sizeof(listnode)); 52 g->next=k->next; 53 g->date=m; 54 k->next=g; 55 } 56 int main() 57 { 58 char ch; 59 int index,index0; 60 char m; 61 linklist head=NULL; 62 listnode *p,*r; 63 ch=getchar(); 64 while(ch!=‘\n‘) 65 { 66 p=(listnode *)malloc(sizeof(listnode)); 67 p->date=ch; 68 if(head==NULL) 69 head=p; 70 else 71 r->next=p; 72 r=p; 73 ch=getchar(); 74 } 75 output(head); 76 printf("\ndelete a node:\n"); 77 scanf("%d",&index); 78 if(index==1) 79 head=head->next; 80 else 81 delete(head,index); 82 output(head); 83 84 printf("\ncreat a node:\n"); 85 scanf("%d%c",&index0,&m); 86 rear_creat(head,index0,m); 87 output(head); 88 return 0; 89 }
时间: 2024-10-16 02:27:48