?? 單向鏈表.txt
字號:
#include<iostream.h>
#include<stdlib.h>
#define NULL 0
struct list{
int d;
list *next;
};
list *head=(list *)malloc(sizeof(list));
list *p1=head;
int e,d;
void insert()
{
if(p1==NULL)
{
cout<<"鏈表是空的,請插入元素:"<<endl;
}
list *p2;p2=(list *)malloc(sizeof(list));
p1->next=p2;
p1=p2;
cout<<"請輸入你要插入的位置,在第個元素前面:"<<endl;
int i,j=1;cin>>i;
cout<<"請輸入你要插入的元素:"<<endl;
cin>>d;
while(j<i)
{
p1=p1->next;++j;
}
p2=p1->next;
p1->next=p2;
p2->d=e;
return;
}
void deleted()
{
if(p1==NULL)
{
cout<<"鏈表已空,無元素可刪!"<<endl;
}
cout<<"請輸入你要刪除的第i元素:"<<endl;
int i,j=1;cin>>i;
list *p2;p2=(list *)malloc(sizeof(list));
p2=p1;
while(j<i)
{
p2=p2->next;++j;
}
p2=p2->next->next;
delete p2->next;
cout<<"刪除完畢!"<<endl;
return;
}
void find()
{
if(p1==NULL)
{
cout<<"鏈表是空的,無數據可查!"<<endl;
}
list *p2;p2=(list *)malloc(sizeof(list));
p1->next=p2;
p1=p2;
cout<<"請輸入你要查找的元素的位置:第i個元素。"<<endl;
int i,j=1;cin>>i;
while(j<i)
{
p2=p2->next;++j;
}
cout<<"你要找的數據是:"<<p2->d<<endl;
return ;
}
void change()
{
if(p1==NULL)
{
cout<<"鏈表是空的,無數據修改!"<<endl;
}
list *p2;
p2=(list *)malloc(sizeof(list));
p1->next=p2;
p1=p2;
cout<<"請輸入你要修改的元素的位置:第i個元素。"<<endl;
int i,j=1;cin>>i;
while(j<i)
{
p2=p2->next;++j;
}
cout<<"你要修改的數據是:"<<p2->d<<endl;
cout<<"請輸入新的數據:"<<endl;
cin>>e;
p2->d=e;
cout<<"修改完畢!"<<endl;
return ;
}
void main()
{
cout<<"請輸入鏈表的長度:"<<endl;
int k;cin>>k;
cout<<"請輸入數據:"<<endl;
list *p2;
p2=head->next;
for( int i=1;i<=k;i++)
{
cin>>p2->d;p2=p2->next;;
}
p2->next=NULL;
p2=p1;
cout<<"鏈表的數據是 "<<endl;
for(i=1;i<=k;i++)
{
cout<<p2->d<<" ";p2=p2->next;
}
look:cout<<" 請輸入你將要進行的操作: 1.插入元素."<<endl;
cout<<" 2.刪除元素."<<endl;
cout<<" 3.查找元素."<<endl;
cout<<" 4.修改數據."<<endl;
cout<<" 5.退出程序."<<endl;
int cn; cin>>cn;
switch(cn)
{
case 1:insert();break;
case 2:deleted();break;
case 3:find();break;
case 4:change();break;
case 5:cout<<"程序已退出,謝謝您的使用!再見!"<<endl; return;
default:cout<<"輸入有誤,請重新輸入. 提示: 輸入1.插入元素;輸入2.刪除元素"<<endl;
break;
}
goto look;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -