More on Linked List Yumei Huo Department of Computer Science

Slides:



Advertisements
Similar presentations
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Advertisements

CS 367 – Introduction to Data Structures
Linear Lists – Linked List Representation
Data Structures ADT List
Data Structure HKOI training /4/2010 So Pak Yeung.
Data Structure Lecture-5
CSC 172 DATA STRUCTURES. SKIP LISTS Read Weiss
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
CS 206 Introduction to Computer Science II 02 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann.
Lists: array implementation list_size = 5 lst Obj 1Obj 2Obj 3Obj 4Obj 5.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
CS 206 Introduction to Computer Science II 02 / 09 / 2009 Instructor: Michael Eckmann.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
Chapter 17 Linked List.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Linked Lists Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 17 © 2002 Addison Wesley.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
Linear Data Structures
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
3/19/2016 5:40 PMLinked list1 Array-based List v.s. Linked List Yumei Huo Department of Computer Science College of Staten Island, CUNY Spring 2009.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
IT Department – Bunda Mulia University
Lecture 6 of Computer Science II
Unit – I Lists.
Elementary data structures
Data Structure By Amee Trivedi.
Program based on queue & their operations for an application
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Linked List Stacks, Linked List Queues, Dequeues
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Doubly Linked List Review - We are writing this code
COP3530- Data Structures Advanced Lists
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Linked List Variations
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Algorithm for deleting a node from a singly linked list
EEL 4854 IT Data Structures Linked Lists
Lists.
Linked List Yumei Huo Department of Computer Science
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Sparse Tables A sparse table refers to a table that is populated sparsely by data and most of its cells are empty With a sparse table, the table can be.
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Doubly Linked Lists or Two-way Linked Lists
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
CS212D: Data Structures Week 5-6 Linked List.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Problem Understanding
LINKED LISTS.
Lecture 16 Section 6.2 Thu, Mar 1, 2007
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Linked Lists & Iterators
CS148 Introduction to Programming II
Array operations Dr. T. Kokilavani Assistant Professor
Chapter 9 Linked Lists.
CMPT 225 Lecture 5 – linked list.
Data Structures & Programming
Problem Understanding
Presentation transcript:

More on Linked List Yumei Huo Department of Computer Science Sequences 2019/7/24 More on Linked List Yumei Huo Department of Computer Science College of Staten Island, CUNY Spring 2009 2019/7/24 Linked list

Outline Variants of Singly linked list Doubly linked list 2019/7/24

Outline Variants of Singly linked list Doubly linked list 2019/7/24

Variants of Singly linked list Singly linked circular list Singly linked list with head node Singly linked circular list with head node 2019/7/24 Linked list

Singly linked circular list Link the last node back to the first A B C D first E 2019/7/24 Linked list

Print all the elements in a singly linked circular list B C D first E Node * tmp=first; if (tmp != 0) { cout << tmp->data; while (tmp->next !=first) tmp=tmp->next; } 2019/7/24 Linked list

Singly linked list with head node Add an additional node, called head node, at the front first  Head node Empty list A B C first  Head node Singly linked list with head node 2019/7/24 Linked list

Singly linked circular list with head node first Head node Empty list A B C first Head node Circular list with head node 2019/7/24 Linked list

Outline Variants of Singly linked list Doubly linked list 2019/7/24

Doubly Linked List Nodes implement Position and store: data link to the previous node link to the next node prev next data node Tail Head A B C D 2019/7/24 Linked list

Operations Comparison Singly Linked List Doubly Linked List Determine the length O(n) Find the kth element O(k) Search for a given element Delete the kth element Insert a new element just after the kth Delete a node pointed by p O(1) Insert a new element before the node pointed by p 2019/7/24 Linked list

Delete a node pointed by p q Head Tail A B C D q Tail Head A B C D q Tail Head A B D Doubly Linked List p q Head A B C D q Head A B C D q Head A B D Singly Linked List 2019/7/24 Linked list

Delete a node pointed by p in a doubly linked list q Head Tail A B C D q Tail Head A B C D q Tail Head A B D Doubly Linked List p->prev->next=p->next; p->next->prev=p->prev; delete p; 2019/7/24 Linked list

Delete a node pointed by p in singly linked list q Head A B C D q Head A B C D q Head A B D Singly Linked List //search p’s previous node q, assume p exists and is not the 1st node //(what if p is the 1st node?): Node * q=head; while (q->next != p) q=q->next; //let q point to p’s next node q->next=p->next; delete p; 2019/7/24 Linked list

Insert a new element ‘X’ before the node pointed by p Head A B C Tail t p Head A B C Tail X q t q p Head A B X C Tail Doubly Linked List t p Head A B C p t Head A B C q X t q p Head A B X C Singly Linked List 2019/7/24 Linked list

Sequences 2019/7/24 Insert a new element ‘X’ before the node pointed by p in a doubly linked list t p Head A B C Tail t p Head A B C Tail X q t q p Head A B X C Tail Doubly Linked List //create a new node containing ‘X’: Node *q = new Node; q->data=‘X’; //insert q before p: q->prev=p->prev; q->next=p; p->prev->next=q; p->prev=q; 2019/7/24 Linked list

Insert a new element ‘X’ before the node pointed by p in a singly linked list Head A B C p t Head A B C q X t q p Head A B X C Singly Linked List //create a new node containing ‘X’: Node * q= new Node; Q->data = ‘X’; //search p’s previous node t, assume p exists and is not the 1st node //(what if p is the 1st node?): Node * t= head; while (q->next != p) q=q->next; //insert q between t and p q->next=p; t->next=q; 2019/7/24 Linked list

Thank you! 2019/7/24 Linked list