1. 程式人生 > >單鏈表彙總(建立,查詢,處理,刪除,……)

單鏈表彙總(建立,查詢,處理,刪除,……)

單鏈表彙總

#include <iostream>
#include <cstdlib>
using namespace std;

//--------------------------------

struct node
{
    int data;
    node *next;
}*head,*p,*r;

//--------------------------------

void get(node *head,int i)//查詢第i個結點的資料域 
{ 
    int j;
    p=head->next;
    j=1;
    while
((p!=NULL)&&(j<i)) { p=p->next; j++; } if((p!=NULL)&&(j==i)) cout<<"已找到您要的結點資料:"<<endl<<p->data<<endl<<endl; else cout<<"i not exsit!"<<endl<<endl; } //--------------------------------
void insert(node *head,int i,int x)//插入到第i個結點 { node *s; int j; p=head; j=0; while((p!=NULL)&&(j<i-1)) { p=p->next; j++; } if(p==NULL) cout<<"no this position!"<<endl; else { s=new node; s->data=x; s->next=p->next; p->next=s; cout
<<"已插入"<<endl<<endl; } } //-------------------------------- void deletes(node *head,int i)//刪除第i個結點的資料 { node *p,*s; int j; p=head; j=0; while((p->next!=NULL)&&(j<i-1)) { p=p->next; j++; } if(p->next==NULL) cout<<"no this position"<<endl<<endl; else { s=p->next; p->next=p->next->next;//or p->next=s->next; cout<<"已刪除"<<endl<<endl; free(s); } } //-------------------------------- int len(node *head)//求單鏈表的實際長度 { int n=0; p=head; while(p!=NULL) { n++; p=p->next; } return n-1; } //-------------------------------- void searchfirst(node *head,int x)//查詢第一個滿足一定條件的結點 { p=head->next; while((p->data!=x)&&(p->next!=NULL)) p=p->next; if(p->data==x)//找到處理 p->data=0-p->data;//處理示例 else cout<<"no found!"<<endl; } //-------------------------------- void searchall(node *head,int x)//查詢所有滿足一定條件的結點 { p=head->next; int n=0; while(p->next!=NULL) { if(p->data==x)//找到處理 n++;//處理示例 p=p->next; } if(p->data==x) n++; if(n!=0) cout<<n<<"個滿足條件"<<endl; else cout<<"no found!"<<endl; } //-------------------------------- int x; int main() { cout<<"//----------------------//"<<endl; cout<<"請輸入連結串列資料,輸入-1結束"<<endl; cout<<"//----------------------//"<<endl; cin>>x; head=new node; head->next=NULL; r=head; while(x!=-1) { p=new node; p->data=x; p->next=NULL; r->next=p; r=p; cin>>x; } p=head->next; int q=1; cout<<endl<<endl<<endl; while(q!=0) { cout<<"輸入任意非0數繼續操作"<<endl; cout<<"輸入0結束操作"<<endl; cin>>q; if(q==0) break; cout<<"-------------------------------"<<endl; cout<<"輸入1查詢結點"<<endl; cout<<"輸入2插入結點"<<endl; cout<<"輸入3刪除結點"<<endl; cout<<"輸入4查詢長度"<<endl; cout<<"輸入5查詢第一個=x的結點進行取反"<<endl; cout<<"輸入6查詢所有=x的結點個數"<<endl; cout<<"-------------------------------"<<endl; cout<<"輸入0列印當前連結串列"<<endl; cout<<"-------------------------------"<<endl; int choose; cin>>choose; switch(choose) { case 1: { cout<<"請輸入您要查詢的結點編號:"<<endl; cin>>x; get(head,x); continue; } case 2: { cout<<"請輸入您要插入的結點編號:"<<endl; int i; cin>>i; cout<<"請輸入新"<<i<<"結點的資料:"<<endl; cin>>x; insert(head,i,x); continue; } case 3: { cout<<"請輸入您要刪除的結點編號:"<<endl; cin>>x; deletes(head,x); continue; } case 4: { cout<<"當前單鏈表長度為:"<<len(head)<<endl<<endl; continue; } case 5: { cout<<"輸入x:"<<endl; cin>>x; searchfirst(head,x); cout<<"已完成"<<endl; continue; } case 6: { cout<<"輸入x:"<<endl; cin>>x; searchall(head,x); cout<<"已完成"<<endl; continue; } case 0: { cout<<"當前連結串列為:"<<endl; p=head->next; while(p->next!=NULL) { cout<<p->data<<" "; p=p->next; } cout<<p->data<<endl; continue; } } } cout<<"//-操作已結束-//"<<endl<<endl<<endl<<endl; cout<<"//------------//"<<endl; cout<<"是否要列印連結串列?"<<endl; cout<<"輸入:1列印,2結束"<<endl; cout<<"//------------//"<<endl; cin>>q; if(q==1) { p=head->next; cout<<"連結串列列印中,請稍等……"<<endl; while(p->next!=NULL) { cout<<p->data<<" "; p=p->next; } cout<<p->data<<endl; } cout<<"已結束"<<endl<<"關閉"; system("pause"); return 0; }