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

Similar documents
.:: UNIT 4 ::. STACK AND QUEUE

CS24 Week 4 Lecture 2


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

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Stack and Queue. Stack:

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

1 P age DS & OOPS / UNIT II

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

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Introduction to the C programming language

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

Introduction to the C programming language

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.

Data Structures and Algorithms

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

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

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

Data Structure. Recitation VII

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

ECE 2400 Computer Systems Programming Fall 2017 Topic 9: Abstract Data Types

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Chapter 9 STACK, QUEUE

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

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

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Information Science 2

CMSC Introduction to Algorithms Spring 2012 Lecture 7

ECE 2400 Computer Systems Programming Fall 2018 Topic 9: Abstract Data Types

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

LIFO : Last In First Out

Stacks and Queues. CSE Data Structures April 12, 2002

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

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

Linked Lists and Abstract Data Structures A brief comparison

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

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

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

ITI Introduction to Computing II

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Data Structure Advanced

Lecture 3: Stacks & Queues

Separate Compilation and Namespaces Week Fall. Computer Programming for Engineers

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

1 Short Answer (7 Points Each)

Stacks and Queues. CSE 373 Data Structures Lecture 6

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

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

DATA STRUCTURES: OVERVIEW AND ITS APPLICATIONS

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

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

Algorithms & Data Structures

csci 210: Data Structures Stacks and Queues

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15

Propedéutico de Programación

Content: Learning Objectives

More Data Structures (Part 1) Stacks

Programming Assignment 2

STACKS AND QUEUES. Problem Solving with Computers-II

Implementing Abstractions

Lecture Data Structure Stack

Object Oriented Programming COP3330 / CGS5409

CSC 2400: Computer Systems. Using the Stack for Function Calls

DATA STRUCTURE UNIT I

12 Abstract Data Types

Chapter 18: Stacks And Queues

Rob Legrand, and Ron K Cytron Washington University in St Louis 1 Brookings Dr Saint Louis, MO

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Unit 4: Stacks and Queues

An Introduction to Trees

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

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

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

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

The C++ Object Lifecycle. EECS 211 Winter 2019

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Queues Fall 2018 Margaret Reid-Miller

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

UNIT-2 Stack & Queue

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

Ashish Gupta, Data JUET, Guna

Lecture Notes on Queues

Chapter 18: Stacks And Queues

CSC 1600 Memory Layout for Unix Processes"

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

CS 216 Exam 1 Fall SOLUTION

final int a = 10; for(int i=0;i<a;i+=2) { for(int j=i;j<a;j++) { System.out.print("*"); } System.out.println(); }

Lecture Notes on Queues

Data Structures (INE2011)

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

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

Basic Data Structures

A queue is a linear collection whose elements are added on one end and removed from the other

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

CS61C : Machine Structures

Transcription:

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

a new element to (the Top of) the stack. an element from (the Top of) the stack (and return it).

the stack (of ints) starts push(1) push(2) push(3) pop() - returns 3 pop() - returns 2 pop() - returns 1

ast n, irst ut Describes the order that elements are added to and removed from a Stack.

- Automatic Memory allocation in a computer program. - Editing: allows Undo/Redo - Web Browsing: allows Back/Forward - Compilers: used for syntax checking

A linear-list Data structure where - addition of elements is restricted to the end (rear) of the list. - removal of elements is restricted to the beginning (front) of the list. first element in the queue last element in the queue

a new element to (the Rear of) the Queue. an element from (the Front of) the Queue (and return it).

the Queue (of ints) starts enq(1) enq(2) enq(3) deq() - returns 1 deq() - returns 2 deq() - returns 3

irst n, irst ut Describes the order that elements are added to and removed from a Queue.

- Web Server: processing service requests - Cell Tower: processing data packets from cell phones. - OS Scheduling: sharing of a single CPU by multiple "running" programs. - Fast Food Drive-Through order processing.

