Stacks, Queues and Linked Lists

Similar documents
Introduction to Data Structures and Algorithms

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Common Data Structures

Data Structures and Algorithms Stacks and Queues

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Chapter 3: Restricted Structures Page 1

DATA STRUCTURE - QUEUE

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Sequential Data Structures

Linked Lists Linked Lists, Queues, and Stacks

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Module 2 Stacks and Queues: Abstract Data Types

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

10CS35: Data Structures Using C

Universidad Carlos III de Madrid

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Data Structures Using C++ 2E. Chapter 5 Linked Lists

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

STACKS,QUEUES, AND LINKED LISTS

Analysis of a Search Algorithm

DATA STRUCTURES USING C

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Algorithms and Data Structures

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Computer Programming using C

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Data Structures and Data Manipulation

Data Structures and Algorithms

Basic Data Structures and Algorithms

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

PES Institute of Technology-BSC QUESTION BANK

Stacks. Linear data structures

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Data Structures UNIT III. Model Question Answer

St S a t ck a ck nd Qu Q eue 1

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Data Structure [Question Bank]

Lecture 12 Doubly Linked Lists (with Recursion)

Cpt S 223. School of EECS, WSU

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Analysis of Algorithms I: Binary Search Trees

Application of Stacks: Postfix Expressions Calculator (cont d.)

System Software Prof. Dr. H. Mössenböck

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Node-Based Structures Linked Lists: Implementation

Big O and Limits Abstract Data Types Data Structure Grand Tour.

3 Abstract Data Types and Search

Data Structures. Level 6 C Module Descriptor

APP INVENTOR. Test Review

Glossary of Object Oriented Terms

Algorithms and Abstract Data Types

DATA STRUCTURE - STACK

Binary Search Trees CMPSC 122

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

No Solution Equations Let s look at the following equation: 2 +3=2 +7

Chapter 8: Bags and Sets

ADTs,, Arrays, Linked Lists

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Sample Questions Csci 1112 A. Bellaachia

Area and Perimeter: The Mysterious Connection TEACHER EDITION

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Algorithms Chapter 12 Binary Search Trees

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Java Software Structures

Data Structures Fibonacci Heaps, Amortized Analysis

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Lecture Notes on Stacks & Queues

Linked Lists: Implementation Sequences in the C++ STL

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Stack Allocation. Run-Time Data Structures. Static Structures

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

BCS2B02: OOP Concepts and Data Structures Using C++

Algorithms and Data Structures

Introduction to Data Structures

Exam study sheet for CS2711. List of topics

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

CmpSci 187: Programming with Data Structures Spring 2015

CSE 326: Data Structures B-Trees and B+ Trees

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Sequences in the C++ STL

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Curriculum Map. Discipline: Computer Science Course: C++

Transcription:

Stacks, Queues and Linked Lists Adnan Aziz 1 Dynamic sets CLRS Part III, page 197 In mathematics, a set is a well-defined collection of elements (elements could be numbers, functions, geometric shapes); could be infinite. Algorithms operate on sets. Two special aspects of these sets is that they are finite and dynamic. Often only operations are insert, delete, test membership. Can get more complicated: extract-min Typical implementation: elements are objects given pointer, fields can be examined and manipulated A common scenario is that one field is a key. E.g., object may contain id, name, birthday, address; any of these could be the key. If the keys are all distinct, can view dynamic set as simply a set of keys. Sometimes objects are drawn from a totally ordered set (e.g., the real numbers). There are two prototypical operations: queries return information about the set, and update modify the set Examples: search(s,k) insert(s,k) 1

delete(s,k) minimum(s) maximum(s) successor(s,k) predecessor(s,k) Note that these operations can use these to enumerate the elements Runtimes are measures in terms of size of the set, i.e., the number of elements. 2 Stacks and Queues CLRS 10.1 Dynamic sets in which elements removed by delete is pre-specified stack always delete most recently inserted element LIFO queue always delete element longest in set FIFO 2.1 Stacks insert usually called push delete usually called pop Can implement stack of at most n elements using an array S[1..n] (See Figure 10.1, CLRS) keep attribute top[s] which indexes the most recently inserted element (top[s] = 0 stack is empty) underflow try popping empty stack overflow try pushing fill stack) Pseudo-code: (ignore overflow; lv. for HW) 2

STACK-EMPTY(S) if top[s] = 0 then return TRUE else return FALSE PUSH(S,x) top[s] <- top[s] + 1 S[top[S]] <- x POP(S) if STACK-EMPTY(S) then error "underflow" else top[s] = top[s] - 1 return S[top[S] + 1] All the operations have O(1) time complexity. 2.2 Queues FIFO head: element which has been in for longest, tail: location at which to insert insert usually called enqueue delete usually called dequeue Can implement queue of at most n 1 elements using an array Q[1..n] (See Figure 10.2, CLRS) 3

keep attribute head[q] which indexes the head, and attribute tail[q] which is the location at which to add the next element head[q] = tail[q] Q is empty head[q] = tail[q]+1 Q is full Implementations of enqueue, dequeue without error checking: ENQUEUE(Q,x) Q[tail[Q]] <- x if tail[q] = length[q] then tail[q] = 1 else tail[q] <- tail[q] + 1 DEQUEUE(Q) x <- Q[head[Q]] if HEAD[Q] = length[q] then head[q] <- 1 else head[q] <- head[q] + 1 return x Runtimes? All O(1) What is the big shortcoming with the array based implementation? 3 Linked Lists CLRS 10.2 4

Conceptually: objects arranged in linear order. Differs from arrays in that in arrays index + 1 gives next element; in linked list, we use a pointer. Will see: can implement all operations on a linked list. Doubly linked list L: each element is an object with a key field, a next field, and a prev field. (Of course, there maybe other satellite data.) It s important that you keep track of the difference between element and key! Given an element x: next(x) pointer to successor (NIL no successor; such an element is called the tail ) prev(x) pointer to predecessor (NIL no predecessor; such an element is called the head ) Variations: singly linked sorted circular list prev of head is tail; next of tail is head (makes some functions easier to write) We will stick to unsorted, doubly linked lists. Example see Figure 10.3, CLRS. 3.1 Searching in Linked Lists Given list L, and a key k, return a pointer to the first object with key k (not present return NIL) LIST-SEARCH(L,k) x <- head[l] while x!= NIL and key[x]!= k do x <- next[x] return x Runtime complexity is Θ(n) 5

3.2 Inserting into a Linked List Given list L, element x (whose key field is already set), insert x into list. intuition splice onto the front LIST-INSERT(L,x) next[x] <- head[l] if head[l]!= NIL then prev[head[l]] <- x head[l] <- x prev[x] <- NIL Runtime? Θ(1) 3.3 Deleting from a Linked List Remove an element x from list L assume given pointer to x we ll splice out x how to generalize to deleting element given only key? use LIST-SEARCH function LIST-DELETE(L,x) if prev[x]!= NIL then next[pred[x]] <- next[x] else head[l] <- next[x] if next[x]!= NIL then pred[next[x]] <- prev[x] 6

3.4 Sentinels Observe: code for delete is complicated by tests for boundary conditions. Can get around this by use of sentinels. Not that helpful clearer code small speedup more memory Section 10.3, CLRS discusses how one can implement linked lists in a language which does not support pointers/heaps/memory management. We don t need to worry about this in C++ but you may enjoy reading this section to get an idea of how new, malloc, delete, free work. 7