Example
Input:
1
2
88
42
99
Output:
1
2
88
Solution
#include<iostream>using namespace std;
typedef struct list{
int number;
list *next;
}list;
//prototypes
list* insert(int,list*);
void print(list*);
int main()
{
list *num=NULL;
int number;
while(1)
{
cin>>number;
if(number==42)
break;
else
num=insert(number,num);
}
print(num);
return 0;
}
//Definations
list* insert(int numb,list *num)
{
list *ptr;
ptr=new list;
ptr->number=numb;
ptr->next=NULL;
if(num==NULL)
{
num=ptr;
}
else
{
list *temp;
temp=num;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
}
return num;
}
void print(list *num)
{
list *temp;
temp=num;
while(temp!=NULL)
{
cout<<temp->number<<endl;
temp=temp->next;
}
}
0 Questions:
Post a Comment