EEL 4854 IT Data Structures Linked Lists

Slides:



Advertisements
Similar presentations
Chapter 3 Lists Dr Zeinab Eid.
Advertisements

Queues and Linked Lists
Chapter 17 Linked List Saurav Karmakar Spring 2007.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Review Learn about linked lists
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.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Data Structures: A Pseudocode Approach with C
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
© 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
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
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
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
CH 1-4 : INTRODUCTION ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
Linked List, Stacks Queues
COMP9024: Data Structures and Algorithms
Lecture 6 of Computer Science II
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Cpt S 122 – Data Structures Abstract Data Types
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.
Ch7. List and Iterator ADTs
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,
Doubly Linked Lists 6/3/2018 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
Data Structure Dr. Mohamed Khafagy.
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
CS212D: Data Structures Week 5-6 Linked List.
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.
CS212D: Data Structures Week 5-6 Linked List.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
LINKED LISTS CSCD Linked Lists.
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Arrays and Linked Lists
Sequences 11/22/2018 3:25 AM Doubly-Linked Lists Doubly-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.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Lists CSE 373 Data Structures.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Problem Understanding
Recall What is a Data Structure Very Fundamental Data Structures
LINKED LISTS.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Lecture 16 Section 6.2 Thu, Mar 1, 2007
Linked Lists & Iterators
Lists CSE 373 Data Structures.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Programming II (CS300) Chapter 07: Linked Lists
Stacks, Queues, and Deques
Linked List Improvements
Jyh-Shing Roger Jang (張智星) CSIE Dept, National Taiwan University
Stacks and Linked Lists
Problem Understanding
Presentation transcript:

EEL 4854 IT Data Structures Linked Lists 8/2/2018 12:17 AM EEL 4854 IT Data Structures Linked Lists

Linked Lists Singly Linked Lists Doubly Linked Lists 8/2/2018 12:17 AM Linked Lists Singly Linked Lists Doubly Linked Lists Circularly Linked Lists

Linked Lists Singly Linked Lists Doubly Linked Lists 8/2/2018 12:17 AM Linked Lists Singly Linked Lists Doubly Linked Lists Circularly Linked Lists

8/2/2018 12:17 AM Problems with Arrays Arrays are convenient for storing objects in a certain order. However, the size of an array must be known in advance. Linked lists do not suffer form this drawback.

8/2/2018 12:17 AM Linked Lists A linked list is a collection of nodes that together form a linear ordering. The ordering is such that each node is an object that stores a reference to an element and a reference, called next, to another node. The first and last node of a linked list are called the head and tail of the list respectively. next node elem  A B C D

List Node Implementation 8/2/2018 12:17 AM List Node Implementation

Linked List Implementation 8/2/2018 12:17 AM Linked List Implementation

Insertion in a Linked List 8/2/2018 12:17 AM Insertion in a Linked List It is easy to insert an element at the head and tail of the list. All we need to do is to: create a node set its next link to refer to the same object as head set head to point to the new node.

Insertion at the Head Algorithm addFirst(v): v.setNext(head) 8/2/2018 12:17 AM Insertion at the Head Algorithm addFirst(v): v.setNext(head) //make v point to the old head node head  v //make variable head point to new node size  size + 1 // increment node count

Insertion at the Tail Inserting at the tail can be done as follows: 8/2/2018 12:17 AM Insertion at the Tail Inserting at the tail can be done as follows: Allocate a new node Insert the new element Have the new node point to null Have the old last node point to the new node Update the tail to point to the new node

Insertion at the Tail Algorithm addLast(v): v.setNext(null) 8/2/2018 12:17 AM Insertion at the Tail Algorithm addLast(v): v.setNext(null) //make new node v point to null object tail.setNext(v) // make old tail node point to new node tail  v //make variable tail point to new node size  size + 1 // increment node count

8/2/2018 12:17 AM Removing at the Head The reverse operation of inserting a new element a the head of a linked list is to remove an element at the head.

Removing at the Head Algorithm removeFirst(): if head = null then 8/2/2018 12:17 AM Removing at the Head Algorithm removeFirst(): if head = null then Indicate an error: list is empty t  head head  head.getNext() // make head point to next node (or null) t.setNext(null) // null out next pointer of removed node size  size – 1 // decrement node count

