#include <iostream> #include <cstdlib> using namespace std; struct node{ int data; node *next; }; struct node *head=NULL; /*用于摧毁单链表的全部数据*/ void DestoryList() { head=NULL; cout<<"Destiryed The List!"<<endl; } /*用于清除单链表的数据*/ void ClearList() { head=NULL; cout<<"Clear Now!"<<endl; } /*判断单链表是否为空*/ bool ListEmpty() { if(head==NULL){ cout<<"List is empty"<<endl; return true; } return false; } /*用于输出单链表的长度*/ int ListLength() { if(head==NULL){ return 0; } else{ int i=0; node *p; p=head; while(p){ i++; p=p->next; } return i; } } /*用于找到第i个元素,并返回其值*/ int GetElem(int i) { if(i<=0){ cout<<"input too small"<<endl; exit(-1); } int j=1; if(ListEmpty()){ cout<<"List is empty"<<endl; exit(-1); } node *p; p=head; while(j!=i && p){ p=p->next; j++; } if(p==NULL){ cout<<"input too lager"<<endl; exit(-1); } else{ return p->data; } } /*用于向链表添加新的元素*/ void AddList(node *a) { if(head==NULL){ head=a; a->next=NULL; } else{ node *p; p=head; while(p){ if(p->next==NULL){ p->next=a; a->next=NULL; break; } p=p->next; } } } /*用于输出链表的所有元素*/ void PrintList() { if(!head){ cout<<"list is empty"<<endl; return; } node *p; p=head; int j=0; while(p){ cout<</*"data"<<*/p->data/*<<"address"<<p*/<<endl; j++; p=p->next; } cout<<"This List have "<<j<<" numbers"<<endl; } /*返回单链表中满足关系的第一个数的位置*/ int LocateElem(int e,char c) { if(c=='='){ if(ListEmpty()){ exit(-1); } int i=1; node *p; p=head; while(p){ if(p->data==e){ return i; } i++; p=p->next; } } return -1; } /*用于在第i个位置插入一个元素*/ void ListInsert(int i,node *a) { int j=1; if(i<1){ exit(-1); } node *p; p=head; if(head==NULL && i==1) { head=a; a->next=NULL; return; } if(head==NULL){ cout<<"ERROR"<<endl; exit(-1); } if(i==1){ a->next=p; head=a; return; } while(j<i-1 && p){ j++; p=p->next; if(p==NULL){ cout<<"error"; exit(-1); } } // cout<<"insert p's data"<<p->data<<endl; a->next=p->next; p->next=a; // cout<<"a.data:"<<a->data<<endl; } /*用于删除第i个元素*/ int ListDelete(int i) { int j=1; if(i<1){ exit(-1); } if(ListEmpty()){ exit(-1); } node *p,*q; p=head; if(i==1){ head=p->next; } while (j<i-1 && p){ p=p->next; j++; if(p==NULL){ cout<<"ERROR"<<endl; exit(-1); } } cout<<"p's data;"<<p->data<<endl; q=p->next; if(q==NULL){ exit(-1); } p->next=q->next; cout<<"q's address"<<p<<" q->next's address"<<p->next<<" p->next's address"<<p->next<<endl; return q->data; } int main() { node *a; for(int i=1; i<19; i++){ a=new node; a->data=i; AddList(a); } /**用于测试destory and clear函数 DestoryList(); cout<<head<<endl; ClearList(); cout<<head<<endl; */ /**用于测试empty函数 cout<<"First test:\t"<<ListEmpty()<<endl; ClearList(); cout<<"Second test:\t"<<ListEmpty()<<endl; */ /**用于测试length函数 cout<<"First test:\t"<<ListLength()<<endl; a=new node; a->data=999; AddList(a); cout<<"Second test:\t"<<ListLength()<<endl; ClearList(); cout<<"Third test:\t"<<ListLength()<<endl; */ /**用于测试get函数 cout<<"1:\t"<<GetElem(-1)<<endl; cout<<"2:\t"<<GetElem(0)<<endl; cout<<"3:\t"<<GetElem(20)<<endl; cout<<"4:\t"<<GetElem(10)<<endl; ClearList(); cout<<"5:\t"<<GetElem(18)<<endl; */ /**用于测试locate函数 ClearList(); for(int i=1; i<19; i++){ a=new node; a->data=1; AddList(a); } cout<<"1:\t"<<LocateElem(2,'=')<<endl; cout<<"2:\t"<<LocateElem(0,'=')<<endl; cout<<"3:\t"<<LocateElem(1,'=')<<endl; ClearList(); for(int i=1; i<19; i++){ a=new node; a->data=i; AddList(a); } cout<<"4:\t"<<LocateElem(0,'=')<<endl; cout<<"5:\t"<<LocateElem(20,'=')<<endl; cout<<"6:\t"<<LocateElem(10,'=')<<endl; */ /**用于测试insert函数 a=new node; a->data=-1; ListInsert(-1,a); PrintList(); ListInsert(0,a); PrintList(); ListInsert(1,a); PrintList(); ListInsert(2,a); PrintList(); a=new node; a->data=-1; ListInsert(3,a); PrintList(); ListInsert(22,a); ListInsert(19,a); PrintList(); ClearList(); ListInsert(1,a); ListInsert(3,a); */ /**用于测试delete函数 ListDelete(-1); ListDelete(0); ListDelete(1); ListDelete(2); ListDelete(19);//由于系统打不开debug文件还未测试; ClearList(); ListDelete(10); PrintList(); */ /*TO found error*/ // node *p; // p=head; // while(p){ // cout<<p->data<<endl; // p=p->next; // } /* eng */ return 0; }