.:: UNIT 4 ::. STACK AND QUEUE

Similar documents
1 P age DS & OOPS / UNIT II

Your Topic Goes Here Queue

Ashish Gupta, Data JUET, Guna

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


LIFO : Last In First Out

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

CS24 Week 4 Lecture 2

Stack and Queue. Stack:

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

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

Chapter 9 STACK, QUEUE

Chapter 18: Stacks And Queues

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Data Structure - Stack and Queue-

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

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

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

Chapter 18: Stacks And Queues

Data Structure. Recitation VII

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

8. Fundamental Data Structures

Data Structures -- Introduction

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

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

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

BBM 201 DATA STRUCTURES

Algorithms, Data Structures, and Problem Solving

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

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

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CSE 230 Intermediate Programming in C and C++

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Lecture 3: Stacks & Queues

Stacks and Queues. CSE Data Structures April 12, 2002

Information Science 2

CMSC 132: Object-Oriented Programming II. Stack and Queue

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

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Data Structures and Algorithms

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

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

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

DATA STRUCTURE UNIT I

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

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

BBM 201 DATA STRUCTURES

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

CMPT 125: Practice Midterm Answer Key

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

CS 206 Introduction to Computer Science II

DS L9: Queues

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

15. Stacks and Queues

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

Stacks and Queues. CSE 373 Data Structures Lecture 6

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

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

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

3. Fundamental Data Structures

SYSC 2006 Winter 2012 Linear Collections: Queues

Dynamic Data Structures

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

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

CS 222: Linked Lists, Queues, Stacks

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

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

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

Postfix (and prefix) notation

Cpt S 122 Data Structures. Data Structures

Linked Structures. See Section 3.2 of the text.

table1 = [ [1, 2, 3, 4], [4, 5, 6, 7], [8, 9,10,11] ] we get: >>> sumcol(table1,0) 13 >>> sumcol(table1,1) 16

csci 210: Data Structures Stacks and Queues

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

Week 6. Data structures

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

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

COP 3502 (Computer Science I) Test #3: Data Structures Date: 11/1/2013 VERSION A

Stacks and Queues

CP2 Revision. theme: dynamic datatypes & data structures

PA3 Design Specification

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

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

Abstract Data Types. Definitions, Implementations, and Uses

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

Data Structures and Algorithms for Engineers

12 Abstract Data Types

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Chapter 5. ADTs Stack and Queue

BBM 201 DATA STRUCTURES

UNIT-2 Stack & Queue

Lecture Data Structure Stack

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13

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

Transcription:

.:: UNIT 4 ::. STACK AND QUEUE

4.1

A stack is a data structure that supports: Push(x) Insert x to the top element in stack Pop Remove the top item from stack A stack is collection of data item arrange sequence arrangement. New item, or nodes as they are often called, can be added to a stack and removed from a stack only from one end. A stack has the LIFO (Last In First out) concept

Example: Arrangement of books (Push) Red Blue Green Yellow Remove books (Pop) Yellow Green Blue Red

Push term used to insert data to existing stack Pop term used to delete data from stack 5 stack operations Create stack Check empty stack Check full stack Push item to stack Pop item from stack

4.2

Create stack Let say, size = 4 3 2 1 0 top = -1 7

Determine whether stack is full / not before insert an item to stack If stack is Full -> insert operation cannot be done 3 2 1 0 top = 3

Insert item into stack If stack full => proceed insert item 3 2 1 0 top top = -1021 3

Delete item from stack If stack empty => proceed delete item 3 2 1 0 top top = -1 3021

Stack is similar to array but its operation only can be done on top of the item in the list.

4.3

There are two ways to implement a stack. One is by static implementation using an array and the other is by dynamic implementation using pointers (linked list).

atas senarai DECLARING STACK [3] [2] [1] typedef struct Tindan { int atas; int senarai[4]; }Tindanan; Tindanan *t; atas [0] [3] senarai CREATING STACK -1 atas [2] [1] [0] senarai t->atas = -1;

CHECK STACK EMPTY if(t->atas == -1) return (1); else return(0); CHECK STACK FULL if(t->atas == 3) return (1); else return(0);

PUSH ITEM INTO STACK cin>>data; t->atas++; t->senarai[t->atas] = data; POP ITEM FROM STACK t->atas--; 0 atas [3] [2] [1] [0] 10 senarai

A linked list is used to implement a stack dynamically. Its size can grow or shrink during program execution. When a stack is represented as a linked list, the first entry of the linked list will be at the top of the stack. The second entry of the linked list will be the second entry of the stack, and so on. The advantage of linked list implementation over an array implementation is that, there is no limit on the number of entries one can add to the stack.

Stack is being created Push(10) t NULL t 10 Push(8) Push(6) Pop t 8 10 t 6 8 10 t 8 10

typedef struct Node { int data; Node *next; }Sstack; Sstack * head; Creating stack head = NULL; head head NULL When head is NULL; the stack is empty.

1. Insert new item to an empty stack Create new node Sstack * pnew; pnew = new Node; Set new node created as first node in the stack pnew->next = head; head pnew pnew head NULL 6 NULL 6 Set head to new node created head = pnew; head 6 pnew

2. Insert new item to an existing stack Create new node Sstack * pnew; pnew = new Node; Set new node created as first node in the stack pnew->next = head; head head pnew 6 8 10 pnew 6 8 10 Set head to new node created head = pnew; head 8 10 pnew 6

Before pop operation can be done, check whether stack is empty or not. Create new pointer (pdel) to point to an item that want to be destroy/delete. 3 steps in pop operation:- pdel = head; head = pdel->next; / head = head->next; delete pdel; head 6 8 10 pdel

4.4

Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end. Accessing the elements of queues follows a First In, First Out (FIFO) order. Like customers standing in a check-out line in a store, the first customer in is the first customer served.

Primary queue operations: Enqueue and Dequeue Like check-out lines in a store, a queue has a front and a rear. Enqueue insert an element at the rear of the queue Dequeue remove an element from the front of the queue Remove (Dequeue) front rear Insert (Enqueue)

4.5

Just as stacks can be implemented as arrays or linked lists, so with queues. Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks

There are several different algorithms to implement Enqueue and Dequeue Naïve way When enqueuing, the front index is always fixed and the rear index moves forward in the array. rear rear rear 3 3 6 3 6 9 front Enqueue(3) front Enqueue(6) front Enqueue(9)

Naïve way (cont d) When dequeuing, the front index is fixed, and the element at the front the queue is removed. Move all the elements after it by one position. (Inefficient!!!) rear rear rear = -1 6 9 9 front front front Dequeue( ) Dequeue( ) Dequeue( )

A better way When an item is enqueued, the rear index moves forward When an item is dequeued, the front index also moves forward by one element The problem here is that the rear index cannot move beyond the last element in the array.

Using a circular array When an element moves past the end of a circular array, it wraps around to the beginning, e.g. OOOOO7963 4OOOO7963 (after Enqueue(4)) After Enqueue(4), the rear index moves from 3 to 4. How to detect an empty or full queue, using a circular array algorithm? Use a counter of the number of elements in the queue.