Stacks and Queues may be implemented in a program using any Linear Data structure, including: - Arrays - Vectors - Linked Lists

For a Linked List (dynamic) implementation, the node class would be: class node { friend class stack; // or queue private: node * next = NULL; string data = ""; }; The node may contain any number of data members needed. Here, there is just one, a string.

After allocating and populating a new node (pointed to by n), we make the new node the new top. Left: general case Right: empty stack case blue: before Pushing red: after Pushing

void stack::push(string newdata) { node * n = new node; // allocate n->data = newdata; // populate n->next = top; // connect new node // to old top. top = n; // new node is the } // new top.

Remove the top node and deallocate it. Return the data from the popped node.

string stack::pop() { string olddata = top->data; //save node * n = top; // pnt to old top top = top->next; // pnt top to next delete n; // dealloc old top return olddata; }

For a Linked List, the Q class would be: class queue { public: void enq(string newdata); string deq(); private: node * front = NULL; node * rear = NULL; };

There are 3 cases we should check for enqueue and dequeue: 1. empty queue 2. queue of one node 3. queue of two or more nodes The diagrams in the following slides show the new node already allocated and populated.

Empty When front and rear are NULL, the queue is empty. Just make both front and rear point to the new node.

1 node in queue When front and rear both point to the same node, there is only one node in the queue. No change to front, just attach the new node after the current rear and make it the new rear.

2 or more nodes When front and rear point to different nodes, there are 2 or more nodes in the queue. No change to front, just attach the new node after the current rear and make it the new rear. [same action as for queue of 1 node, so these two cases can be combined for enqueue]

void queue::enq(string newdata) { node * n = new node; // allocate n->data = newdata; // populate if (front == NULL) { // empty Q front = n; rear = n; } else { // Q of 1 or more rear->next = n; rear = n; }

empty queue When front and rear are NULL, the queue is empty. There is nothing to remove and return, so just return some data that means "none", such as Empty String

queue of one node Save a copy of the data in the front node. Deallocate the one node. Set both front and rear to NULL to make it empty. Return the data.

2 or more nodes Save a copy of the data in the front node. Make front point to the next node. Deallocate the old front. Return the data.

string queue::deq() { if (front==null) return ""; // empty Q case node * n = front; // ptr to old front string olddata = n->data; // save for return if (front==rear) { // Q of 1 node front = NULL; // make the queue rear = NULL; // empty. } else // 2 or more nodes front = front->next; // move front to next delete n; // dealloc old front return olddata; }

We want to write methods that are and The four methods written above do: - allocate, populate and insert [push and enq] OR - save return data, remove, deallocate, return data. [pop and deq]

In many cases, we need to Push/Enqueue an already allocated and populated node; and we may need to use a Popped/Dequeued node elsewhere instead of destroying it.

So, it would have been better to split these into at least two methods: - one that only manipulates pointers - another that allocates/deallocates and populates (or saves/returns data), that invokes the first method.

void stack::push(node * n) { // ptr to node already alloc/pop n->next = top; top = n; } void stack::push(string newdata) { node * n = new node; // allocate n->data = newdata; // populate push(n); // put on stack } Now programmers have a choice: Just give data, and have it allocated/populated/pushed; OR, I already have a node with data populated...just push it.

Term Stack Top Push Pop LIFO Queue Front Rear Enqueue Dequeue FIFO Definition A linear-list Data structure where addition of elements to and removal of elements from (the list) are restricted to the first element of the list. First element of a Stack. Stack Operation: add an element to the top. Stack Operation: remove an element from the top. Order of adding/removing items to/from a Stack. Last In, First Out A linear-list Data structure where addition of elements is restricted to the end of the list, and removal of elements is restricted to the beginning of the list. First element of a queue, the next to be removed. Last element of a queue, the last to be added. Queue Operation: add an element to the rear. Queue Operation: remove an element from the front. Order of adding/removing items to/from a Queue. First In, First Out