u011172887 2013-09-28 06:25 采纳率: 20%
浏览 1058
已结题

数据结构中用C++怎么把两个链表合成一个链表,一下是一对代码,请问主函数怎么写

求代码!#include
template
struct Node
{
DataType data;
Node*next;
};
template
class Linklist
{
public:
Linklist(DataType a[],int n);
~Linklist();
int Length();
DataType Get(int i);
int Locate(DataType x);
void Insert(int i,DataType x);
DataType Delete(int i);
void PrintList();
private:
Node*first;
};
template
Linklist::Linklist(DataType a[],int n)
{
first=new Node;first->next=NULL;
for(i=0;i {
s=new Node;s->data=a[i];
s->next=first->next;first->next=s;
}
}
template
Linklist::~Linklist()
{
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}
template
int Linklist::Length()
{
Node*first;
p=first->next;count=0;
while(p!=NULL)
{
p=p->next;
count++;
}
return count;
}
template
DataType Linklist::Get(int i)
{
Node*first;
p=first->next;count=1;
while(p!=NULL&&count {
p=p->next;
count++;
}
if(p==NULL)throw"位置";
else return p->data;
}
template
int Linklist::Locate(DataType x)
{
p=first->next;count=1;
while(p!=NULL)
{
if(p->data==x)return count;
p=p->next;
count++;
}
return 0;
}
template
void Linklist::Insert(int i,DataType x)
{
p=first;count=0;
while(p!=NULL&&count {
p=p->next;
count++;
}
if(p==NULL)throw"位置";
else{
s=new Node;s->data=x;
s->next=p->next;p->next=s;
}
}
template
void Linklist::PrintList()
{

p=first->next;
while(p!=NULL)
{
    cout<<p->data;
    p=p->next;
}

}
void main()
{
int i,L1[6];
for(i=0;i<=5;i++)
{
cin>>L1[i];
}
LinklistL1(a,6);

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 请问这个是什么意思?
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services
    • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
    • ¥15 模糊pid与pid仿真结果几乎一样