链表的基本操作,创建、删除、插入、反转

来源:互联网 发布:杭州大数据培训 编辑:程序博客网 时间:2024/06/09 20:53

直接贴代码,自己学习的记录过程,

#include<iostream>#include<malloc.h>#include<stdlib.h>using namespace std;typedef struct LNode{int data;LNode *next;}LNode,*linklist;void showmenu(){cout<<"***************************function*********************"<<endl<<"**1 creat an list"<<endl<<"**2 print the data of the list"<<endl<<"**3 insert a node to the list"<<endl<<"**4 delete a node of the list"<<endl<<"**5 reverse a list"<<endl<<"********************************************************"<<endl;}/*void choosefun(){cout<<"which you want to do? press 1~5"<<endl;int n;cin>>n;switch(n){case 1:goto fun1;   break;case 2:goto fun2;   break;case 3:goto fun3;   break;case 4:goto fun4;   break;case 5:goto fun5;   break;}}*/void creatlist(linklist &L){cout<<"input the size of the list(integer only):";int n;cin>>n;LNode *p,*q;L=new LNode;L->next=NULL;q=L;cout<<"input n element to creat a list:"<<endl;for(int i=n;i>0;--i){p=new LNode;cin>>p->data;p->next=q->next;q->next=p;q=p;}}void printlist(linklist L){LNode *p;cout<<"the num you input is:"<<endl;p=L->next;while(p!=NULL){cout<<p->data<<"\t";p=p->next;}cout<<endl;}int getelement(linklist L){cout<<"please input the num you want to get or press q to exit:"<<endl;int i;cin>>i;if(i==113)exit(0);int e;int j=1; //counterLNode *p;p=L->next;while(p!=NULL && j<i){p=p->next;++j;}if(!p || j>i)exit(1);e=p->data;return e;}/*void deletelist(linklist &L){int i,j=1;cout<<"please input the num you want to delete or press q to exit:"<<endl;cin>>i;if(i==113)exit(2);LNode *p,*q;p=L->next;q=p;while(p!=NULL && j<i){q=p;p=p->next;++j;}if(!p || j>i)exit(3);q->next=p->next;}*//*//an  unknow issuesvoid deletelist(linklist &L, int key){LNode *p;p=L->next;if(p->data==key){L->next=p->next;}else{while(p->next!=NULL){if(p->next->data!=key)p=p->next;elsep->next=p->next->next;exit(4);}}}*/void deletelist(linklist &L){cout<<"input the num you want to delete or press q to exit:";int key;cin>>key;if(key==113)exit(6);LNode *p,*q;p=L->next;if(p==NULL){cout<<"the list is empty!!!"<<endl;exit(7);}while(p->data!=key && p->next!=NULL){q=p;p=p->next;}if(p->data==key){if(p==L->next){L->next=p->next;}else{q->next=p->next;}}cout<<"the num you input has been deleted!"<<endl;}void insertlist(linklist &L){cout<<"input the location the num insert or press q to exit:";int loc;cin>>loc;if(loc==113)exit(9);LNode *innode;innode=new LNode;if(innode==NULL)exit(9);cout<<"input the num you want to insert or press q to exit:";cin>>innode->data;if(innode->data==113)exit(8);LNode *p;p=L;  //p=L->next;int j=0;while(p!=NULL && j<loc-1){p=p->next;++j;}if(p==NULL || j>loc-1)exit(10);innode->next=p->next;p->next=innode;}void sortlist(linklist &L){}void reverselist(linklist &L){LNode *p;LNode *tmp;tmp = L->next;    while (tmp->next != NULL)    {        p = tmp->next;        tmp->next = p->next;        p->next = L->next;        L->next = p;    }}void main(){linklist L;creatlist(L);showmenu();printlist(L);//int a_getele=getelement(L);//cout<<"a_getele:"<<a_getele<<endl;deletelist(L);printlist(L);insertlist(L);printlist(L);reverselist(L);printlist(L);}


0 0