Implement stack using array

PROBLEM :

Implement a Stack using Array .

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 class is
class Stack
{
private:
    int arr[1000];
    int top;
public:
    Stack(){top=-1;}
    int pop();
    void push(int);
};

 */

/* The method push to push element into the stack */
void Stack :: push(int x)
{
    if(top==999)
        return ;
    else
        arr[++top]=x;
}

/*The method pop which return the element poped out of the stack*/
int Stack :: pop()
{
     if(top==-1)
        return(-1) ;
    else
        return(arr[top--]) ;
}


--------------------------------------------------------------------------------
COMPLETE STACK IMPLEMENTATION IN ARRAY :
--------------------------------------------------------------------------------

// strck implementation using array

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

typedef struct STACK
{
int top ;
int capasity ;
int *array ;
}stack ;

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

int main()
{
stack *s ;
int no,choice,i,ele ;

cout<<"\n enter the capacity of the stack " ;
cin>>no ;

create_stack(&s,no) ;

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 create_stack(stack **s,int no)
{
(*s)=(stack*)malloc(sizeof(stack)) ;
(*s)->top=-1;
(*s)->capasity=no ;
(*s)->array=(int*)malloc((*s)->capasity*(sizeof(int))) ;
}

void push_stack(stack **s,int data)
{
if(isfull_stack(*s))
{
cout<<"\n SORRY !!! can't take more input !!! STACK FULLL" ;
return ;
}
(*s)->array[++((*s)->top)]=data ;
}

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

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

int isfull_stack(stack *s)
{
if(s->top==s->capasity-1)
return 1 ;
return 0 ;
}

int isempty_stack(stack *s)
{
if(s->top==-1)
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 )