原题目是:两个带头结点的链表a,b。从a中删除从第i个元素起length个元素,并插入到b中第j个元素之前。
以下是我自己写的,用vs2005调试不成功,望高手修改过告诉我,谢谢。
#include <iostream>
using namespace std;
//结构体
struct Node
{
int data;
Node *next;
};
//创建链表a
Node *ahead=NULL;
Node *atail=NULL;
Node *pnewa;
for(int a=1;a<10;a++)
{
pnewa=new Node;
if(pnewa=null)
{
cout << "memory insufficient!"<<endl;
return null;
}
pnewa->data=a;
pnewa->next=null;
if(atail=null)
ahead =pnewa;
else
atail->next=pnewa;
btail=pnewa;
}
//创建链表b
Node *bhead=NULL;
Node *btail=NULL;
Node *pnewb;
int b;
for(b=1;b<15;b++)
{
pnewb=new Node;
if(pnewb=null)
{
cout << "memory insufficient!"<<endl;
return null;
}
pnewb->data=b;
pnewb->next=null;
if(btail=null)
bhead =pnewb;
else
btail->next=pnewb;
btail=pnewb;
}
void main()
{
int i,j,length;
int a,b;
cout <<"intput i,j,length"<<endl;
cin>>i>>j>>length;
//查找a的第i-1个元素
int *pa,ma;
pa=ahead;
while(ma<i-1&&!pa)
{
pa=pa->next;
ma++;
}
//查找第i个后length个元素
if(pa->next!=null)
{
int *q,n;
q=pa->next;
while(n<length&&!q)
{
q=q->next;
n++;
}
}
else
return null;
//查找b中第j个元素
int *pb=bhead;
int mb;
while(mb<j-1&&!pb)
{
pb=pb->next;
mb++;
}
if(!pb)
{
q->next=pb->next;
pb->next=pa->next;
}
Node *pNode=bhead;
while(pNode!=null)
{
cout <<pNode->data<<'\t';
pNode =pNode->next;
}
}