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

Similar documents
15. Stacks and Queues

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

The Stack and Queue Types

PA3 Design Specification

Lecture P5: Abstract Data Types

Abstract Data Types (ADTs) & Design. Selecting a data structure. Readings: CP:AMA 19.5, 17.7 (qsort)

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

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

Solution for Data Structure

Postfix (and prefix) notation

DEEPIKA KAMBOJ UNIT 2. What is Stack?

CS 206 Introduction to Computer Science II

STACKS AND QUEUES. Problem Solving with Computers-II

Abstract Data Types (ADTs) & Design

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

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

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

Stacks, Queues and Hierarchical Collections

CS 206 Introduction to Computer Science II

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

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

Lecture P5: Abstract Data Types

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

CS24 Week 4 Lecture 2

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

Stacks and Queues. CSE Data Structures April 12, 2002

Lecture 3: Stacks & Queues

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

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

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

ADTs: Stacks and Queues

Lecture Data Structure Stack

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

This document contains the questions and solutions to the CS107 midterm given in Winter 2018 by instructor Chris Gregg. This was a 120-minute exam.


Programming Abstractions

12 Abstract Data Types

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

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

.:: UNIT 4 ::. STACK AND QUEUE

Programming Abstractions

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

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

UNIT 6A Organizing Data: Lists Principles of Computing, Carnegie Mellon University

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

Programming Abstraction in C++

Programming. Lists, Stacks, Queues

Chapter 9 STACK, QUEUE

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

LIFO : Last In First Out

UNIT 6A Organizing Data: Lists. Last Two Weeks

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

CS W3134: Data Structures in Java

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

BBM 201 DATA STRUCTURES

March 13/2003 Jayakanth Srinivasan,

List, Stack and Queue Implementation

Lecture 4 Stack and Queue

Programming Abstractions

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

Arrays and Linked Lists

CS 106B Lecture 5: Stacks and Queues

September 10,

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Linear Data Structure

csci 210: Data Structures Stacks and Queues

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

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

CS107, Lecture 9 C Generics Function Pointers

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Final Exam. Fall Semester 2015 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Data Structures using OOP C++ Lecture 9

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

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

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

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

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

BBM 201 DATA STRUCTURES

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

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

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

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

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

Foundations of Data Structures

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

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)

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Stacks Fall 2018 Margaret Reid-Miller

CSC148 Week 2. Larry Zhang

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

An Introduction to Trees

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

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

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Dynamic Data Structures (II)

Transcription:

Stacks and Queues Stack - Abstract Data Type. Stacks and queues ubiquitous -structure in computing. Part of many important algorithms. Good example of abstract types. Good example to practice programming with arrays Good example to practice programming with linked lists a stack is a collection of items such that the last item to enter is the first one to exit last in, first out (LIFO) based on the idea of a stack of books, or plates essential Stack operations: push() // add new item to stack pop() // remove top item from stack additional Stack operations: top() // fetch top item (but don t remove it) size() // number of items is empty() Stack Applications Stack - Abstract Data Type - C Interface page-visited history in a Web browser undo sequence in a text editor checking for balanced brackets HTML tag matching postfix (RPN) calculator chain of function calls in a program typedef struct stack_internals *stack; stack stack_create(void); void stack_free(stack stack); void stack_push(stack stack, int item); int stack_pop(stack stack); int stack_is_empty(stack stack); int stack_top(stack stack); int stack_size(stack stack);

Stack - Abstract Data Type - using C Interface stack s; s = stack_create(); stack_push(s, 10); stack_push(s, 11); stack_push(s, 12); printf("%d\n", stack_size(s)); // prints 3 printf("%d\n", stack_top(s)); // prints 12 printf("%d\n", stack_pop(s)); // prints 12 printf("%d\n", stack_pop(s)); // prints 11 printf("%d\n", stack_pop(s)); // prints 10 Implementation of stack is opaque (hidden from user). User programs can not depend on how stack is implementated. Stack implementation can change without risk of breaking user programs. This type of information hiding is crucial to managing complexity in large software systems. Queue Abstract Data Type a queue is a collection of items such that the first item to enter is the first one to exit, i.e. first in, first out (FIFO) based on the idea of queueing at a bank, shop, etc. Essential Queue operations: enqueue() // add new item to queue dequeue() // remove front item from queue Additional Queue operations: front() // fetch front item (but don t remove it) size() // number of items is empty() Queue Applications Queue - Abstract Data Type - C Interface waiting lists, bureaucracy access to shared resources (printers, etc.) phone call centres multiple processes in a computer queue queue_create(void); void queue_free(queue queue); void queue_enqueue(queue queue, int item); int queue_dequeue(queue queue); int queue_is_empty(queue queue); int queue_front(queue queue); int queue_size(queue queue);

Queue - Abstract Data Type - C Interface Again mplementation of stack is opaque.. queue q; q = queue_create(); queue_enqueue(q, 10); queue_enqueue(q, 11); queue_enqueue(q, 12); printf("%d\n", queue_size(q)); // prints 3 printf("%d\n", queue_front(q)); // prints 10 printf("%d\n", queue_dequeue(q)); // prints 10 printf("%d\n", queue_dequeue(q)); // prints 11 printf("%d\n", queue_dequeue(q)); // prints 12 Queue implementation can change without risk of breaking user programs. Implementing A Stack with a Linked List Adding to the Tail of a List a stack can be implemented using a linked list, by adding and removing at the head [push() and pop()] for a queue, we need to either add or remove at the tail can either of these be done efficiently? push pop top of stack First element of list adding an item at the tail is achieved by making the last node of the list point to the new node we first need to scan along the list to find the last item

Adding to the Tail of a List Efficiency Issues struct node *add_to_tail( *new_node, struct node *he if (head == ) { // list is empty head = new_node; else { // list not empty struct node *node = head; while (node->!= ) { node = node->; // scan to end node-> = new_node; return head; Unfortunately, this implementation is very slow. Every time a new item is inserted, we need to traverse the entire list (which could be very large). We can do the job much more efficiently if we retain a direct link to the last item or tail of the list: if (tail == ) { // list is empty head = node; else { // list not empty tail-> = node; tail = node; Note: there is no way to efficiently remove items from the tail. (Why?) Queues Reverse Polish Notation a queue is a collection of items such that the first item to enter is the first one to exit, i.e. first in, first out (FIFO) based on the idea of queueing at a bank, shop, etc. First element of queue dequeue enqueue Some early calculators and programming languages used a convention known as Reverse Polish Notation (RPN) where the operator comes after the two operands rather than between them: 1 2 + result = 3 3 2 * result = 6 4 3 + 6 * result = 42 1 2 3 4 + * + result = 15

Postfix Calculator postfix.c #include <stdio.h> #include <ctype.h> #include "stack.h" A calculator using RPN is called a Postfix Calculator; it can be implemented using a stack: when a number is entered: push it onto the stack when an operator is entered: pop the top two items from the stack, apply the operator to them, and push the result back onto the stack. int main(void) { int ch; stack s = stack_create(); while ((ch = getc(stdin))!= EOF) { if (ch == \n ) { printf("result: %d\n", stack_pop(s)); else if (isdigit(ch)) { ungetc(ch, stdin); // put first digit back int num; scanf("%d", &num); // now scan entire number stack_push(s, num); postfix.c else if (ch == + ch == - ch == * ) { int a = stack_pop(s); int b = stack_pop(s); int result; if (ch == + ) { result = b + a; else if (ch == - ) { result = b - a; else { result = b * a; stack_push(s, result);