Linked Lists http://www.doc.ic.ac.uk/~wjk/C++Intro/RobMillerL7.html#S7-5.

Slides:



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

Linked Lists Geletaw S..
Linked Lists.
Chapter 17 Linked Lists.
Stacks, Queues, and Linked Lists
Linked Lists.
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
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.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: 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.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
1 CSC 211 Data Structures Lecture 10 Dr. Iftikhar Azim Niaz 1.
C++ is Fun – Part 14 at Turbine/Warner Bros.! Russell Hanson.
Starting Out with C++, 3 rd Edition 1 Chapter 17 Linked Lists.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
1 Working with Pointers An exercise in destroying your computer.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Linked Lists and Generics Written by J.J. Shepherd.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
Lectures linked lists Chapter 6 of textbook
Lists CS 3358.
List ADT & Linked Lists.
Linked lists.
CSCI 3333 Data Structures Linked Lists.
Data Structures and Algorithms IT12112
Chapter 18: Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Introduction to C++ Linear Linked Lists
Linked lists.
Sequences 08/30/17 08/30/17 Unit III. Linked Lists 1.
Presentation transcript:

Linked Lists http://www.doc.ic.ac.uk/~wjk/C++Intro/RobMillerL7.html#S7-5

Linked List ADT A linked list is a series of connected nodes (or links) where each node is a data structure. Dynamically allocated data structures can be linked together to form a chain. A linked list can grow or shrink in size as the program runs.This is possible because the nodes in a linked list are dynamically allocated.

Advantages of Linked Lists over Arrays Linked lists are more complex to code and manage than arrays, but they have some distinct advantages. A linked list can easily grow and shrink in size - The programmer doesn’t need to know how many nodes will be in the list. They are created in memory as needed. Speed of insertion or deletion from the list - Inserting and deleting elements into and out of arrays requires moving elements of the array. When a node is inserted, or deleted from a linked list, none of the other nodes have to be moved.

Composition of a Linked List Each node in the linked list contains - a) One or more members that represent data (e.g. inventory records, customer names, addresses, telephone numbers, etc). b) A pointer, that can point to another node. Data Members Pointer

Composition of a Linked List A linked list is called “linked” because each node in the series (i.e. the chain) has a pointer to the next node in the list, e.g. a) The list head is a pointer to the first node in the list. b) Each node in the list points to the next node in the list. c) The last node points to NULL (the usual way to signify the end). NULL struct Node { char element; Node *next; }; *next; pHead pTail

Composition of a Linked List The pointers play a big role in maintaining the linked list. The nodes in a linked list can be spread out over memory. Therefore, it is not possible to calculate the address of the next node, like it is possible to calculate the next element of an array. If a pointer from one node to another is lost, the list from that point is lost forever. Therefore, extreme care should be taken when assigning or re-assigning a pointer in a linked list.

Composition of a Linked List B pHead C If the pointer between any nodes (say B and C) is re-assigned erroneously to null or anything else, the access to the rest of the nodes ( C, D and E ) is then lost. If you loose the head pointer, you loose the entire list.

Creating a Linked List Just like any other data type, the information about the node has to be first be declared. Step 1) Declare a data structure for the nodes. struct Node { Object element; Node *next; };

Creating a Linked List a) In this example, the first member of the Node struct is a object called element. It holds the node’s data. This could just as well be just an integer x, or a structure of student records. b) The second member is a pointer called next. It holds the address of any object that is a structure of type Node. Hence each Node struct can point to the next node in the list. The Node struct contains a pointer to an object of the same type as that being declared. It is called a self-referential data structure. This makes it possible to create nodes that point to other nodes of the same type.

Creating a Linked List Next, since there is no physical relationship between nodes, a pointer needs to be created to point to the first logical node in the list. Step 2) Declare a pointer to serve as the head of the list: Node *head = NULL; Once you have done these 2 steps (i.e. declared a node data structure, and created a NULL head pointer, you have an empty linked list. null head

Declaring a Linked List in a Class class LinkedList{ public: struct Node { Object element; Node *next; Node (Object e, Node* n = NULL ) { element = e, next = n } }; … private: Node* pHead;

Linked List Operations There are 5 basic linked list operations: Appending a node (to the head or to the tail) Traversing a list Inserting a node (into a sorted list) Deleting a node (from the head, tail or middle) Destroying a list

Appending a Node When appending a node, there are several things that need to be taken into consideration. For instance: Memory has to be allocated for a new node, and the data stored in the memory The insertion point has to be determined – this could be to the head of the list, to the tail of the list or somewhere in the middle Once the insertion point is determined, the new node’s logical predecessor has to be identified Then, the new node has to point to its successor Finally, the predecessor has to point to the new node

Appending a Node F D A E B C newNode F D A B E pHead C InsertAfterThis Once the logical predecessor of the new node (InsertAfterThis) is identified, if it is equal to NULL, this signifies that the list is empty or that the insertion is always done to the head of the list. In this instance, the code for inserting the node is similar to: newNode->next = pHead; pHead = newNode;

Appending a Node F D A E B C newNode F D A B E pHead C InsertAfterThis InsertAfterThis However, if the logical predecessor of the new node (InsertAfterThis) is identified and it points to a node, this could mean that the node is appended to the middle of the list or to the end. Regardless of where the insertion of the new node is going to be, the code for inserting the node is similar to: newNode->next = InsertAfterThis->next; InsertAfterThis->next = newNode;

Appending to the Tail When appending always to the end of the list, here is one algorithm that could be used. a) Create a new node. b) Store data in the new node. c) if there are no nodes in the list Make the new node the first node. else Traverse the List to find the last node. Add the new node to the end of the list. endif.

Add a Node to an Empty List NULL h next pHead New Node

