+ All Categories
Home > Education > Stack in Sata Structure

Stack in Sata Structure

Date post: 13-Apr-2017
Category:
Upload: muhazzab-chouhadry
View: 46 times
Download: 2 times
Share this document with a friend
49
Stacks • Stack: what is it? • ADT • Applications • Implementation(s)
Transcript
Page 1: Stack in Sata Structure

Stacks

• Stack: what is it?• ADT• Applications• Implementation(s)

Page 2: Stack in Sata Structure

ABSTRACT DATA TYPE A list could be described by the type of information that it holds and by the

operations that can be performed on the list. In this sense the list is an example of an ABSTRACT DATA TYPE. It is possible to think about a list without knowing the details of how it is implemented. An ABSTRACT DATA TYPE (ADT) has a set of rules (behaviour) and attributes imposed upon it that reflect a real world object.

Eg. A waiting queue at a bank or cinema has some clearly defined rules: • The queue contains customers. • Customers join the rear of the queue. • Customers leave from the front of the queue. • Customers leave the queue in the order in which they joined it. • The queue can be empty.

It is quite sensible to talk about the abstract properties of a queue without concerning ourselves about the implementation of a computer model for the queue.

Page 3: Stack in Sata Structure

LINEAR DATA STRUCTURES

All data structures introduced thus far are special subclasses of linear lists. There are two ways of storing data structures in the computer’s memory. The first of these storage-allocation methods, which takes advantage of the one-dimensional property of the computer’s memory, is called sequential allocation.

Page 4: Stack in Sata Structure

LINEAR DATA STRUCTURES

The second allocation method, which is based on the storage of the address or location of each element in the list, is known as linked allocation. Both methods of allocation are discussed in detail in this section.

Several subclasses of linear lists can be defined. The most important of these subclasses are called stacks and queues.

Page 5: Stack in Sata Structure

LINEAR DATA STRUCTURES (STACK)

One of the most important subclasses of linear lists is the family of stack structure. In this section we first introduce the concepts associated with this subclass of linear structures. Next, several important operations, such as insertion and deletion, for the stack structure are given. In particular, we describe the implementation of these operations for a stack that is represented by a vector.

Page 6: Stack in Sata Structure

Stack Abstract Data Type (ADT)• A stack is a special case of a list. • Rather than being allowed to insert a new item into the list at

any place, additions to a stack are restricted to one end identified as the top of the stack.

• Deletions from the stack are also restricted to the top of the stack.

• Usually, only the item at the top of the stack is accessible to someone using the stack ADT.

• A stack is often referred to as a FILO (First In Last Out) list. The stack only allows addition and removal from of the top element so the order of removal is the opposite to that of addition.

Page 7: Stack in Sata Structure

What is a stack?• Stores a set of elements in a particular order• Stack principle: LAST IN FIRST OUT• = LIFO• It means: the last element inserted is the first one

to be removed• Example

• Which is the first element to pick up?

Page 8: Stack in Sata Structure

Stack

Page 9: Stack in Sata Structure

Last In First Out

BA

DCBA

CBA

DCBA

EDCBAtop

top

top

toptop

A

Page 10: Stack in Sata Structure

Stack

Page 11: Stack in Sata Structure

Stack Applications

• Real life– Pile of books– Plate trays

• More applications related to computer science– Program execution stack (read more from your

text)– Evaluating expressions

Page 12: Stack in Sata Structure

Application

• Word processors, editors, etc: – Can implement undo operations

• At runtime:– Runtime system uses a stack to keep track of

function calls and returns, which variables are currently accessible, etc(activation records)

Page 13: Stack in Sata Structure

Applications of Stack• Reversing the string

– push each character on to a stack as it is read.

– When the line is finished, we then pop characters off the stack, and they will come off in the reverse order.

Page 14: Stack in Sata Structure

Stack Operations

Stack Create an empty stack

~Stack Destroy an existing stack

isEmpty Determine whether the stack is empty

isFull Determine whether the stack is full

push Add an item to the top of the stack

pop Remove the item most recently added

Peek/top Retrieve the item most recently added

Clear Clears the contents of stack

Page 15: Stack in Sata Structure

Stack Implementation• Static Implementation

(Using arrays)• Dynamic Implementation

(Using dynamic lists)

Page 16: Stack in Sata Structure

Stack Implementation Using Arrays

• For the static implementation of stack an array will be used.

• This array will hold the stack elements.• The top of a stack is represented by an integer

type variable which contains the index of an array containing top element of a stack.

Page 17: Stack in Sata Structure

Stack Implementation Using Arrays

4

3

2

1

0

Empty stack StackSize = 5top = -1

70

1

2

3

4

top

Push 7

70

81

2

3

4

top

Push 8 Push 9

70

81

92

3

4

topPush 4

70

81

92

43

4

top

Push 5

70

81

92

43

54top

top = StackSize – 1,Stack is full,We can’t push more elements.

Page 18: Stack in Sata Structure

Stack using an Array

top

2571 2 5 7 1

0 1 32 4

top = 3

Page 19: Stack in Sata Structure

Stack Implementation Using Arrays

push(element){

if (top == StackSize – 1)cout<<“stack is full”;

elseStack[++top] = element;

}

Page 20: Stack in Sata Structure

Stack Implementation Using Arrays

4

3

2

1

0

Empty stack top = -1We can’t pop mpre elements

70

1

2

3

4

top

Pop

70

81

2

3

4

top

70

81

92

3

4

top

70

81

92

43

4

top

70

81

92

43

54top

top = StackSize – 1,Stack is full,We can’t push more elements.

PopPop Pop Pop

Page 21: Stack in Sata Structure

Stack Implementation Using Arrays

pop(){

if (top == –1)cout<<“stack is empty”;

elsereturn Stack[top--];

}

Page 22: Stack in Sata Structure

Stack Implementation Using Arrays

topElement() //returns the top element of stack //without removing it.

{if (top == –1)

cout<<“stack is empty”;else

return Stack[top];}

Page 23: Stack in Sata Structure

Stack Implementation Using Arrays

isEmpty() //checks stack is empty or not{

if (top == –1)return true

elsereturn false

}

Page 24: Stack in Sata Structure

Stack Implementation Using Arrays

template <class Element_Type>class Stack{

private:/* This variable is used to indicate stack is full or not*/unsigned int Full_Stack;/* This variable is used to indicate top of the stack */int Top_of_Stack;/* This pointer points to the array which behaves as stack, the space for this array is allocated dynamically */Element_Type *Stack_Array  

Continue on next slide…

Page 25: Stack in Sata Structure

Stack Implementation Using Arrays

//This constructor creates a stack.Stack(unsigned int Max_Size)

{Full_Stack = Max_Size;Top_of_Stack = -1;Stack_Array = new Element_Type[Max_Size]; 

}/* This Destructor frees the dynamically allocated

space to the array */ ~Stack(){

delete Stack_Array;}

Continue on next slide…

Page 26: Stack in Sata Structure

Stack Implementation Using Arrays

/*This function Return TRUE if the stack is full, FALSE otherwise.*/bool Is_Full(){ if (Top_of_Stack == Full_Stack-1)returns True;elsereturns False;}

/*This function Return TRUE if the stack is empty, FALSE otherwise.*/bool Is_Empty(){ if(Top_of_Stack == -1)returns True;elsereturns False;}

Continue on next slide…

Page 27: Stack in Sata Structure

Stack Implementation Using Arrays

// If stack is not full then push an element x in itvoid Push(Element_Type x){ if(is_Full())

cout<<“stack is full”;else

Stack_Array[++Top_of_Stack] = x;}//if Stack is not empty then pop an element form itElement_Type pop(){ if(is_Empty())

cout<<“stack is empty”;else

return Stack_Array[Top_of_Stack--];}

Continue on next slide…

Page 28: Stack in Sata Structure

Stack Implementation Using Arrays

// This function makes the stack empty void Make_Empty(){ Top_of_Stack = -1;}/* This function returns the top element of stack */Element_Type Top(){if(is_Empty())cout<<“stack is emepty”;elsereturn Stack_Array[Top_of_Stack];}

};

Page 29: Stack in Sata Structure

Stack Using Linked List

• We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements.

• As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run the fastest.

Page 30: Stack in Sata Structure

Stack Using Linked List

• For a singly-linked list, insert at start or end takes constant time using the head and current pointers respectively.

