Implement Stack using Linked List

PROBLEM :

Implement a Stack using Linked List .

Input (To be used for Expected Output):

The first line of the input contains an integer 'T' denoting the number of test cases. Then T test cases follow.
First line of each test case contains an integer Q denoting the number of queries .
A Query Q is of 2 Types
(i) 1 x   (a query of this type means  pushing 'x' into the stack)
(ii) 2     (a query of this type means to pop element from stack and print the poped element)

The second line of each test case contains Q queries seperated by space.

Output:
The output for each test case will  be space separated integers having -1 if the stack is empty else the element poped out from the stack .
You are required to complete the two methods push which take one argument an integer 'x' to be pushed into the stack  and pop which returns a integer poped out from the stack.

Constraints:
1<=T<=100
1<=Q<=100
1<=x<=100

Example:
Input
1
5
1 2 1 3 2 1 4 2  

Output
3 4

Explanation:

In the first test case for query
1 2    the stack will be {2}
1 3    the stack will be {2 3}
2       poped element will be 3 the stack will be {2}
1 4    the stack will be {2 4}
2       poped element will be 4

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*
The structure of the node of the stack is
struct StackNode
{
    int data;
    StackNode *next;
};

and the structure of the class is
class Stack {
private:
  StackNode *top;
public :
    void push(int);
    int pop();
};
 */

/* The method push to push element into the stack */
void Stack :: push(int x)
{
    struct StackNode *temp ;
    temp=(struct StackNode*)malloc(sizeof(struct StackNode)) ;
    temp->data=x ;
   
    temp->next=top ;
    top=temp ;
}

/*The method pop which return the element poped out of the stack*/
int Stack :: pop()
{
        if(top==NULL)
            return -1 ;
        else
        {
            struct StackNode *temp ;
            int ele ;
           
            temp=top ;
            top=temp->next ;
            ele=temp->data ;
            free(temp) ;
            return ele ;
        }
}


--------------------------------------------------------------------------------
COMPLETE STACK IMPLEMENTATION IN link-list :
--------------------------------------------------------------------------------

//link-list implemetation of stack

#include<iostream>
using namespace std ;
#include<malloc.h>

typedef struct STACK
{
int data ;
struct STACK *next ;
}stack ;

void create_stack(stack **,int ) ;
void push_stack(stack **,int ) ;
int pop_stack(stack **) ;
int top_stack(stack *) ;
int isempty_stack(stack *) ;

int main()
{
stack *s ;
s=NULL ;

int no,choice,i,ele ;
do
{
cout<<"\n 1. push elemet in the stack " ;
cout<<"\n 2. pop element from the stack " ;
cout<<"\n 3. get stack top " ;
cout<<"\n enter choice" ;
cin>>choice ;
switch(choice)
{
case 1 :cout<<"\n enter no of elemets to push " ;
cin>>no ;
cout<<"\n enter th elemets " ;
for(i=0;i<no;i++)
{
cin>>ele ;
push_stack(&s,ele) ;
}
break ;
case 2 :ele=pop_stack(&s) ;
if(ele!=-1)
cout<<"\n elemet poped is = "<<ele ;
break ;
case 3 :ele=top_stack(s) ;
if(ele!=-1)
cout<<"\n top element of stack is = "<<ele ;
break ;
default : cout<<"\n TRY AGAIN !!!!" ;
}
cout<<"\n enter 1 to continue " ;
cin>>no ;
}while(no==1) ;

return 0 ;
}

void push_stack(stack **s,int ele)
{
stack *temp ;
temp=(stack*)malloc(sizeof(stack)) ;
temp->data=ele ;
temp->next=NULL ;

temp->next=(*s) ;
(*s)=temp ;
}

int pop_stack(stack **s)
{
if(isempty_stack(*s))
{
cout<<"\n SORRY !!! stack is empty !! no elemet to POP" ;
return -1 ;
}

stack *temp ;
temp=(*s) ;
(*s)=temp->next ;
int ele=temp->data ;

free(temp) ;
return ele ;
}

int top_stack(stack *s)
{
if(isempty_stack(s))
{
cout<<"\n SORRY !!! stack is empty !! no elemet on TOP" ;
return -1 ;
}
return(s->data) ;
}

int isempty_stack(stack *s)
{
if(s==NULL)
return 1 ;
return 0 ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )