数据结构-线性链表-线性表链式存储结构练习

Description
由键盘输入若干个整数,判断是几个,并逆序输出,要求用线性表的链式存储结构实现!!!
Input
输入若干个整数
Output
输出为两行,第一行为整数的个数,第二行为整数的逆序序列,每一行末尾都有空格和换行!!!
 
Sample Input
1 2 3 4 5
Sample Output
5
5 4 3 2 1
#include <iostream>
#include <stdio.h>
#define Max_size 105
using namespace std; 
typedef struct Lnode{//定义存储结构
    int data,length;
    Lnode *next;
}Lnode,*Linklist;

void creatlist_h(Linklist &L){//创建链表头插 
    L=new Lnode;
    int cnt=0,i;
    char c;
    L->next=NULL;
    while(cin>>i){
    	Lnode* p=new Lnode;
        p->data=i;
		p->next=L->next;
        L->next=p;
        cnt++;
        c=getchar();
        if(c=='\n') break; 
    }
    L->length=cnt;
}


void Print(Linklist L){//输出链表
    Lnode* q=new Lnode;
    q=L->next;
    while(q){
    	cout<<q->data<<" ";
    	q=q->next;
	}
    cout<<"\n";
}
int listlength(Linklist L){//链表的长度 
    return L->length;
}

int main(){
	Linklist L;
    creatlist_h(L);
    cout<<listlength(L)<<" \n";
    Print(L);
    return 0;
}

 注:本代码采用链式存储结构进行链表的简单操作,如果你的code报RE,请检查存储结构,和勾链

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值