LINKED LISTS CSCD 216 - Linked Lists.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Lists CS 3358.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Linked Lists.
Linked Lists Definition of Linked Lists Examples of Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Linked lists CSCI 2170.
CHP-5 LinkedList.
M180: Data Structures & Algorithms in Java
Data Structures: A Pseudocode Approach with C
Data Structures & Algorithms
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Unit – I Lists.
Data Structure By Amee Trivedi.
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Lectures linked lists Chapter 6 of textbook
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,
Review Deleting an Element from a Linked List Deletion involves:
CE 221 Data Structures and Algorithms
Lists CS 3358.
Lecture - 6 On Data Structures
Data Structure Dr. Mohamed Khafagy.
Linked Lists.
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCE 210 Data Structures and Algorithms
Data Structures Linked list.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists.
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Doubly Linked Lists or Two-way Linked Lists
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
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.
CS2013 Lecture 4 John Hurley Cal State LA.
Lists CSE 373 Data Structures.
Linked Lists in C and C++
Linked Lists Chapter 4.
Problem Understanding
Chapter 17: Linked Lists.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
LAB#4 Linked List.
Lists CSE 373 Data Structures.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
BY PROF. IRSHAD AHMAD LONE.
Problem Understanding
Presentation transcript:

LINKED LISTS CSCD 216 - Linked Lists

Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked Lists Implementations Singly-linked lists Doubly-linked lists CS 103

Introduction When dealing with many problems we need a dynamic list, dynamic in the sense that the size requirement need not be known at compile time. Thus, the list may grow or shrink during runtime. A linked list is a data structure that is used to model such a dynamic list of data items, so the study of the linked lists as one of the data structures is important. CSCD 216 - Linked Lists

Linear Data Structure with no restriction Linked list consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes CSCD 216 - Linked Lists

Linear Data Structure with no restriction CSCD 216 - Linked Lists

Singly Linked Lists A singly linked list is a data structure consisting of a sequence of nodes nodes are not adjacent in memory Each node stores element link to the next node next element node  A B C D CSCD 216 - Linked Lists

Definition Details Each item has a data part (one or more data members), and a link that points to the next item One natural way to implement the link is as a pointer; that is, the link is the address of the next item in the list It makes good sense to view each item as an object, that is, as an instance of a class. We call that class: Node The last item does not point to anything. We set its link member to NULL. CS 103

Examples of Linked Lists (A Waiting Line) A waiting line of customers: John, Mary, Dan, Sue (from the head to the tail of the line) A linked list of strings can represent this line: John Mary Dan Sue tail head CS 103

Examples of Linked Lists (A Stack of Numbers) A stack of numbers (from top to bottom): 10, 8, 6, 8, 2 A linked list of ints can represent this stack: 10 8 6 8 2 Tail Head CS 103

Examples of Linked Lists (A Sorted Set of Non-redundant Elements) A set of characters: a, b, d, f, c The elements must be arranged in sorted order: a, b, c, d, f A linked list of chars can represent this set: a b c d f tail_ptr head_ptr CS 103

Examples of Linked Lists (A Polynomial) A polynomial of degree n is the function Pn(x)=a0+a1x+a2x2+…+anxn. The ai’s are called the coefficients of the polynomial The polynomial can be represented by a linked list (2 data members and a link per item): a0,0 a1,1 a2,2 an,n head_ptr tail_ptr CS 103

Operations on Linked Lists Insert a new item At the head of the list, or At the tail of the list, or Inside the list, in some designated position Search for an item in the list The item can be specified by position, or by some value Delete an item from the list Search for and locate the item, then remove the item, and finally adjust the surrounding pointers size( ); isEmpty( ) CS 103

Inserting at the Head Allocate a new node Insert new element Make new node point to old head Update head to point to new node CSCD 216 - Linked Lists

Inserting at the Tail Allocate a new node Insert new element Have new node point to null Have old last node point to new node Update tail to point to new node CSCD 216 - Linked Lists

Inserting inside a linked list CSCD 216 - Linked Lists

Removing at the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node CSCD 216 - Linked Lists

Removing at the Tail Removing at the tail of a singly linked list cannot be efficient! There is no constant-time way to update the tail to point to the previous node CSCD 216 - Linked Lists

Deletion from inside a linked list CSCD 216 - Linked Lists

Linked List Implementation of Lists Need to know where the first node is the rest of the nodes can be accessed No need to move the list for insertion and deletion operations No memory waste CSCD 216 - Linked Lists

Linked List Implementation of Lists Linked List Array Traverse List O(N) O(N) Find FindKth (L,i) O(i) O(1) Delete O(1) O(N) Insert CSCD 216 - Linked Lists

Programming Details There are 3 special cases for linked lists Insert an element at the front of the list there is no really obvious way Delete an element from the front of the list changes the start of the list Delete an element in general requires to keep track of the node before the deleted one How can we solve these three problems ? CSCD 216 - Linked Lists

Programming Details Keep a header node in position 0 Write a FindPrevious routine returns the predecessor of the cell To delete the first element FindPrevious routine returns the position of header CSCD 216 - Linked Lists

Variations of Linked Lists Circular linked lists The last node points to the first node of the list How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) A B C Head CSCD 216 - Linked Lists

Variations of Linked Lists Doubly linked lists Each node points to not only successor but the predecessor There are two NULL: at the first and last nodes in the list Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards A B C   Head CSCD 216 - Linked Lists

Doubly Linked List Traversing list backwards not easy with regular lists Insertion and deletion more pointer fixing Deletion is easier Previous node is easy to find CSCD 216 - Linked Lists

Doubly Linked List A doubly linked list is often more convenient! Nodes store: element link to the previous node link to the next node Special trailer and header nodes prev next elem node trailer header nodes/positions elements CSCD 216 - Linked Lists

Insertion We visualize operation insertAfter(p, X), which returns position q p A B C p q A B C X p q A B X C CSCD 216 - Linked Lists

Insertion Algorithm Algorithm insertAfter(p,e): Create a new node v link v to its predecessor link v to its successor link p’s old successor to v link p to its new successor, v return v (the position for the element e) CSCD 216 - Linked Lists

Deletion We visualize remove(p), where p == last() A B C D p A B C p D CSCD 216 - Linked Lists

Deletion Algorithm Algorithm remove(p): CSCD 216 - Linked Lists

Worst-case running time In a doubly linked list + insertion at head or tail is in O(1) + deletion at either end is on O(1) -- element access is still in O(n) CSCD 216 - Linked Lists

Array versus Linked Lists Linked lists are more complex to code and manage than arrays, but they have some distinct advantages. Dynamic: a linked list can easily grow and shrink in size. We don’t need to know how many nodes will be in the list. They are created in memory as needed. In contrast, the size of an array is fixed at compilation time. Easy and fast insertions and deletions To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements. With a linked list, no need to move other nodes. Only need to reset some pointers. CSCD 216 - Linked Lists