链表

#include<iostream.h>
typedef struct node{
	int data;
	node *next;
}*ptrn;
ptrn creatlink()
{
	ptrn head,p1,p2,p3;
	head=new node;
	int a;
	int b;
	p1=0;
	cout<<"输入一个整数表示链表中的一个元素,以-1表示结束"<<endl;
	for(int i=0;;i++)
	{
	
	    cin>>a;
		if(a==-1)
			break;
		else
		{
			p2=new node;
			p2->data=a;
			if(p1==0)
			{
				p1=p2;
				p3=p2;
			}
			else
			{
               p3->next=p2;
			   p3=p2;
			}
		}
		
	}
	if(p1)
		p3->next=0;
	head=new node;
	head->next=p1;
	return head;
}
int linklength(ptrn head)
{
	ptrn temp;
	temp=head->next;
	for(int j=1;;j++)
	{
		
		temp=temp->next;
		if(temp==0)
			break;
	}
	return j;
}
ptrn oppositelink(ptrn head)
{
	ptrn pa,pb,pc;
	pa=0;
	pb=new node;
	ptrn temp1;
	int n=linklength(head);
	
	for(int i=0;i<n;i++)
	{
		temp1=head->next;
		for(int j=n-i;j>0;j--)
		{
           pb=temp1;
		   temp1=temp1->next;
		}
		
		if(pa==0)
		{
			pa=pb;
			pc=pb;
		}
		else
		{
			pc->next=pb;
			pc=pb;

		}
	}
	if(pa)pc->next=0;
	head->next=pa;
	return head;
}
void main()
{
	ptrn head=creatlink();
	linklength(head);
	cout<<"此链的倒序为"<<endl;
    head=oppositelink(head);
	ptrn tem;
	tem=head->next;
	for(int k=0;k<linklength(head);k++)
	{
		
		cout<<tem->data<<endl;
		tem=tem->next;
	}

}

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