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

Similar documents
CHAPTER 3 STACKS AND QUEUES

STACKS AND QUEUES CHAPTER 3

Chapter3 Stacks and Queues

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

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Fall, 2015 Prof. Jungkeun Park

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

Chapter 3. Stack & Queue 1

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

Data Structure - Stack and Queue-

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

Lecture Data Structure Stack

Infix to Postfix Conversion

Stacks and Queues. Introduction to Data Structures Kyuseok Shim SoEECS, SNU.

Data Structure. Chapter 4 List (Part II) Department of Communication Engineering National Central University Jhongli, Taiwan.

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

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

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

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

CHAPTER 3 STACKS AND QUEUES. Iris Hui-Ru Jiang Fall 2008

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

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

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

Types of Data Structures

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

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Content: Learning Objectives

Data Structures Week #3. Stacks

Linear Data Structure

Stacks and their Applications

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

Top of the Stack. Stack ADT

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

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

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

An Introduction to Trees

Top of the Stack. Stack ADT

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Lecture 4 Stack and Queue

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

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

DS Assignment II. Full Sized Image

Lecture No.04. Data Structures

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

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

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

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

Postfix (and prefix) notation

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Queue: Queue Representation: Basic Operations:

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

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR


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

STACKS AND QUEUES. Problem Solving with Computers-II

Queue Definition. Ordered list with property:

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

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

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

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

[CS302-Data Structures] Homework 2: Stacks

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

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

Chapter 2 Arrays and Structures

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

C Structures, Unions, Bit Manipulations, and Enumerations

Abstract Data Type: Stack

UNIT VI. STACKS AND QUEUES

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

Stacks, Queues and Hierarchical Collections

Stacks and Their Applications

Discussion of project # 1 permutations

1. Stack Implementation Using 1D Array

Chapter 2. Stack & Queues. M hiwa ahmad aziz

S.E. Sem. III [CMPN] Data Structures. Primitive Linear Non Linear

UNIT-2 Stack & Queue

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

CSE 401 Midterm Exam 11/5/10

Stack and Its Implementation

Lab 7 1 Due Thu., 6 Apr. 2017

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

CSE 230 Intermediate Programming in C and C++

Graphs: Graph Data Structure:

Bharati Vidyapeeth s College Of Engineering for Women Pune-43 Department E & TC. SE- Unit Test I Subject-DS

Information Science 2

Stacks. Revised based on textbook author s notes.

BSc.(Hons.) Business Information Systems, BSc. (Hons.) Computer Science with Network Security, BSc.(Hons.) Software Engineering

Application of Stack (Backtracking)

CS 8391 DATA STRUCTURES

Stack Abstract Data Type

CSE 214 Computer Science II Stack

Your Topic Goes Here Queue

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

DATA STRUCTURE UNIT I

Transcription:

Data Structure Chapter 3 Stacks and Queues Instructor: Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 29 Spring

Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2

Figure 3.: Inserting and Deleting Elements in Stack top top top E top D D top C C C B B B B A A A A A top D C B A Stack: an ordered list in which insertions and deletions are made at one end called the top The last element inserted into a stack is the first element removed Last-In-First-Out (LIFO) list C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 3

Implementation of Stack by Array a n- a 2 a a a a a 2 a n- Array index 2 3 n- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 4

Functions for Stacks Create #define MAX_STACK_SIZE /*maximum stack size*/ typedef struct{ int key; /* other fields */ }element; element stack[max_stack_size]; Int top = -; IsFull top >=MAX_STACK_SIZE-; IsEmpty top < Add Delete C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 5

Program 3.: Add to Stack void add (int *top, element item) { /* add an item to the global stack */ if (*top >=MAX_STACK_SIZE-) { stack_full(); return; } stack[++*top]=item; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 6

Program 3.2: Delete from a Stack Element delete (int *top) { /* return the top element from the stack */ if (*top == -) return stack_empty(); /* returns an error key */ return stack[(*top)--]; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 7

How Does the System Stack Work after a Function Call? Old frame pointer Return address 2 Old frame pointer Return address Local variables Old frame pointer Return address fp main fp: a pointer to the current stack frame a: function name fp a C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 8

Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 9

Figure 3.4: Inserting and Deleting Elements in A Queue rear rear rear D rear C C B B B A A A A rear front front front front front Queue: an ordered list in which insertions take place at one end and all deletions take place at the opposite end The first element inserted into a queue is the first element removed First-In-First-Out (FIFO) list D C B a a a 2 a n- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 front rear

Functions of Queue CreateQ #define MAX_QUEUE_SIZE /* Maximum queue size */ typedef struct{ int key; /* other fields */ } element; element queue[max_queue_size]; int rear = -; int front = -; IsFullQ rear == MAX_QUEUE_SIZE- IsEmptyQ front == rear AddQ DeleteQ C.E., NCU, Taiwan Angela Chih-Wei Tang, 29

Program 3.3: Add to A Queue void addq (int *rear, element item) { /* add an item to the queue */ if (*rear == MAX_QUEUE_SIZE-) { queue_full(); return; } queue[++*rear] = item; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2

Program 3.4: Delete from A Queue element deleteq (int *front, int rear) { /* remove element at the front of the queue */ if (*front == rear) return queue_empty(); /*return an error key */ return queue[++*front]; } Is there any problem with current queue representation? C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 3

Initialization: front=rear= Circular Queues front will always point one position counterclockwise from the first element in the queue circular queue assigns next element to q[] when rear == MaxSize If front == rear, is queue empty or full? front = ; rear = 2 3 2 J3 J2 J n- n-2 front = ; rear = 3 n-4 n-3 J4 J3 n- J2 J n-2 front = n-4; rear = C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 4 n-3 n-4

Examples of Full Circular Queues J8 J7 Jn- J6 J5 J3 n- Jn J2 n-2 front = ; rear = n- n-3 n-4 J8 J7 J6 J5 J2 J4 J3 n- Jn- n-4 n-3 n-2 front = n-3; rear = n-4 Solution: A circular queue of size MAX_QUEUE_SIZE will be Permitted to hold at most MAX_QUEUE_SIZE- elements! C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 5

Program 3.5: Add to A Circular Queue void addq (int front, int *rear, element item) { /* add an item to the queue */ *rear = (*rear+)% MAX_QUEUE_SIZE; if (front == *rear) { queue_full(rear); /* reset rear and print error */ return; } queue[*rear] = item; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 6

Program 3.6: Delete from A Circular Queue element deleteq (int *front, int rear) { } element item; /* remove front element from the queue and put it in item */ if (*front == rear) return queue_empty(); /* queue_empty returns an error key */ *front = (*front+)% MAX_QUEUE_SIZE; return queue[*front]; C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 7

Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 8

A Mazing Problem An Example Entrance C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 9 Exit : open path : barrier

Allowable Moves NW [i-][j-] N [i-][j] NE [i-][j+] W [i][j-] X [i][j] [i][j+] E [i+][j-] SW [i+][j] S [i+][j+] SE C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2

A Mazing Problem Data Structure Stack : store the coordinates and direction #define MAX_STACK_SIZE /*maximum stack size*/ typedef struct { short int row; short int col; short int dir; } element; element stack[max_stack_size]; Save current position and arbitrarily pick a possible move we can return to it and try another path if fails! Use of anothr 2-D array to mark any position that has been visited C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 2

Outline Stack Queue A Mazing Problem Evaluations of Expressions C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 22

Order in Which A Second-Degree Polynomial Is Evaluated C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 23

Figure 3.3: Infix and Postfix Notation Expressions are converted from Infix notation into Postfix notation before compiler can process them! Infix? Postfix 2+3*4 234*+ a*b+5 ab*5+ (+2)*7 2+7* a*b/c ((a/(b-c+d))*(e-a)*c a/b-c+d*e-a*c ab*c/ abc-d+/ea-*c* ab/c-de*+ac*- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 24

Infix to Postfix Step : Fully parenthesize the expression Step 2: Move all binary operators so that they replace their corresponding right parentheses Step 3: Delete all parentheses An example: a/b-c+d*e-a*c After step ((((a/b)-c)+(d*e))-a*c)) After steps 2 and 3 ab/c=de*+ac*- C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 25

Figure 3.5: Translation of a+b*c (Infix) to PostFix Token Stack [] [] [2] Top a - a Output + + a b + ab * + * ab c + * abc eos - abc*+ C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 26

Figure 3.6: Translation of a*(b+c)*d to Postfix Token Stack [] [] [2] Top a - a * * a ( * ( a b * ( ab + * ( + 2 ab c * ( + 2 abc ) * abc+ * * abc+* d * abc+*d eos * abc+*d* Output C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 27

Postfix Evaluation : The Representations of Postfix Expression & Stack Expression character array Binary operators: +-*/% Operands: 2 3 4 5 6 7 8 9 Stack int array Operands only Declarations: #define MAX_STACK_SIZE /* maximum stack size */ #define MAX_EXPR_SIZE /* max size of expression */ typedef enum{lparen, rparen, plus, minus, times, divide, mod, eos, operand} precedence; int stack[max_stack_size]; /* global stack */ char expr[max_expr_size]; /* input string */ C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 28

Figure 3.4: Postfix Evaluation Input postfix expression: 6 2 / 3 4 2 * + Token Stack Top [](bottom) [] [2] 6 6 2 6 2 / 6/2 3 6/2 3-6/2-3 4 6/2-3 4 2 6/2-3 4 2 2 * 6/2-3 4*2 + 6/2-3+4*2 C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 29

Postfix Expression Evaluation Step : Make a single left-to-right scan of the postfix Step 2: Place the operands on a stack until we find an operator 2.: Remove the correct number of operands for the operator from the stack 2.2: Perform the operation 2.3: Place the result back on the stack Step 3: Go to step until we reach the end of the expression Step 4: Remove the answer from the top of the stack C.E., NCU, Taiwan Angela Chih-Wei Tang, 29 3