数据结构之单链表实现(C++)

来源:互联网 发布:网络信息安全检查表 编辑:程序博客网 时间:2024/05/18 01:48
#include <iostream>#include <iomanip>using namespace std;typedef  int ElemType;typedef struct LNode{ElemType data;struct LNode *next;}LNode,*LinkList;class Linear{private:LNode* LinkList;        // the head pointer of the linked listint length;// the current length of the linked listpublic:Linear();// the constructor function of the Linear class~Linear();// the destructor function of the Linear classbool clearList();// delete all the nodes to clear the listbool isEmpty();// check out whether the list is empty int getLen();// get the length of the listvoid insertData(const ElemType locaData,const ElemType &e); // insert the new node whose data is equal to evoid insertNode(int i,const ElemType &e);void deleteNode(int i);                                // delete the No.i nodevoid deleteData(const ElemType &e);// delete the new node whose data is equal to e;void traverList();// traverse the list and output all the data in the listvoid getElem(int i,ElemType &e);// get the data of the No.i nodeint locateElem(const ElemType &e);// find the node whose data is equal to e;if not exit,return -1LNode* getHead();// return the address of the head pointor};Linear::Linear()// the constructor function of the Linear class{LNode *fNode=new LNode;LinkList=fNode;fNode->data=length=0;fNode->next=NULL;}Linear::~Linear()// the destructor function of the Linear class{if(!clearList())clearList();}bool Linear::clearList()// delete all the nodes to clear the list{LNode *pTemp=LinkList;while(pTemp->next!=NULL){delete pTemp;pTemp=pTemp->next;length--;}delete pTemp;return true;}bool Linear::isEmpty()// check out whether the list is empty {if(length!=0)return false;elsereturn true;}int Linear::getLen()// get the length of the list{return length;}void Linear::insertData(const ElemType locaData,const ElemType &e)  // insert the new node whose data is equal to e;{ if(locateElem(locaData)){LNode *newNode=new LNode;newNode->data=e;LNode *pTemp=LinkList->next;for(int i=1;i<length;i++){if(i==locateElem(locaData))break;pTemp=pTemp->next;}newNode->next=pTemp->next;pTemp->next=newNode;length++;}elsecout<<" There exits no corresponding data in the table"<<endl;}void Linear::insertNode(int i,const ElemType &e){LNode *pTemp=LinkList;for(int j=0;j<length;j++){if(j==i-1)break;pTemp=pTemp->next;}LNode *newNode=new LNode;newNode->data=e;newNode->next=pTemp->next;pTemp->next=newNode;length++;}void Linear::deleteNode(int i)                             // delete the No.i node{if(i<1||i>length){cout<<"The data you enter is illegal"<<endl;}else{LNode *pTemp=LinkList->next;for(int j=1;j<length+1;j++){if(j==i-1)break;pTemp=pTemp->next;}pTemp->next=pTemp->next->next;length--;}}void Linear::deleteData(const ElemType &e)// delete the new node whose data is equal to e;{if(locateElem(e)){int nTemp=locateElem(e);LNode *newNode=new LNode;LNode *p=LinkList;for(int i=0;i<length;i++){if(i==nTemp-1)break;p=p->next;}p->next=p->next->next;length--;}elsecout<<" There exits no corresponding data in the table"<<endl;}void Linear::traverList()// traverse the list and output all the data in the list{cout<<"当前链表数据元素为:"<<endl;LNode *pTemp=LinkList;while(pTemp->next!=NULL){cout<<pTemp->next->data<<"";pTemp=pTemp->next;}cout<<"当前链表长度为"<<getLen()<<endl;}void Linear::getElem(int i,ElemType &e)// get the data of the No.i node{if(i<1||i>length){cout<<"The data you entered is illegal."<<endl;return;}else{LNode *pTemp=LinkList->next;for(int j=1;j<length+1;j++){if(j==i){e=pTemp->data;break;}pTemp=pTemp->next;}}}int Linear::locateElem(const ElemType &e)// find the node whose data is equal to e;if not exit,return -1{bool bFind=false;LNode *pTemp=LinkList->next;for(int i=1;i<length+1;i++){if(pTemp->data==e){bFind=true;return i;}pTemp=pTemp->next;}return bFind;}LNode* Linear::getHead()// return the address of the head pointor{return LinkList;}

void welcome(){cout<<endl;cout<<"              欢迎使用                       "<<endl;cout<<"请选择功能"<<endl;cout<<"##1.遍历链表##2.插入元素"<<endl;cout<<"##3.删除元素"<<endl;cout<<"##4.得到头指针地址   ##5.判断链表是否为空"<<endl;cout<<"##6.链表长度      ##7.得到某一结点元素"<<endl;cout<<"##8.退出程序  ##9.清除屏幕"<<endl;cout<<endl;cout<<endl;}int main(){Linear l;char option;while(1){welcome();while(1){cout<<"请输入您要选择的功能"<<endl;cin>>option;switch(option){case '1':l.traverList();break;case '2':{cout<<"请选择插入方式,如果输入非零数,则按值插入,如果输入为0,则按位插入。";int op;cin>>op;if(op==0){int n;cout<<"请输入要插入结点的位置序号"<<endl;cin>>n;if(n<1||n>l.getLen()+1){cout<<"数据不合法"<<endl;break;}cout<<"请输入要插入结点的数据域"<<endl;int e;cin>>e;l.insertNode(n,e);}else{int n;cout<<"请输入要插入结点的位置数据域"<<endl;cin>>n;if(l.locateElem(n)<0||l.locateElem(n)>l.getLen()){cout<<"数据不合法"<<endl;break;}cout<<"请输入要插入新结点的数据域"<<endl;int e;cin>>e;l.insertData(n,e);}break;}case '3':{cout<<"请选择删除方式,如果输入非零数,则按值删除,如果输入为0,则按位删除。";int op;cin>>op;if(op==0){int n;cout<<"请输入要删除结点的位置序号"<<endl;cin>>n;l.deleteNode(n);}else{cout<<"请输入要删除的结点的数据域"<<endl;int e;cin>>e;l.deleteData(e);}break;}case '4':{cout<<"当前链表头指针地址是"<<l.getHead()<<endl;break;}case '5':{if(l.isEmpty())cout<<"当前链表是空链表"<<endl;elsecout<<"当前链表是非空链表"<<endl;break;}case '6':{cout<<"当前链表长度是"<<l.getLen()<<endl;break;}case '7':{int n,e=-1;cout<<"请输入您要查找的结点位置"<<endl;cin>>n;l.getElem(n,e);if(e!=-1)cout<<"数据是"<<e<<endl;break;}case '8':{exit(1);}case '9':{system("cls");welcome();break;}default:cout<<"输入不合法"<<endl;break;}}}return 0;}

0 0