Method for keeping two stacks within a single linear array, Data Structure & Algorithms

Q. Define a method for keeping two stacks within a single linear array S in such a way that neither stack overflows until entire array is used and a whole stack is never shifted to a different location within the array. Write down the routines for pushing and poping elements in two the stacks.          

Ans.

/* The 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/12/2012 9:07:36 AM | Location : United States







Related Discussions:- Method for keeping two stacks within a single linear array, Assignment Help, Ask Question on Method for keeping two stacks within a single linear array, Get Answer, Expert's Help, Method for keeping two stacks within a single linear array Discussions

Write discussion on Method for keeping two stacks within a single linear array
Your posts are moderated
Related Questions
I =PR/12 Numbers of years .Interest rate up to 1yrs . 5.50 up to 5yrs . 6.50 More than 5 yrs . 6.75 design an algorithm based on the above information

algorithm for insertion in a queue using pointers

1. develop an algorithm which reads two decimal numbers x and y and determines and prints out wether x>y or y>x. the input values, x and y, are whole number > or equal to 0, which

Linked list representations contain great advantages of flexibility on the contiguous representation of data structures. However, they contain few disadvantages also. Data structur

INSERT FUNCTION /*prototypes of insert & find functions */ list * insert_list(list *); list * find(list *, int); /*definition of  anyinsert function */ list * inser

Write an algorithm for binary search. Algorithm for Binary Search 1.  if (low> high) 2.  return (-1) 3.  Mid = (low + high)/2 4.  if ( X = = a[mid]) 5.  return (mid); 6.

multilist representation of graph

Unlike a binary-tree, each node of a B-tree may have a number of keys and children. The keys are stored or saved in non-decreasing order. Each key has an related child that is the

Before programming a problem solution those employees a stack, we have to decide how to represent a stack by using the data structures which exist in our programming language. Stac

Explain the Assertions in Ruby Ruby offers no support for assertions whatever. Moreover, because it's weakly typed, Ruby doesn't even enforce rudimentary type checking on opera