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.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Chapter 17 Linked Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
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.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
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.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Data Structures Using C++ 2E
Chapter 17 Linked List.
Reference: Vinu V Das, Principles of Data Structures using C and C++
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.
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.
C++ Classes and Data Structures Jeffrey S. Childs
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
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.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
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.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Pointers and Linked Lists
Pointers and Linked Lists
Lectures linked lists Chapter 6 of textbook
Lists CS 3358.
Chapter 4 Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Linked Lists.
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
General List.
21 Data Structures.
Presentation transcript:

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 grow and shrink in size, with no upper limit  Fast insertion and deletion Ω head

Node struct nodeType { elemType data; nodeType *next; } nodeType *head; data pointer to node Note the recursive form of the data structure Note the recursive form of the data structure. Note the self-referencing form of the data structure. head

Basic List Operations Append (item)  -- insert at back InsertAtFront(item)  --insert at front of list InsertInOrder(iterm)  -- insert in (some type of) order Delete(item)  -- remove a particular item DestroyList()  -- delete the entire list IsEmpty()  -- true if list is empty; false, otherwise PrintList()  -- for diagnostic purposes  -- example of traversing a list (visiting each node in a list)

List Class Interface (list.h) typedef int elemType; class List { public: List(); // Constructor ~List(); // Destructor void append(elemType item); void insertAtFront(elemType item); void insertInOrder(elemType item); elemType removeFromFront(); void deleteNode(elemType item); void clear(); // remove all nodes bool isEmpty(); void print(); private: struct nodeType { elemType data; nodeType *next; // note “self-reference” }; nodeType *head; };

List() Operation (Constructor) A) Let head point to NULL Ω head Ω In list.cpp, assume the following directive: #define EMPTY // empty value List(){ head = NULL; } eeeeeenow eieiie List::List() { head = NULL; }

insertAtFront(item) Operation newNode A) Declare newNode pointer B) Create a new node, insert data newNode xxx 123 ΩΩ head Ω

insertAtFront (item) Operation C) Insert new node at front newNode xxx 123 head Ω Ω

void List::insertAtFront(elemType item){ // Prepare a new node nodeType *newNode; // pointer to new node newNode = new nodeType; // Create new node newNode->data = item; // Store data newNode->next = NULL; // Insert new node at front newNode->next = head; head = newNode; } insertAtFront(item) Operation

Does this algorithm work with an empty list? // Insert new node at front newNode->next = head; head = newNode;

Your Turn Write a C++ implementation of a removeFromFront() method, which removes an item from the front of a list and returns the item removed.

elemType removeFromFront() Operation temPtr A) Declare temPtr B) If (isEmpty())... return EMPTY Ω head Ω

elemType removeFromFront() Operation Else... C) Let temPtr point to where head points to: temPtr  head temPtr Ω head Ω

elemType removeFromFront() Operation E) Save the item removed. D) Let head point to the 2 nd Node. temPtr head Ω temPtr head Ω elemType result = temPtr->data;

elemType removeFromFront() Operation G) Return the item removed. F) Delete the node. temPtr head Ω return result; delete temPtr;

elemType List::removeFromFront item){ if (isEmpty()) return NULL; nodePtr *temPtr; temPtr = head; head = head->next; elemType result = temPtr->data; delete temPtr; return result; } elemType removeFromFront() Operation

Your Turn Sketch a diagram of a linked list (with 5 nodes). Using the diagram, indicate the steps necessary to implement the append(item) method, which inserts an item at the end of the list.

append(item) Operation temPtrnewNode A) Declare temPtr, newNode pointers B) Create a new node, insert data newNode xxx 123 temPtr ΩΩ head Ω

append(item) Operation C) If list empty, make new node the first D) Otherwise, find the last node head Ω newNode xxx 123 Ω temPtr head Ω

append (item) Operation E) Insert new node at end temPtr newNode xxx 123 head Ω While temPtr.next != Null, move temPtr temPtr head Ω Ω Ω Ω

void List::append(elemType item){ // Prepare a new node nodeType *newNode; // pointer to new node nodeType *temPtr; // To move across list newNode = new nodeType; // Create new node newNode->data = item; // Store data newNode->next = NULL; // With empty list, make new node the first if (head == NULL) head = newNode; else{... append (item) Operation

... // Start at head of list temPtr = head; // Find the last node while (temPtr->next != NULL){ temPtr = temPtr->next; } // Insert node at end temPtr->next = newNode; }

insertInOrder(item) Operation temPtr newNode A) Declare temPtr, prevPtr, and newNode pointers B) Create a new node, insert data newNode xxx 5 temPtr Ω Ω head Ω prevPtr

