Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Similar documents
Lecture Data Structure Stack

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 3. Stacks And Queues

Infix to Postfix Conversion

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Fall, 2015 Prof. Jungkeun Park

CHAPTER 3 STACKS AND QUEUES

Lecture No.04. Data Structures

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Linear Data Structure

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

STACKS AND QUEUES CHAPTER 3

Data Structures Week #3. Stacks

1. Stack Implementation Using 1D Array

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out


Chapter3 Stacks and Queues

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

STACKS AND QUEUES. Problem Solving with Computers-II

Data Structure - Stack and Queue-

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Lecture 14. Arrays II. Selection Sort & Parallel Arrays. CptS 121 Summer 2016 Armen Abnousi

Stacks and their Applications

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

[CS302-Data Structures] Homework 2: Stacks

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Top of the Stack. Stack ADT

UNIT-2 Stack & Queue

Top of the Stack. Stack ADT

Stack and Its Implementation

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Formal Languages and Automata Theory, SS Project (due Week 14)

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

CS 206 Introduction to Computer Science II

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Introduction to Computer and Program Design 2. Lesson 6. Stacks. James C.C. Cheng Department of Computer Science National Chiao Tung University

Stack Abstract Data Type

The program simply pushes all of the characters of the string into the stack. Then it pops and display until the stack is empty.

Two abstract data types. performed on them) Stacks Last In First Out (LIFO) Queues First In First Out (FIFO)

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

CS 211. Project 5 Infix Expression Evaluation

DS Assignment II. Full Sized Image

Write a C program to add two Complex numbers using functions illustrating-

15. Stacks and Queues

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

Content: Learning Objectives

Stacks. Revised based on textbook author s notes.

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Application of Stack (Backtracking)

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

Computer Science BS Degree - Data Structures (I2206) Abstract Data Types (ADT)

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP

(3-2) Basics of a Stack. Instructor - Andrew S. O Fallon CptS 122 (January 26, 2018) Washington State University

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Operators & Expressions

Associate Professor Dr. Raed Ibraheem Hamed

Constants and Variables

Lecture 12 ADTs and Stacks

Data Structures using OOP C++ Lecture 9

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

An Introduction to Trees

BBM 201 DATA STRUCTURES

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Chapter 2. Stack & Queues. M hiwa ahmad aziz

Chapter 3. Stack & Queue 1

Data Structures & Algorithms Quick Guide. Data Structures & Algorithms Overview

Data Structures. 1. Each entry in a linked list is a called a (a)link (b)node (c)data structure (d)none of the above

Problem with Scanning an Infix Expression

Monday, November 13, 2017

Stacks, Queues (cont d)

The Stack and Queue Types

Linked List. April 2, 2007 Programming and Data Structure 1

Department of Computer Science

Postfix (and prefix) notation

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

infix expressions (review)

March 13/2003 Jayakanth Srinivasan,

NCS 301 DATA STRUCTURE USING C

Transcription:

Data Structures & Algorithm Analysis Lec(3) Stacks Lecturer: Souad Alonazi

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?

What are stacks Top Stack of coins Stack of books Stack of dishes Computer stack 3

Last In First Out A top B A top C B A top D C B A top E D C B A top D C B A

Stack Applications Real life Pile of books Plate trays The Towers of Hanoi More applications related to computer science Program execution stack (read more from your text) Evaluating expressions

Evaluation of Expressions Expression Representation Techniques : Infix Prefix Expression Postfix Expression

From Postfix to Answer Computer System Uses Postfix form to represent expression. The reason to convert infix to postfix expression is that we can compute the answer of postfix expression easier by using a stack. Following is the example that shows evaluation of the Postfix expression using stack as data structure.

From Postfix to Answer Ex: 10 2 8 * + 3 - First, push(10) into the stack 10

From Postfix to Answer Ex: 10 2 8 * + 3 - Then, push(2) into the stack 2 10

From Postfix to Answer Ex: 10 2 8 * + 3 - Push(8) into the stack 8 2 10

From Postfix to Answer Ex: 10 2 8 * + 3-8 2 10 Now we see an operator *, that means we can get an new number by calculation

From Postfix to Answer Ex: 10 2 8 * + 3-8 Now we see an operator *, that means we can get an new number by calculation Pop the first two numbers 2 10 2 * 8 = 16

From Postfix to Answer Ex: 10 2 8 * + 3 - Now we see an operator *, that means we can get an new number by calculation Push the new number back 16 10 2 * 8 = 16

From Postfix to Answer Ex: 10 2 8 * + 3 - Then we see the next operator + and perform the calculation 16 10 26 10 + + 16 =

From Postfix to Answer Ex: 10 2 8 * + 3 - Then we see the next operator + and perform the calculation Push the new number back 26 10 + 16 = 26

From Postfix to Answer Ex: 10 2 8 * + 3 - We see the next number 3 Push (3) into the stack 3 26

Compute the Answer Ex: 10 2 8 * + 3 - The last operation 26-3 = 23

From Postfix to Answer Ex: 10 2 8 * + 3 - The last operation 23 26-3 = 23 answer!

From Postfix to Answer Algorithm: maintain a stack and scan the postfix expression from left to right If the element is a number, push it into the stack If the element is a operator O, pop twice and get A and B respectively. Calculate BOA and push it back to the stack When the expression is ended, the number in the stack is the final answer

Transform Infix to Postfix Now, we have to design an algorithm to transform infix expression to postfix

Transform Infix to Postfix Observation 1: The order of computation depends on the order of operators The parentheses must be added according to the priority of operations. The priority of operator * and / is higher then those of operation + and If there are more than one equal-priority operators, we assume that the left one s priority is higher than the right one s This is called left-to-right parsing.

Transform Infix to Postfix Observation 1: The order of computation depends on the order of operators (cont.) For example, to add parentheses for the expression 10 + 2 * 8-3, we first add parenthesis to 2 * 8 since its priority is highest in the expression. Then we add parenthesis to 10 + (2 * 8) since the priorities of + and are equal, and + is on the left of -. Finally, we add parenthesis to all the expression and get ((10 + (2 * 8)) - 3).

Transform Infix to Postfix Observation 1: The order of computation depends on the order of operators (cont.) The computation order of expression ((10 + (2 * 8)) - 3) is: 2 * 8 = 16 10 + 16 = 26 26 3 = 23 ((10 + 16) -3) (26 3) 23

Transform Infix to Postfix Simplify the problem, how if there are only +/- operators?

Transform Infix to Postfix Simplify the problem, how if there are only +/- operators? The leftmost operator will be done first Ex: 10-2 + 3 8+3 11

Transform Infix to Postfix Simplify the problem, how if there are only +/- operators? Algorithm: maintain a stack and scan the postfix expression from left to right When we get a number, output it When we get an operator O, pop the top element in the stack if the stack is not empty and then push(o) into the stack

Transform Infix to Postfix Simplify the problem, how if there are only +/- operators? Algorithm: maintain a stack and scan the postfix expression from left to right When we get a number, output it When we get an operator O, pop the top element in the stack if the stack is not empty and then push(o) into the stack When the expression is ended, pop all the operators remain in the stack

Transform Infix to Postfix Ex: 10 + 2-8 + 3 We see the first number 10, output it 10

Transform Infix to Postfix Ex: 10 + 2-8 + 3 + We see the first operator +, push(+) into the stack because at this moment the stack is empty 10

Transform Infix to Postfix Ex: 10 + 2-8 + 3 We see the number 2, output it + 10 2

Transform Infix to Postfix Ex: 10 + 2-8 + 3 We see the operator -, pop the operator + and push(-) into the stack - 10 2 +

Transform Infix to Postfix Ex: 10 + 2-8 + 3 We see the number 8, output it - 10 2 + 8

Transform Infix to Postfix Ex: 10 + 2-8 + 3 We see the operator +, pop the operator - and push(+) into the stack + 10 2 + 8 -

Transform Infix to Postfix Ex: 10 + 2-8 + 3 We see the number 3, output it + 10 2 + 8-3

Transform Infix to Postfix Ex: 10 + 2-8 + 3 We come to the end of the expression, then we pop all the operators in the stack 10 2 + 8 3 +

Transform Infix to Postfix Ex: 10 + 2-8 + 3 When we get an operator, we have to push it into the stack and pop it when we see the next operator. The reason is, we have to wait for the second operand of the operator

Transform Infix to Postfix How to solve the problem when there are operators +, -, *, /?

Transform Infix to Postfix Observation 2: scan the infix expression from left to right, if we see higherpriority operator after lower-priority one, we know that the second operand of the lower-priority operator is an expression Ex: a + b * c = a + ( b * c ) a bc* + That is, the expression b c * is the second operand of the operator +

Transform Infix to Postfix So, we modify the algorithm to adapt the situation

Transform Infix to Postfix Algorithm: maintain a stack and scan the postfix expression from left to right When we get a number, output it When we get an operator O, pop the top element in the stack until there is no operator having higher priority then O and then push(o) into the stack When the expression is ended, pop all the operators remain in the stack

Transform Infix to Postfix Ex: 10 + 2 * 8-3 We see the first number 10, output it 10

Transform Infix to Postfix Ex: 10 + 2 * 8-3 We see the first operator +, push it into the stack + 10

Transform Infix to Postfix Ex: 10 + 2 * 8-3 We see the number 2, output it + 10 2

Transform Infix to Postfix Ex: 10 + 2 * 8-3 We see the operator *, since the top operator in the stack, +, has lower priority then *, push(*) * + 10 2

Transform Infix to Postfix Ex: 10 + 2 * 8-3 We see the number 8, output it * + 10 2 8

Transform Infix to Postfix Ex: 10 + 2 * 8-3 - We see the operator -, because its priority is lower then *, we pop. Also, because + is on the left of it, we pop +, too. Then we push(-) 10 2 8 * +

Transform Infix to Postfix Ex: 10 + 2 * 8-3 We see the number 3, output it - 10 2 8 * + 3

Transform Infix to Postfix Ex: 10 + 2 * 8-3 Because the expression is ended, we pop all the operators in the stack 10 2 8 * + 3 -

Infix user 2+3*4 a*b+5 (1+2)*7 a*b/c (a/(b-c+d))*(e-a)*c a/b-c+d*e-a*c compiler Postfix 234*+ ab*5+ 12+7* ab*c/ abc-d+/ea-*c* ab/c-de*ac*- Postfix: no parentheses, no precedence

Array-based Stack Implementation Allocate an array of some size (pre-defined) Maximum N elements in stack Bottom stack element stored at element 0 last index in the array is the top Increment top when one element is pushed, decrement after pop

Stack Implementation: CreateS, isempty, isfull Stack creates(max_stack_size) ::= #define MAX_STACK_SIZE 100 /* maximum stack size */ typedef struct { int key; /* other fields */ } element; element stack[max_stack_size]; int top = -1; Boolean isempty(stack) ::= top< 0; Boolean isfull(stack) ::= top >= MAX_STACK_SIZE-1;

Push void push(int *top, element item) { /* add an item to the global stack */ if (*top >= MAX_STACK_SIZE-1) { stack_full( ); return; } stack[++*top] = item; }

Pop element pop(int *top) { /* return the top element from the stack */ if (*top == -1) return stack_empty( ); /* returns and error key */ return stack[(*top)--]; }

4-2 Stack Linked List Implementation Head Top count top Data nodes (a) Conceptual (a) Physical 56

4-2 Stack Linked List Implementation count top Stack head structure Stack count top end stack <integer> <node pointer> data next Stack node structure node data next end node <datatype> <node pointer> 57

List-based Stack Implementation: Push void push(pnode top, element item) { /* add an element to the top of the stack */ pnode temp = (pnode) malloc (sizeof (node)); if (IS_FULL(temp)) { fprintf(stderr, The memory is full\n ); exit(1); } temp->item = item; temp->next= top; top= temp; }

Pop element pop(pnode top) { /* delete an element from the stack */ pnode temp = top; element item; if (IS_EMPTY(temp)) { fprintf(stderr, The stack is empty\n ); exit(1); } item = temp->item; top = temp->next; free(temp); return item; }