Appending a Node to the end of a List pHead e next NULL f next g next h next New Node

Appending a Node to the Tail void LinkedList::appendNode(object e) { Node *newNode, *Walker;   newNode = new Node; // create a new node newNode->element = e; newNode->next = NULL; if ( pHead==NULL) // if the list is empty add to head pHead = newNode; else { Walker = pHead; // Initialize Walker to head of list while (Walker->next != NULL) // Find the last node in the list Walker = Walker->next;  Walker->next = newNode; // Insert newNode as the last node } }

Appending a Node to the Tail In the previous algorithm, the list had to be traversed all the way to the end to find the last node in the list. One way to make this simpler would be to have a pointer that always pointed to the last node. In this method, traversing the list would then not be required.

Appending a Node to the Tail without Traversing the List void LinkedList::appendNode(object e) { Node *newNode;   newNode = new Node; // create a new node newNode->element = e; newNode->next = NULL; if ( pHead==NULL) // if the list is empty add to head { pHead = newNode; pTail = newNode; } else // Insert newNode as the last node { pTail->next = newNode; pTail = newNode; } }

Appending a Node to the Head When appending always to the head of the list, here is one algorithm that could be used. a) Create a new node. b) Store data in the new node. c) if the list is empty Make the new node the head of the list. else Add the new node to the top of the list Make the new node the head of the list endif.

Appending a Node to the Head pHead f next g next h next NULL e NEXT newNode

Appending a Node to the Head void LinkedList::appendNode(object e) { Node *newNode;   newNode = new Node; // create a new node newNode->element = e; newNode->next = NULL; if ( pHead==NULL) // appending to an empty list { pHead = newNode; } else // appending to an existing list { newNode->next = pHead; pHead = newNode; } }

Appending a Node to the Head In the previous algorithm, when appending a node to an existing list, the order in which the statements are written is extremely important. What would happen if the statement were written in reverse order, in this manner? pHead = newNode; newNode->next = pHead;

Appending a Node to the Head This would result with the new node pointing to itself, which would make the program go in a never-ending loop when accessing the list. This would also result in loosing access to all the nodes that were currently on the linked list. pHead NULL f next g next h next e NEXT newNode

Traversing a Linked List When traversing a list, it is important to remember NOT to move the head pointer from node to node. This would result in loosing access to nodes in the list. Instead, always assign another pointer to the head of the list, and use that pointer to traverse it. Assign list head to walking pointer While walking pointer is not NULL Display the info pointed to by walking pointer Assign walking pointer to its own next member end While

Traversing a Linked List void LinkedList::displayList() { Node *Walker; Walker = pHead; while(Walker != NULL) printInfo(Walker->element) ; Walker = Walker->next; }

Inserting a Node into an Ordered List Create a new node. Store data in the new node. if there are no nodes in the list Make the new node the first node. else Find the first node whose value is greater than or equal the new value, or the end of the list (whichever is first). Insert the new node before the found node, or at end of the list if no node was found. endif

Inserting a Node 7 next 2 next pHead 12 null 9 null newNode

Inserting a Node 7 next 2 next 12 null 9 null nodePtr pHead previousNode 9 null newNode

Inserting a Node 7 next 2 next 12 null 9 null previousNode pHead nodePtr 9 null newNode

Inserting a Node Once the position in which to insert is found, the node needs to be inserted. The order in which the statements are written is important when inserting the node. newNode->next = nodePtr; previousNode->next = newNode; OR newNode->next = previousNode->next;

Inserting a Node The segment of code from the previous slide will result in the following list: previousNode 7 next 2 next pHead 12 null nodePtr 9 next newNode

Inserting a Node However, if the order in which the statements were written were reversed in this manner: previousNode->next = newNode; newNode->next = previousNode->next; This would result with the new node pointing to itself, which would make the program go in a never-ending loop when accessing the list.

Inserting a Node The segment of code from the previous slide will result in the following list, where the nodes after the new node are lost, and the list becomes a never ending loop, when traversed. previousNode 7 next 2 next pHead 12 null nodePtr 9 next newNode

Inserting a Node into an Ordered List void LinkedList::insertNode(Object e) { Node *newNode, *nodePtr, *previousNode; newNode = new Node; // Allocate a new node & store the new object newNode->element = e; newNode->next = NULL;  if (pHead==NULL) // empty list – add to head pHead = newNode; else Walker = pHead; // assign Walker to head and use Walker to find where to insert while ( Walker != NULL && Walker->element.value < newNode->element.value ) { previousNode = Walker; Walker = Walker->next; } if (previousNode == NULL) // new node has the smallest value, insert at head { head = newNode; newNode->next = Walker; } else { previousNode->next = newNode; newNode->next = Walker; } }

Deleting a Node Deleting a node is similar to adding a node. The node to be deleted needs to be identified, along with its predecessor. Then, a) Remove the node from the list without breaking the links created by the next pointers. Delete the node from memory. It is important to remember to delete the node so that resources are released.

Deleting a Node Assume that we need to delete node that contains the value 7. nodePtr 7 next 2 next pHead 12 null previousNode The bypassed node is destroyed with the statement delete nodePtr;

Deleting a Node void LinkedList::deleteNode(int num) { ListNode *Walker, *previousNode; if (!pHead) // If the list is empty, do nothing. return; if (pHead->element.value == num) // If it is the first node in the list (special case) { Walker = pHead->next; // set the new head to walker delete pHead; // delete node pHead = Walker; // reassign new head } else { Walker = pHead; // Initialize Walker to head of list while (Walker != NULL && Walker->element.value != num) // skip all nodes whose value is not num { previousNode = Walker; Walker = Walker->next; } previousNode->next = Walker->next; // Link the previous node to the node after Walker delete Walker; // delete Walker } }