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

Similar documents
The Bucharest University of Economic Studies. Data Structures. Heap Priority Queues Maps and Sets

Postfix (and prefix) notation

PA3 Design Specification

Lecture Data Structure Stack

Stack Abstract Data Type

STACKS AND QUEUES. Problem Solving with Computers-II

The Bucharest University of Economic Studies. Data Structures. Associate Professor Mihai DOINEA

Data Structures Unit 02

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

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

CS 206 Introduction to Computer Science II

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

The Stack and Queue Types

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Stack and Its Implementation

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

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

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

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

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

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Stacks Fall 2018 Margaret Reid-Miller

Stacks, Queues and Hierarchical Collections

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

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

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

CSE 230 Intermediate Programming in C and C++

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

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

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Stacks. Revised based on textbook author s notes.

Motivation for Queues

CS 206 Introduction to Computer Science II

March 13/2003 Jayakanth Srinivasan,

15. Stacks and Queues

Chapter 2. Stack & Queues. M hiwa ahmad aziz

Linear Data Structure

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

12 Abstract Data Types

DATA STRUCTURE UNIT I

Lecture 12 ADTs and Stacks

1 P age DS & OOPS / UNIT II

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

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

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS W3134: Data Structures in Java

Queues Fall 2018 Margaret Reid-Miller

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

Stacks and Queues. CSE Data Structures April 12, 2002

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Data Structure - Stack and Queue-

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

.:: UNIT 4 ::. STACK AND QUEUE

Infix to Postfix Conversion

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours


Lecture 4 Stack and Queue

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Stacks and their Applications

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

Arrays and Linked Lists

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

Top of the Stack. Stack ADT

Stack and Queue. Stack:

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Top of the Stack. Stack ADT

Data Abstraction and Specification of ADTs

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Content: Learning Objectives

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

10/26/2017 CHAPTER 3 & 4. Stacks & Queues. The Collection Framework

CS24 Week 4 Lecture 2

Programming Abstractions

An Introduction to Trees

Stacks and Queues

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

Programming Abstractions

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

Algorithms and Data Structures Exercise I

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

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

DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES

3137 Data Structures and Algorithms in C++

List, Stack, and Queues

Lecture No.04. Data Structures

Lab 7 1 Due Thu., 6 Apr. 2017

4.3 Stacks and Queues

Stacks and Queues. Stack - Abstract Data Type. Stack Applications. Stack - Abstract Data Type - C Interface

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


Transcription:

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

Agenda Definition Graphical representation Internal interpretation Characteristics Operations Implementations

Stack It is a linear structure in which the two basic operations, insertion and deletion are made on a LIFO based rule. These operations are made in a single part of the structure called the head of the stack, or the top. push STACK pop

Stack A logical structure implemented with different type of data structures: linked lists or arrays; A homogenous structure with elements of the same type; Basic operations: Adding an element, called the push operation Removing an element, called the pop operation Looking at the top element, called the peek operation struct Stack{ int Stack * }; info; next;

Stack as an ADT The implementation rule is LIFO Last In First Out The pop operation from a NULL stack is considered an error The push operation into a stack is considered a limitation The element from the top of a stack is the only accessible element top 2 4 1 3 6

Stack graphical representation push pop K C A T S Stack mechanism

Stacks with Linked Lists top of the stack 2 1005 1002 1003 5 1328 new node 2 NULL 3241 3242 8 2500 1328 1329 1005 1006 34 NULL 2500 2501

Stacks with Linked Lists 2 1005 1002 1003 5 1328 top of the stack 2 1002 3241 3242 8 2500 1328 1329 1005 1006 34 NULL 2500 2501

Stacks with Linked Lists top of the stack 2 1005 1002 1003 5 1328 temporary node 2 1002 3241 3242 8 2500 1328 1329 1005 1006 34 NULL 2500 2501

Function call simulation 0x00f11464h int suma(int x, int y) { int z = 0, r = 2; z = x + y; return z; } void main(){ int a = suma(3,8); }

Function call simulation initialization. SP:0x0018FA18h BP:0x0018FA1Ch

Function call simulation push b b: 8. SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch

Function call simulation push a a: 3 b: 8. SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch

Function call simulation push IP IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch

Function call simulation push BP BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch

Function call simulation mov BP, SP BP:0x0018FA08h BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch

Function call simulation sub r: 2 esp,8 SP:0x0018FA00h BP:0x0018FA08h z: 0 BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch

Function call simulation mov r: 2 SP, BP SP:0x0018FA00h BP:0x0018FA08h z: 0 BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch

Function call simulation pop r: 2 BP SP:0x0018FA00h BP:0x0018FA08h pop BP z: 0 BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch

Function call simulation return BP:0x0018FA08h pop BP return r: 2 z: 0 BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA00h SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch

Function call simulation BP:0x0018FA08h pop BP return r: 2 z: 0 BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA00h SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch

PUSH Pseudocode 1. memory allocation (newnode); 2. newnode initialization; 3. if (stack_full) stack overflow; else newnode >next = empty(head)? NULL: head; head = newnode;

POP Pseudocode 1. declaring a temporary node (tmpnode); 2. if (stack_empty) stack underflow; else value = head->info; tmpnode = head; head = head >next; free(tmpnode); return value;

QUEUES It is a linear structure in which the two basic operations, insertion and deletion are made on a FIFO based model. The insertion is made on the tail section and the deletion is made on the front section of the queue. dequeue get Queue put enqueue

Queue A logical structure implemented with different type of data structures: linked lists or arrays; A homogenous structure with elements of the same type; Basic operations: Adding an element, called the enqueuer or put operation Removing an element, called the dequeuer or get operation struct Queue{ Node* head; Node* tail; }; struct Node{ int Node * }; info; next;

Queue Implementation model FIFO Fifo In First Out 5 2 7 1 front back

Queue Implementation put E U E U Q get

PUT Pseudocode 1. memory allocation for a new node (newnode); 2. newnode initialization; 3. if (full queue) queue overflow; else if (empty queue) head = tail = newnode ; else tail >next = newnode;

GET Pseudocode 1. declaring a temporary node (tmpnode); 2. if (empty queue) queue underflow; else value = head >info; tmpnode = head; head = head >next; free(tmpnode); return value;

Queues with Linked Lists head of the queue 2 1005 1002 1003 5 1328 1005 1006 2 NULL 3241 3242 8 2500 1328 1329 tail of the queue 34 NULL 2500 2501

Queues with Linked Lists head of the queue 2 1005 1002 1003 5 1328 1005 1006 8 2500 1328 1329 tail of the queue 2 NULL 3241 3242 34 3241 2500 2501

Queues with Linked Lists temporary node 2 1005 1002 1003 head of the queue 5 1328 1005 1006 8 2500 1328 1329 tail of the queue 2 NULL 3241 3242 34 3241 2500 2501

Queue Circular array Initial state (back < front && empty == true): back front

Queue Circular array Enqueue (2): 2 front back

Queue Circular array Enqueue (4): 2 4 front back

Queue Circular array Enqueue (1): 1 2 4 back front

Queue Circular array Enqueue (3): 1 3 2 4 back front

Queue Circular array Dequeue => return 2: 1 3 2 4 back front

Queue Circular array Dequeue => return 4: 1 3 2 4 front back

Queue Circular array Dequeue => return 1: 1 3 2 4 back front

Queue Circular array Dequeue => return 3: 1 3 2 4 back front

Palindrome Able was I ere I saw Elba

Ring buffer

Infix Postfix Prefix notations Evaluating mathematical expressions by using the stack data structure: reordering the elements so that the expression can be evaluated easily by one single crossing over the entire expression; The Polish notation invented by Jan Lukasiewicz also called the prefix expression; The Reverse Polish notation is called postfix notation.

