链表转置.cpp

来自「内含三个文件」· C++ 代码 · 共 50 行

CPP
50
字号
#include<iostream>
using namespace std;
#include<stdlib.h>
struct Lnode 
{
	int data;
	Lnode *next;
}; 
Lnode *CreateList(int n)//创建链表
{
	int a,i;
    Lnode *h,*p;
    h=NULL; 
    cout<<"输入元素:"; 
    for(i=0;i<n;i++)
	{	
	    cin>>a;
        p=new Lnode;
        p->data=a;//输入的数赋给链表
        p->next=h;
        h=p; //转置          
	 }
     return h;
}
void put_out(Lnode *h) //输出函数
{    
	Lnode *p=h;
	while(p!=NULL)
    p=p->next;
	p=h;
	cout<<"转置结果:";
	while(p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->next; 
    }
    cout<<endl;
 }
void main()
{
	Lnode *p,*q;
	int n;	 
	cout<<"输入元素的个数:";
	cin>>n;
    p=CreateList(n);
	q=p;
	put_out(p); 
	system("pause");
}

⌨️ 快捷键说明

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