DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Chapter 17 Linked Lists.
Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
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.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Data Structures: A Pseudocode Approach with C
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.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: 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.
Data Structures Using C++ 2E
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.
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.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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.
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.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
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
Chapter 5 Record Storage and Primary File Organizations
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,
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.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Prof. I. J. Chung Data Structure #4 Professor I. J. Chung.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
C++ Programming:. Program Design Including
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.
UNIT-3 LINKED LIST.
The Bag and Sequence Classes with Linked Lists
LINKED LISTS CSCD Linked Lists.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 18: Linked Lists.
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.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Data Structures & Algorithms
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Chapter 9 Linked Lists.
Presentation transcript:

DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST

What is linked list? A method of organizing stored data in a computers’ memory or on storage medium based on the logical order of the data All stored data records are assigned a physical address in memory that that the computer uses to locate the information A linked list arranges the data by logic rather than by physical address

What is linked list? Linked list consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes. Data fieldsLinks Node

What is linked list? The last node has a reference to null The entry point into a linked list is called the head of the list

What is linked list? Address of record IDNamePhone No.Next Name Aziz Johan Suraya Malik eof Table 1

What is linked list? In the Table 1, each data record is assigned a memory address The first field holds the physical memory address of the record and the last field holds the physical memory address of the next logical record The list is said linked because each record is linked to the next based on the last field

What is linked list? What happen if new record is added? Assumed with a numerical ID of “2222”. When this record added to the list, the list change reflect the new linking logic, based on numerical ID The “Next Name” field changes for ID 1111 to accommodate the added record with ID 2222 as in Table 2

What is linked list? Address of record IDNamePhone No.Next Name Aziz Jenab Johan Suraya Malik eof Table 2

What is linked list? In the Table 3, the same data is organized numerically by the ID number The linked list still connects each record to the next using the “Next Name” field.

What is linked list? Address of record IDNamePhone No.Next Name Aziz Jenab Johan Malik Suraya eof Table 3

Linked List vs Array AdvantagesDisadvantages Linked listThe number of nodes can grow and shrink on demand Not allowed direct access to the individual elements Uses more memory to store a reference to the next node ArraySimple codingCannot be easily extended or reduced to fit the data set Less memoryExpensive to maintain new insertions & deletion

Types of linked list? Singly linked list Doubly linked list Circularly linked list

Singly linked list The simplest kind of linked list, which has one link per node. This link points to the next node in the list, or to a null value or empty list if it is the final node. Divided into two parts: the first part holds or points to information about the node, and second part holds the address of next node. Travels one way.

Doubly linked list Two-way linked list. Each node has two links: one points to the previous node, or points to a null value or empty list if it is the first node; and one points to the next, or points to a null value or empty list if it is the final node.

Circularly linked list The first and final nodes are linked together. To traverse a circular linked list, you begin at any node and follow the list in either direction until you return to the original node. Circularly-linked lists can be seen as having no beginning or end. This type of list is most useful for managing buffers for data ingest, and in cases where you have one object in a list and wish to iterate through all other objects in the list in no particular order. The pointer pointing to the whole list may be called the access pointer.

Declaration of linked list

Declarations for Linked Lists 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 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 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 void list_head_insert(node*& head_ptr, const node::value_type& entry); insert_ptr = new node; Place the data in the new node's data_field. Connect the new node to the front of the list.

Inserting a Node at the Front The correct new node can be completely created in one step by calling an appropriate node constructor null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_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(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.

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: 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 What is the name of this orange pointer ? 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 Look at the pointer which is in the node *previous_ptr previous_ptr

Pseudocode for Inserting Nodes What is the name of this orange pointer ? 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) 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 Write one C++ statement which will do the insertion 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 previous_ptr

Pseudocode for Inserting Nodes What might cause this statement to fail to compile? 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 previous_ptr list_head_insert(previous_ptr->link_field, entry);

Pseudocode for Inserting Nodes Use a node member function to get the link field if needed 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 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 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. We’ll 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.  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.  Set up remove_ptr.  head_ptr = remove_ptr->link( ); remove_ptr

Removing the Head Node  Set up.  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 Here’s what the linked list looks like after the removal finishes null head_ptr

Summary 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.

References Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch.