1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
| #include <bits/stdc++.h> using namespace std;
struct Node//定义一个结点 { int data; struct Node* next; };
struct Node* CreateListHead() { struct Node* headNode=(struct Node*)malloc(sizeof(struct Node)); headNode->next=NULL; return headNode; }
struct Node* CreateNode(int data) { struct Node* newNode=(struct Node*)malloc(sizeof(struct Node)); newNode->data=data; newNode->next=NULL; return newNode; }
void ShowList(struct Node* headNode) { struct Node* pmove=headNode->next; while(pmove){ int x; x=pmove->data; cout<<x<<' '; pmove=pmove->next; } cout<<endl; }
void ShowNode(struct Node* headNode,int i) { struct Node* pos=headNode->next; int j=1;
while(pos!=headNode && j<=i-1){ j++; pos=pos->next; }
int x=pos->data; cout<<"链表中第i位置的元素为:"<<x<<endl; }
void InsertNodeHead(struct Node* headNode,int data) { struct Node* newNode=CreateNode(data); newNode->next=headNode->next; headNode->next=newNode; }
void InsertNodeTail(struct Node* headNode,int data) { struct Node* newNode=CreateNode(data); struct Node* p=headNode->next;
if(p==NULL) newNode=headNode->next;
else{ while(p->next!=NULL){ p=p->next; } p->next=newNode; } }
void DeleteNodeHead(struct Node* headNode,int posdata) { struct Node* posNode=headNode->next; struct Node* posNodeFront=headNode;
if(posNode==NULL) cout<<"链表为空"<<endl; else{ while(posNode->data!=posdata){ posNodeFront=posNode; posNode=posNodeFront->next;
if(posNode==NULL){ cout<<"未找到相关信息"<<endl; break; } } posNodeFront->next=posNode->next; free(posNode); } }
void InsertNode(struct Node* headNode,int i,int newdata) { struct Node* FrontNode=headNode; struct Node* NextNode=FrontNode->next; struct Node* newNode=CreateNode(newdata);
for(int j=1;j<=i-1;j++) { FrontNode=NextNode; NextNode=FrontNode->next; }
FrontNode->next=newNode; newNode->next=NextNode; }
void DeleteNode(struct Node* headNode,int i) { struct Node* FrontNode=headNode; struct Node* NextNode=FrontNode->next; NextNode=NextNode->next;
if(FrontNode==NULL) cout<<"该链表为空"<<endl; else{ for(int j=1;j<=i-1;j++){ FrontNode=FrontNode->next; NextNode=NextNode->next; }
if(NextNode==NULL){ FrontNode->next=NextNode;
if(NextNode==NULL){ NextNode=FrontNode; } } else FrontNode->next=NextNode; } }
void DeleteNode_data(struct Node* headNode,int e) { struct Node* frontNode=headNode; struct Node* posNode=frontNode->next; struct Node* nextNode=posNode->next;
if(frontNode==NULL) cout<<"该链表为空"<<endl; else{ while(posNode->data!=e) { frontNode=frontNode->next; posNode=posNode->next; nextNode=nextNode->next; } frontNode->next=posNode->next; free(posNode); cout<<"已成功删除"<<endl; } }
bool ClearList(struct Node* headNode) { if(headNode->next ==headNode){ cout<<"该链表为空,不需删除!"<<endl; return false; }
struct Node* p=headNode->next; struct Node*q; while(p != NULL){ q=p->next; free(p); p=q; } p=headNode->next; cout<<"清空成功!"<<endl; return true; }
int main() { struct Node* L=CreateListHead(); while(1){ cout<<endl; cout<<"**1,用头插法创建单链表"<<endl; cout<<"**2,用尾插法创建单链表"<<endl; cout<<"**3,在单链表中查找第i个元素"<<endl; cout<<"**4,在单链表中第i个位置插入元素"<<endl; cout<<"**5,在单链表中删除第i个位置的元素"<<endl; cout<<"**6,在单链表中删除e元素"<<endl; cout<<"**7,遍历单链表"<<endl; cout<<"**8,销毁单链表"<<endl; cout<<"**9,退出系统"<<endl<<endl;
int choice; cout<<"请输入您要进行的操作:"; cin>>choice;
switch(choice) { case 1:{ int n; cout<<"请输入您本次要输入的元素的个数:"; cin>>n; cout<<endl<<"请输入您要输入的元素:"; for(int i=1;i<=n;i++) { int data; cin>>data; InsertNodeHead(L,data); }
cout<<endl<<"您链表中的元素有:"; ShowList(L); break; }
case 2:{ int n; cout<<"请输入您本次要输入的元素的个数:"; cin>>n; cout<<endl<<"请输入您要输入的元素:"; for(int i=1;i<=n;i++) { int data; cin>>data; InsertNodeTail(L,data); }
cout<<endl<<"您链表中的元素有:"; ShowList(L); break; }
case 3:{ int i; cout<<"请输入要查找的位置:"; cin>>i; cout<<endl;
ShowNode(L,i);
break; }
case 4:{ int i,newdata; cout<<"请输入要插入的位置:"; cin>>i; cout<<endl<<"请输入要插入的元素:"; cin>>newdata;
InsertNode(L,i,newdata);
cout<<endl<<"更新后的链表为:"; ShowList(L); break; }
case 5:{ int i; cout<<"请输入要删除的位置:"; cin>>i; DeleteNode(L,i);
cout<<endl<<"更新后的链表为:"; ShowList(L); break; }
case 6:{ int data; cout<<"请输入您要删除的元素的值:"; cin>>data; cout<<endl; DeleteNode_data(L,data); break;}
case 7:ShowList(L);break;
case 8:ClearList(L);break;
case 9:exit(0); } }
system("pause"); return 0; }
|