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

What is a spanning tree of a graph, What is a Spanning tree of a graph? ...

What is a Spanning tree of a graph? A Spanning Tree is any tree having of vertices of graph tree and some edges of graph is known as a spanning tree.

Sparse matrix, How sparse matrix stored in the memory of a computer?

How sparse matrix stored in the memory of a computer?

Ruby implementation of the symbol abstract data type, Ruby implementation o...

Ruby implementation of the Symbol ADT Ruby implementation of the Symbol ADT, as mentioned, hinges on making Symbol class instances immutable that corresponds to the relative la

Graphs, c program to represent a graph as an adjacency multilist form

c program to represent a graph as an adjacency multilist form

Representation of a sparse matrix, Let us assume a sparse matrix from stora...

Let us assume a sparse matrix from storage view point. Assume that the entire sparse matrix is stored. Then, a significant amount of memory that stores the matrix consists of zeroe

Worst case and average case, Worst Case: For running time, Worst case runn...

Worst Case: For running time, Worst case running time is an upper bound with any input. This guarantees that, irrespective of the type of input, the algorithm will not take any lo

Searching, Searching is the procedure of looking for something: Finding one...

Searching is the procedure of looking for something: Finding one piece of data that has been stored inside a whole group of data. It is frequently the most time-consuming part of m

Technique for direct search, Technique for direct search is    Hashing ...

Technique for direct search is    Hashing is the used for direct search.

Visual Basic Assignment, When writing a code for a program that basically a...

When writing a code for a program that basically answers Relative Velocity questions how do you go at it? How many conditions should you go through?

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