• Removing an element at the start is constant time but removal at the end required traversing the list to the node one before the last.

• Make sense to place stack elements at the start of the list because insert and removal are constant time.

Page 31: Stack in Sata Structure

Stack Using Linked List

• No need for the current pointer; head is enough.

top

2571

1 7 5 2

head

Page 32: Stack in Sata Structure

Stack Operation: Listint pop(){ int x = head->get(); Node* p = head; head = head->getNext(); delete p; return x;}

top

257 1 7 5 2

head

Page 33: Stack in Sata Structure

Stack Operation: Listvoid push(int x){ Node* newNode = new Node(); newNode->set(x); newNode->setNext(head); head = newNode;}

top

257

9

7 5 2

head

push(9)

9

newNode

Page 34: Stack in Sata Structure

Stack Operation: Listint top(){ return head->get();} int IsEmpty(){ return ( head == NULL );}

• All four operations take constant time.

Page 35: Stack in Sata Structure

Stack: Array or List• Since both implementations support stack

operations in constant time, any reason to choose one over the other?

• Allocating and deallocating memory for list nodes does take more time than preallocated array.

• List uses only as much memory as required by the nodes; array requires allocation ahead of time.

• List pointers (head, next) require extra memory.• Array has an upper limit; List is limited by

dynamic memory allocation.

Page 36: Stack in Sata Structure

Use of Stack• Example of use: prefix, infix, postfix

expressions.• Consider the expression A+B: we think of

applying the operator “+” to the operands A and B.

• “+” is termed a binary operator: it takes two operands.

• Writing the sum as A+B is called the infix form of the expression.

Page 37: Stack in Sata Structure

Prefix, Infix, Postfix

• Two other ways of writing the expression are

+ A B prefixA B + postfix

• The prefixes “pre” and “post” refer to the position of the operator with respect to the two operands.

Page 38: Stack in Sata Structure

Prefix, Infix, Postfix

• Consider the infix expressionA + B * C

• We “know” that multiplication is done before addition.

• The expression is interpreted as A + ( B * C )

• Multiplication has precedence over addition.

Page 39: Stack in Sata Structure

Prefix, Infix, Postfix

• Conversion to postfix

A + ( B * C ) infix form

Page 40: Stack in Sata Structure

Prefix, Infix, Postfix

• Conversion to postfix

A + ( B * C ) infix formA + ( B C * ) convert multiplication

Page 41: Stack in Sata Structure

Prefix, Infix, Postfix

• Conversion to postfix

A + ( B * C ) infix formA + ( B C * ) convert multiplicationA ( B C * ) + convert addition

Page 42: Stack in Sata Structure

Prefix, Infix, Postfix

• Conversion to postfix

A + ( B * C ) infix formA + ( B C * ) convert multiplicationA ( B C * ) + convert additionA B C * + postfix form

Page 43: Stack in Sata Structure

Prefix, Infix, Postfix

• Conversion to postfix

(A + B ) * C infix form

Page 44: Stack in Sata Structure

Prefix, Infix, Postfix

• Conversion to postfix

(A + B ) * C infix form( A B + ) * C convert addition

Page 45: Stack in Sata Structure

Prefix, Infix, Postfix

• Conversion to postfix

(A + B ) * C infix form( A B + ) * C convert addition( A B + ) C * convert multiplication

Page 46: Stack in Sata Structure

Prefix, Infix, Postfix

• Conversion to postfix

(A + B ) * C infix form( A B + ) * C convert addition( A B + ) C * convert multiplicationA B + C * postfix form

Page 47: Stack in Sata Structure

Precedence of Operators

• The five binary operators are: addition, subtraction, multiplication, division and exponentiation.

• The order of precedence is (highest to lowest)

• Exponentiation • Multiplication/division *, /• Addition/subtraction +, -

Page 48: Stack in Sata Structure

Precedence of Operators

• For operators of same precedence, the left-to-right rule applies:

A+B+C means (A+B)+C.

• For exponentiation, the right-to-left rule applies

A B C means A ( B C )

Page 49: Stack in Sata Structure

Infix to Postfix

Infix PostfixA + B A B +12 + 60 – 23 12 60 + 23 –(A + B)*(C – D ) A B + C D – *A B * C – D + E/F A B C*D – E

F/+


Recommended