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
Illumination of wire frame The colour or shade that a surface appears to the human eye depends primarily on three  factors : Colour and strength of incoming illumination

Describe an algorithm to play the Game of Nim using all of the three tools (pseudocode, flowchart, hierarchy chart)

Illustrates the program segment for Quick sort. It uses recursion. Program 1: Quick Sort Quicksort(A,m,n) int A[ ],m,n { int i, j, k; if m { i=m; j=n+1; k

In the book the following methods are presented: static void selectionSort(Comparable[] list) static void insertionSort(Comparable[] list) static boolean linearSearch(Comparable

Merge sort: Merge sort is a sorting algorithm that uses the idea of split and conquers. This algorithm splits the array into two halves, sorts them separately and then merges t


Q. Explain Dijkstra's algorithm for finding the shortest path in the graph given to us.  Ans: The Dijkstra's algorithm: This is a problem which is concerned with finding

Define the Carrier set of the Symbol ADT Carrier set of the Symbol ADT is the set of all finite sequences of characters over Unicode characters set (Unicode is a standard char


#question.explain different types of errors in data transmission.