2021-9-23【数据结构】【单链表逆置】

线性表

栈&队列

二叉树

持续更新中,尽请期待!!!

#include<iostream>
using namespace std;

typedef char datatype;
typedef struct node
{
   
	datatype data;
	struct node *next;
} Lnode, *linklist;

void creat(linklist &head)
{
   
	char ch;
	linklist s,r;
	head = new Lnode;
	r=head;
	while((ch=getchar())!='*')//到“ * ”结束
	{
   
		s = new Lnode;
		s->data=ch;
		r->next=s;
		r=s;
	}
	r->next=NULL;
}
void invert(linklist &head)
{
   
	linklist p, q, r;
	p=head->next;
	q=p->next;
	while(q!=NULL)
	{
   
		r=q->next;
		q->next=p;
		p=q;
		q=r;
	}
	head->next->next=NULL;
	head->next=p;
}
void print(linklist &head)
{
   
	linklist p=head->next;
	while(p!=NULL)
	{
   
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}
int main()
{
   
	linklist head;
	creat(head);
	print(head);
	invert(head);
	print(head);
	return 0;
}


全部评论

相关推荐

头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务