8/2/2018 12:17 AM Removing at the Tail Removing at the tail of a singly linked list is not efficient. There is no constant-time way to update the tail to point to the previous node.

Linked Lists Singly Linked Lists Doubly Linked Lists 8/2/2018 12:17 AM Linked Lists Singly Linked Lists Doubly Linked Lists Circularly Linked Lists

8/2/2018 12:17 AM Doubly linked Lists It is time consuming to remove any node other than the head in a singly linked list. There is a type of list, called doubly linked list, that allows to go in both directions in a linked list. Such list allows quick insertions and removals at both ends and in the middle of the list.

8/2/2018 12:17 AM Doubly Linked Lists A doubly linked list provides a natural implementation of the List ADT. Nodes consist of: element link to the previous node link to the next node There are also special trailer and header nodes in the list. prev next elem node trailer header nodes/positions elements

List Node Implementation 8/2/2018 12:17 AM List Node Implementation

Header and Trailer Nodes 8/2/2018 12:17 AM Header and Trailer Nodes To simplify programming, it is convenient to add special nodes at both ends of a doubly linked list: a header node just before the head of the list, and a trailer node just after the tail of the list. These sentinel nodes do not store any elements. The header has a valid next reference but a null prev reference. The trailer has a valid prev reference but a null next reference.

Removing at the End of a List 8/2/2018 12:17 AM Removing at the End of a List We visualize remove(p), where p = last() A B C D p A B C D p A B C

Removing at the End of a List 8/2/2018 12:17 AM Removing at the End of a List

Inserting at the Head of a List 8/2/2018 12:17 AM Inserting at the Head of a List

Inserting at the Head of a List 8/2/2018 12:17 AM Inserting at the Head of a List

Inserting in the Middle of a List 8/2/2018 12:17 AM Inserting in the Middle of a List Let w be the node following v in the linked list. Assume, we would like to insert node z after node v. We need to complete the following steps: make z’s prev link refer to v make z’s next link refer to w make w’s prev link refer to z make v’s next link refer to z

Inserting in the Middle of a List 8/2/2018 12:17 AM Inserting in the Middle of a List We visualize operation insertAfter(p, X), which returns position q p A B C A B C p X q A B X C p q

Inserting in the Middle of a List 8/2/2018 12:17 AM Inserting in the Middle of a List

Removing in the Middle of a List 8/2/2018 12:17 AM Removing in the Middle of a List Assume node v is located between nodes u and w. To remove node v from the middle of the list, we do the following: Have u and w point to each other instead of v. This is called linking out of v. Null out v’s prev and next pointers so as not to retain old references into the list.

Removing in the Middle of a List 8/2/2018 12:17 AM Removing in the Middle of a List

Removing in the Middle of a List 8/2/2018 12:17 AM Removing in the Middle of a List

Linked Lists Singly Linked Lists Doubly Linked Lists 8/2/2018 12:17 AM Linked Lists Singly Linked Lists Doubly Linked Lists Circularly Linked Lists

Circularly Linked Lists 8/2/2018 12:17 AM Circularly Linked Lists A circularly linked list has the same kind of nodes as a singly linked list, that is each node has a next pointer and a reference to an element. But, there is no head or tail in a circularly linked list. For instead of having the last node’s next pointer be null, in a circularly linked list, it points back to the first node. Even though a circularly linked list has no beginning or end, we nevertheless need some node to be marked as a special node, called cursor, from which to start if we ever need to traverse the list.

Circularly Linked List Operations 8/2/2018 12:17 AM Circularly Linked List Operations The following are simple update methods for circularly linked lists: add(v): Insert a new node v immediately after the cursor; if the list is empty, then v becomes the cursor and its next pointer points to itself. remove(): remove and return the node v immediately after the cursor (not the cursor itself, unless it is the only node in the list); if the list becomes empty, the cursor is set to null. advance(): Advance the cursor to the next node in the list.

Implementation of a Circularly Linked List 8/2/2018 12:17 AM Implementation of a Circularly Linked List

-Fin- Linked Lists