1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "02_linklist.h"
4 Linklist* creat_linklist(void)
5 {
6 Linklist* head=(Linklist*)malloc(sizeof(Linklist));
7 if(NULL==head)
8 {printf("failed!\n");
9 return NULL;}
10 head->data.len=0;
11 head->next=NULL;
12 return head;
13 }
14 void insert_linklistHead(Linklist* head,dataType num)
15 {
16 Linklist* temp=(Linklist*)malloc(sizeof(Linklist));
17 if(NULL==temp)
18 {printf("failed!\n");
19 return;
20 }
21 temp->data.text=num;
22 temp->next=NULL;
23 temp->next=head->next;
24 head->next=temp;
25 head->data.len++;
26 return;
27 }
28 void show_linklist(Linklist* head)
29 {
30 Linklist* p=head;
31 while(p->next!=NULL)
32 {
33 p=p->next;
34 printf("%d\t",p->data.text);
35 }
36 putchar(10);
37 return;
38 }
39
40
41 void insert_linklistTail(Linklist* head,dataType num)
42 {
43 Linklist* temp=(Linklist*)malloc(sizeof(Linklist));
44 if(NULL==temp)
45 {printf("failed!\n");
46 return;
47 }
48 temp->data.text=num;
49 temp->next=NULL;
50 Linklist* p=head;
51 while(p->next!=NULL)
52 {p=p->next;
53 }
54 p->next=temp;
55 head->data.len++;
56 return;
57 }
58
59 void delet_linklistHead(Linklist* head)
60 {
61 Linklist* p=head->next;
62 if(head->next==NULL)
63 {printf("empty link,nothing to delete!\n");}
64 head->next=p->next;
65 free(p);
66 head->data.len--;
67 return;
68 }
69
70 void delet_linklistTail(Linklist* head)
71 {
72 if(head->next==NULL)
73 {printf("empty link,nothing to delete!\n");}
74 Linklist* p=head;
75 while(p->next->next!=NULL)
76 {
77 p=p->next;
78 }
79 p->next=NULL;
80 head->data.len--;
81 return;
82 }
83
84 void insertAnywhere(Linklist* head,int n,int b)
85 {
86 if(head->next==NULL)
87 {
88 printf("empty link,nothing to delete!\n");
89 }
90 Linklist* temp=(Linklist*)malloc(sizeof(Linklist));
91 temp->data.text=b;
92 if(n<1 ||n>head->data.len+1)
93 {printf("wrong n,failed!\n");
94 return;
95 }
96 Linklist *p=head;
97 for(int i=0;i<n-1;i++)
98 {
99 p=p->next;
100 }
101 temp->next=p->next;
102 p->next=temp;
103 head->data.len++;
104
105 }
106
107 void deleteAnywehere(Linklist* head,int n)
108 {
109
110 if(head->next==NULL)
111 {
112 printf("empty link,nothing to delete!\n");
113 }
114 if(n<1 ||n>head->data.len)
115 {printf("wrong n,failed!\n");
116 return;
117 }
118 Linklist* p=head;
119
120 for(int i=0;i<n-1;i++)
121 {
122 p=p->next;
123 }
124 Linklist* q=p->next;
125 p->next=p->next->next;
126 free(q);
127 q=NULL;
128 return;
129 }
130
131
132 void showAnywhere(Linklist* head,int n)
133 {
134 Linklist* p=head;
135 for(int i=0;i<n;i++)
136 {
137 p=p->next;
138 }
139 printf("%d\t",p->data.text);
140 putchar(10);
141 return;
142 }
~
~
~
practice0402
最新推荐文章于 2024-08-07 07:30:00 发布