Algorithms for push and pop operation, Data Structure & Algorithms

Q. Suggest a method of implementing two stacks in one array such that as long as space is there in an array, you should be capable to add an element in either stack. Using proposed method or technique, write algorithms for push and pop operations for both the discussed stacks.          

 

Ans.

/* Two Stacks Into One Array */

#include

#define MAX 50

int mainarray[MAX];

int top1=-1,top2=MAX;

void push1(int elm)

{

if((top2-top1)==1)

{

clrscr();

printf("\n Array has been Filled !!!");

return;

}

mainarray[++top1]=elm;

}

void push2(int elm)

{

if((top2-top1)==1)

{

clrscr();

printf("\n Array has been Filled !!!");

return;

}

mainarray[--top2]=elm;

}

int pop1()

{

int temp;

if(top1<0)

{

clrscr();

printf(" \n This Stack is Empty !!!");

return -1;

}

temp=mainarray[top1];

top1--;

return temp;

}

int pop2()

{

int temp;

if(top2>MAX-1)

{

clrscr();

printf(" \n This Stack Is Empty !!!");

return -1;

}

temp=mainarray[top2];

top2++;

return temp;

}

Posted Date: 7/13/2012 2:06:37 AM | Location : United States







Related Discussions:- Algorithms for push and pop operation, Assignment Help, Ask Question on Algorithms for push and pop operation, Get Answer, Expert's Help, Algorithms for push and pop operation Discussions

Write discussion on Algorithms for push and pop operation
Your posts are moderated
Related Questions
Define Dynamic Programming  Dynamic  programming  is  a  method  for  solving  problems  with  overlapping  problems.  Typically, these sub problems arise from a recurrence rel

Complexity is the rate at which the needed storage or consumed time rise as a function of the problem size. The absolute growth based on the machine utilized to execute the program

What do you mean by complexity of an algorithm? The complexity of an algorithm M is the function f(n) which gives the running time and/or storage space need of the algorithm i

Data type An implementation of an abstract data type on a computer. Therefore, for instance, Boolean ADT is implemented as the Boolean type in Java, and bool type in C++;

Linear search is not the most efficient way to search an item within a collection of items. Though, it is extremely simple to implement. Furthermore, if the array elements are arra

Illustrate the Visual realism applications a)   Robot Simulations : Visualization of movement of their links and joints  and end effector movement etc. b)  CNC programs ver


Write an algorithm, using a flowchart, which inputs the heights of all 500 students and outputs the height of the tallest person and the shortest p erson in the school.

: Write an algorithm to evaluate a postfix expression. Execute your algorithm using the following postfix expression as your input: a b + c d +*f ­ .

Example of Area Subdivision Method The procedure will be explained with respect to an illustrative problem, with the image consisting of five objects, namely a triangle (T), qu