Algorithmic implementation of multiple stacks, Data Structure & Algorithms

Assignment Help:

So far, we now have been concerned only with the representation of single stack. What happens while a data representation is required for several stacks? Let us consider an array X whose dimension is m. For convenience, we will assume that the indexes of array commence from 1 and end at m. If we contain only 2 stacks to implement in the similar array X, then the solution is simple.

Assume A and B are two stacks. We may define an array stack A with n1 elements and an array stack B along with n2 elements. Overflow might occur when either stacks A contains more than n1 elements or stack B have more than n2 elements.

Assume, rather than that, we define a single array stack along n = n1 + n2 elements for stack A & B together. Let the stack A "grow" to the right, and stack B "grow" to the left. In this case, overflow will takes place only when A and B together have more than n = n1 + n2 elements. It does not matter how several elements individually are there in each stack.

However, in the case of more than 2 stacks, we cannot represent these in the similar way since a one-dimensional array has two fixed points X(1) and X(m) only and each of stack needs a fixed point for its bottom most element. While more than two stacks, say n, are to be sequentially represented, initially we can divide the obtainable memory X(1:m) into n segments. If the sizes of stacks are known, then, we can assign the segments to them in proportion to the probable sizes of the several stacks. If the sizes of the stacks are not known, then, X(1:m) might be divided into equal segments. For each stack i, we will use BM (i) to represent a position one less than the position in X for the bottom most element of that stack. TM(i), 1 < i < n will point to the topmost element of stack i. We will use the boundary condition BM (i) = TM (i) if the ith stack is empty .If we grow the ith stack in lower memory indexes than i+1st stack, then, with roughly equal initial segments we have

BM (i) = TM (i) =   m/n (i - 1), 1 < i < n, as the initial values of BM (i) & TM (i).

All stacks are empty and memory is divided in roughly equal segments.

Figure illustrates an algorithm to add an element to the ith stack. Figure illustrates an algorithm to delete an element from the ith stack.

ADD(i,e)

Step1: if TM (i)=BM (i+1)

Print "Stack is full" and exit

Step2: [Increment the pointer value through one]

TM (i)← TM (i)+1

X(TM (i))← e

Step3: Exit

//remove the topmost elements of stack i.

DELETE(i,e)

Step1: if TM (i)=BM (i)

Print "Stack empty" and exit

Step2: [remove the topmost item]

e←X(TM (i))

TM (i)←TM(i)-1

Step3: Exit


Related Discussions:- Algorithmic implementation of multiple stacks

Implementation of stack using arrays, A Stack has an ordered list of elemen...

A Stack has an ordered list of elements & an array is also utilized to store ordered list of elements. Therefore, it would be very simple to manage a stack by using an array. Thoug

Frequency counts for all statements, Evaluate the frequency counts for all ...

Evaluate the frequency counts for all statements in the following given program segment. for (i=1; i ≤ n; i ++) for (j = 1; j ≤ i; j++) for (k =1; k ≤ j; k++) y ++;

What are the specific needs for realism, Normal 0 false false...

Normal 0 false false false EN-IN X-NONE X-NONE MicrosoftInternetExplorer4

Advantages of dry running a flowchart, Advantages of dry running a flowchar...

Advantages of dry running a flowchart When dry running a flowchart it's advisable to draw up a trace table illustrating how variables change their values at every stage in the

Darw a flowchart to inputs top speeds of 5000 cars, Write an algorithm in t...

Write an algorithm in the form of a flowchart that: inputs top speeds (in km/hr.) of 5000 cars Outputs fastest speed and the slowest speed Outputs average (mean) s

Algorithm to delete the specific node from binary searchtree, Q. Write down...

Q. Write down an algorithm to delete the specific node from binary search tree. Trace the algorithm to delete a node (10) from the following given tree. Ans. Algor

Rotations in binary tree, H o w can you r ot a t e a B i n a r y...

H o w can you r ot a t e a B i n a r y Tr e e? E x pl a i n r i g h t a n d l eft r ot a tion s by taking an e x a mpl e.   If after

Sorting, compare and contrast the bubble sort,quick sort,merge sort and rad...

compare and contrast the bubble sort,quick sort,merge sort and radix sort

Define a procedure called make-avl-tree, This question deals with AVL trees...

This question deals with AVL trees. You must use mutable pairs/lists to implement this data structure: (a) Define a procedure called make-avl-tree which makes an AVL tree with o

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