求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!! 50

求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!求一个C++的对线性表进行插入,删除等基本... 求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!
求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!
求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!
要用类进行编写!!! 敢不敢不要粘别的网站上的 高手都跑哪去了?
展开
 我来答
科技鸟
2009-04-19 · TA获得超过1252个赞
知道小有建树答主
回答量:865
采纳率:0%
帮助的人:0
展开全部
#include<iostream>
using namespace std;
int len=0;
struct list//结构的声明
{
int data;
list *next;
};
list *head;
list *create()//建立链表,这是第一步;
{
list *p,*q;
head=NULL;
int temp;
cout<<"Now create the list,Input the data,end by -1:"<<endl;
cin>>temp;
len=0;
while(temp!=-1)
{
len++;
p=new list;
p->data=temp;
if(head==NULL)
head=p;
else
{
q->next=p;
}
q=p;
cin>>temp;
}
if(head!=NULL)
q->next=NULL;
return head;
}
void display(list *head)//显示链表的所有数据。
{
if(head==NULL)
{
cout<<"The list is empty!"<<endl;
return;
}
cout<<"the list is:"<<endl;
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}
void search(list *phead)//输入序号查找它的数据,验证此序号的数是否存在。
{
//cout<<"len="<<len<<endl;
int m;
list *r=NULL;
int count;
cout<<"Input the number you want to search:"<<endl;
cin>>m;
do
{
if(len<m||m<=0)
do
{
cout<<"not found,input the number you want to search,end by -1 !"<<endl;
cin>>m;

}while(len<m&&m!=-1);
if(m==-1)
break;
count=1;
r=new list;
r=phead;
while(1)
{
if(m==1)
{
cout<<"the data you want to search is: "<<endl;
cout<<r->data<<endl;
break;
}
else
{
count++;
r=r->next;
if(count==m)
{
cout<<"the data you want to search is: "<<endl;
cout<<r->data<<endl;
break;
}
if(r==NULL)
{
cout<<"Not found!"<<endl;
break;
}
}
}
cout<<"Input the number you want to search,end by -1:"<<endl;
cin>>m;
}while(m!=-1);
}
void find(list *head)//输入数据查找它的位置
{
int m,located=1,g;
list *temp1;
temp1=head;
cout<<"input the data you want to search: "<<endl;
cin>>m;
do
{
g=0;

if(temp1->data==m)
{
cout<<"have found,it located in "<<located<<endl;
located=1;
temp1=head;
cout<<"input the data you want to search,end by -1:"<<endl;
cin>>m;
}
else
{
located++;
temp1=temp1->next;
}
if(temp1==NULL)
{
cout<<"not found!input the data you want to search,end by -1:"<<endl;
located=1;
temp1=head;
cin>>m;
}
}while(m!=-1);
}
void insert(list *&head)//插入数据
{
int node,data,count;
struct list *temp1=NULL,*temp2=NULL,*temp3=NULL;

//temp=head;
cout<<"input the node and data:\n";
cin>>node>>data;
count=1;
if(node>len||node<=0)
{
do
{
cout<<"the node inputed wrong!try to input again:"<<endl;
cin>>node>>data;
}while(node>len||node<=0);
//return;
}
temp1=new list;
temp1->data=data;
if(head==NULL)
{
head=temp1;
head->next=NULL;
len++;
return ;
}
if(node==1)
{
temp1->next=head;
head=temp1;
len++;
return ;
}

temp2=head;
temp3=head->next;
while(temp3!=NULL)
{
count++;
if(count<node)
{
temp2=temp3;
temp3=temp3->next;
}
else
break;
}
temp2->next=temp1;
temp1->next=temp3;
len++;
return ;
}
list *del_node(list *&head)//输入节点删除对应的数据
{
cout<<"input the node you want to delete:"<<endl;
int node,count=1;
cin>>node;
if(node<=0||node>len)
{
do
{
cout<<"input wrong,try again!"<<endl;
cin>>node;
}while(node<=0||node>len);
}
if(head==NULL)
{
len--;
return head;

}
list *temp1=NULL,*temp2=NULL;

temp1=head;
if(node==1)
{
head=temp1->next;
delete temp1;
len--;
return head;
}
temp2=temp1->next;
while(temp2!=NULL)
{
count++;
if(count==node)
{
temp1->next=temp2->next;
delete temp2;
len--;
return head;
}
temp1=temp2;
temp2=temp2->next;
}
return head;
}
void del_data(list *&head)//输入数据删除对应的节点
{
cout<<"input the data you want to delete:"<<endl;
int data;
cin>>data;
list *temp1=NULL,*temp2=NULL;
temp1=head;
if(temp1->data==data)
{
head=temp1->next;
delete temp1;
len--;
return;
}
temp2=temp1->next;
while(temp2!=NULL)
{
if(temp2->data==data)
{
temp1->next=temp2->next;
delete temp2;
len--;
return;
}
temp1=temp2;
temp2=temp2->next;
}
cout<<"Not found the data!"<<endl;
return;
}
void main()
{
list *head=NULL;
head=create();
int choose;
cout<<"\nInput 1: display the list.\n"
<<"Input 2: input location to search the data.\n"
<<"Input 3: input the data to find its location.\n"
<<"Input 4: insert the data."
<<"\nInput 5: delete the node's data:"
<<"\nInput 6: delete the data you input:"
<<endl;
cin>>choose;
do{
switch(choose)//构建一个菜单供客户选择操作。
{
case 1:display(head);break;
case 2:search(head);break;
case 3:find(head);break;
case 4:insert(head);break;
case 5:del_node(head);break;
case 6:del_data(head);break;
default:cout<<"input wrong!try again!"<<endl;break;
}
cout<<"Input the choose,end by -1"<<endl;
cin>>choose;
}while(choose!=-1);

}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
伟大复兴1234
2009-04-20 · 超过11用户采纳过TA的回答
知道答主
回答量:27
采纳率:0%
帮助的人:0
展开全部
这谁提问阿
怎么这么像我们最新的课程
难道你是40班的..
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式