#include <iostream> #include<cstdlib> using namespace std; enum operation{create_List=1,print_List,insert_Node,delete_Node,delete_List,quit};//枚舉類型,用於菜單選擇結果 struct node //結點結構 { int data ; node * next; }; operation Menu(); //菜單函數 node * CreateList( ); //建立鏈表函數聲明 void PrintList( node *); //輸出鏈表中結點信息函數聲明 node * InsertNode(node *,node *); //在鏈表中插入結點函數聲明 node * DeleteNode(node *,int); //在鏈表中刪除結點函數聲明 node * deleteList(node *head); //刪除整個鏈表 void Create(); //對應操作菜單--創建鏈表的操作 void Print( ); //對應操作菜單--遍歷鏈表的操作 void Insert( ); //對應操作菜單--插入鏈表結點的操作 void Delete( ); //對應操作菜單--刪除鏈表結點的操作 void DeleteAll(); //對應操作菜單--刪除整個鏈表的操作 int n=0; //全局整型變量存放鏈表中結點個數 node * head=NULL ; //全局指針變量存放鏈表頭結點地址-頭指針 int main() { operation menu_choice; //存放菜單選擇項 do //循環現實直到用戶退出程序 { menu_choice=Menu(); //菜單顯示及用戶選擇 switch(menu_choice) //用戶選擇功能匹配 { case create_List: system("cls"); cout<<"1 創建鏈表"<<endl<<endl; Create( ); break; case print_List: system("cls"); cout<<"2 遍歷鏈表"<<endl<<endl; Print(); break; case insert_Node: system("cls"); cout<<"3 插入鏈表結點"<<endl<<endl; Insert(); break; case delete_Node: system("cls"); cout<<"4 刪除鏈表結點"<<endl<<endl; Delete(); break; case delete_List: system("cls"); cout<<"5 刪除整個鏈表"<<endl<<endl; DeleteAll(); break; case quit : default: cout<<"退出鏈表操作,結束程序"<<endl; return 0; } }while(menu_choice!=quit); return 0; } /*創建鏈表*/ node * CreateList( ) //建立鏈表函數 { node * s, * p ; // s指向新結點,p指向鏈表中最后的結點 s = new node ; //動態建立第一個新結點 cout<<"請輸入一個整數值作為新結點的數據信息,輸入0時建立鏈表結束"<<endl; cout<<"第"<<n+1<<"個結點"<<endl; cin >> s->data ; //輸入新結點數據 head = NULL ; //頭指針初始值為NULL if( s->data==0) //第一個結點數據就為0,建立一個空鏈表 { cout<<"您建立的空鏈表"<<endl; delete s ; //釋放數據為0的結點 } else //建立非空鏈表 { while ( s->data != 0 ) //通過判斷新結點數據來進行循環 { if ( head == NULL ) head = s ; //頭指針賦值 else p->next = s ; //將新結點插入已有鏈表的最后 p = s ; // p指向鏈表中最后的結點 n=n+1;//結點個數增1 s = new node ; //動態建立一個新結點 cout<<"請輸入一個整數值作為新結點的數據信息,輸入0時建立鏈表結束"<<endl; cout<<"第"<<n+1<<"個結點"<<endl; cin >> s->data ; //輸入新結點數據 } p -> next = NULL ; //設置鏈表尾部為空 delete s ; //釋放數據為0的結點 cout<<endl<<"鏈表建立完成..."; cout<<"建立的鏈表中共有"<<n<<"個節點"<<endl<<endl; } return ( head ) ; //返回頭指針 } /*遍歷鏈表*/ void PrintList( node * head) //輸出鏈表中結點信息函數,鏈表遍歷 { node *p=head; int i=1; cout<<endl<<"遍歷鏈表..."<<endl; if (head!=NULL) //如果鏈表非空,即鏈表中有結點 do //循環輸出接點數據,直到移動到鏈表尾,即最后一個結點 { cout<<"第"<<i++<<"個結點數據為:"<<p->data<<endl; p=p->next; }while(p!=NULL) ; else { cout<<"鏈表是空鏈表!"<<endl; } cout<<"鏈表中共有"<<n<<"個節點"<<endl; } /*插入結點*/ node * InsertNode(node *head,node * s) //插入結點的函數,head為鏈表頭指針,s指向要插入的新結點 {node *p,*q; p=head; //使p指向鏈表中的第一個結點 if(head==NULL) //原來的鏈表是空表 { head=s; //使head指向的新結點作為頭結點 s->next=NULL; } else //原來的鏈表不是空表 {while((s->data>p->data) && (p->next!=NULL)) //用循環定位要插入的結點位置p,使s插入到p之前的位置 {q=p; //q記錄下當前的p,即q指向p的前一個結點 p=p->next; //p后移一個結點 } if(s->data<=p->data) //要插入的結點數據比最后一個結點數據小 { if(head==p) //判斷是否插入鏈表中的第一個結點之前 { head=s; //插到原來第一個結點之前 s->next=p; } else //插到q指向的結點之后,p指向的結點之前 { q->next=s; s->next=p; } } else //要插入的結點數據比最后一個結點數據還大 { p->next=s; // 插到鏈表最后的結點之后,作為鏈表的尾結點 s->next=NULL; } } n=n+1; //結點數加1 cout<<"成功完成一個新結點插入..."<<endl; return (head); } /*刪除結點*/ node *DeleteNode(node *head,int delData) //刪除數據為delDate的結點的函數 {node *p,*q; p=head; //使p指向第一個結點 if (head==NULL) //是空表 { cout<<"該鏈表是空鏈表,不能進行結點刪除!"<<endl; return(head); } //先找到要刪除的結點 while(delData!=p->data && p->next!=NULL) //p指向的不是所要找的結點且后面還有結點 { q=p; //q用來記錄p前一個結點 p=p->next; } //p后移一個結點 if(delData==p->data) //找到了要刪除的結點 { if(p==head) //如果要刪除的是頭結點 head=p->next; //若p指向的是首結點,把第二個結點地址賦予head else q->next=p->next; //否則將下一結點地址賦給前一結點地址 cout<<"成功刪除數據為"<<delData<<"的結點"<<endl; n=n-1; } else cout<<"要刪除的數據為"<<delData<<"的結點在鏈表中沒有找到"<<endl; //找不到該結點 return(head); } /*刪除整個鏈表*/ node * deleteList(node *head) //刪除整個鏈表 { node *p,*s; p=head; if(head==NULL) cout<<"鏈表本身就為空鏈表"; else { while(p->next!=NULL) { s=p; p=p->next; delete s; n--; } delete p; n--; head=NULL; } cout<<"整個鏈表刪除成功!"<<endl; return head; } /*菜單函數*/ operation Menu() { int choice; cout<<endl<<endl; cout<<"+-------------鏈表操作菜單---------------+"<<endl; cout<<"| |"<<endl; cout<<"| 1->創建鏈表 |"<<endl; cout<<"| 2->遍歷鏈表 |"<<endl; cout<<"| 3->插入節點 |"<<endl; cout<<"| 4->刪除節點 |"<<endl; cout<<"| 5->刪除鏈表 |"<<endl; cout<<"| 6->退出 |"<<endl; cout<<"| |"<<endl; cout<<"+----------------------------------------+"<<endl; cout<<endl<<endl<<"請輸入功能序號"; cin>>choice; return operation(choice); } /*對應操作菜單--創建鏈表的操作*/ void Create() { if(head==NULL) //如果鏈表中已有結點,不允許重新建立 { head=CreateList( ); } else { cout<<"已創建過鏈表,不允許再次創建"<<endl; cout<<"如果想重新創建,先刪除原先鏈表"<<endl; } } /*對應操作菜單--遍歷鏈表的操作*/ void Print( ) { PrintList(head); } /*對應操作菜單--插入鏈表結點的操作*/ void Insert( ) { char IsGo; //是否繼續操作標志 IsGo='y'; cout<<endl<<"開始進行結點插入操作"<<endl; node *stu; while(IsGo=='y'||IsGo=='Y') { stu=new node; //創建要插入的新結點 cout<<endl<<"輸入要插入的新結點數據:"; cin>>stu->data; //輸入要插入的新結點數據 head=InsertNode(head,stu); //調用插入函數,返回鏈表頭指針 cout<<"是否繼續插入新結點? (繼續插入請按y或Y,退出請按其它鍵)"; cin>>IsGo; } cout<<endl<<"結點插入操作結束"<<endl; } /*對應操作菜單--刪除鏈表結點的操作*/ void Delete( ) { char IsGo; //是否繼續操作標志 int del_num; //要刪除的結點的數據 IsGo='y'; cout<<endl<<"開始進行結點插入操作"<<endl; while(IsGo=='y'||IsGo=='Y') { cout<<endl<<"輸入要刪除的節點的數據:"; //輸入要插入的結點 cin>>del_num; //輸入要刪除的結點的數據 head=DeleteNode(head,del_num); //刪除后鏈表的頭地址 cout<<"是否繼續刪除結點? (繼續插入請按y或Y,退出請按其它鍵)"; cin>>IsGo; } cout<<endl<<"結點刪除操作結束"<<endl; } /*對應操作菜單--刪除整個鏈表的操作*/ void DeleteAll() { head=deleteList(head); }