1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists Geletaw S..
Singly linked lists Doubly linked lists
Pointers.
Lists CS 3358.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Chapter 3 Lists Dr Zeinab Eid.
Linked Lists.
Linear Lists – Linked List Representation
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
CHP-5 LinkedList.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
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
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
Data Structures: A Pseudocode Approach with C
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
© 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
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Data Structures Using C++ 2E
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
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”
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List data structure This is a new data structure. The List data structure is among the most generic of data structures. In daily life, we use shopping.
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Unit – I Lists.
C++ Programming:. Program Design Including
Linked Lists Chapter 6 Section 6.4 – 6.6
Lists CS 3358.
Lecture - 6 On Data Structures
Data Structure Dr. Mohamed Khafagy.
Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
LINKED LISTS CSCD Linked Lists.
Arrays and Linked Lists
Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
CS2013 Lecture 4 John Hurley Cal State LA.
Problem Understanding
Linked Lists & Iterators
LINEAR DATA STRUCTURES
Problem Understanding
Presentation transcript:

1 Linked Lists (Lec 6)

2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications  ADT for links  ADT for singly linked lists Outline

3 Sequential data structures in general, suffer from the following drawbacks: Inefficient implementation of insertion and deletion operations Inefficient use of storage memory

4 Linked Lists A linked list is a series of connected nodes Each node contains at least – A piece of data (any type) – Pointer to the next node in the list Head: pointer to the first node The last node points to NULL A  Head BCA datapointer node

5 A linked representation serves to counteract the drawbacks of sequential representation by exhibiting the following merits:  Efficient implementation of insertion and deletion operations. Unlike sequential data structures, there is complete absence of data movement of neighbouring elements during the execution of these operations.  Efficient use of storage memory. The operation and management of linked data structures are less prone to instigate memory fragmentation.

6  A linked representation of data structure known as a linked list is a collection of nodes.  Each node is a collection of fields categorized as data items and links.  The data item fields hold the information content or data to be represented by the node.  The link fields hold the addresses of the neighbouring nodes or of those nodes which are associated with the given node as dictated by the application.

7 Implementation of linked lists to frame chunks of memory into nodes with the desired number of data items and fields to determine which nodes are free and which have been allotted for use to obtain nodes from the free storage area or storage pool for use GETNODE(X) to return or dispose nodes from the reserved area or pool to the free area after use RETURN(X)

8 Linked Lists8 Singly Linked Lists A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores – element – link to the next node next elem node ABCD 

9 Singly Linked Lists A singly linked list is a linear data structure each node of which has one or more data item fields (DATA) but only a single link field (LINK) LINK DATA

10 Linked Lists10 Inserting at the Head 1.Allocate a new node 2.Insert new element 3.Make new node point to old head 4.Update head to point to new node

11 Linked Lists11 Removing at the Head 1.Update head to point to next node in the list 2.Allow garbage collector to reclaim the former first node

12 Linked Lists12 Singly linked list with ‘tail’ sentinel public class SLinkedListWithTail { protected Node head; // head node of the list protected Node tail; // tail node of the list /** Default constructor that creates an empty list */ public SLinkedListWithTail() { head = null; tail = null; } //... update and search methods would go here... }

13 Linked Lists13 Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node

14 Linked Lists14 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

15 Insertion in a singly linked list Algorithm 6.1 : To insert a data element ITEM in a non empty singly liked list START, to the right of node NODE. Procedure INSERT_SL(START, ITEM, NODE) /* Insert ITEM to the right of node NODE in the list START */ Call GETNODE(X); DATA(X) = ITEM; LINK(X) = LINK(NODE);  Node X points to the original right neighbour of node NODE */ LINK(NODE) = X; end INSERT_SL.

16 Algorithm 6.3 : Deletion of a node which is to the right of node NODEX in a singly linked list START Procedure DELETE_SL(START, NODEX) if (START = NIL) then Call ABANDON_DELETE; /*ABANDON_DELETE terminates the delete operation */ else {TEMP = LINK(NODEX); LINK(NODEX) = LINK(TEMP); Call RETURN(TEMP); } end DELETE_SL. Deletion in a singly linked list

17 Linked Lists17 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 prevnext elem trailer header nodes/positions elements node

18 Linked Lists18 Insertion We visualize operation insertAfter(p, X), which returns position q ABXC ABC p ABC p X q pq

19 Linked Lists19 Insertion Algorithm Algorithm insertAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p){link v to its predecessor} v.setNext(p.getNext()){link v to its successor} (p.getNext()).setPrev(v){link p’s old successor to v} p.setNext(v){link p to its new successor, v} return v{the position for the element e}