Infix Postfix Prefix notations Evaluating mathematical expressions: Prefix or Polish notation: the operators are written before the operands Postfix or revers Polish notation: the operators are placed after the operands

Infix Postfix Prefix notations The postfix or the reverse Polish notation is much easily interpreted than the infix, normal mathematical expressions: the order of operations is straight forward; the parentheses are not necessary; the evaluation are automatically made by the use of a stack data structure;

Infix Postfix Prefix notations The algorithm for casting an infix expression to a postfix notation is called Dijkstra Shunting-Yard algorithm: using a stack for storing the operators in order to be transferred into the postfix notation; each operator has an adequate hierarchy level as depicted in the next table;

Infix Postfix Prefix notations Operators hierarchy level 1 2 3 4 ( [ { ) ] } + - * /

Infix Postfix Prefix notations Mathematical expression (the infix notation) Polish notation (prefix expression) Reverse Polish notation (postfix expression) 4 + 5 + 4 5 4 5 + 4 + 5 * 5 + 4 * 5 5 4 5 5 * + 4 * 2 + 3 + * 4 2 3 4 2 * 3 + 4 + 2 + 3 + + 4 2 3 4 2 + 3 + 4 * (2 + 3) * 4 + 2 3 4 2 3 + *

Dijkstra Shunting-Yard algorithm Scan the entire infix expression from left to right for tokens (operators, operands and parentheses) For each token, test whether: If token is an operator: All operators from the top of the stack that are higher or equal precedence than the incoming one are popped out from the stack and appended to the postfix expression; after this operation the new token is pushed on the stack;

Dijkstra Shunting-Yard algorithm If token is an operand, append it to the postfix expression; If token is a left parenthesis, push it on the stack; If token is a right parenthesis: Pop all the operators from the stack and append them to the postfix expression, till the matching left parenthesis is found, which is popped out without being added to the result; When all tokens of the infix expression have been scanned, pop all the elements from the stack and append them to the postfix expression;

Infix to Postfix 2*(4+3)+9/3 Stack: Output:2

Infix to Postfix 2*(4+3)+9/3 Stack:* Output:2

Infix to Postfix 2*(4+3)+9/3 Stack:*( Output:2

Infix to Postfix 2*(4+3)+9/3 Stack:*( Output:24

Infix to Postfix 2*(4+3)+9/3 Stack:*(+ Output:24

Infix to Postfix 2*(4+3)+9/3 Stack:*(+ Output:243

Infix to Postfix 2*(4+3)+9/3 Stack:* Output:243+

Infix to Postfix 2*(4+3)+9/3 Stack:+ Output:243+*

Infix to Postfix 2*(4+3)+9/3 Stack:+ Output:243+*9

Infix to Postfix 2*(4+3)+9/3 Stack:+/ Output:243+*9

Infix to Postfix 2*(4+3)+9/3 Stack:+/ Output:243+*93

Infix to Postfix 2*(4+3)+9/3 Stack: Output:243+*93/+

Postfix evaluation algorithm Scan the entire postfix expression from left to right for tokens (operands and operators): For each token, test whether: If token is an operand is pushed on the stack; If token is an operator: The left element is popped from the stack into a x variable; The right element is popped from the stack into a y variable; The operation between x operator y is computed; The result is pushed on the stack; The last value standing on the stack is the result of the expression.

Infix->Postifix->Evaluation E = 2+5*3-(3*2+1)

Bibliografie Ion IVAN, Marius POPA, Paul POCATILU, coordonatori Structuri de date, Editura ASE, Bucuresti, 2008, ISBN 978-606-505-031-0 Ion SMEUREANU Programarea in limbajul C/C++, Editura CISON, 2001 Michael MAIN Data Structures & Other Objects Using Java, Editura Pearson, 2012, ISBN: 9780132911504 http://www.acs.ase.ro http://www.itcsolutions.eu