同时创建两条单链表,头插法插入节点,遍历,查找,删除,求长度,冒泡排序,反转,2条有序链表链接成一条链表后依然有序

2021-05-18 09:29

阅读:352

标签:ops   bre   链表   std   def   最大   步骤   next   操作   

  1 #include   2 #include   3 /*
  4 链表每日一练:创建2条空链表,头插法插入节点,遍历,查找,删除,求长度,冒泡排序,反转,2条有序链表链接成一条链表后依然有序。
  5 */
  6 typedef struct node
  7 {
  8     int data;
  9     struct node * next;
 10 }NODE;
 11 //创建空链表
 12 NODE * createList()
 13 {
 14     NODE * head = (NODE *)malloc(sizeof(NODE));
 15     head->next = NULL;
 16 
 17     return head;
 18 }
 19 //头插法插入节点
 20 void insertNode(NODE *head,int insertData)
 21 {
 22     NODE *sur = (NODE *)malloc(sizeof(NODE));
 23     sur->data = insertData;
 24 
 25     sur->next = head->next;
 26     head->next = sur;
 27 }
 28 //遍历
 29 void traverList(NODE *head)
 30 {
 31     head = head->next;
 32     while(head)
 33     {
 34         printf("%d\n",head->data);
 35         head = head->next;
 36     }
 37 }
 38 //查找
 39 NODE * lookNode(NODE *head,int lookData)
 40 {
 41     head = head->next;
 42     while(head)
 43     {
 44         if(head->data == lookData)
 45             break;
 46         else
 47             head = head->next;
 48     }
 49     return head;
 50 }
 51 //删除
 52 void deleteNode(NODE *head,NODE *p)
 53 {
 54     while(head->next != p)
 55         head = head->next;
 56     head->next = p->next;
 57     free(p);
 58 }
 59 //求长度
 60 int lenList(NODE * head)
 61 {
 62     int len = 0;
 63     head = head->next;
 64     while(head)
 65     {
 66         len++;
 67         head = head->next;
 68     }
 69 
 70     return len;
 71 }
 72 //冒泡排序
 73 void Popsort(NODE *head,int len)
 74 {
 75     int i,j;
 76     NODE * sur = NULL;
 77     NODE *p,*q,*temp;
 78     p = q = temp = NULL;
 79     for(i = 0;i1;i++)
 80     {
 81         sur = head;
 82         p = sur->next;
 83         q = p->next;
 84         for(j = 0;j1;j++)
 85         {
 86             if(p->data > q->data)
 87             {
 88                 sur->next = q;
 89                 p->next = q->next;
 90                 q->next = p;
 91 
 92                 temp = q;
 93                 q = p;
 94                 p = temp;
 95             }
 96             sur = sur->next;
 97             p = p->next;
 98             q = q->next;
 99         }
100     }
101 }
102 //链表反转,分2步:1,先将原有链表分成2个链表,一个是空链表,一个是无头结点的链表
103 //                   2.然后将无节点的链表逐个插入至空链表中
104 //步骤1:差分链表
105 NODE *breakList(NODE *head)
106 {
107     NODE * p = head->next;
108     head->next = NULL;
109 
110     return p;
111 }
112 //插入
113 void ReverList(NODE *head,NODE *p)
114 {
115     while(p)
116     {
117         NODE * q = p->next;
118         p->next = head->next;
119         head->next = p;
120         p = q;
121     }    
122 }
123 //合并链表,合并后使之仍然有序
124 NODE * mergeList(NODE *head1,NODE *head2)
125 {
126     NODE * sur = createList();
127     head1 = head1->next;
128     head2 = head2->next;
129     while(head1&&head2)
130     {
131         if(head1->data >= head2->data)//特别注意这一点:链表头插法从小到大输出,实际上插入结点的时候,是先把最大的插进链表
132                                       //最后吧最小的插进链表。和数组正好是反的。即:数组从前往后放,头插链表从后往前放。
133         {
134             insertNode(sur,head1->data);
135             head1 = head1->next;
136         }
137         else
138         {    
139             insertNode(sur,head2->data);
140             head2 = head2->next;
141         }
142     }
143     if(NULL == head1)
144     {
145         while(head2)
146         {
147             insertNode(sur,head2->data);
148             head2 = head2->next;
149         }
150     }
151     else
152     {
153         while(head1)
154         {
155             insertNode(sur,head1->data);
156             head1 = head1->next;
157         }
158     }
159     return sur;
160 }    
161 int main(void)
162 {
163     //创建2条空链表
164     NODE * head1 = createList();
165     NODE * head2 = createList();
166     //头插法插入节点
167     for(int i = 0;i50;i++)
168         insertNode(head1,rand()%100);
169     for(i = 1;i60;i += 2)
170         insertNode(head2,i);
171     //遍历链表
172     traverList(head1);
173     printf("-------链表2------------\n");
174     traverList(head2);
175     printf("------查找99这个结点---------\n");
176     //链表查找,如果找到返回该结点指针,否则返回NULL
177     NODE * plook1 = lookNode(head1,99);
178     NODE * plook2 = lookNode(head2,99);
179     //执行删除操作
180     if(plook1)
181         deleteNode(head1,plook1);
182     if(plook2)
183         deleteNode(head2,plook2);
184     printf("------删除后链表-------1\n");
185     traverList(head1);
186     printf("------------删除后链表2----------\n");
187     traverList(head2);
188     //链表冒泡排序
189     printf("-----------链表排序--------\n");
190     int len1 = lenList(head1);
191     int len2 = lenList(head2);
192     Popsort(head1,len1);
193     Popsort(head2,len2);
194     printf("-------链表1------------\n");
195     traverList(head1);
196     printf("-------链表2------------\n");
197     traverList(head2);
198     printf("------链表1反转-------\n");
199     NODE *Newhead1 = breakList(head1);
200     NODE *Newhead2 = breakList(head2);
201     ReverList(head1,Newhead1);
202     traverList(head1);
203     printf("------链表2反转-------\n");
204     ReverList(head2,Newhead2);
205     traverList(head2);
206     printf("------合并2条有序的链表,使之合并后仍然有序-------\n");
207     traverList(mergeList(head1,head2));
208 
209     return 0;
210 }

 

同时创建两条单链表,头插法插入节点,遍历,查找,删除,求长度,冒泡排序,反转,2条有序链表链接成一条链表后依然有序

标签:ops   bre   链表   std   def   最大   步骤   next   操作   

原文地址:https://www.cnblogs.com/wangchaomahan/p/9744878.html


评论


亲,登录后才可以留言!