20 Linked Lists20 Deletion We visualize remove(p), where p == last() ABCD p ABC D p ABC

21 Linked Lists21 Deletion Algorithm Algorithm remove(p): t = p.element{a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()){linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null){invalidating the position p} p.setNext(null) return t

22 Circularly Linked Lists For further improvement in processing of a singly linked list one may replace the null pointer in the last node with the address of the first node in the list. Such a list is called as a circularly linked list or a circular linked list or simply a circular list.

23 Advantages of circularly linked list accessibility of a node delete operations relative efficiency in the implementation of list based operations Disadvantages of circularly linked lists  getting into an infinite loop owing to the circular nature of pointers in the list solution to this problem is to designate a special node to act as the head of the list, known as list head or head node

24 headed circularly linked list or circularly linked list with head node. HEAD NODE

25 To enhance greater flexibility of movement, the linked representation could include two links in every node, each of which points to the nodes on either side of the given node Such a linked representation known as doubly linked list Each node has one or more data fields but only two link fields termed left link (LLINK) and right link (RLINK). The LLINK field of a given node points to the node on its left and its RLINK field points to the one on its right. A doubly linked list may or may not have a head node. Again, it may or may not be circular. Doubly Linked Lists

26 Advantages of a doubly linked list  The availability of two links LLINK and RLINK permit forward and backward movement during the processing of the list.  The deletion of a node X from the list calls only for the value X to be known. Disadvantages of a doubly linked list  memory requirement LLINKRLINK DATA

27 Insertion in a doubly linked list Algorithm 6.4 : To insert node X to the right of node Y in a headed circular doubly linked list P Procedure INSERT_DL(X, Y) LLINK(X) = Y; RLINK(X) = RLINK(Y); LLINK(RLINK(Y)) = X; RLINK(Y) = X; end INSERT_DL.

28 Deletion in a doubly linked list Algorithm 6.5 : Delete node X from a headed circular doubly linked list P procedure DELETE_DL(P, X) if (X = P) then ABANDON_DELETE; else {RLINK(LLINK(X)) = RLINK(X); LLINK(RLINK(X)) = LLINK(X); Call RETURN(X); } end DELETE_DL.

29 Multiply Linked Lists A multiply linked list as its name suggests is a linked representation with multiple data and link fields

30 Applications Addition of polynomials Representation of a sparse matrix

31 ADT for Links Data objects: Addresses of the nodes holding data and null links Operations:  Allocate node (address X) from Available Space to accommodate data GETNODE ( X)  Return node (address X) after use to Available Space RETURN(X)  Store a value of one link variable LINK1 to another link variable LINK2 STORE_LINK ( LINK1, LINK2)  Store ITEM into a node whose address is X STORE_DATA (X, ITEM)  Retrieve ITEM from a node whose address is X RETRIEVE_DATA (X, ITEM)

32 ADT for Singly Linked Lists Data objects: A list of nodes each holding one (or more) data field(s) DATA and a single link field LINK. LIST points to the start node of the list. Operations:  Check if list LIST is empty CHECK_LIST_EMPTY ( LIST) (Boolean function )  Insert ITEM into the list LIST as the first element INSERT_FIRST (LIST, ITEM)  Insert ITEM into the list LIST as the last element INSERT_LAST (LIST, ITEM)  Insert ITEM into the list LIST in order INSERT_ORDER (LIST, ITEM)

33  Delete the first node from the list LIST DELETE_FIRST(LIST)  Delete the last node from the list LIST DELETE_LAST(LIST)  Delete ITEM from the list LIST DELETE_ELEMENT (LIST, ITEM)  Advance Link to traverse down the list ADVANCE_LINK (LINK)  Store ITEM into a node whose address is X STORE_DATA(X, ITEM)  Retrieve data of a node whose address is X and return it in ITEM RETRIEVE_DATA(X, ITEM)  Retrieve link of a node whose address is X and return the value in LINK1 RETRIEVE_LINK (X, LINK1)

34 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 Head BC

35 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 Head B  C 

36 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 a C++ 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.