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
Beauty Salon is a system to be designed to manage the booking and the payment of a single beauty parlour. Beauty Therapists: A beauty parlour has a number of staff members mo

There are ten stations on a railway line: Train travels in both directions (i.e. from 1 to 10 and then from 10 to 1).  Fare between each station is $2. A passenger input


For AVL trees the deletion algorithm is a little more complicated as there are various extra steps involved in the deletion of node. If the node is not a leaf node, then it contain

AVL trees and the nodes it contains must meet strict balance requirements to maintain O(log n) search time. These balance restrictions are kept maintained via various rotation func

For preorder traversal, in the worst case, the stack will rise to size n/2, where n refer to number of nodes in the tree. Another method of traversing binary tree non-recursively t

Q. Construct a binary tree whose nodes in inorder and preorder are written as follows: Inorder : 10, 15, 17, 18, 20, 25, 30, 35, 38, 40, 50 Preorder: 20, 15, 10

In a sorted list, Binary search is carried out by dividing the list into two parts depends on the comparison of the key. Since the search interval halves each time, the iteration o

write an algorithm for the gpa of six students

AVL tree An AVL tree is a binary search tree in which the height of the left and right subtree of the root vary by at most 1 and in which the left and right subtrees are again