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

Similar documents
CHAPTER 3 STACKS AND QUEUES

STACKS AND QUEUES CHAPTER 3

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

Chapter3 Stacks and Queues

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Infix to Postfix Conversion

Fall, 2015 Prof. Jungkeun Park

BBM 201 DATA STRUCTURES

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

BBM 201 DATA STRUCTURES

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

Data Structure - Stack and Queue-

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

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and 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

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

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

Lecture Data Structure Stack

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Associate Professor Dr. Raed Ibraheem Hamed

Data Structures Week #3. Stacks

DATA STRUCTURE UNIT I

Content: Learning Objectives

Lecture No.04. Data Structures

Chapter 3. Stack & Queue 1

Review of the C Programming Language for Principles of Operating Systems

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


GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

資料結構 Data Structures. Hsiao-Lung Chan, Ph.D. Dept Electrical Engineering Chang Gung University, Taiwan

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

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

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

Postfix (and prefix) notation

Review of the C Programming Language

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Informatics Ingeniería en Electrónica y Automática Industrial

Sample Question Paper

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

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

Stacks, Queues and Hierarchical Collections

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

Chapter 2. Stack & Queues. M hiwa ahmad aziz

A flow chart is a graphical or symbolic representation of a process.

Stacks and Queues. CSE Data Structures April 12, 2002

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

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

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

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

Linear Data Structure

Expressions and Precedence. Last updated 12/10/18

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

Lecture 02 C FUNDAMENTALS

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

Data Types and Variables in C language

JAVA OPERATORS GENERAL

12 Abstract Data Types

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

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

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

An Introduction to Trees

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

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

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

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

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Computers Programming Course 6. Iulian Năstac

List, Stack and Queue Implementation

Code No: R Set No. 1

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

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

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

Operators. Java operators are classified into three categories:

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Guide for The C Programming Language Chapter 5

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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

STACKS AND QUEUES. Problem Solving with Computers-II

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

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Chapter 3: Operators, Expressions and Type Conversion

1. Stack Implementation Using 1D Array

CHAPTER 5. Trees CHAPTER 5 1/70

.:: UNIT 4 ::. STACK AND QUEUE

March 13/2003 Jayakanth Srinivasan,

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

UNIT- 3 Introduction to C++

// The next 4 functions return true on success, false on failure


Stacks Calculator Application. Alexandra Stefan

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

Transcription:

CHAPTER 3 Stacks And Queues 1

Stack Stack: a Last-In-First-Out (LIFO/FILO) list Push Pop C A B C B A Top 2

An application of stack: stack frame of function call Old frame pointer fp Return address al Old frame pointer fp Local variables Old frame pointer Return address stack frame of invoking function main Return address main system stack before a1 is invoked system stack after a1 is invoked (a) (b) System stack after function call a1

Abstract Data Type for stack structure Stack is objects: a finite ordered list with zero or more elements. functions: for all stack Stack, item element, max_stack_size positive integer Stack CreateS(max_stack_size) ::= create an empty stack whose maximum size is max_stack_size Boolean IsFull(stack, max_stack_size) ::= if (number of elements in stack == max_stack_size) return TRUE else return FALSE Stack Push(stack, item) ::= if (IsFull(stack)) stack_full else insert item into top of stack and return 4

Boolean IsEmpty(stack) ::= if(stack == CreateS(max_stack_size)) return TRUE else return FALSE Element Pop(stack) ::= if(isempty(stack)) return else remove and return the item on the top of the stack. 5

Implementation : using Array Stack CreateS(max_stack_size) ::= #define MAX_STACK_SIZE [1..n] /* maximum stack size */ typedef struct { int key; /* other fields */ } element; element stack[max_stack_size]; int top = 0; Boolean IsEmpty(Stack) ::= top== 0; Boolean IsFull(Stack) ::= top == MAX_STACK_SIZE; 6

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

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

Queue Queue: a First-In-First-Out (FIFO/LILO) list Delete Insert C A D B (1) Add Rear (2) Delete Front D C B A Front Rear 9

Different Queue (1) FIFO Queue (2) Priority Queue (3) Double-ended Queue (4) Double-ended Priority Queue Delete ADD front rear 10

Application: Job scheduling front rear Q[0] Q[1] Q[2] Q[3] Comment -1-1 Queue is empty -1 0 J1 Jop 1 is added -1 1 J1 J2 Jop 2 is added -1 2 J1 J2 J3 Jop 3 is added 0 2 J2 J3 Jop 1 is deleted 1 2 J3 Jop 2 is deleted 11

Abstract Data Type of queue structure Queue is objects: a finite ordered list with zero or more elements. functions: for all queue Queue, item element, max_ queue_ size positive integer Queue CreateQ(max_queue_size) ::= create an empty queue whose maximum size is max_queue_size Boolean IsFullQ(queue, max_queue_size) ::= if(number of elements in queue==max_queue_size) return TRUE else return FALSE Queue AddQ(queue, item) ::= if (IsFullQ(queue)) queue_full else insert item at rear of queue and return queue 12

Boolean IsEmptyQ(queue) ::= if (queue ==CreateQ(max_queue_size)) return TRUE else return FALSE Element DeleteQ(queue) ::= if (IsEmptyQ(queue)) return else remove and return the item at front of queue. 13

Implementation : using Array Queue CreateQ(max_queue_size) ::= # define MAX_QUEUE_SIZE [1 n] typedef struct { int key; /* other fields */ } element; element queue[max_queue_size]; int rear = 0; int front = 0; 14

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; } 15

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 */ else return queue [++ *front]; } problem: there may be available space when IsFullQ is true, i.e., movement is required. 16

Implementation : regard an array as a circular queue EMPTY QUEUE front = 0 front = 0 rear = 0 rear = 3 17

Create a circular queue Queue CreateQ(max_queue_size) ::= # define MAX_QUEUE_SIZE [0 n-1] typedef struct { int key; /* other fields */ } element; element queue[max_queue_size]; int rear = 0; int front = 0; 18

Add to a circular queue void addq(int *front, int *rear, element item) { /* add an item to the queue */ *rear = (*rear +1) % MAX_QUEUE_SIZE; if (*front == *rear) then queue_full( ); return; } queue[*rear] = item; } 19

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 */ else *front = (*front+1) % MAX_QUEUE_SIZE; return queue[*front]; } 20

Problem: one space is left when queue is full FULL QUEUE FULL QUEUE front =0 rear = 5 front =4 rear =3 21

Create a circular queue (with n spaces used) Queue CreateQ(max_queue_size) ::= # define MAX_QUEUE_SIZE [0 n-1] typedef struct { int key; /* other fields */ } element; element queue[max_queue_size]; int rear = 0; int front = 0; boolean tag =0 22

Add to a circular queue (with n spaces used) void addq(int front, int *rear, element item) { if (*rear=*front) and tag =1) then queue_full(); else *rear = (*rear +1) % MAX_QUEUE_SIZE; queue[*rear]=item if (* front == *rear) tag=1; } 23

Delete from a circular queue (with n spaces used) element deleteq(int* front, int rear) { element item; if ((*front=*rear) and (tag=0)) then queue_empty(); else *front = (*front+1) % MAX_QUEUE_SIZE; return queue[*front]; if (*front=*rear) then tag=0; } 24

Evaluation of Expressions X = a / b - c + d * e - a * c a = 4, b = c = 2, d = e = 3 Interpretation 1: ((4/2)-2)+(3*3)-(4*2)=0 + 9-8=1 Interpretation 2: (4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2=-2.66666 How to generate the machine instructions corresponding to a given expression? precedence rule + associative rule 25

Token Operator Precedence Associativity ( ) [ ] -> founction call array element struct or union member 17 left-to-right -- ++ decreament,increament 16 left-to-right -- ++! - - + & * Sizeof decreament,increament logical not one s complement unaryminus or plus address or indirection size (in bytes) 15 right-to-left (type) type cast 14 right-to-left */% multiplicative 13 left-to-right 26

+ - binary add or subtract 12 left-to-right << >> shift 11 left-to-right > >= relational 10 left-to-right < <= ==!= equality 9 left-to-right & bitwise and 8 left-to-right ^ bitwise exclusive or 7 left-to-right bitwise or 6 left-to-right && logical and 5 left-to-right [[ logical or 4 left-to-right 27

?: conditional 3 right-to-left = += -= /= *= %= <<= >>= &= [= ^= assignment 2 right-to-left, comma 1 left-to-right 1.The precedence column is taken from Harbison and Steele. 2.Postfix form 3.prefix form 28

user Infix 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 29

Infix to Postfix Conversion (Intuitive Algorithm) (1) Fully parenthesize expression a / b - c + d * e - a * c --> ((((a / b) - c) + (d * e)) - a * c)) (2) All operators replace their corresponding right parentheses. ((((a / b) - c) + (d * e)) - a * c)) (3) Delete all parentheses. ab/c-de*+ac*- two passes 30

Infix a*b/c a/b-c+d*e-a*c a*(b+c)/d-g Prefix /*abc -+- /abc*de*ac -/*a+bcdg (1) evaluation (2) transformation 31

Question: Please transform the following expression into postfix. ( A B) ( C^( D E) F) G Ans: Postfix : AB CDE ^ F G 32

Question: Use array to implement the following operations for stack S (1)PUSH an item into S. (2)POP an item from S. 33

Ans: (1)void PUSH (item x, stack s) { if (top==n) then stackfull else { Top=Top+1; S[Top]=x; } } 34

(2) item POP(stack S) { if (Top== ) then stockempty; else { } } X=S[Top]; Top=Top-1; 35

Reference Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed Fundamentals of Data Structures in C, W. H. Freeman & Co Ltd, 1992. Ellis Horowitz, Sartaj Sahni, and Dinesh Mehta Fundamentals of Data Structures in C++ Silicon Pr, 2006 Richard F.Gilberg, Behrouz A. Forouzan, Data Structures: A Pseudocode Approach with C, S Baker & Taylor Books, 2004 Fred Buckley, and Marty Lewinter A Friendly Introduction to Graph Theory Prentice Hall, 2002 資料結構 - 使用 C 語言 蘇維雅譯, 松崗,2004 資料結構 - 使用 C 語言 蔡明志編著, 全華,2004 資料結構 ( 含精選試題 ) 洪逸編著, 鼎茂,2005 36