单链表的初始化,增删改查主要思想记录
#includeusing namespace std; struct LNode{ int data; LNode *next; }; //在第i位后面添加e bool NextInsert(LNode *L,int i,int e){ if(i<1||L->next==NULL) return false; LNode *p=new LNode; p=L->next; for(int j=0;p!=NULL&&j p=p->next; } LNode *r=new LNode; r->next=p->next; r->data=e; p->next=r; return true; } //按序号查找 LNode *GetElem(LNode *L,int i){ if(i<1) return NULL; if(i==0) return L; LNode *p=new LNode; p=L->next; for(int j=0;p!=NULL&&j p=p->next; } return p; } //按值查找 int LocateElem(LNode *L,int e){ LNode *p=L->next; int i=1; while(p!=NULL){ if(p->data==e) return i; else{ p=p->next; i++; } } return 0; } //初始化 LNode *List_TailInsert(LNode *L){ L=new LNode[1]; int x; cin>>x; LNode *r=L,*p; while(x!=9999){ p=new LNode; p->data=x; r->next=p; r=p; cin>>x; } p->next=NULL; return L; } //打印单链表 void PrintList(LNode *L){ LNode *p=L->next; while(p!=NULL){ cout< data<<" "; p=p->next; } cout< LNode *p=L->next; LNode *r; while(p!=NULL){ r=p; p=p->next; delete r; } } int main(void){ LNode *L=new LNode[1]; L->next=NULL; cout<<"请输入数据,输入9999结束:"< >i>>x; if(NextInsert(L,i,x)){ PrintList(L); } int j; LNode *t; cout<<" 请输入查询第几位"< >j; t=GetElem(L,j); cout< data< >k; k=LocateElem(L,k); cout<



