Lecture 6b Linked List Variations. Similar but not the same

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

Unordered Linked Lists

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

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

Analysis of a Search Algorithm

Node-Based Structures Linked Lists: Implementation

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

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

Lecture 12 Doubly Linked Lists (with Recursion)

ADTs,, Arrays, Linked Lists

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

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

Linked Lists Linked Lists, Queues, and Stacks

LINKED DATA STRUCTURES

DATA STRUCTURES USING C

Data Structures and Algorithms Lists

10CS35: Data Structures Using C

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

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

Sequential Data Structures

PES Institute of Technology-BSC QUESTION BANK

Common Data Structures

Project 4 DB A Simple database program

Sequences in the C++ STL

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

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

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Linked Lists: Implementation Sequences in the C++ STL

Universidad Carlos III de Madrid

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

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Singly linked lists (continued...)

Introduction to Data Structures and Algorithms

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

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Data Structures and Algorithms

Review of Hashing: Integer Keys

Ordered Lists and Binary Trees

Queue Implementations

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.

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Data Structure [Question Bank]

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Linked List as an ADT (cont d.)

STACKS,QUEUES, AND LINKED LISTS

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

Abstract Data Types. Chapter 2

Data Structures Fibonacci Heaps, Amortized Analysis

Introduction to Object-Oriented Programming

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Cpt S 223. School of EECS, WSU

Data Structures and Algorithms

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

CSE 211: Data Structures Lecture Notes VII

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING LESSON PLAN

Binary Heap Algorithms

Chapter 8: Bags and Sets

Lecture Notes on Binary Search Trees

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

Converting a Number from Decimal to Binary

Lecture 11 Array of Linked Lists

Sample Questions Csci 1112 A. Bellaachia

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

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

Basic Data Structures and Algorithms

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

Persistent Binary Search Trees

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

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

D06 PROGRAMMING with JAVA

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

CmpSci 187: Programming with Data Structures Spring 2015

Data Structures and Data Manipulation

Lecture Notes on Binary Search Trees

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

Stacks. Linear data structures

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Data Structures. Level 6 C Module Descriptor

Data storage Tree indexes

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

ECDL. European Computer Driving Licence. Spreadsheet Software BCS ITQ Level 2. Syllabus Version 5.0

From Last Time: Remove (Delete) Operation

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 13-1

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

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

Linked List Problems

Data Structures Using C++

Chapter 13 Disk Storage, Basic File Structures, and Hashing.

Algorithms and Abstract Data Types

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

BCS2B02: OOP Concepts and Data Structures Using C++

Physical Data Organization

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

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

Introduction to data structures

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Transcription:

Lecture 6b Linked List Variations Similar but not the same

Linked List Variations: Overview The linked list implementation used in List ADT is known as Singly (Single) Linked List Each node has one pointer (a single link), pointing to the next node in sequence Using pointers allow the node to be noncontiguous: flexible organizations in chaining up the nodes Several high level ADTs utilize variations of linked list as internal data structure Let's look at a few common choices 2

Common Variations: At a glance Using list node with one pointer: 1. Tailed Linked List 2. Circular Linked List 3. Linked List with a dummy head node Using list node with two pointers: 1. Doubly linked list 2. Circular doubly linked list Other variations are possible: Once you understand the fundamental, it is quite easy to extend to other organizations! 3

Tailed Linked List Head and tail: First and last

Tailed Linked List Motivation: The last node in singly linked list takes the longest time to reach If we keep adding item to the end of list (some applications require this) very inefficient Simple addition: Keep an additional pointer to point to the last node head tail a 1 a 2 a 3 a 4 5

Circular Linked List Go round and round

Circular Linked List Motivation: Sometimes we need to repeatedly go through the list from 1 st node to last node, then restart from 1 st node,. Simple addition: Just link the last node back to the first node No NULL pointer in the linked list head a 1 a 2 a 3 a 4 7

