标签:for mes 链式 reverse cout enter 元素 put 链接
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 //创建链表,有n个结点 15 void CreateList(LinkList &L,int n) 16 { 17 L = (LinkList)malloc(sizeof(LNode)); 18 L->next = NULL;//创建一个带头结点的单链表 19 L->data = 0;//用于记录当前链表的元素个数 20 21 LNode *p; 22 for(int i = n;i > 0;i--) 23 { 24 p = (LinkList)malloc(sizeof(LNode));//生成新节点 25 sc("%d",&p->data); 26 p->next = L->next; 27 L->next = p; 28 } 29 } 30 31 //插入元素 32 bool LinkInsert_L(LinkList &L,int i,ElemType e) 33 { 34 LNode *p = L; 35 int j = 0; 36 while(p && j < i - 1) 37 { 38 p = p->next; 39 ++j; 40 } 41 42 //判断是否非法位置 43 if(!p || j > i - 1) { 44 puts("Oh! Baby! The insertion position is invalid. Please re-enter the insertion position!"); 45 return 0; 46 } 47 48 LNode *s = (LinkList)malloc(sizeof(LNode)); 49 s->data = e; 50 s->next = p->next; 51 p->next = s; 52 return 1; 53 } 54 55 //链表删除元素 56 bool ListDelete_L(LinkList &L,int i,int &e) 57 { 58 //删除第i个元素 59 LNode *p = L; int j = 0; 60 while(p->next && j < i - 1) 61 { 62 p = p->next; 63 ++j; 64 } 65 66 //判断是否非法位置 67 if(!(p->next) || j > i - 1) { 68 puts("Oh! Baby! The position is invalid. Please re-enter the position!"); 69 return 0; 70 } 71 72 LNode *q = (LinkList)malloc(sizeof(LNode)); 73 q = p->next; 74 p->next = q->next; 75 e = q->data; 76 free(p); 77 return 1; 78 } 79 80 //合并链表 81 void MergeList(LinkList &la,LinkList &lb,LinkList &lc) 82 { 83 LNode *pa,*pb,*pc; 84 pa = la->next; pb = lb->next; 85 lc = pc = la; 86 87 while(pa && pb) 88 { 89 if(pa->data <= pb->data){ 90 pc->next = pa; 91 pc = pa; 92 pa = pa->next; 93 } 94 else{ 95 pc->next = pb; 96 pc = pb; 97 pb = pb->next; 98 } 99 } 100 pc->next = pa? pa:pb; 101 free(lb); 102 } 103 104 //获取链表长度 105 int getListLength(LinkList &L) 106 { 107 LNode *p = L; 108 int cnt = 0; 109 while(p->next != NULL) 110 { 111 p = p->next; 112 ++cnt; 113 //cout << p->data << " "; 114 } 115 return cnt; 116 } 117 118 //获取链表的某个元素 119 bool getElem(LinkList &L,int q,int &e) { 120 121 int length = getListLength(L); 122 if(q < 1 || q > length) 123 { 124 puts("Oh! Baby! The position is invalid. Please re-enter the position!"); 125 return 0; 126 } 127 LNode *p = L; 128 int j = 0; 129 while(p->next && j < q - 1) 130 { 131 p = p->next; 132 j++; 133 } 134 if(p->next == NULL) 135 { 136 puts("Sorry, didn‘t find it"); 137 return 0; 138 } 139 else{ 140 p = p->next; 141 e = p->data; 142 return 1; 143 } 144 } 145 146 void Reverse_List(LinkList &la,LinkList &lb) 147 { 148 LNode *p = la; 149 while(p->next != NULL) 150 { 151 p = p->next; 152 if(p != NULL) 153 LinkInsert_L(lb,1,p->data); 154 } 155 } 156 157 //遍历链表 158 void LinkList_Traver(LinkList &L) 159 { 160 LNode *p = L; 161 puts("Let‘s do it! Traver! Traver_List!"); 162 while(p->next != NULL) 163 { 164 p = p->next; 165 cout << p->data << " "; 166 } 167 puts(""); 168 } 169 170 //销毁链表 171 bool List_Destroed(LinkList &L) 172 { 173 LNode *p; 174 if(L == NULL) return 0; 175 while(L->next) 176 { 177 p = L->next; 178 free(L); 179 L = p; 180 } 181 return 1; 182 } 183 int main() 184 { 185 LinkList la,lb,lc,ld; 186 int n; 187 sc("%d",&n); 188 CreateList(la,n); 189 CreateList(lb,n); 190 CreateList(ld,0); 191 192 193 // for(int i = 1;i <= 5;i++) 194 // { 195 // LinkInsert_L(la,i,i); 196 // } 197 198 cout << "la length = " << getListLength(la) << endl; 199 cout << "lb length = " << getListLength(lb) << endl; 200 201 int ele; 202 if(getElem(la,5,ele)) 203 { 204 cout << ele << endl; 205 } 206 207 if(getElem(lb,1,ele)) 208 { 209 cout << ele << endl; 210 } 211 212 LinkList_Traver(la); 213 LinkList_Traver(lb); 214 215 MergeList(la,lb,lc); 216 LinkList_Traver(la); 217 LinkList_Traver(lc); 218 219 Reverse_List(la,ld); 220 cout << "Traver ld = "; 221 LinkList_Traver(ld); 222 223 List_Destroed(la); 224 LinkList_Traver(la); 225 return 0;; 226 }
标签:for mes 链式 reverse cout enter 元素 put 链接
原文地址:https://www.cnblogs.com/mch5201314/p/11614021.html