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

Similar documents

Stack and Queue. Stack:

BBM 201 DATA STRUCTURES

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

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

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

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

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

BBM 201 DATA STRUCTURES

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

.:: UNIT 4 ::. 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.

Stacks and Queues. CSE 373 Data Structures Lecture 6

Linear Data Structure

UNIT VI. STACKS AND QUEUES

STACKS AND QUEUES. Problem Solving with Computers-II

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

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

Chapter 5. ADTs Stack and Queue

Data Structures And Algorithms

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Data Structure - Stack and Queue-

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

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

Name CPTR246 Spring '17 (100 total points) Exam 3

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

Data Structure. Recitation VII

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

DS Assignment II. Full Sized Image

Lecture Data Structure Stack

Ashish Gupta, Data JUET, Guna

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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

LIFO : Last In First Out

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

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

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Abstract Data Type: Stack

Postfix (and prefix) notation

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

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

ITI Introduction to Computing II

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Chapter 18: Stacks And Queues

15. Stacks and Queues

csci 210: Data Structures Stacks and Queues

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

1 P age DS & OOPS / UNIT II

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

DATA STRUCTURE UNIT I

Matriculation number:

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

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Chapter 9 STACK, QUEUE

Chapter 18: Stacks And Queues

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Bachelor Level/ First Year/ Second Semester/ Science Full Marks: 60 Computer Science and Information Technology (CSc. 154) Pass Marks: 24

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

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

Lecture 3: Stacks & Queues

Data Structures. Lecture 5 : The Queues

03/29/2004. A dispenser has three essential features: adding, removing, accessing. Dispensers

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

[CS302-Data Structures] Homework 2: Stacks

List, Stack, and Queues

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

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

Data Structures and Algorithms (DSA) Course 6 Lists (Recapitulation) Iulian Năstac

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

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

CS24 Week 4 Lecture 2

CSC 1052 Algorithms & Data Structures II: Linked Queues

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

Data Structures and Algorithms(3)

Data Structures and Algorithms for Engineers

UNIT-2 Stack & Queue

CSC 1052 Algorithms & Data Structures II: Queues

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Abstract Data Types. Abstract Data Types

10 MCQ of Data Structure Paper-2(CS/IT) For NIC SSC Scientific Assistant IBPS IT Officer Exam 2017

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Linked Structures. See Section 3.2 of the text.

Discussion of project # 1 permutations

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

CSC148 Week 2. Larry Zhang

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

Data Structures (INE2011)

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

BBM 201 DATA STRUCTURES

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

12 Abstract Data Types

Stacks, Queues and Hierarchical Collections

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 >= MAX_STACK_SIZE 1) return 1; return 0; } int isempty (stack *s) { if (s->top == -1) return 1; return 0; } 5

Operations void push( stack *s, element e ) { } element top( stack *s ) { return s->list[s->top]; } (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 proper ( ){ [ ] is not proper ( { ) } is not proper )([ ] is not proper ( [ ] ) ) is not 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 3 5 4 1 2 4 2 4 0 1 4 1 4 2 4 3 0 0 0 1 1 2 3 3 3 0 1 3 1 3 1 3 2 0 2 1 4 5 5 6 6 7 8 1 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 1,A,B,C A to B A to C A to C A to C 3,A,B,C 2,A,C,B A to B 2,C,B,A 1,B,C,A A to B 2,C,B,A 1,B,C,A A to B 2,C,B,A 1,B,C,A A to B 2,C,B,A 1,B,C,A B to C 1,C,A,B A to B A to B A to B C to B 2,C,B,A 2,C,B,A 2,C,B,A 2,C,B,A 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) % MAX_Q_SIZE)) return 1; return 0; int isempty (queue *q) } { if (q->front == q->rear) return 1; return 0; } 26

Operations element front( queue *q ) { return q->list[(q->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