链表
背景:读数读到 -1 为止,
且将读到的数存入链表中,
再输出
//#include<iostream>
//#include<cstdlib>
#include<bits\stdc++.h>
using namespace std;
typedef struct _node{
int value;
struct _node *next;
}Node;
int main()
{
int num;
Node *head=NULL;
do{
cin>>num;
if(num!=-1)
{
// add to linked-list
Node *p=(Node*)malloc(sizeof(Node));
p->value=num;
p->next=NULL;
// find the last;
Node *last=head;
if(last==NULL)
{
head=p;
}
else
{
while(last->next!=NULL)
{
last=last->next;
}
// attach;
last->next=p;
}
}
}while(num!=-1) ;
Node *pp=NULL;
for(pp=head;pp!=NULL;pp=pp->next)
{
cout<<pp->value<<endl;
}
return 0;
}