Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Chapter 17 Linked Lists.
COMP171 Fall 2005 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.
Linked Lists Chapter 4.
CS 367 – Introduction to Data Structures
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
Algorithm Analysis.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
1 Linked Lists It’s a conspiracy!. 2 Linked list: a data structure used to represent an ordered list Consists of a sequence of nodes A node consists of.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
Copyright © 2002 Pearson Education, Inc. Slide 1.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Linked Structures, LinkedStack
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST.
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
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
COMP 53 – Week Eight Linked Lists.
Pointers and Linked Lists
CSC212 Data Structure - Section BC
Pointers and Linked Lists
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Linked Lists Chapter 6 Section 6.4 – 6.6
CISC181 Introduction to Computer Science Dr
Chapter 4 Linked Lists
The Bag and Sequence Classes with Linked Lists
8-1.
8-1.
Pointers and Linked Lists
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Programming Abstractions
Chapter 17: Linked Lists.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Linked Lists.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Presentation transcript:

Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Linked List Linked List Linked List Is a series of connected nodes, where each node is a data structure with data field and pointer(s) field. Is a series of connected nodes, where each node is a data structure with data field and pointer(s) field. Advantages over array implementation Advantages over array implementation Can grow and shrink in size, with no upper limit Can grow and shrink in size, with no upper limit Fast insertion and deletion Fast insertion and deletion Ω head

For this presentation, nodes in a linked list are objects, as shown here. data_field link_field 10 data_field link_field 15 data_field link_field 7 null class node { public: typedef double value_type;... private value_type data_field; node *link_field; }; Declarations for Linked Lists

typedef Keyword typedef int age_t; typedef int count_t; typedef double gpa_t; … age_t myAge = 21; count_t numElements = 5; gpa_t myGPA = 3.8; typedef int age_t; typedef int count_t; typedef double gpa_t; … age_t myAge = 21; count_t numElements = 5; gpa_t myGPA = 3.8; void someFunc(age_t age, count_t count); is clearer than void someFunct(int age, int count); void someFunc(age_t age, count_t count); is clearer than void someFunct(int age, int count);

Why Use typedef? Using typedefs can make your code clearer Using ypedefs can make your code easier to modify

The data_field of each node is a type called value_type, defined by a typedef. data_field link_field 10 data_field link_field 15 data_field link_field 7 null class node { public: typedef int value_type;... private value_type data_field; node *link_field; }; Declarations for Linked Lists

Each node also contains a link_field which is a pointer to another node. data_field link_field 10 data_field link_field 15 data_field link_field 7 null class node { public: typedef int value_type;... private value_type data_field; node *link_field; }; Declarations for Linked Lists

Your Turn Write the public interface for the Node classe.g, Constructors, accessors, modifiers. Write the public interface for the Node classe.g, Constructors, accessors, modifiers.

Solution class node{ public: //CONSTRUCTORS node(); node(const valueType& value, node* ptr); //CONSTANT FUNCTIONS valueType getData(); node* getNext(); //MODIFIER FUNCTIONS void setData(valueType newData); void setLink(node* newLink); private: valueType data; node* next; };

Declarations for Linked Lists A program can keep track of the front node by using a pointer variable such as head_ptr in this example. Notice that head_ptr is not a node -- it is a pointer to a node. head_ptr data_field link_field 10 data_field link_field 15 data_field link_field 7 null

Declarations for Linked Lists A program can keep track of the front node by using a pointer variable such as head_ptr. Notice that head_ptr is not a node -- it is a pointer to a node. We represent the empty list by storing null in the head pointer. head_ptr null

void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front We want to add a new entry, 13, to the front of the linked list shown here null head_ptr entry 13

Inserting a Node at the Front Create a new node, pointed to by a local variable insert_ptr null head_ptr entry 13 insert_ptr void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Front insert_ptr = new node; null head_ptr entry 13 insert_ptr void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node; Place the data in the new node's data_field. void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node; Place the data in the new node's data_field. Connect the new node to the front of the list. void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_ptr); The correct new node can be completely created in one step by calling an appropriate node constructor. void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_ptr); Make the old head pointer point to the new node. void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Front insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; null head_ptr 13 When the function returns, the linked list has a new node at the front. void list_head_insert(node*& head_ptr, const node::value_type& entry);

void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; } Inserting a Node at the Front

void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; } Does the function work correctly for the empty list ?

head_ptr entry 13 null Inserting a Node at the Front Does the function work correctly for the empty list ? void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; }

Inserting a Node at the Front head_ptr entry 13 null insert_ptr 13 void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; } null

Inserting a Node at the Front head_ptr entry 13 insert_ptr 13 null void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; }

Inserting a Node at the Front head_ptr 13 null void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; } When the function returns, the linked list has one node.

Caution! Always make sure that your linked list functions work correctly with an empty list.

Pseudocode for Inserting Nodes Nodes are often inserted at places other than the front of a linked list. There is a general pseudocode that you can follow for any insertion function...

Pseudocode for Inserting Nodes Determine whether the new node will be the first node in the linked list. If so, then there is only one step: list_head_insert(head_ptr, entry);

Pseudocode for Inserting Nodes Determine whether the new node will be the first node in the linked list. If so, then there is only one step: Determine whether the new node will be the first node in the linked list. If so, then there is only one step: The function we already wrote list_head_insert(head_ptr, entry);

