Thursday, 12 February 2015

Life, the Universe, and Everything

Leave a Comment
Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

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;
    }
}




If You Enjoyed This, Take 5 Seconds To Share It