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



Similar documents
DATA STRUCTURES USING C

Linked Lists Linked Lists, Queues, and Stacks

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

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

Data Structures and Algorithms Stacks and Queues

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

Data Structure [Question Bank]

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

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.

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

recursion, O(n), linked lists 6/14

DATA STRUCTURE - STACK

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

Universidad Carlos III de Madrid

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

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

STACKS,QUEUES, AND LINKED LISTS

Analysis of a Search Algorithm

Common Data Structures

Data Structures and Algorithms

Module 2 Stacks and Queues: Abstract Data Types

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

10CS35: Data Structures Using C

Introduction to Data Structures and Algorithms

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

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

Data Structures. Level 6 C Module Descriptor

Lecture 12 Doubly Linked Lists (with Recursion)

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

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

Introduction to Data Structures

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

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

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

Cpt S 223. School of EECS, WSU

PES Institute of Technology-BSC QUESTION BANK

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

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

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

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

Data Structures and Data Manipulation

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

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

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

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

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

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!

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Sequential Data Structures

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

Abstract Data Types. Chapter 2

Algorithms and Abstract Data Types

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

Node-Based Structures Linked Lists: Implementation

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

Stacks. Linear data structures

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

Java Software Structures

Chapter 3: Restricted Structures Page 1

Stacks. Data Structures and Data Types. Collections

Data Structures Using C++

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

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

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

Data Structures in the Java API

Glossary of Object Oriented Terms

Lecture 2: Data Structures Steven Skiena. skiena

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

Linked Lists: Implementation Sequences in the C++ STL

Data Structures/Stacks and Queues

CmpSci 187: Programming with Data Structures Spring 2015

Algorithms and Data Structures

DATA STRUCTURE - QUEUE

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

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

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Algorithms and Data Structures

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

LINKED DATA STRUCTURES

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

Data Structures Fibonacci Heaps, Amortized Analysis

7.1 Our Current Model

Sample Questions Csci 1112 A. Bellaachia

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

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Cpt S 223. School of EECS, WSU

Sequences in the C++ STL

BCS2B02: OOP Concepts and Data Structures Using C++

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

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Converting a Number from Decimal to Binary

Symbol Tables. Introduction

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Ordered Lists and Binary Trees

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Chapter 8: Bags and Sets

Transcription:

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

Learning Goals After this unit, you should be able to... Differentiate an abstraction from an implementation. Define and give examples of problems that can be solved using the abstract data types stacks, queues and deques. Compare and contrast the implementations of these abstract data types using linked lists and circular arrays in C++. Demonstrate how dynamic memory management is handled in C++ (e.g., allocation, deallocation, memory heap, run-time stack). Gain experience with pointers in C++ and their tradeoffs and risks (dangling pointers, memory leaks). Explain the difference between the complexity of a problem (sorting) and the complexity of a particular algorithm for solving that problem. Manipulate data in stacks, queues, and deques (irrespective of any implementation).

Linked Lists: an alternative to arrays Can you think of any limitations with the array data structure?

Another note on abstract Data Types (Public) interface specifications Language independent

Linked Lists: an alternative to arrays A Linked List is an abstract data type for representing lists as collections of linked items Instead of having an overall representation of the list, the ordering of the list is represented locally That is, the information about what element comes next in a list is stored as a pointer within the element object. No list object (element) knows about any other elements in the list, just the ones to which it is adjacent

Linked Lists: an abstraction This is a pictorial representation of a singly linked list Note that it is not necessarily represented contiguously in memory, nor in the order the elements occur in the list itself

Linked Lists: implementation Let s take a look at how this is represented in C++

struct hockey_player { int jersey_number; string name; hockey_player* next; };... hockey_player * head = NULL;... void insert(int num, string name) { hockey_player * temp; temp = new hockey_player; temp->jersey_number = num; temp->name = name; } temp->next = head; head = temp; }

Comparison of Worst Case Complexities (for n entries): Assume we have a head pointer (only) for the linked list below. (Later: would your answers change if we also had a tail pointer?) Operation Array Array Linked List Linked List (unordered) (ordered) (unordered) (ordered) Insert at start O(1) O(n) O(1) O(1) Insert at end O(1) O(1) O(1) O(1) Insert after current position O(1) O(n) O(1) O(1) Find (search for) a value O(n) O(n)/O(lgn) O(n) O(n) Delete at current position O(1) O(n) O(1) O(1)

