1 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef struct LinkList { 5 int data; 6 LinkList *next; 7 8 }LinkList , *linklist; 9 //创建头结点 10 int CreateList(linklist &L) { 11 L = (linklist)malloc(sizeof(LinkList)); 12 if(!L) 13 exit(0); 14 L->next = NULL; 15 L->data = 0; 16 } 17 //尾插法 18 int PushBack(linklist &L) { 19 linklist L1 = NULL; 20 L1 = L ; 21 char c = 'N'; 22 for( ; ; ){ 23 linklist q = NULL; 24 CreateList(q); 25 printf("请输入元素 "); 26 scanf("%d" , &(q->data)); 27 L1->next = q ; 28 L1= q ; 29 fflush(stdin); 30 printf("是否继续输入Y/N"); 31 scanf("%c" , &c); 32 fflush(stdin); 33 if(c != 'Y') 34 return 0; 35 } 36 } 37 //头插法 38 int PushFront(linklist &L) { 39 char c = 'N'; 40 linklist p = NULL ; 41 // L1 = L ; 42 p = L; 43 for( ; ; ){ 44 linklist q = NULL ; 45 CreateList(q); 46 printf("请输入元素 "); 47 scanf("%d" , &(q->data)); 48 p = L->next; 49 L->next = q ; 50 q->next = p ; 51 fflush(stdin); 52 printf("是否继续输入Y/N"); 53 scanf("%c" , &c); 54 fflush(stdin); 55 if(c != 'Y') 56 return 0; 57 } 58 } 59 //输出所有元素 60 void PrintfList(linklist &L) { 61 linklist L1 = L ; 62 while(L1->next != NULL) 63 { 64 L1 = L1->next; 65 printf("///%d///" , L1->data); 66 } 67 68 } 69 70 int deleteList(linklist &L , int n) { 71 if(L->next == NULL) 72 return 0 ; 73 linklist L1 = L ; 74 while(L1->next->data != n) 75 L1 = L1->next; 76 L1->next = L1->next->next; 77 78 } 79 //两个有序链表归并 80 LinkList *MergeList(linklist &La , linklist &Lb ) { 81 linklist L = NULL , l = NULL, la = La->next , lb = Lb->next; 82 CreateList(L); 83 l = L; 84 while(lb && la){ 85 if(la->data >= lb->data){ 86 l->next = la ; 87 l = l->next; 88 la = la -> next; 89 } 90 else{ 91 l -> next = lb; 92 l = l->next; 93 lb = lb -> next; 94 } 95 } 96 l->next = lb ? lb : la ; 97 return L ; 98 } 99 //插入元素 100 int insertList(linklist &L , int n ,int data) { 101 linklist L1 = L , q ; 102 for(int i = 0 ; i < n , L1->next!=NULL ; i++ ) 103 { 104 if(i = n-1) 105 { 106 CreateList(q); 107 q ->data = data ; 108 q->next = L1->next; 109 L1->next = q; 110 return n; 111 } 112 L1 = L1 -> next; 113 } 114 return 0; 115 } 116 int main() { 117 LinkList *q = NULL , *p = NULL ,*L = NULL; 118 CreateList(q); 119 PushFront(q); 120 PrintfList(q); 121 printf(" "); 122 CreateList(p); 123 PushFront(p); 124 PrintfList(p); 125 L = MergeList(q , p ); 126 printf(" "); 127 PrintfList(L); 128 // deleteList(q , 3); 129 // PrintfList(q); 130 131 }