C语言 数据结构实训1(链表及顺序表去重、链表逆置、链表合并、插入排序)
一、链表及顺序表去重
(1)链表去重
1 //链表去重 2 #include <stdio.h> 3 #include <stdlib.h> 4 #define null 0 5 typedef struct node{ 6 int data; 7 struct node *next; 8 }Sqlist; 9 Sqlist *CreateList(){ 10 Sqlist *head,*q,*p; 11 int a[15]={2,3,5,5,6,65,6,5,12,6,23,65,3,12,3}; 12 head=(Sqlist *)malloc(sizeof(Sqlist)); 13 q=head; 14 for(int i=0;i<15;i++){ 15 p=(Sqlist *)malloc(sizeof(Sqlist)); 16 p->data=a[i]; 17 q->next=p; 18 q=p; 19 } 20 q->next=null; 21 return head; 22 } 23 void DispList(Sqlist *l){ 24 Sqlist *q; 25 q=l->next; 26 while(q!=null){ 27 printf("%d ",q->data); 28 q=q->next; 29 } 30 printf("\n"); 31 } 32 int main(){ 33 Sqlist *l,*r,*p,*s; 34 l=CreateList(); 35 printf("单链表去重:\n"); 36 printf("(1)创建一个链表:\n"); 37 DispList(l); 38 p=l->next; 39 while(p!=null){ 40 r=p->next; 41 s=p; 42 while(r!=null){ 43 if(p->data==r->data){ 44 s->next=r->next; 45 }else{ 46 s=s->next; 47 } 48 r=r->next; 49 } 50 p=p->next; 51 } 52 printf("(2)去除结点数据域相同的结点:\n"); 53 DispList(l); 54 }
(2)顺序表去重
1 //顺序表去重 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <stdbool.h> 5 #define null 0 6 #define MAXSIZE 100 7 typedef struct{ 8 int data[MAXSIZE]; 9 int length; 10 }Sqlist; 11 void CreateList(Sqlist *l,int a[],int n){ 12 int i; 13 for(i=0;i<n;i++){ 14 l->data[i]=a[i]; 15 } 16 l->length=n; 17 } 18 bool ListDelete(Sqlist *l,int i){ 19 if(l->length==0)return false; 20 int j; 21 for(i=i-1;i<l->length-1;i++) 22 l->data[i]=l->data[i+1]; 23 l->length--; 24 return true; 25 } 26 27 void Displist(Sqlist *l){ 28 int i,j; 29 i=l->length; 30 for(j=0;j<i-1;j++)printf("%d ",l->data[j]); 31 printf("\n"); 32 } 33 int main(){ 34 Sqlist *l; 35 int a[15]={2,3,12,2,3,12,5,7,3,12,3,22,12,56,19}; 36 l=(Sqlist *)malloc(sizeof(Sqlist)); 37 printf("顺序表去重:\n"); 38 printf("(1)创建一个顺序表\n"); 39 CreateList(l,a,15); 40 Displist(l); 41 int i,j,flag; 42 j=0; 43 while(j!=l->length){ 44 i=j+1; 45 flag=j+2; 46 while(i!=l->length){ 47 if(l->data[i]==l->data[j]){ 48 ListDelete(l,flag); 49 flag++; 50 i++; 51 }else{ 52 flag++; 53 i++; 54 } 55 } 56 j++; 57 } 58 printf("(2)输出去重后的顺序表:\n"); 59 Displist(l); 60 }
二、链表逆置
1 //单链表逆置,原链表进行 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <stdbool.h> 5 #define null 0 6 typedef struct node{ 7 int data; 8 struct node *next; 9 }Sqlist; 10 Sqlist *CreateList(){ 11 Sqlist *head,*q,*p; 12 int a[10]={1,2,3,4,5,6,7,8,9,10}; 13 head=(Sqlist *)malloc(sizeof(Sqlist)); 14 q=head; 15 for(int i=0;i<10;i++){ 16 p=(Sqlist *)malloc(sizeof(Sqlist)); 17 p->data=a[i]; 18 q->next=p; 19 q=p; 20 } 21 q->next=null; 22 return head; 23 } 24 void DispList(Sqlist *l){ 25 Sqlist *q; 26 q=l->next; 27 while(q!=null){ 28 printf("%d ",q->data); 29 q=q->next; 30 } 31 printf("\n"); 32 } 33 void ReverseList(Sqlist *l){ 34 Sqlist *pre,*cur,*aft; 35 pre=null; 36 cur=l->next; 37 while(cur!=null){ 38 aft=cur->next; 39 cur->next=pre; 40 pre=cur; 41 cur=aft; 42 } 43 l->next=pre; 44 } 45 46 int main(){ 47 Sqlist *l,*p,*r; 48 printf("单链表逆置:\n"); 49 printf("(1)创建一个单链表:\n"); 50 l=CreateList(); 51 DispList(l); 52 ReverseList(l); 53 printf("(2)反转这个单链表:\n"); 54 DispList(l); 55 }
三、链表合并
1 /*链表合并*/ 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <stdbool.h> 5 #define null 0 6 typedef struct node{ 7 int data; 8 struct node *next; 9 }Sqlist; 10 Sqlist *CreateList(int a[],int n){ 11 Sqlist *head,*q,*p; 12 head=(Sqlist *)malloc(sizeof(Sqlist)); 13 q=head; 14 for(int i=0;i<n;i++){ 15 p=(Sqlist *)malloc(sizeof(Sqlist)); 16 p->data=a[i]; 17 q->next=p; 18 q=p; 19 } 20 q->next=null; 21 return head; 22 } 23 void DispList(Sqlist *l){ 24 Sqlist *q; 25 q=l->next; 26 while(q!=null){ 27 printf("%d ",q->data); 28 q=q->next; 29 } 30 printf("\n"); 31 } 32 int main(){ 33 Sqlist *s,*t,*p,*q,*r,*w; 34 int a[6]={2,3,4,1,7,9}; 35 int b[8]={4,6,12,55,23,17,10,22}; 36 s=CreateList(a,6); 37 t=CreateList(b,8); 38 printf("创建两个链表:\n"); 39 DispList(s); 40 DispList(t); 41 Sqlist *l; 42 l=(Sqlist *)malloc(sizeof(Sqlist)); 43 r=l; 44 p=t->next; 45 q=s->next; 46 while(p!=null && q!=null){ 47 w=p->next; 48 r->next=p; 49 p->next=q; 50 r=q; 51 p=w; 52 q=q->next; 53 } 54 if(p!=null)r->next=p; 55 else if(q!=null)r->next=q; 56 printf("输出合并后的链表:\n"); 57 DispList(l); 58 }
四、插入排序
1 //插入一个元素,使线性表仍非递减有序 ---单链表 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <stdbool.h> 5 #define null 0 6 typedef struct node{ 7 int data; 8 struct node *next; 9 }Sqlist; 10 Sqlist *CreateList(int a[],int n){ 11 Sqlist *head,*q,*p; 12 head=(Sqlist *)malloc(sizeof(Sqlist)); 13 q=head; 14 for(int i=0;i<n;i++){ 15 p=(Sqlist *)malloc(sizeof(Sqlist)); 16 p->data=a[i]; 17 q->next=p; 18 q=p; 19 } 20 q->next=null; 21 return head; 22 } 23 void DispList(Sqlist *l){ 24 Sqlist *q; 25 q=l->next; 26 while(q!=null){ 27 printf("%d ",q->data); 28 q=q->next; 29 } 30 printf("\n"); 31 } 32 int main(){ 33 Sqlist *l; 34 int m; 35 int a[10]={5,7,8,10,14,17,21,22,26,33}; 36 l=CreateList(a,10); 37 printf("插入排序:\n"); 38 printf("(1)创建一个递增链表:\n"); 39 DispList(l); 40 printf("请输入要插入的数:"); 41 scanf("%d",&m); 42 Sqlist *p,*r,*q; 43 p=l; 44 while(p!=null){ 45 q=p->next; 46 if(q==null){ 47 r=(Sqlist *)malloc(sizeof(Sqlist)); 48 r->data=m; 49 p->next=r; 50 r->next=null; 51 break; 52 } 53 if(q->data>m){ 54 r=(Sqlist *)malloc(sizeof(Sqlist)); 55 r->data=m; 56 p->next=r; 57 r->next=q; 58 break; 59 }else{ 60 p=p->next; 61 } 62 } 63 printf("(2)插入新节点得链表:\n"); 64 DispList(l); 65 }

浙公网安备 33010602011771号