Pseudocode for Inserting Nodes Determine whether the new node will be the first node in the linked list. If so, then there is only one step: list_head_insert(head_ptr, entry); A pointer to the head of the list

Pseudocode for Inserting Nodes Determine whether the new node will be the first node in the linked list. If so, then there is only one step: list_head_insert(head_ptr, entry); The data to put in the new node

Pseudocode for Inserting Nodes Otherwise (if the new node will not be first): Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position.

Pseudocode for Inserting Nodes null head_ptr Otherwise (if the new node will not be first): Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. In this example, the new node will be the second node previous_ptr

Pseudocode for Inserting Nodes null head_ptr Otherwise (if the new node will not be first): Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position What is the name of this orange pointer ? Look at the pointer which is in the node *previous_ptr previous_ptr

Pseudocode for Inserting Nodes null head_ptr Otherwise (if the new node will not be first): Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position This pointer is called previous_ptr->link_field (although this name may be private to the node) What is the name of this orange pointer ? previous_ptr

Pseudocode for Inserting Nodes null head_ptr Otherwise (if the new node will not be first): Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position previous_ptr->link_field points to the head of a small linked list, with 10 and 7 previous_ptr

Pseudocode for Inserting Nodes null head_ptr Otherwise (if the new node will not be first): Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. The new node must be inserted at the front of this small linked list. 13 Write one C++ statement which will do the insertion. previous_ptr

Pseudocode for Inserting Nodes null head_ptr Otherwise (if the new node will not be first): Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. 13 What might cause this statement to fail to compile? previous_ptr list_head_insert(previous_ptr->link_field, entry);

Pseudocode for Inserting Nodes null head_ptr Otherwise (if the new node will not be first): Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. 13 Use a node member function to get the link field if needed. previous_ptr list_head_insert(previous_ptr->link( ), entry);

Pseudocode for Inserting Nodes Determine whether the new node will be the first node in the linked list. If so, then there is only one step: list_head_insert(head_ptr, entry); Otherwise (if the new node will not be first): Set a pointer named previous_ptr to point to the node which is just before the new node's position. Make the function call: list_head_insert(previous_ptr->link( ), entry);

Pseudocode for Inserting Nodes The process of adding a new node in the middle of a list can also be incorporated as a separate function. This function is called list_insert in the linked list toolkit of Section 5.2. The process of adding a new node in the middle of a list can also be incorporated as a separate function. This function is called list_insert in the linked list toolkit of Section 5.2.

Pseudocode for Removing Nodes Nodes often need to be removed from a linked list. As with insertion, there is a technique for removing a node from the front of a list, and a technique for removing a node from elsewhere. Well look at the pseudocode for removing a node from the front of a linked list.

Removing the Head Node null head_ptr 13 Start by setting up a temporary pointer named remove_ptr to the head node. remove_ptr

Removing the Head Node null head_ptr 13 Set up remove_ptr. head_ptr = remove_ptr->link( ); remove_ptr Draw the change that this statement will make to the linked list.

Removing the Head Node null head_ptr 13 Set up remove_ptr. head_ptr = remove_ptr->link( ); remove_ptr

Removing the Head Node Set up remove_ptr. head_ptr = remove_ptr->link( ); delete remove_ptr; // Return the node's memory to heap null head_ptr 13 remove_ptr

Removing the Head Node Heres what the linked list looks like after the removal finishes null head_ptr

It is easy to insert a node at the front of a list. The linked list toolkit also provides a function for inserting a new node elsewhere It is easy to remove a node at the front of a list. The linked list toolkit also provides a function for removing a node elsewhere--you should read about this function and the other functions of the toolkit. Summary

Doubly Linked List head Ω class node {... private: elementType data; node* next; node* previous; }; class list {... private: node* head; int count; }; Ω

insertAtFront() head Ω Ω newPtr Ω Ω head Ω newPtr Ω

insertAtFront() void list::insertAtFront(valueType item) { node* temp = new node(item, NULL, NULL); if (isEmpty()) head = temp; else { temp->setNext(head); head->setPrevious(temp); head = temp; } count++; }

removeFromFront() head Ω Ω Ω temp Ω Ω

removeFromFront() valueType list::removeFromFront() { valueType result = NIL; if (!isEmpty()) { result = head->getData(); node* temp = head; head->setPrevious(NULL); head = head->getNext(); delete temp; count--; } return result; }

insertAtBack() head Ω Ω newPtr Ω Ω head Ω newPtr Ω temp

insertAtBack() void list::insertAtBack(valueType item) { node* newPtr = new node(item, NULL, NULL); // find the last node if (isEmpty()) head = newPtr; else { node* temp = head; while (temp->getNext() != NULL) { temp = temp->getNext(); } temp->setNext(newPtr); newPtr->setPrevious(temp); } count++; }

removeFromBack() head Ω Ω Ω temp Ω Ω

removeFromBack() valueType list::removeFromBack() { valueType result = NIL; if (!isEmpty()) { // find last node node* temp = head; while (temp->getNext() != NULL) { temp = temp->getNext(); } // remove last node temp->getPrevious()->setNext(NULL); delete temp; } return result; }

Tail Pointer (to the last node) head Ω class node {... private: elementType data; node* next; }; class list {... private: node* head; node* tail; int count; }; tail