Circular Linked List: Even Better Circular Linked List can be made even better: Keep the tail pointer instead of head pointer We now know both the first node and the last node with a single pointer Simple addition: Keep track of the tail pointer How do we get to the first node with just the tail pointer? tail a 1 a 2 a 3 a 4 8

Circular Linked List: Common Code Given a circular linked list: Idea: How do we know we have passed through every nodes in the list? If we land on a node again (e.g. the first node), then we have finished one round curptr = head; do { // visit the node curptr points to curptr = curptr->next; } while (curptr!= head); Simple solution as long as the list is not empty 9

Dummy Head Node There is a dummy in front!!

Linked List with Dummy Head Node Motivation: Insert/Remove the first node in linked list is a special case: Because we need to update the head pointer Idea: Maintain an extra node at the beginning of the list Not used to store real element Only to simplify the coding head a 1 a 2 a 3 a 4 11

Doubly Linked List Two is better than one

Doubly Linked List: Motivation Singly Linked List only facilitates movement in one direction Can get to the next node in sequence easily Cannot go to the previous node Doubly Linked List facilitates movement in both directions Can get to the next node in sequence easily Can get to the previous node in sequence easily 13

A single node in the Doubly Linked List struct DListNode { int item; DListNode *prev; DListNode *next; }; item prev next Store a single integer in this example DListNode A pointer to the previous node in list A pointer to the next node in list, similar to singly linked list 14

An example of Doubly Linked List List of four items < a 1, a 2, a 3, a 4 > head next pointer prev pointer a 1 a 2 a 3 a 4 We need: A single DListNode head pointer to indicate the first node NULL in the prev pointer field of first node NULL in the next pointer field of last node 15

Doubly Linked List: Operations Insertion and removal in doubly linked list has the similar steps as in singly linked list: Locate the point of interest through list traversal Modify the pointers in affected nodes However, insertion and removal affects more nodes in doubly linked list: Both the nodes before and after the point of operation are affected We only show the general case for insertion and removal in the next section Try to figure out the code for other special cases 16

Doubly Linked List: General Insertion Assume we have the following: newptr pointer: Pointing to the new node to be inserted cur pointers: Use list traversal to locate this node The new node is to be inserted before this node head cur a 1 a i a i+1 newptr Insert here a new 17

Doubly Linked List: General Insertion Step 1: newptr->next = cur; newptr->prev = cur->prev; cur a i a i+1 newptr a new Step 2: cur->prev->next = newptr; cur->prev = newptr; cur a i a i+1 newptr a new 18

Doubly Linked List: General Deletion Assume we have the following: cur pointer: Points to the node to be deleted head cur Delete this node a 0 a i a i+1 a i+2 19

Deletion: Using Doubly Linked List cur Step 1: a i a i+1 cur->prev->next = cur->next; cur->next->prev = cur->prev; a i+2 cur Step 2: a i a i+1 delete cur; cur = NULL; a i+2 20

Linked List Variation: More? By using the ideas discussed, we can easily construct: Tailed Double Linked List Doubly Linked List with dummy head node Circular Doubly Linked List etc Rather than memorizing the variations: Make sure you understand the basic of pointer manipulation Make sure you can reason about the pros and cons of each type of organization 21

http://visualgo.net/list VisuAlgo version: With Tail Pointer, Not Circular, Without Dummy Head Operations Supported (integer list only): Create List: Random, R Sorted, R Fixed Size, User Defined List Insert: At Head, At Tail, At Index K Remove: At Head, At Tail, At Index K Search Please explore: http://visualgo.net/list, Single Linked List http://visualgo.net/list?mode=dll, Doubly Linked List 22

C++ STL list Do we have to code ListLL.cpp (and all these variations and special cases) every time we need to use a Linked List? Fortunately, no We can use C++ STL list http://en.cppreference.com/w/cpp/container/list 23

Summary Singly Linked List with Dummy Head Node Tailed Singly Linked List Circular Singly Linked List Doubly Linked List Exposure to http://visualgo.net/list Exposure to C++ STL list http://en.cppreference.com/w/cpp/container/list 24