您的位置:首页 > 其它

带头结点链表的实现

2014-08-27 00:27 246 查看
#include<iostream>
using namespace std;
typedef struct head
{
int a;
struct head *next;
}NLode;

void intial(NLode **head)
{
*head=(NLode *)malloc(sizeof(NLode));
(*head)->next=NULL;
}

void insert(NLode *head,int a)
{
NLode *p,*q;
p=head;
q=(NLode*)malloc (sizeof(NLode));

while(p->next!=NULL)
{
p=p->next;

}
q->a=a;
q->next=p->next;
p->next=q;

}

void printSL(NLode *head)
{
NLode *p;
p=head->next;
while(p!=NULL)
{

cout<<p->a<<"   "<<endl;
p=p->next;
}
}

void main()
{
NLode *head1,*head2;

intial(&head1);
intial(&head2);
int a,b,num=0,i;
cin>>a;
cin>>b;
for(i=0;i<a;i++)
{
cin>>num;
insert(head1,num);
}
num=0;
for(i=0;i<b;i++)
{
cin>>num;
insert(head2,num);
}
cout<<"A:"<<endl;
printSL(head1);
cout<<"B:"<<endl;
printSL(head2);

system("pause");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: