Linked lists.

Slides:



Advertisements
Similar presentations
Chapter 17 Linked Lists.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
Data Structures Using C++
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Review Learn about linked lists
Data Structures & Algorithms
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Linked Lists Dr. Youssef Harrath
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.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
Data Structures Using Java1 Chapter 4 Linked Lists.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Data Structures Using C++1 Chapter 5 Linked Lists.
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,
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.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Pointers and Arrays Dynamic Variables and Arrays.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Lectures linked lists Chapter 6 of textbook
Linking in double direction
Review Deleting an Element from a Linked List Deletion involves:
Data Structure and Algorithms
List ADT & Linked Lists.
UNIT-3 LINKED LIST.
Traversing a Linked List
Linked Lists.
Sequences 9/18/ :20 PM C201: Linked List.
Chapter 20: Binary Trees.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Chapter 18: Linked Lists.
Popping Items Off a Stack Lesson xx
Chapter 16 Linked Structures
Linked Lists.
Dynamic Memory.
Data Structures & Algorithms
Linked lists.
Linked Lists.
Sequences 08/30/17 08/30/17 Unit III. Linked Lists 1.
Presentation transcript:

Linked lists

Definition of LL A linked list is a collection of components called nodes Every node (except the last one) contains the address of the next node The address of first node is stored in separate location called as head or first Every node in linked list has 2 components: one to store relevant information (data) and other to store the address called as link

Declaration of LL Because the node of linked list contains 2 components it should be declared as class or struct struct nodeType { int info; nodeType *link; };

Properties of LL VALUE EXPLANATION head 2000 head -> info 17 Because head is 2000 and the info of the node at location 2000 is 17 head -> link 2800 head -> link -> info 19 Because head -> link is 2800 and the info of the node at location 2800 is 92

Suppose that current is a pointer which has type as pointer head Suppose that current is a pointer which has type as pointer head. Then, the statement : current=head copies the value of head into current value current 2000 current -> info 17 current -> link 2800 current -> link -> info 92

Item insertion to LL Consider linked list before insertion variable declaration nodeType *head, *p, *q, *newnode; Suppose that p points to the node with info 65, and a new node with info 50 is to be created and inserted after p

newNode = new nodeType;//create newNode newNode -> info = 50;//store 50 in the new node newNode -> link = p -> link; p -> link = newNode;

p -> link = newNode; newNode -> link = p -> link; newNode points back to itself and the remainder of the list is lost by using to pointers insertion code can be simplified suppose q points to the node with info 34

newNode -> link = q; p -> link = newNode; Above statements insert newNode between p and q

Deletion We have LL shown below How to delete node with info 34 p->link = p->link->link; the node with info 34 is removed from the list. However, the memory is still occupied by this node, and this memory is inaccessible; that is, this node is dangling. To deallocate the memory, we need a pointer to this node.

Deallocate deleted node The following statements delete the node from the list and deallocate the memory occupied by this node. q = p->link; p->link = q->link; delete q;

Operations on LL Print the LL Destroy the LL current = first; //set current so that it points to //the first node while (current != NULL) //while more data to print { cout << current->info << " "; current = current->link; } }//end print while (first != NULL) //while there are nodes in the list { temp = first; //set temp to the current node first = first->link; //advance first to the next node delete temp; //deallocate the memory occupied by temp } last = NULL; //initialize last to NULL; first has already //been set to NULL by the while loop

Building LL Suppose that the nodes are in the usual info-link form, and info is of type int. Assume to process the following data: 2, 15, 8, 24, 34 Will be needed: three pointers to build the list: one to point to the first node in the list, which cannot be moved; one to point to the last node in the list; and one to create the new node. nodeType *first, *last, *newNode; int num;

Building LL… nodeType *first, *last, *newNode; int num; /*Suppose that first points to the first node in the list. Initially, the list is empty, so both first and last are NULL*/ first = NULL; last = NULL; cin >> num; //read a number in num newNode=new nodeType; /*allocate memory of type nodeType and store the address of the allocated memory in newNode*/ newNode->info = num; /*copy the value of num into the info field of newNode*/ newNode->link = NULL; /*initialize the link field of newNode to NULL*/ if (first == NULL) //if first is NULL, the list is empty; //make first and last point to newNode { first = newNode; last = newNode; } else //list is not empty last->link = newNode; //insert newNode at the end of the list //set last so that it points to the //actual last node in the list

Building LL… nodeType *first, *last, *newNode; int num; /*Suppose that first points to the first node in the list. Initially, the list is empty, so both first and last are NULL*/ first = NULL; last = NULL; 1 cin >> num; //read a number in num 2 newNode=new nodeType; /*allocate memory of type nodeType and store the address of the allocated memory in newNode*/ 3 newNode->info = num; /*copy the value of num into the info field of newNode*/ 4 newNode->link = NULL; /*initialize the link field of newNode to NULL*/ 5 if (first == NULL) //if first is NULL, the list is empty; //make first and last point to newNode { 5a first = newNode; 5b last = newNode; } 6 else //list is not empty 6a last->link = newNode; //insert newNode at the end of the list 6b last = newNode; //set last so that it points to the //actual last node in the list

After statement 1 executes, num is 2 After statement 1 executes, num is 2. Statement 2 creates a node and stores the address of that node in newNode. Statement 3 stores 2 in the info field of newNode, and statement 4 stores NULL in the link field of newNode Because first is NULL, we execute statements 5a and 5b. We now repeat statements 1 through 6b. After statement 1 executes, num is 15. Statement 2 creates a node and stores the address of this node in newNode. Statement 3 stores 15 in the info field of newNode, and statement 4 stores NULL in the link field of newNode Because first is not NULL, we execute statements 6a and 6b.

Operations over LL Print the LL Destroy the LL current = first; //set current so that it points to //the first node while (current != NULL) //while more data to print { cout<<current->info<<" "; current = current->link; } //end print while (first != NULL) //while there are nodes in the list { temp = first; //set temp to the current node first = first->link; //advance first to the next node delete temp; //deallocate the memory occupied by temp } last = NULL; //initialize last to NULL; first has already been set to NULL by the while loop

Building LL Backward For the previously given data 2, 15, 8, 24, 34 the linked list is as shown below as Backward LL Forward LL

Pseudo code of building Backward LL 1. Initialize first to NULL. 2. For each item in the list, Create the new node, newNode. Store the item in newNode. Insert newNode before first. Update the value of the pointer first.

C++ function to build Backward LL nodeType* buildListBackward() { nodeType *first, *newNode; int num; cout<<"Enter a list of integers ending with -1."<<endl; cin >> num; first = NULL; while (num != -1) newNode = new nodeType; //create a node newNode->info = num; //store the data in newNode newNode->link = first; //put newNode at the beginning of the list first = newNode; //update the head (first) pointer of the list cin >> num; //read the next number } return first; } //end buildListBackward

QUICK REVIEW A linked list is a list of items, called nodes, in which the order of the nodes is determined by the address, called a link, stored in each node. The pointer to a linked list—that is, the pointer to the first node in the list—is stored in a separate location called the head or first. A linked list is a dynamic data structure. The length of a linked list is the number of nodes in the list. Item insertion and deletion from a linked list do not require data movement; only the pointers are adjusted. A (single) linked list is traversed in only one direction.