Traversing a Linked List

Slides:



Advertisements
Similar presentations
Chapter 17 Linked Lists.
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Informática II Prof. Dr. Gustavo Patiño MJ
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
CS Data Structures Chapter 4 Lists.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
 2008 Pearson Education, Inc. All rights reserved. 1 Member data stores a value of type parameter NODETYPE Member nextPtr stores a pointer to the next.
1 Data Structures and Algorithms Pointers and Dynamic Data.
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 4 Linked.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
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.
  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.
UNIT-II Topics to be covered Singly linked list Circular linked list
Chapter 14 Dynamic Data and Linked Lists
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
CISC181 Introduction to Computer Science Dr
Linked lists.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCE 210 Data Structures and Algorithms
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Pointers Psst… over there.
Doubly linked lists.
Pointers and References
Pointers Psst… over there.
Chapter 16-2 Linked Structures
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Indirection.
Linked Lists Chapter 4.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Linked List Functions.
Linked Lists.
Traversing a Linked List
Pointers and References
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked lists.
Dynamic Memory CSCE 121.
Variable Storage Memory Locations (Logical) Variable Classes Stack
Presentation transcript:

Traversing a Linked List Linked Lists Introduction Building a Linked List Traversing a Linked List Deleting a Node Dr. Hyrum D. Carroll (with some material from Dale & Weems’s Programming and Problem Solving with C++ slides)

Declarations for a Dynamic Linked List // Type declarations struct NodeType { char info; NodeType* link; }; // Variable DECLARATIONS NodeType* head; NodeType* ptr; . info . link ‘A’ 0x600

ptr is a pointer to a node . info . link ‘A’ 0x600 ptr

*ptr is the entire node pointed to by ptr ‘A’ 0x600 . info . link *ptr

ptr->info is a node member . info . link ‘A’ 0x600 ptr->info or (*ptr).info // Equivalent

ptr->link is a node member . info . link ‘A’ 0x600 ptr->link or (*ptr).link // Equivalent

Pointer Dereferencing and Member Selection ptr . info . link ‘A’ 0x600 ptr ptr . info . link ‘A’ 0x600 *ptr ptr . info . link (*ptr).info or ptr->info ‘A’ 0x600

Building a Linked List

Building a Linked List newNodePtr head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List newNodePtr 0x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List newNodePtr 0x200 head ‘L’ newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List newNodePtr 0x200 head ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List newNodePtr 0x200 head ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List newNodePtr 0x500 0x200 head ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List newNodePtr 0x500 0x200 head ‘I’ ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List newNodePtr 0x500 0x200 head ‘I’ 0x200 ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List newNodePtr 0x500 0x200 head ‘I’ 0x200 ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List ‘I’ 0x200 ‘L’ NULL newNodePtr 0x300 0x500 0x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List ‘T’ ‘I’ 0x200 ‘L’ NULL newNodePtr 0x300 0x500 0x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL newNodePtr 0x300 0x500 0x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Building a Linked List ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL newNodePtr 0x300 0x500 0x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

Traversing a Linked List

Traversing a Linked List ptr ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘T’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr 0x500 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr 0x500 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr 0x500 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr 0x200 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr 0x200 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr 0x200 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr NULL ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr NULL ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Traversing a Linked List ptr NULL ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

Deleting a Node

Deleting Node ’I’ prev curr ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

Deleting Node ’I’ prev curr NULL 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

Deleting Node ’I’ prev curr NULL 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x200 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x200 ? ? ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

Deleting Node ’I’ prev curr 0x300 NULL 0x300 0x200 head ‘T’ 0x200 ‘L’ NULL 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

Deleting Node ’I’ prev curr 0x300 NULL 0x300 0x200 head ‘T’ 0x200 ‘L’ NULL 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

Deleting Node ’I’ prev curr 0x300 NULL ‘T’ 0x200 ‘L’ NULL 0x300 0x200 head 0x300 0x200 ‘L’ NULL // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer