seyOrd

导航

完整的链表例子_byseyOrd

 

 

 

  1 #include<malloc.h>
  2 
  3 #include<stdlib.h>
  4 
  5 
  6 
  7 typedef int Elemtype;
  8 
  9 typedef int Status;
 10 
 11 
 12 
 13 typedef struct LNode{
 14 
 15     Elemtype data;
 16 
 17     struct LNode *next;
 18 
 19 }LNode, *LinkList;
 20 
 21 
 22 
 23 //函数声明
 24 
 25 Status CreatList1(LinkList &L, int n);                                   //头插法构建
 26 
 27 Status PrintfList(LinkList L);                                          //链表输出
 28 
 29 Status CreatList2(LinkList &L, int n);                                   //尾插法构建
 30 
 31 Status InverseList(LinkList &L);                                            //逆置链表
 32 
 33 
 34 
 35 int main()
 36 
 37 {
 38 
 39     LinkList L;
 40 
 41     int n, i, e;
 42 
 43 
 44 
 45     printf("请输入建立的线性表结点个数n=");
 46 
 47     scanf_s("%d", &n);
 48 
 49     CreatList1(L, n);
 50 
 51     PrintfList(L);                                                   //头插法建立链表
 52 
 53 
 54 
 55     printf("请输入建立的线性表结点个数n=");
 56 
 57     scanf_s("%d", &n);
 58 
 59     CreatList2(L, n);
 60 
 61     PrintfList(L);                                                   //尾插法建立链表
 62 
 63 
 64 
 65     InverseList(L);                                                //逆置链表
 66 
 67     PrintfList(L);
 68 
 69 }
 70 
 71 
 72 
 73 //头插法创建链表
 74 
 75 Status CreatList1(LinkList &L, int n)
 76 
 77 {
 78 
 79     int i;
 80 
 81     LinkList p;
 82 
 83     L = (LinkList)malloc(sizeof(LNode));
 84 
 85     L->next = NULL;
 86 
 87     for (i = n; i > 0; i--)
 88 
 89     {
 90 
 91         p = (LinkList)malloc(sizeof(LNode));
 92 
 93         p->data = rand() % 100 + 1;
 94 
 95         printf("头插法的第%d个数据:%d\n", i, p->data);
 96 
 97         p->next = L->next;
 98 
 99         L->next = p;
100 
101     }
102 
103     return 0;
104 
105 }
106 
107 //尾插法构建线性表
108 
109 Status CreatList2(LinkList &L, int n)
110 
111 {
112 
113     LinkList p, q;
114 
115     int i;
116 
117     L = (LinkList)malloc(sizeof(LNode));
118 
119     L->next = NULL;
120 
121     for (q = L, i = 0; i < n; i++)
122 
123     {
124 
125         p = (LinkList)malloc(sizeof(LNode));
126 
127         p->data = rand() % 100 + 1;
128 
129         printf("尾插法的第%d个数据:%d", i + 1, p->data);
130 
131         q->next = p;
132 
133         q = p;
134 
135         printf("\n");
136 
137     }
138 
139     q->next = NULL;
140 
141     return 0;
142 
143 }
144 
145 
146 
147 //输出链表
148 
149 Status PrintfList(LinkList L)
150 
151 {
152 
153     LNode *p;
154 
155     if (L->next == NULL)
156 
157         printf("The List is Empty!!!\n");
158 
159     for (p = L->next; p != NULL; p = p->next)
160 
161         printf("%3d ", p->data);
162 
163     printf("\n");
164 
165     return 0;
166 
167 }
168 
169 //逆置链表
170 
171 Status InverseList(LinkList &L)
172 
173 {
174 
175     LinkList p, q;
176 
177     for (p = L->next; p->next != NULL; p = p->next);
178 
179     for (q = L->next; q != p; q = L->next)
180 
181     {
182 
183         L->next = q->next;
184 
185         q->next = p->next;
186 
187         p->next = q;
188 
189     }
190 
191     return 0;
192 
193 }

 

posted on 2020-04-20 15:28  seyOrd  阅读(198)  评论(0)    收藏  举报