c链表结点的删除和添加

 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!='
')
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("
delete a node:
");
77     scanf("%d",&index);
78     if(index==1)
79         head=head->next;
80     else
81         delete(head,index);
82     output(head);
83 
84     printf("
creat a node:
");
85     scanf("%d%c",&index0,&m);
86     rear_creat(head,index0,m);
87     output(head);
88     return 0;
89 }
 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 
10 typedef listnode *linklist;
11 linklist r=NULL,head=NULL;
12 
13 void rear_creat(datetype ch)
14 {
15     linklist p=NULL;
16     p=(listnode *)malloc(sizeof(listnode));
17     p->date=ch;
18     if(head==NULL)
19     {
20         head=p;
21     }
22     else
23     {
24         r->next=p;
25     }
26     r=p;
27 }
28 void output(linklist head)
29 {
30     while(head!=NULL)
31     {
32         printf("%c ",head->date);
33         head=head->next;
34     }
35 
36 }
37 void insert_rear(int index,int num)
38 {
39     listnode *p=NULL,*u;
40     u=head;
41     int n=1;
42     p=(listnode *)malloc(sizeof(listnode));
43     p->date=index;
44     while(num>n)
45     {
46         u=u->next;
47         n++;
48     }
49     p->next=u->next;
50     u->next=p;
51 }
52 void delete(int num)
53 {
54     int n=1;
55     linklist p=head,q;
56     while(num>n)
57     {
58         n++;
59         q=p;
60         p=p->next;    
61     }
62     q->next=p->next;
63 }
64 
65 int main()
66 {
67     datetype index,ch;
68     int num;
69     ch=getchar();
70     while(ch!='
')
71     {
72         rear_creat(ch);
73         ch=getchar();
74     }
75     output(head);
76     scanf("%c%d",&index,&num);
77     insert_rear(index,num);
78     output(head);
79     scanf("%d",&num);
80     delete(num);
81     output(head);
82 }
原文地址:https://www.cnblogs.com/a1225234/p/4453175.html