St S a t ck a ck nd Qu Q eue 1



Similar documents
Data Structures and Algorithms Stacks and Queues

Module 2 Stacks and Queues: Abstract Data Types

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

DATA STRUCTURE - QUEUE

Linked Lists Linked Lists, Queues, and Stacks

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Common Data Structures

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

CmpSci 187: Programming with Data Structures Spring 2015

10CS35: Data Structures Using C

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

Chapter 3: Restricted Structures Page 1

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Lecture Notes on Stacks & Queues

Introduction to Data Structures

PES Institute of Technology-BSC QUESTION BANK

Sequential Data Structures

DATA STRUCTURE - STACK

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Programming with Data Structures

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Universidad Carlos III de Madrid

DATA STRUCTURES USING C

Introduction to Data Structures and Algorithms

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Stacks. Linear data structures

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Basic Data Structures and Algorithms

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

CS 2412 Data Structures. Chapter 2 Stacks and recursion

Analysis of a Search Algorithm

CS 2412 Data Structures. Chapter 3 Queues

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

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Review of Hashing: Integer Keys

Data Structures UNIT III. Model Question Answer

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

STACKS,QUEUES, AND LINKED LISTS

Computer Systems Architecture

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

Abstract Data Types. Chapter 2

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Data Structures/Stacks and Queues

Lecture 2: Data Structures Steven Skiena. skiena

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Data Structures and Algorithms C++ Implementation

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Algorithms and Data Structures

Data Structures and Algorithms

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Java Software Structures

Data Structures Using C++

Data Structure [Question Bank]

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

I/O Management. General Computer Architecture. Goals for I/O. Levels of I/O. Naming. I/O Management. COMP755 Advanced Operating Systems 1

Stacks. Data Structures and Data Types. Collections

C++ INTERVIEW QUESTIONS

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Data Structure with C

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Chapter 5. Recursion. Data Structures and Algorithms in Java

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Algorithms and Data Structures Written Exam Proposed SOLUTION

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Cpt S 223. School of EECS, WSU

From Last Time: Remove (Delete) Operation

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

14 Stacks, Queues, And Linked Lists

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

STACK Data Structure:

CompSci-61B, Data Structures Final Exam

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Application of Stacks: Postfix Expressions Calculator (cont d.)

Algorithm Design and Recursion

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Lecture Notes on Binary Search Trees

Lecture 12 Doubly Linked Lists (with Recursion)

Fast nondeterministic recognition of context-free languages using two queues

Sample Questions Csci 1112 A. Bellaachia

Transcription:

Stack and Queue 1

Stack Data structure with Last-In First-Out (LIFO) behavior In Out C B A B C 2

Typical Operations Pop on Stack Push isempty: determines if the stack has no elements isfull: determines if the stack is full in case of a bounded sized stack top: returns the top element in the stack push: inserts an element into the stack pop: removes the top element from the stack push is like inserting at the front of the list pop is like deleting from the front of the list 3

Creating and Initializing a Stack Declaration #define MAX_STACK_SIZE 100 typedef struct { int key; /* just an example, can have any type of fields depending on what is to be stored */ } element; typedef struct { element list[max_stack_size]; int top; /* index of the topmost element */ } stack; Create and Initialize stack Z; Z.top = -1; 4

Operations int isfull (stack *s) { if (s->top >= } int isempty (stack *s) MAX_STACK_SIZE 1) { if (s->top == -1) return 1; return 0; return 1; return 0; } 5

Operations element top( stack *s ) { return s->list[s->top]; } void push( stack *s, element e ) { } (s->top)++; s->list[s->top] = e; void pop( stack *s ) { (s->top)--; } 6

Application: Parenthesis Matching Given a parenthesized expression, test whether the expression is properly parenthesized Examples: ( )( { } [ ( { } { } ( ) ) ] ) ( ){ [ ] is not proper ( { ) } is not proper )([ ] is not proper ( [ ] ) ) is not proper is proper 7

Approach: Whenever a left parenthesis is encountered, it is pushed in the stack Whenever a right parenthesis is encountered, pop from stack and check if the parentheses match Works for multiple types of parentheses ( ), { }, [ ] 8

Parenthesis matching while (not end of string) do { a = get_next_token(); if (a is ( or { or [ ) push (a); if (a is ) or } or ] ) { if (is_stack_empty( )) { print ( Not well formed ); exit(); } x = top(); pop(); if (a and x do not match) { print ( Not well formed ); exit(); } } } if (not is_stack_empty( )) print ( Not well formed ); 9

Recursion can be implemented as a stack fib (5) Fibonacci recurrence: fib(n) = 1 if n =0 or 1; = fib(n 2) + fib(n 1) otherwise; fib (3) fib (4) fib (1) fib (2) fib (2) fib (3) fib (0) fib (1) fib (0) fib (1) fib (1) fib (2) fib (0) fib (1) 10

Fibonacci Recursion Stack 1 0 0 3 2 2 1 1 2 1 5 4 4 4 4 4 4 3 3 0 0 0 1 1 2 3 3 3 1 3 1 3 2 2 4 5 5 6 0 1 1 6 7 8 11

Tower of Hanoi A B C 12

Tower of Hanoi A B C 13

Tower of Hanoi A B C 14

Tower of Hanoi A B C 15

Towers of Hanoi Function void towers (int n, char from, char to, char aux) { /* Base Condition */ if (n==1) { printf ( Disk 1 : %c -> %c \n, from, to) ; return ; } /* Recursive Condition */ towers (n-1, from, aux, to) ; printf ( Disk %d : %c -> %c\n, n, from, to) ; towers (n-1, aux, to, from) ; } 16

TOH Recursion Stack 3,A,B,C 2,A,C,B A to B 2,C,B,A 1,A,B,C A to C 1,B,C,A A to B 2,C,B,A A to B A to C 1,B,C,A A to B 2,C,B,A A to C 1,B,C,A A to B 2,C,B,A 1,B,C,A A to B 2,C,B,A B to C A to B 2,C,B,A A to B 2,C,B,A 2,C,B,A 1,C,A,B C to B 1,A,B,C 17

Queue Data structure with First-In First-Out (FIFO) behavior In Out C B A B A 18

Typical Operations on Queue REAR Enqueue isempty: determines if the queue is empty isfull: determines if the queue is full in case of a bounded size queue front: returns the element at front of the queue enqueue: inserts an element at the rear dequeue: removes the element in front Dequeue FRONT 19

Possible Implementations Linear Arrays: (static/dynamicaly allocated) Circular Arrays: (static/dynamically allocated) front rear front rear Linked Lists: Use a linear linked list with insert_rear and delete_front operations Can be implemented by a 1-d array using modulus operations 20

Circular Queue [3] [4] [2] [5] [1] [6] [0] [7] front=0 rear=0 21

Circular Queue [2] [1] [3] [4] [5] [6] [2] [1] [3] B A C D [4] rear = 4 [5] [6] After insertion of A, B, C, D [0] [7] front=0 [0] [7] front=0 rear=0 22

Circular Queue [2] [1] [3] [4] [5] [6] [2] [1] [3] B A C D [4] rear = 4 [5] [6] After insertion of A, B, C, D [0] [7] front=0 [0] [7] front=0 rear=0 front=2 [2] [3] C D [4] rear = 4 [5] [1] [6] After deletion of of A, B [0] [7] 23

front: index of queue-head (always empty why?) rear: index of last element, unless rear = front [3] [4] rear = 3 [3] [4] front=4 [2] [5] [2] [5] [1] [6] [1] [6] [0] front=0 rear=0 [7] Queue Empty [0] [7] Queue Full Queue Empty Condition: front == rear Queue Full Condition: front == (rear + 1) % MAX_Q_SIZE 24

Creating and Initializing a Circular Queue Declaration #define MAX_Q_SIZE 100 typedef struct { int key; /* just an example, can have any type of fields depending on what is to be stored */ } element; typedef struct { element list[max_q_size]; int front, rear; } queue; Create and Initialize queue Q; Q.front = 0; Q.rear = 0; 25

Operations int isfull (queue *q) { if (q->front == ((q->rear + 1) % } return 1; return 0; MAX_Q_SIZE)) int isempty (queue *q) { if (q->front == q->rear) return 1; return 0; } 26

Operations element front( queue *q ) { return q->list[(q->front >front + 1) % MAX_Q_SIZE]; } void enqueue( queue *q, element e) { q->rear = (q->rear + 1)% MAX_Q_SIZE; q->list[q->rear] = e; } void dequeue( queue *q ) { q-> front = (q-> front + 1)% MAX_Q_SIZE; } 27

Exercises Implement the Queue as a linked list. Implement a Priority Queue which maintains the items in an order (ascending/ descending) and has additional functions like remove_max and remove_min Maintain a Doctor s appointment list 28