创建链表

#include<iostream.h>
typedef struct node
{
	int data;
	node *next;
}*ptrn;
ptrn linked1()
{
	int da;
	ptrn head,p1,p2;                                              //一个头结点,一个新结点,一个尾结点;
	cout<<"请输入链表1中的数据,-1代表输入完毕"<<endl;
	head=0;
	for(int i=0;;i++)
	{
        cin>>da;
		p1=new node;
		if(da==-1)
			break;
	    p1->data=da;
		if(head==0)
		{
			head=p1;
			p2=p1;                                                //赋的是地址;                                        两个链表连接;
		}
		else
		{
			p2->next=p1;
			p2=p1;
		}
	}
	if(head)
		p2->next=0;
	return head;
}
ptrn linked2()
{
	int da;
	ptrn head,p3,p2;
    cout<<"请输入链表2中的数据,-1代表输入完毕"<<endl;
	head=0;
	for(int i=0;;i++)
	{
        cin>>da;
		p3=new node;
		if(da==-1)
			break;
	   else
		   p3->data=da;
		if(head==0)
		{
			head=p3;
			p2=p3;                                                //赋的是地址;
		}
		else
		{
			p2->next=p3;
			p2=p3;
		}
	}
	if(head)
		p2->next=0;
	return head;
}
ptrn joint(ptrn one,ptrn two)
{
	ptrn temp;
	temp=one;
	for(int i=0;;i++)
	{
		if(temp->next!=0)
		{
            temp=temp->next;
		}
		else
			break;
	}
	temp->next=two;
	return one;
}
void accessnode(ptrn b)
{
	ptrn te;
	te=b;
	if(te==0)
		cout<<"此链表无内容"<<endl;
	else
		for(int i=0;;i++)
		{
			cout<<te->data<<endl;
			te=te->next;
			if(te==0)
				break;
			else
				continue;
		}


}
void main()
{
	ptrn head1=linked1();
	ptrn head2=linked2();
	ptrn head3=joint(head1,head2);
	accessnode(head3);
}

原文地址:https://www.cnblogs.com/zztong/p/6695321.html