insertInOrder(item) Operation C) If list empty, make new node the first D) Otherwise, set temPtr to head, prevPtr to NULL temPtr head Ω newNode xxx 5 Ω prevPtr Ω head Ω 2 468

insertInOrder(item) Operation E) Skip all nodes whose data value < item. A pointer to the previous node is necessary in order to link a new node. 5 temPtr prevPtr head Ω 2 468

insertInOrder(item) Operation F) Link the new node to the one following it in order. G) Link the previous node to the new node. temPtr prevPtr head Ω newNode xxx 5 Ω

Your Turn Sketch a diagram of linked list with 5 nodes. The list is pointed to by head. Write an algorithm to print the data content of each node.

print() Operation (List traversal) temPtr A) Declare temPtr B) temPtr  head (Why not let head move down the list?) head Ω prevPtr

print() Operation (cont.) C) While not at end of list, print current item. while (temPtr != NULL) { cout data << ‘ ‘; D) Move the temPtr. temPtr = temPtr->next; } head Ω 2 468

deleteNode(item) Operation temPtr A) Declare temPtr, prevPtr pointers (Why 2 ptrs?) B) If list is empty, done. head Ω prevPtr item 6

deleteNode(item) Operation C ) If first node is to be deleted (if (item == head  data): 1.1. Let temPtr = head  next 2.2. Delete the first node ( delete head; ) 3.3. Let head = temPtr temPtr head Ω 468 temPtr head Ω 2 468

deleteNode(item) Operation D) Otherwise, find the node to be deleted. Set temPtr to head While (temPtr != NULL && tempPtr  data != item) 1. Set prevPtr = temPtr 2. temPtr = temPtr  next temPtr prevPtr head Ω 2 468

deleteNode(item) Operation The loop is exited for one of 2 reasons: 1. temPtr == NULL 2. temPtr->data == item If (temPtr != NULL) Let prevPtr  next = temPtr  next Delete node pointed to by temPt temPtr prevPtr head Ω

clear() Operation A)A) Traverse the list, deleting each node. B) B) Let head point to NULL. temPtr Ω head 2 468

clear() Operation (cont) temPtr1 Ω head Let temPtr1 point to head. While (temPtr1 != NULL) Let temPtr2 point to temPtr1->next delete current node (pointed to by temPtr1) Let temPtr point to temPtr2 Let head point to NULL temPtr2

Other Forms of Linked List Linked list with a header node Advantages Can simplify algorithms for basic insertion and deletion, since even an empty list has a node. Can contain global values, like current node count, smallest value, etc. head 2 46 Ω

deleteNode(item) Suppose: temPtr points to a node to be deleted prevPtr points to a node before that node A list with a single node is not a special case prevPtr = temPtr.next; 5 head 2 prevPtr temPtr 46 Ω

Circular Linked List Last pointer, instead of ending the list, points to the first one. In fact, head and back has little meaning Can process list from anywhere. May arbitrarily designate one node as current, and use a pointer to process list. current

Traversing Circular Linked List current if (current != NULL){ temPtr = current; do { cout data next; }while (temPtr != current); }

Doubly Linked List (Data Structure) struct nodeType { elemType data; nodeType *next; // ptr to next node nodeType *prev; // ptr to previous node }; nodeType *head; nodeType *back;

Doubly Linked List Each node has two pointers  To the next node  To the previous node Allows for list processing in either direction with equal ease. Ω head Ω back

append(item) head back A) Prepare a new node. 123 B) If list is empty, head and back point to new node Ω Ω 123 Ω Ω temPtr

append(item) C) Else, insert at back Back  next = temPtr temPtr  prev = back Back = temPtr Ω head Ω back 123 temPtr Ω

Append(item) void Dlist::append(elemType item){ // Crreate a new node nodeType *newNode; newNode = new nodeType; newNode->data = item; newNode->next = NULL; newNode->prev = NULL; if (head == NULL){ head = newNode; back = newNode; } else { back->next = newNode; newNode->prev = back; back = newNode; }