Linked Lists, Stacks, and Queues

Similar documents
Dynamic Arrays and Amortized Analysis

Hashing. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

3. Fundamental Data Structures

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Linked Structures. See Section 3.2 of the text.

Recursion: The Beginning

Stack and Queue. Stack:

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

Dynamic Arrays and Amortized Analysis

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

Lecture Notes for Advanced Algorithms

k-selection Yufei Tao Department of Computer Science and Engineering Chinese University of Hong Kong

Association Rule Mining: FP-Growth

Recursion: The Beginning

Week 6. Data structures

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Data Structure. Recitation VII

Dynamic Data Structures

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

Finding Strongly Connected Components

Algorithms and Data Structures

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

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

III Data Structures. Dynamic sets

CSE373: Data Structures & Algorithms Lecture 12: Amortized Analysis and Memory Locality. Linda Shapiro Winter 2015

CS 211. Project 5 Infix Expression Evaluation

CSE373: Data Structures & Algorithms Lecture 12: Amortized Analysis and Memory Locality. Lauren Milne Spring 2015

Basic Data Structures (Version 7) Name:

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

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

Lecture Notes: External Interval Tree. 1 External Interval Tree The Static Version

Algorithm Design and Analysis

csci 210: Data Structures Stacks and Queues

Introduction to Programming, Aug-Dec 2006

Programming Assignment 3

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

Introduction to Programming in C Department of Computer Science and Engineering

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Algorithm Design and Analysis Homework #4

Priority Queues and Binary Heaps

Sequence Abstract Data Type

malloc(), calloc(), realloc(), and free()

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

LIFO : Last In First Out

CSE 230 Intermediate Programming in C and C++

CS 206 Introduction to Computer Science II

Binary Search and Worst-Case Analysis

CSCE 2014 Final Exam Spring Version A

CS 230 Programming Languages

The RAM Computation Model

Lab 7 1 Due Thu., 6 Apr. 2017

Algorithms, Data Structures, and Problem Solving

8. Fundamental Data Structures

SFU CMPT Lecture: Week 8

Linked Lists and Abstract Data Structures A brief comparison

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

infix expressions (review)

DATA STRUCTURES AND ALGORITHMS

SOLUTIONS (INCOMPLETE, UNCHECKED)

DEEPIKA KAMBOJ UNIT 2. What is Stack?

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

and 6.855J February 6, Data Structures

ITI Introduction to Computing II

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

Object-Oriented Principles and Practice / C++

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

Information Science 2

// The next 4 functions return true on success, false on failure

Notes on the Exam. Question 1. Today. Comp 104:Operating Systems Concepts 11/05/2015. Revision Lectures (separate questions and answers)

Randomized Algorithms, Hash Functions

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

! 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)

Binary Heaps in Dynamic Arrays

Comp 204: Computer Systems and Their Implementation. Lecture 25a: Revision Lectures (separate questions and answers)

Lecture 2: Algorithms, Complexity and Data Structures. (Reading: AM&O Chapter 3)

CS W3134: Data Structures in Java

811312A Data Structures and Algorithms, , Exercise 1 Solutions

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

CS 241 Analysis of Algorithms

DECLARAING AND INITIALIZING POINTERS

DDS Dynamic Search Trees

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Object Oriented Programming COP3330 / CGS5409

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

DATA STRUCTURES AND ALGORITHMS

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked

CS350 - Exam 1 (100 Points)

DATA STRUCTURES AND ALGORITHMS

Algorithms and Data Structures

Lesson 13 - Vectors Dynamic Data Storage

MODULE 3: LINKED LIST

Introduction hashing: a technique used for storing and retrieving information as quickly as possible.

CSC148H Week 3. Sadia Sharmin. May 24, /20

Data structure and algorithm in Python

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

Transcription:

Department of Computer Science and Engineering Chinese University of Hong Kong

In a nutshell, a data structure describes how data are stored in memory, in order to facilitate certain operations. In all the problems we have studied so far, the input set of elements is always stored in an array this is the only data structure (albeit a simple one) we have so far. In this lecture, we will learn another simple data structure the linked list for managing a set. Then, we will utilize a linked list to implement two other slightly more sophisticated structures: the stack and the queue.

As mentioned earlier, so far we have been using an array to store a set of elements. Recall that an array is a sequence of consecutive memory cells. This requires that its length l be specified at the time it is created, which creates two issues: It is not generally possible to increase the length as we wish, because the memory cells immediately after the array may have been allocated for other purposes. Sometimes we may want to reduce the space consumption by shrinking an array, if the set has some elements removed. It is not easy to achieve the purpose easily.

Linked List Next, we introduce the linked list, which is a sequence of nodes such that: The information of each node is stored in consecutive memory cells. The node s address is the address of the first cell. Every node stores pointers to its succeeding and preceding nodes (if they exist), where a pointer to a node u is simply the address of u. We will refer to the pointer to the preceding node as the back-pointer, and the one to the succeeding node as the next-pointer. The first node of a linked list is called the head, and the last node is called the tail.

