编程题,设计一个算法,通过遍历一趟,将链表中所有结点的链接方向逆转,仍利用原表的存储空间。

#include <iostream>
using namespace std;
struct Node{
	int num;
	Node *next;
};
int n;
Node *creat()
{
	Node *head,*p1,*p2;
	n=0;
	p1=p2=new Node;
	cin>> p1->num;
	head==NULL;
	while(p1->num!=0)
	{
		n++;
		if(n==1)
		head=p1;
		else
		p2->next=p1;
		p2=p1;
		p1=new Node;
		cin>>p1->num;	
	}	
	p2->next=NULL;
	return head;
} 
Node *rollback(Node *head)
{
	Node *p=head;
	Node *q=NULL;
	Node *Next;
	while(p)
	{
		Next=p->next;
		p->next=q;
		q=p;
		p=Next;
	}
	return q;
}
void print(Node *head)
{
	while(head)
	{
		cout<< head->num;
		head=head->next;
	}
}
int main()
{
	Node *head=creat();
	Node *q=rollback(head);
	print(q);
	return 0;
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值