1 #include<bits/stdc++.h> 2 using namespace std; 3 #define sc scanf 4 #define ElemType int 5 //线性表的链式表示和实现 6 7 typedef struct LNode{ 8 int data; 9 struct LNode *next; 10 }LNode,*LinkList; 11 //关于上面为啥是这样子的,看下面链接即可 12 //https://zhidao.baidu.com/question/489369235.html 13 14 //还有就是下面每个函数传参数的类型,为啥是那样? 15 //你需要一定的基础,就是对指针和引用的了解 16 //还有指针引用和引用指针的区别 ,传送门:https://blog.csdn.net/eye123456789/article/details/79794461 17 18 //创建链表,有n个结点 19 void CreateList(LinkList &L,int n) 20 { 21 L = (LinkList)malloc(sizeof(LNode)); 22 L->next = NULL;//创建一个带头结点的单链表 23 L->data = 0;//用于记录当前链表的元素个数 24 25 LNode *p; 26 for(int i = n;i > 0;i--) 27 { 28 p = (LinkList)malloc(sizeof(LNode));//生成新节点 29 sc("%d",&p->data); 30 p->next = L->next; 31 L->next = p; 32 } 33 } 34 35 //插入元素 36 bool LinkInsert_L(LinkList &L,int i,ElemType e) 37 { 38 LNode *p = L; 39 int j = 0; 40 while(p && j < i - 1) 41 { 42 p = p->next; 43 ++j; 44 } 45 46 //判断是否非法位置 47 if(!p || j > i - 1) { 48 puts("Oh! Baby! The insertion position is invalid. Please re-enter the insertion position!"); 49 return 0; 50 } 51 52 LNode *s = (LinkList)malloc(sizeof(LNode)); 53 s->data = e; 54 s->next = p->next; 55 p->next = s; 56 return 1; 57 } 58 59 //链表删除元素 60 bool ListDelete_L(LinkList &L,int i,int &e) 61 { 62 //删除第i个元素 63 LNode *p = L; int j = 0; 64 while(p->next && j < i - 1) 65 { 66 p = p->next; 67 ++j; 68 } 69 70 //判断是否非法位置 71 if(!(p->next) || j > i - 1) { 72 puts("Oh! Baby! The position is invalid. Please re-enter the position!"); 73 return 0; 74 } 75 76 LNode *q = (LinkList)malloc(sizeof(LNode)); 77 q = p->next; 78 p->next = q->next; 79 e = q->data; 80 free(p); 81 return 1; 82 } 83 84 //合并链表 85 void MergeList(LinkList &la,LinkList &lb,LinkList &lc) 86 { 87 LNode *pa,*pb,*pc; 88 pa = la->next; pb = lb->next; 89 lc = pc = la; 90 91 while(pa && pb) 92 { 93 if(pa->data <= pb->data){ 94 pc->next = pa; 95 pc = pa; 96 pa = pa->next; 97 } 98 else{ 99 pc->next = pb; 100 pc = pb; 101 pb = pb->next; 102 } 103 } 104 pc->next = pa? pa:pb; 105 free(lb); 106 } 107 108 //获取链表长度 109 int getListLength(LinkList &L) 110 { 111 LNode *p = L; 112 int cnt = 0; 113 while(p->next != NULL) 114 { 115 p = p->next; 116 ++cnt; 117 //cout << p->data << " "; 118 } 119 return cnt; 120 } 121 122 //获取链表的某个元素 123 bool getElem(LinkList &L,int q,int &e) { 124 125 int length = getListLength(L); 126 if(q < 1 || q > length) 127 { 128 puts("Oh! Baby! The position is invalid. Please re-enter the position!"); 129 return 0; 130 } 131 LNode *p = L; 132 int j = 0; 133 while(p->next && j < q - 1) 134 { 135 p = p->next; 136 j++; 137 } 138 if(p->next == NULL) 139 { 140 puts("Sorry, didn't find it"); 141 return 0; 142 } 143 else{ 144 p = p->next; 145 e = p->data; 146 return 1; 147 } 148 } 149 150 void Reverse_List(LinkList &la,LinkList &lb) 151 { 152 LNode *p = la; 153 while(p->next != NULL) 154 { 155 p = p->next; 156 if(p != NULL) 157 LinkInsert_L(lb,1,p->data); 158 } 159 } 160 161 //遍历链表 162 void LinkList_Traver(LinkList &L) 163 { 164 LNode *p = L; 165 puts("Let's do it! Traver! Traver_List!"); 166 while(p->next != NULL) 167 { 168 p = p->next; 169 cout << p->data << " "; 170 } 171 puts(""); 172 } 173 174 //销毁链表 175 bool List_Destroed(LinkList &L) 176 { 177 LNode *p; 178 if(L == NULL) return 0; 179 while(L->next) 180 { 181 p = L->next; 182 free(L); 183 L = p; 184 } 185 return 1; 186 } 187 int main() 188 { 189 LinkList la,lb,lc,ld; 190 int n; 191 sc("%d",&n); 192 CreateList(la,n); 193 CreateList(lb,n); 194 CreateList(ld,0); 195 196 197 // for(int i = 1;i <= 5;i++) 198 // { 199 // LinkInsert_L(la,i,i); 200 // } 201 202 cout << "la length = " << getListLength(la) << endl; 203 cout << "lb length = " << getListLength(lb) << endl; 204 205 int ele; 206 if(getElem(la,5,ele)) 207 { 208 cout << ele << endl; 209 } 210 211 if(getElem(lb,1,ele)) 212 { 213 cout << ele << endl; 214 } 215 216 LinkList_Traver(la); 217 LinkList_Traver(lb); 218 219 MergeList(la,lb,lc); 220 LinkList_Traver(la); 221 LinkList_Traver(lc); 222 223 Reverse_List(la,ld); 224 cout << "Traver ld = "; 225 LinkList_Traver(ld); 226 227 List_Destroed(la); 228 LinkList_Traver(la); 229 return 0;; 230 }