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,请检查存储结构,和勾链