stacks in algorithems & data structure

17
Oxford University Press © 2012 Data Structures Using C++ by Dr Varsha Patil Algorithms and Data Algorithms and Data Structures Structures Ahmad Khan Lecture 4

Upload: faran-nawaz

Post on 14-Jun-2015

144 views

Category:

Software


0 download

DESCRIPTION

stacks in algorithems and data structure

TRANSCRIPT

Page 1: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil

Algorithms and Algorithms and Data StructuresData Structures

Ahmad KhanLecture 4

Page 2: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil

Stack

2

Page 3: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil

Objectives

Understand all the aspects of a stack as data type including :Last In, First Out (LIFO) data accessPush, Pop, and other stack operationsContiguous implementation of a stackLearn realization of stack using arrays(: Contiguous stack)Learn to choose appropriate realization suitable for practical applicationsLearn and implement the multi-stacksUse of stacks in expression conversion, recursion, reversing data and many more

3

Page 4: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil4

A stack is an ordered list in which all insertions and deletions are made at one end, called the top

In computer programming processing of function calls and their terminations uses stack. Stack is used to remember the place where the call was made; so that it can return there after the function is complete

Page 5: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil

Following are some examples in which where we need to use stacks are generally used:

Handling function calls in programs very often restricts the access only at one end. In such implementation we need to use stacks. We can keep track of the return address to earlier function after furnishing/finishing a function call using stacksIf we intend to store a group of data together in a sequential manner in computer’s memory, then arrays can be one of the possible data structures.

5

Page 6: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil

Arrays An array is a finite ordered collection of homogeneous data

elements which provides direct access (or random access) to any of its elements.

An array as a data structure is defined as a set of pairs (index, value) such that with each index a value is associated.index — indicates the location of an element in an array.value - indicates the actual value of that data element.

Declaration of an array in ‘C++’:

int Array_A[20];

6

Page 7: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil

Examples of Stacks

7

Following figure shows some examples of stack1.Stack of Book2.Stack of chairs3.Stack of cups

Page 8: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil8

1) The Stack Full Condition (Stack Capacity =3)

Primitive Operations

Page 9: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil9

2) The Pop Operation

Page 10: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil10

3) The Empty Stack

Page 11: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil11

4) The Get Top Operation

Page 12: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil12

Representation Of Stack S Using Sequential Organization (Arrays)

Page 13: Stacks in algorithems & data structure

Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil13

……….. C B A

top

The elements are stored in the stack from the first location onwards The first element is stored at 0th location of the array ‘Stack’ which

means at STACK[0], the second element at STACK[1], ith element at Stack [i–1], and nth element at Stack[n–1]

Associated with the array will be an integer variable, top, which points to the top element in the stack

The initial value of top is −1, when the stack is empty It can hold elements from index 0 and maximum it can grow up to

n−1 as this is the static stack using arrays

Page 14: Stacks in algorithems & data structure

14

Implementation of StackStack Class

Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 14

class STACK{private:

int Stack[50];int MaxCapacity;int top;

public:STACK( ) { MaxCapacity= 50; top=-

1;}int GetTop( );int Pop( );void Push( int Element);int Empty( );int CurrSize( );int IsFull ();

};

Page 15: Stacks in algorithems & data structure

15

Implementation of StackGetTop, Pop, Empty

Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 15

int STACK :: GetTop (){

if (!Empty())return (Stack[top]);

}int STACK :: Pop (){

if (!Empty())return

(Stack[top--]);}int STACK :: Empty ()

{if (top==-1)

return 1;else

return 0;}

Page 16: Stacks in algorithems & data structure

16

Implementation of StackIsFull, CurrSize, Push

Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 16

int STACK :: IsFull (){

if (top==MaxCapacity-1)return 1;

elsereturn 0;

}int STACK :: CurrSize(){

return(top+1);}void STACK :: Push (int Element){

if (!IsFull())Stack[+

+top]=Element;}

Page 17: Stacks in algorithems & data structure

17

Implementation of Stackmain

Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 17

int main( ){

STACK S;S.Push(1);S.Push(2);cout<<"\t Top element of stack =

"<<S.GetTop()<<endl;cout<<"\t After pop = "<<S.Pop()<<endl;cout<<"\t After pop = "<<S.Pop()<<endl;getch();

}