使用C++,用头插法尾插法,建立链表实现输出、查找、插入、删除、链表合并 。谢谢
1个回答
展开全部
struct node
{
int data;
node *next;
};
node * createonhead()
{
node *head=NULL;
node *p;
int d;
cin>>d;
while(d!=0)
{
p=new node;
p->data=d;
p->next=NULL;
if(head==NULL)
head=p;
else
{
p->next=head;
head=p;
}
cin>>d;
}
return head;
}
node * createontail()
{
node *head=NULL;
node *p,*q;
int d;
cin>>d;
while(d!=0)
{
p=new node;
p->data=d;
p->next=NULL;
if(head==NULL)
{
head=p;
q=p;
}
else
{
q->next=p;
q=p;
}
cin>>d;
}
return head;
}
void show(node *head)
{
node *p;
p=head;
while(p!=NULL)
{
cout<<p->data<<"\t";
p=p->next;
}
cout<<endl;
}
void find(int n,node *head)
{
node *p;
p=head;
while(p->data!=n && p!=NULL)
p=p->next;
if(p->data==n)
cout<<"found data"<<endl;
if(p==NULL)
cout<<"not found data!"<<endl;
}
node * insert(node *h,int n,int d)
{
node *p,*q;
p=h;
while(p!=NULL && p->data!=n)
p=p->next;
if(p==NULL)
{
cout<<"no found insertion station"<<endl;
exit(0);
}
if(p->data==n)
{
if(p==h)
{
q=new node;
q->data=d;
q->next=h;
h=q;
}
else
{
q=h;
while(q->next!=p)
q=q->next;
p=new node;
p->data=d;
p->next=q->next;
q->next=p;
}
}
return h;
}
node *drop(int n,node *head)
{
node *p,*q;
p=head;
while(p->data!=n && p!=NULL)
p=p->next;
if(p==NULL)
{
cout<<"no node you want to delete\n";
exit(0);
}
if(p->data==n)
{
if(p==head)
head=head->next;
delete p;
}
else
{
q=head;
while(q->next!=p)
q=q->next;
q->next=p->next;
delete p;
}
return head;
}
node * join(node *h1,node *h2)
{
node *head;
head=h1;
p=head;
while(p->next!=NULL)
p=p->next;
p->next=h2;
return head;
}
{
int data;
node *next;
};
node * createonhead()
{
node *head=NULL;
node *p;
int d;
cin>>d;
while(d!=0)
{
p=new node;
p->data=d;
p->next=NULL;
if(head==NULL)
head=p;
else
{
p->next=head;
head=p;
}
cin>>d;
}
return head;
}
node * createontail()
{
node *head=NULL;
node *p,*q;
int d;
cin>>d;
while(d!=0)
{
p=new node;
p->data=d;
p->next=NULL;
if(head==NULL)
{
head=p;
q=p;
}
else
{
q->next=p;
q=p;
}
cin>>d;
}
return head;
}
void show(node *head)
{
node *p;
p=head;
while(p!=NULL)
{
cout<<p->data<<"\t";
p=p->next;
}
cout<<endl;
}
void find(int n,node *head)
{
node *p;
p=head;
while(p->data!=n && p!=NULL)
p=p->next;
if(p->data==n)
cout<<"found data"<<endl;
if(p==NULL)
cout<<"not found data!"<<endl;
}
node * insert(node *h,int n,int d)
{
node *p,*q;
p=h;
while(p!=NULL && p->data!=n)
p=p->next;
if(p==NULL)
{
cout<<"no found insertion station"<<endl;
exit(0);
}
if(p->data==n)
{
if(p==h)
{
q=new node;
q->data=d;
q->next=h;
h=q;
}
else
{
q=h;
while(q->next!=p)
q=q->next;
p=new node;
p->data=d;
p->next=q->next;
q->next=p;
}
}
return h;
}
node *drop(int n,node *head)
{
node *p,*q;
p=head;
while(p->data!=n && p!=NULL)
p=p->next;
if(p==NULL)
{
cout<<"no node you want to delete\n";
exit(0);
}
if(p->data==n)
{
if(p==head)
head=head->next;
delete p;
}
else
{
q=head;
while(q->next!=p)
q=q->next;
q->next=p->next;
delete p;
}
return head;
}
node * join(node *h1,node *h2)
{
node *head;
head=h1;
p=head;
while(p->next!=NULL)
p=p->next;
p->next=h2;
return head;
}
更多追问追答
追问
这个不能运行啊,编译一堆错误得。
追答
你得在头上写
#include
using namespace std;
最后还得自己写个主函数呢。
int main()
{}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询