⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 单链表置逆.cpp

📁 共有10个文件代码
💻 CPP
字号:
#include<iostream.h>
#include"stdlib.h"//malloc函数需要包含的头文件
struct nodetype
{
	int data;
	nodetype *next;
}typedef Linklist;
nodetype *create()
{   
	int d,n;
	nodetype *h=NULL,*s,*t,*p;
	cout<<"建立一个单链表:"<<endl;
	cout<<"输入单链表元素个数:";
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cout<<"输入第"<<i<<"结点值";
		cin>>d;
		if(i==1)
		{
			h=(nodetype*)malloc(sizeof(nodetype));
			h->data=d;
			h->next=NULL;
			t=h;
		}
		else{
			s=(nodetype*)malloc(sizeof(nodetype));
			s->data=d;
			s->next=NULL;
			t->next=s;
			t=s;//t始终指向最后一个结点
		}
	}
	p=h;
	cout<<"创建的链表为:"<<endl;
	while(p!=NULL)					
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
	return h;
}
nodetype *invert(nodetype *h)
{
	nodetype *p,*q,*r;
	p=h;q=p->next;
	while(q!=NULL)
	{
		r=q->next;
		q->next=p;
		p=q;q=r;
	}
	h->next=NULL;
	h=p;
	cout<<"倒置的链表为:"<<endl;
	while(p!=NULL)					
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;	
	return h;
}
void main()
{
   nodetype *head;
   head=create();
   head=invert(head);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -