數據結構 P28-29 算法實現 線性表的鏈式存儲結構——鏈表的查找、插入與刪除

/*鏈表的創建與查找-2.8-插入-2.9-刪除-2.10*/

#include<iostream>
using namespace std;

struct node
{
int date;           //數據域
node *next;      //指針域
};

int main()
{
int x=1;
node *head,*p;          //定義頭結點和節點
head= new node();   //動態分配頭結點
p=head;                    //初始化節點
p->date=0;              //指向頭結點的數據 數據爲0
p->next=NULL;        //指向下一個節點的地址

while(x<7)                   
{
p->next=new node();      //動態分配節點
p=p->next;                 //指向下一個節點

p->date=x;                     //給節點賦值
++x;
}
p=head;
cout<<"List : ";

for(int i=0;i<x;++i)           //輸出鏈表
{
cout<<p->date<<",";
p=p->next;
}

cout<<endl;
cout<<"x= "<<x<<endl;
int d;
cout<<"請輸入要查找的位置: ";
while(1){
cin>>d;
if(d>x)
continue;
else
break;
}
p=head;
for(int i=0;i<x;++i)                  //查找
{ if(i==d-1)
{ cout<<"第"<<d<<"位地址所保存的數據爲:";cout<<p->date;break;}
p=p->next;
}

cout<<endl;
int b,a;
cout<<"請輸入要插入的數";
cin>>b;
cout<<"請輸入要插入的位置";
cin>>a;
node *s;
s=new node();
s->date=b;
p=head;
for(int i=0;i<x;++i)                  //插入
{ if(i==a-2)
{  
s->next=p->next;
        p->next=s;
break;
}
p=p->next;
}
p=head;
for(int i=0;i<x+1;++i)                
{cout<<p->date<<",";
p=p->next;}

node *q;
cout<<endl<<"請輸入你想刪除的位置";
cin>>d;
p=head;
for(int i=0;i<x;++i)                  //刪除
{ if(i==d-2)
{
q=p->next;
p->next=q->next;
delete q;
break;
}
p=p->next;
}
p=head;
for(int i=0;i<x;++i)                
{cout<<p->date<<",";
p=p->next;}
while (1)
{
}
return 0;
}

/*頭插法*/

#include<iostream>

int a,b;
using namespace std;

 struct node
{
       int date;
  node *next;
 };

 class List
 {
node *head;
 public:
List() {head=NULL;}
void inser(int b);
void output();
 };

 void List:: inser(int b)
 {
node *p,*s;  //p指向鏈表節點的指針 s新增的節點
cout<<"請輸入要插入的數據:";
cin>>b;
p=head;
s=new node();               //創建一個新的節點s
s->date=b;
     s->next=NULL;
if(head==NULL)            //如果沒有節點 則插入的節點爲頭結點
        head=s;
else
           {
  s->next=p;         //把頭結點作爲新節點的下一個節點地址
          head=s;              //把新插入的節點作爲頭結點
      }
 }

 void List::output()
 {
node *p;
p=head;
cout<<"現在的鏈表爲:";
while (p->next!=NULL)
{
cout<<p->date<<",";
p=p->next;
}
cout<<p->date<<endl;;
 }

int main()
{
List L;

while (1)
{
L.inser(b);
    L.output();
}
    return 0;
}

/*尾插法*/

#include<iostream>
#define OK 1
#define  ERROR 0
int a,b;
using namespace std;

 struct node
{
       int date;
  node *next;
 };

 class List
 {
node *head;
 public:
List() {head=NULL;}
void inser(int b);
void output();
 };

 void List:: inser(int b)
 {
node *p,*s;  //p指向鏈表節點的指針 s新增的節點
cout<<"請輸入要插入的數據:";
cin>>b;
p=head;
s=new node();
s->date=b;
     s->next=NULL;
if(head==NULL)
        head=s;
else
     {
while(p->next!=NULL) //把指針移向最後一個節點
{
p=p->next;
}
p->next=s;           //把新節點作爲最後一個節點的下一個節點地址
     }
 }


 void List::output()
 {
node *p;
p=head;
cout<<"現在的鏈表爲:";
while (p->next!=NULL)
{
cout<<p->date<<",";
p=p->next;
}
cout<<p->date<<endl;
 }


int main()
{
List L;

while (1)
   {
L.inser(b);
    L.output();
}
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章