Algorithms for push and pop operation, Data Structure & Algorithms

Assignment Help:

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;

}


Related Discussions:- Algorithms for push and pop operation

Explain the term group support system, (a) Explain the term Group Support S...

(a) Explain the term Group Support System and elaborate on how it can improve groupwork. (b) Briefly explain three advantages of simulation. (c) Explain with the help of a

Quick sort, This is the most extensively used internal sorting algorithm. I...

This is the most extensively used internal sorting algorithm. In its fundamental form, it was invented by C.A.R. Hoare in the year of 1960. Its popularity lies in the easiness of i

Darw a flowchart to input 3 numbers, This algorithm inputs 3 numbers, every...

This algorithm inputs 3 numbers, every number goes through successive division by 10 until its value is less than 1. An output is produced which comprise the number input and a val

Infix expression has balanced parenthesis or not, Q. By making use of stack...

Q. By making use of stacks, write an algorithm to determine whether the infix expression has balanced parenthesis or not.

Applications of binary trees, In computer programming, Trees are utilized ...

In computer programming, Trees are utilized enormously. These can be utilized for developing database search times (binary search trees, AVL trees, 2-3 trees, red-black trees), Gam

Entity relationship diagram, This question is based on the requirements of ...

This question is based on the requirements of a system to record band bookings at gigs. (A 'gig' is an event at which one or more bands are booked to play). You do not need to know

What are the dynamic arrays, What are the Dynamic arrays Dynamic arrays...

What are the Dynamic arrays Dynamic arrays are convenient for programmers since they can never be too small-whenever more space is needed in a dynamic array, it can simply be e

Process of analysis, The objective analysis of an algorithm is to determine...

The objective analysis of an algorithm is to determine its efficiency. Efficiency is based on the resources which are used by the algorithm. For instance, CPU utilization (Ti

Determine the components of illumination, Determine the Components of Illum...

Determine the Components of Illumination The light reaching the eye when looking at a surface has clearly come from a source (or sources) of illumination and bounced off the su

What is ruby, What is Ruby Ruby has numerous simple types, including nu...

What is Ruby Ruby has numerous simple types, including numeric classes such as Integer, Fixnum, Bignum, Float, Big Decimal, Rational, and Complex, textual classes like String,

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd