BBM 201 DATA STRUCTURES

Similar documents
BBM 201 DATA STRUCTURES

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

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


Lecture Data Structure Stack

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

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Stack and Queue. Stack:

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

ITI Introduction to Computing II

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

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

.:: UNIT 4 ::. STACK AND QUEUE

Data Structure - Stack and Queue-

Lecture 4 Stack and Queue

STACKS AND QUEUES. Problem Solving with Computers-II

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

Abstract Data Type: Stack

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

8. Fundamental Data Structures

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

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

DEEPIKA KAMBOJ UNIT 2. What is Stack?

15. Stacks and Queues

Associate Professor Dr. Raed Ibraheem Hamed

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

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

UNIT-2 Stack & Queue

LIFO : Last In First Out

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

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

CHAPTER 3 STACKS AND QUEUES

PA3 Design Specification

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

Linear Data Structure

Stacks and Queues. CSE Data Structures April 12, 2002

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

DS Assignment II. Full Sized Image

Stacks and queues (chapters 6.6, 15.1, 15.5)

Stacks, Queues and Hierarchical Collections

Postfix (and prefix) notation

csci 210: Data Structures Stacks and Queues

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

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

Chapter 18: Stacks And Queues

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

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

BBM 201 DATA STRUCTURES

Fall, 2015 Prof. Jungkeun Park

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

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

Lecture 3: Stacks & Queues

1 P age DS & OOPS / UNIT II

! A data type for which: ! An ADT may be implemented using various. ! Examples:

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

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

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

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

Chapter 2. Stack & Queues. M hiwa ahmad aziz

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

[CS302-Data Structures] Homework 2: Stacks

Ashish Gupta, Data JUET, Guna

Chapter 18: Stacks And Queues

ADTs Stack and Queue. Outline

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

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

March 13/2003 Jayakanth Srinivasan,

The Stack and Queue Types

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

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

Motivation for Queues

Stacks and Queues. CSE 373 Data Structures Lecture 6

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

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

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

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

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

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

Queues Fall 2018 Margaret Reid-Miller

Queue: Queue Representation: Basic Operations:

Chapter 9 STACK, QUEUE

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

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

Chapter 5. ADTs Stack and Queue

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

12 Abstract Data Types

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

CSC148H Week 3. Sadia Sharmin. May 24, /20

Chapter3 Stacks and Queues

Data Structure. Recitation VII

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Data Structures Week #3. Stacks

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

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Foundations of Data Structures

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Transcription:

BBM 201 DATA STRUCTURES Lecture 5: Stacks and Queues 2017 Fall

Stacks A list on which insertion and deletion can be performed. Based on Last-in-First-out (LIFO) Stacks are used for a number of applications: Converting a decimal number into binary Program execution Parsing Evaluating postfix expressions Towers of Hanoi

Stacks A stack is an ordered lists in which insertions and deletions are made at one end called the top.

Stacks A top B A top C B A top B A top

Towers of Hanoi Object of the game is to move all the disks (animals) over to Tower 3. But you cannot place a larger disk onto a smaller disk.

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Stack Operations 1. Pop() 2. Push(x) 3. Top() 4. IsEmpty() An insertion (of, say x) is called push operation and removing the most recent element from stack is called pop operation. Top returns the element at the top of the stack. IsEmpty returns true if the stack is empty, otherwise returns false. All of these take constant time - O(1)

Example Push(2) Push(10) Pop() Push(7) Push(5) Top(): 5 IsEmpty(): False

Array implementation of stack (pseudocode) int A[10] top ß -1 //empty stack Push(x) { top ß top + 1 A[top]ß x } Pop() { top ß top 1 } For an empty stack, top is set to -1. In push function, we increment top. In pop, we decrement top by 1.

Array implementation of stack (pseudocode) Top() { return A[top] } IsEmpty() { if(top == -1) return true else return false }

Stack Data Structure #define MAX_STACK_SIZE 100 typedef struct{ int VALUE; }element; element stack[max_stack_size]; int top=-1;

Push Stack void push (element item) { if(top>=max_stack_size){ isfull(); return; } stack[++top]=item; }

Pop Stack element pop() { if(top==-1) return empty_stack(); return stack[top--]; }

Implementation of Stacks Using Arrays 3

More array implementation

Check For Balanced Parentheses using Stack Expression Balanced? (A+B) {(A+B)+(C+D)} {(x+y)*(z) [2*3]+(A)] {a+z)

Check For Balanced Parantheses using Stack Expression Balanced? ( ) Yes { ( ) ( ) } Yes { ( ) ( ) No [ ] ( ) ] No { ) No Need: Count of openings = Count of closings AND Any parenthesis opened last should be closed first.

Idea: Create an empty list Scan from left to right If opening symbol, add it to the list Push it into the stack If closing symbol, remove last opening symbol of the same type using Pop from the stack Should end with an empty list

Check For Balanced Parantheses: Pseudocode CheckBalancedParanthesis(exp) { nß length(exp) Create a stack: S for iß 0 to n-1 { if exp[i] is ( or { or [ Push(exp[i]) elseif exp[i] is ) or } or ] {if (S is not empty) (top does not pair with exp[i]) {return false} else pop()}} Return S is empty? Create a stack of characters and scan this string by using push if the character is an opening parenthesis and by using pop if the character is a closing parenthesis. (See next slide)

Examples The pseudo code will return false. The pseudo code will return true.

Queues A queue is an ordered list on which all insertions take place at one end called the rear/back and all deletions take place at the opposite end called the front. Based on First-in-First-out (FIFO)

Comparison of Queue and Stack

Queues front,top A rear front B A rear front C B A rear front Queue is a list with the restriction that insertion can be made at one end (rear) And deletion can be made at other end (front).

Built-in Operations for Queue 1. Enqueue(x) or Push(x) 2. Dequeue() or Pop() 3. Front(): Returns the element in the front without removing it. 4. IsEmpty(): Returns true or false as an answer. 5. IsFull() Each operation takes constant time, therefore has O(1) time complexity.

Example Enqueue(2) Enqueue(5) Enqueue(3) Dequeue()à2 Front()à5 IsEmpty()àFalse Applications: Printer queue Process scheduling

Array implementation of queue (Pseudocode) int A[10] front ß -1 rear ß -1 IsEmpty(){ if (front == -1 && rear == -1) return true else return false} Enqueue(x){ if IsFull(){ return elseif IsEmpty(){ front ß rear ß 0} else{ rear ß rear+1} A[rear]ß x}

Array implementation of queue (Pseudocode) Dequeue(){ if IsEmpty(){ return elseif (front == rear){ front ß rear ß -1} else{ front ß front+1} At this stage, we cannot Enqueue an element anymore.

Queue Data Structure #define MAX_QUEUE_SIZE 100 typedef struct{ int value; }element; element queue[max_queue_size]; int front=-1; int rear=-1;

Add Queue void addq( element item) { if(rear==max_queue_size-1){ isfull(); return; } queue[++rear]=item; }

Delete Queue element deleteq(element item) { if(front==rear) return isempty(); return queue[++front]; }

Circular Queue When the queue is full (the rear index equals to MAX_QUEUE_SIZE) We should move the entire queue to the left Recalculate the rear Shifting an array is time-consuming! O(MAX_QUEUE_SIZE)

Circular Queue More efficient queue representation

Full Circular Queue

Enqueue for circular array (Pseudocode) Current position = i Next position = (i+1)% N previous position = (i+n-1)%n Enqueue(x){ if (rear+1)%n == front return elseif IsEmpty(){ front ß rear ß 0} else{ rear ß (rear+1)%n} A[rear]ß x}

Dequeue for circular array (Pseudocode) Dequeue(x){ if IsEmpty(){ return elseif (front == rear){ front ß rear ß -1} else{ front ß (front+1)%n}

Add Circular Queue void addcircularq( element item) { rear=(rear+1)%max_queue_size; if(front==rear){ isfull(rear); return; } queue[rear]=item; }

Delete Circular Queue void deletecircularq() { if(front==rear) return isempty(); front=(front+1)%max_queue_size; return queue[front]; }

References BBM 201 Notes by Mustafa Ege http://www.mycodeschool.com/videos