Comparison of Worst Case Complexities (for n entries): Assume we have a head pointer (only) for the linked list below. (Later: would your answers change if we also had a tail pointer?) Operation Array Array Linked List Linked List (unordered) (ordered) (unordered) (ordered) Insert at start O(1) O(n) O(1) O(1) Insert at end O(1) O(1) O(1) O(1) Insert after current position O(1) O(n) O(1) O(1) Find (search for) a value O(n) O(n)/O(lgn) O(n) O(n) Delete at current position O(1) O(n) O(1) O(1)

Comparison of Worst Case Complexities (for n entries): Assume we have a head pointer (only) for the linked list below. (Later: would your answers change if we also had a tail pointer?) Operation Array Array Linked List Linked List (unordered) (ordered) (unordered) (ordered) Insert at start O(1) O(n) O(1) O(1) Insert at end O(1) O(1) O(1) O(1) Insert after current position O(1) O(n) O(1) O(1) Find (search for) a value O(n) O(n)/O(lgn) O(n) O(n) Delete at current position O(1) O(n) O(1) O(1)

Comparison of Worst Case Complexities (for n entries): Assume we have a head pointer (only) for the linked list below. (Later: would your answers change if we also had a tail pointer?) Operation Array Array Linked List Linked List (unordered) (ordered) (unordered) (ordered) Insert at start O(1) O(n) O(1) O(1) Insert at end O(1) O(1) O(1) O(1) Insert after current position O(1) O(n) O(1) O(1) Find (search for) a value O(n) O(n)/O(lgn) O(n) O(n) Delete at current position O(1) O(n) O(1) O(1)

Comparison of Worst Case Complexities (for n entries): Assume we have a head pointer (only) for the linked list below. (Later: would your answers change if we also had a tail pointer?) Operation Array Array Linked List Linked List (unordered) (ordered) (unordered) (ordered) Insert at start O(1) O(n) O(1) O(1) Insert at end O(1) O(1) O(1) O(1) Insert after current position O(1) O(n) O(1) O(1) Find (search for) a value O(n) O(n)/O(lgn) O(n) O(n) Delete at current position O(1) O(n) O(1) O(1)

Stacks A stack operates on the LIFO principle: Last In, First Out. Push and pop operations have real-life analogies: a stack of trays or plates in a cafeteria, Pez dispenser, etc. Software examples: supporting a Web browser s Back button, passing variables to functions, keeping track of function calls (e.g., recursion), storing local variables, etc. Some stack operations and their complexities: - push(item) (add to top) - pop() (take off top) - returntop() (without removing) - empty() (is stack empty?) - getsize() (how many in stack?)

A stack can be implemented using either an array or a linked list:

Queues A queue operates on the FIFO principle: First In, First Out Real-life analogies: line-up at a bank or supermarket Software examples: Web browser requests to a server Some queue operations and their complexities: - enqueue(item) - dequeue() - getfirst(item) (without removing) - empty() (is it empty?) - getsize() (how many?)

A queue can be implemented using either a circular array or a linked list:

Doubly-Linked Lists In what ways does a doubly-linked list differ from a singly-linked list, besides the presence of an extra pointer? What are the advantages and disadvantages?

Deques A deque (pronounced deck ) is a double-ended queue. It acts like a queue, but we can insert at either end, and remove from either end. Like a queue, a deque can be implemented using either an array or a linked list. It is really an abstraction of the queue and stack.

Learning Goals After this unit, you should be able to... Differentiate an abstraction from an implementation. Define and give examples of problems that can be solved using the abstract data types stacks, queues and deques. Compare and contrast the implementations of these abstract data types using linked lists and circular arrays in C++. Demonstrate how dynamic memory management is handled in C++ (e.g., allocation, deallocation, memory heap, run-time stack). Gain experience with pointers in C++ and their tradeoffs and risks (dangling pointers, memory leaks). Explain the difference between the complexity of a problem (sorting) and the complexity of a particular algorithm for solving that problem. Manipulate data in stacks, queues, and deques (irrespective of any implementation).