大虾,帮个小忙
#include <iostream>using namespace std;
typedef struct node
{
char data;
struct node *next;
}link;
link * get(link *l, int i)
{
link *p;int j=0;
p=l;
while((j<i) && (p->next!=NULL))
{p=p->next;j++;}
if(j==i)
return p;
else
return NULL;
}
link * ins (link *l, char ch,int i)
{ link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"输入有误"<<endl;
else
{
s=(link *)malloc(sizeof(link));
s->data=ch;
s->next=p->next;
p->next=s;
}
return l;
}
link * find(link *l, char ch)
{
link *p; int i=0; int j=0;
p=l;
while(p!=NULL)
{ i++;
if(p->data!=ch)
p=p->next;
else {cout<<"您查找的数据在第"<<i-1<<"个位置."<<endl;
j=1;p=p->next;
}
}
if(j!=1)
cout<<"您查找的数据不在线性表中."<<endl;
return l;
}
link * del(link *l, int i)
{
link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"输入有误"<<endl;
else
{
s=p->next;
p->next=s->next;
free(s);
}
return l;
}
link * add(link *l )
{
link *p,*s;
cout<<"请输入一串单字符数据,以#结束!"<<endl;
char ch;
link *HEAD;
link *R,*P,*L;
HEAD=(link *)malloc(sizeof(link));
HEAD->next=NULL;
R=HEAD;
getchar();
ch=getchar();
while(ch!='#')
{
P=(link *)malloc(sizeof(link));
P->data=ch;P->next=NULL;
R->next=P;R=R->next;
getchar();
ch=getchar();
}
L=HEAD;
cout<<"当前输入的线性表为:"<<endl;
P=L;P=P->next;
if(L!=NULL)
do
{cout<<P->data<<" ";
P=P->next;
}while(P!=NULL);
cout<<endl;
p=l;
while(p->next!=NULL)
p=p->next;
s=L;
p->next=s->next;
p=l;
return l;
}
link * print(link *l)
{ int i,k;
char ch;
link *p,*q;
cout<<"当前线性表为:"<<endl;
p=l;p=p->next;
if(l!=NULL)
do
{cout<<p->data<<" ";
p=p->next;
}while(p!=NULL);
cout<<endl;
cout<<"请选择您要的操作:";
cout<<" 1、插入";
cout<<" 2、查找";
cout<<" 3、删除";
cout<<" 4、合并";
cout<<" 0、退出";
cout<<endl;
cin>>k;
if(k==1)
{
cout<<"请输入您要插入的数据值:";
cin>>ch;
cout<<"请输入您要插入的位置:";
cin>>i;
p=ins(l,ch,i);
q=print(l);
}
else if(k==2)
{
cout<<"请输入您要查找的数据值:";
cin>>ch;
p=find(l,ch);
q=print(l);
}
else if(k==3)
{
cout<<"请输入您要删除的数据的位置:";
cin>>i;
p=del(l,i);
q=print(l);
}
else if(k==4)
{ p=add(l);
q=print(l);
}
else if(k==0)
;
else
{cout<<"输入错误!"<<endl;
q=print(l);}
return l;
}
int main()
{
cout<<"请输入一串单字符数据,以#结束!"<<endl;
char ch;
//link *head;
link *r,*p,*q,*l;
l=(link *)malloc(sizeof(link));
l->next=NULL;
r=l;
ch=getchar();
// getchar();
while(ch!='#')
{
p=(link *)malloc(sizeof(link));
p->data=ch;p->next=NULL;
r->next=p;r=r->next;
ch=getchar();
// getchar();
}
//l=head;
q=print(l);
return 0;
}
程序中合并好象有点问题,那位大虾帮改下