Linked List The figure below shows the memory contents of a linked list of three nodes u 1, u 2, u 3, whose addresses are a, b, and c, respectively. a address c b u 3 u 1 b b u 2 a c The back-pointer of node u 1 (the head) is nil, denoted by. The next-pointer of u 3 (the tail) is also nil. Each node can be stored anywhere in memory. Furthermore, the addresses of the nodes do not need to follow the ordering of the nodes in the linked list.

Example Below is a possible linked for storing a set of 5 integers {14, 65, 78, 33, 82}. b 78 a d e c a c 65 b 82 c e 14 d 33 b d The linked list stores the sequence (65, 78, 33, 82, 14), as shown in the logical view below: 65 78 33 82 14

Two (Simple) Facts about a Linked List Suppose that we use a linked list to store a set S of n integers (one node per integer). Fact 1: The linked list uses O(n) space, namely, it occupies O(n) memory cells. Fact 2: Starting from the head node, we can enumerate all the integers in S in O(n) time.

A major advantage of using a linked list to manage a set S is that it supports updates to the set including: Insertion: Add a new element to S. Deletion: Remove an existing element from S.

Insertion into a Linked List To insert a new element e into S, we simply append e to the linked list: 1 Identify the tail node u. 2 Create a new node u new. Store e in u new. 3 Set the next-pointer of u to the address of u new. 4 Set the back-pointer of u new to the address of u. After the insertion, u new becomes the new tail of the linked list. Time of Insertion If we already know the address of the tail node, an insertion takes O(1) time.

Example Before the insertion: b 78 a d e c a c 65 b 82 c e 14 d 33 b d 65 78 33 82 14 After inserting 57: f 57 e b 78 a d e c a c 65 b 82 c e 14 d f 33 b d 65 78 33 82 14 57 Remember: the address of the new node can be anywhere in the memory.

Deletion from a Linked List Given a pointer to (i.e., the address of) a node u in the linked list, we can delete it as follows: 1 Identify the preceding node u prec of u. 2 Identify the succeeding node u succ of u. 3 Set the next-pointer of u prec to the address of u succ. 4 Set the back-pointer of u succ to the address of u prec. 5 Free up the memory of u. Time of Deletion Obviously O(1).

Example Before the deletion: f 57 e b 78 a d e c a c 65 b 82 c e 14 d f 33 b d 65 78 33 82 14 57 After deleting 78: f 57 e a d e c 65 c 82 c e 14 d f 33 a d 65 33 82 14 57 Remember: the address of the new node can be anywhere in the memory.

Performance Guarantees of the Linked List When used to manage a set of n integers: Space O(n) Insertion O(1) time: if the address of the tail is known. Deletion O(1) time: if the address of the node to be removed is known. O(n) time to enumerate all the elements in the set: if the address of the head/tail is known. Remark: We have explained how to perform an insertion by appending it to the end. Since the node ordering in a linked list can be arbitrary, there are many other ways of performing an insertion in O(1) time. For example, you can do so by inserting the new element at the beginning of the linked list (think: how?).

Next, we will deploy the linked list to implement two data structures: stack and queue.

Stack The stack on a set S of n elements supports two operations: Push(e): Inserts a new element e into S. Pop: Removes the most recently inserted element from S, and returns it. In other words, a stack obeys the rule of First-In-Last-Out (FILO).

Example At the beginning, the stack is empty. Consider the following sequence of operations: Push(35): S = {35}. Push(23): S = {35, 23}. Push(79): S = {35, 23, 79}. Pop: Returns 79, and removes it from S. Now S = {35, 23}. Pop: Returns 23, and removes it from S. Now S = {35}. Push(47): S = {35, 47}. Pop: Returns 47, and removes it from S. Now S = {35}.

Implementing a Stack with a Linked List At all times, we store the elements of the underlying set S in a linked list L. Push(e): Insert e at the end of L. Pop: Delete the tail node of L, and return the element stored in the tail. At all times, we keep track of the address of the tail node. Guarantees: (n) space consumption, where n is the size of S. Push in O(1) time. Pop in O(1) time.

Queue The queue on a set S of n elements supports two operations: En-queue(e): Inserts a new element e into S. De-queue: Removes the least recently inserted element from S, and returns it. Obeys First-In-First-Out (FIFO).

Example At the beginning, the queue is empty. Consider the following sequence of operations: En-queue(35): S = {35}. En-queue(23): S = {35, 23}. En-queue(79): S = {35, 23, 79}. De-queue: Returns 35, and removes it from S. Now S = {23, 79}. De-queue: Returns 23, and removes it from S. Now S = {79}. En-queue(47): S = {79, 47}. De-queue: Returns 79, and removes it from S. Now S = {47}.

Implementing a Queue with a Linked List At all times, we store the elements of the underlying set S in a linked list L. En-queue(e): Insert e at the end of L. De-queue: Delete the head node of L, and return the element stored in the head. At all times, we keep track of the addresses of the head and tail nodes. Guarantees: O(n) space consumption, where n is the size of S. En-queue in O(1) time. De-queue in O(1) time.

At this moment, you should have developed a stronger sense of what is a data structure, and of what its theoretical guarantees may look like. In general, a data structure stores a set of elements so that some operations can be performed efficiently. In terms of guarantees, we typically care about: Its space consumption: how many memory cells does it occupy in the worst case. The worst-case running time of each of its operations.