Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists.
Data Structures: A Pseudocode Approach with C
DATA STRUCTURES USING C++ Chapter 5
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Review Learn about linked lists
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Data Structures: A Pseudocode Approach with C
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Linked Lists
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: 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.
Summary of lectures (1 to 11)
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
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.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
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.
Algorithms and Data Structures
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
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.
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.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
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,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
  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
LINKED LISTS.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
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.
Programming Circular Linked List.
Chapter 12 – Data Structures
Review Deleting an Element from a Linked List Deletion involves:
12 C Data Structures.
Data Structure and Algorithms
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.
CSCI 3333 Data Structures Linked Lists.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Linked Lists.
Chapter 16 Linked Structures
Introduction to C++ Linear Linked Lists
Lecture 14 Linked Lists CSE /26/2018.
Lists.
Linked Lists.
General List.
Linked lists.
Linked Lists.
Presentation transcript:

Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics

 Consists of items that are linked to each other  Each item on the list is associated with a reference (pointer) that indicates where the next item is found  A dynamic data structure: grows and shrinks as necessary at runtime  There are no unused/empty locations

 Simplest form of linked list  Linked list object holds a reference to the first node on the list  Each node object consists of a reference to the data object being stored, and a reference to the next node in the list

 The pointer to the first node in the list is referred to as the head pointer, because it points to the head node in the list.  In addition to the head pointer, there are usually other pointers associated with the list. These can include a pointer to the last element in the list (tail pointer) and a pointer that traverses the list to find data (navigator or traversal pointer).

Memory Loc ValueName Pointer to Linked list first element ……… 1000AliPerson.Name Person.Age Next_Person ……… 5500ZainPerson.Name 55265Person.Age 5530NULLNext_Person ………

. head Linked List object Node objects Data objects size 3

 To find the n th item in a linked list: Follow the head pointer to the first node Find the reference to the next node Follow it to the second node Find the reference to the next node Follow it to the third node Etc, until n th node is reached Data pointer points to the n th item

. head Linked List object size tail 3 tail reference makes it easier to add to the end of the linked list

 Oftentimes it is convenient to create a structure that contains the head pointer of a list and also information about the list itself (e.g., number of elements currently in the list, maximum number of elements to be allowed in the list, etc).  This extra data is referred to as metadata.

 With a linked list, there are a standard set of functions that operate on the list: Creating the list  Initialize pointers to NULL; Inserting nodes  Insert at beginning  Insert at middle  Insert at last Deleting nodes  Delete from beginning, middle, last Traversing the list Destroying the list

#include struct person { char name[25]; int age; struct person *Next_Person; }; struct linkedlist { struct person *head; struct person *tail; int nodeCount; }; void initialize(linkedlist *ll); void InsertFront(linkedlist *ll); void InsertRear(linkedlist *ll); void DeleteRear(linkedlist *ll); void getData(person *p); void PrintList(linkedlist *ll);

void main (void) { linkedlist ll; //linked list created initialize(&ll); //linked list initialized int choice = 0; //list is empty, lets create person dynamically do { cout << " 1: insert item in front" << endl; cout << " 2: Insert item in rear" << endl; cout << " 3: Delete item from front" << endl; cout << " 4: Delete item from rear" << endl; cout << " 5: Print List" << endl; cout << " 8: Exit" << endl; cin >> choice; if (choice == 1) { InsertFront(&ll);} if (choice == 2) { InsertRear(&ll);} if (choice == 3) {DeleteFront(&ll); } if (choice == 4) { DeleteRear(&ll);} if (choice == 5) { PrintList(&ll); } }while (choice != 8); }

void initialize(linkedlist *ll) { ll->head = NULL; ll->tail = NULL; ll->nodeCount = 0; } Head NULL nodeCount 0 Tail NULL ll

else { person *p; p = (person*) malloc(sizeof(person)); getData(p); p->Next_Person = ll->head ; ll->head = p; ll->nodeCount++; //increment counter } void InsertFront(linkedlist *ll) { if (ll->head == NULL && ll->nodeCount == 0) //means empty list { person *p; p = (person*) malloc(sizeof(person)); getData(p); ll->head = p; ll->tail = p; ll->nodeCount++;}

void getData(person *p) { cin >> p->name ; cin >> p->age ; p->Next_Person = NULL; //just to initialize }

headsize tail 0.. Build the new node, and put the reference to the new data item in it.

headsize tail 1 Make both head and tail point at the new node, and increment the list’s size.

. headsize tail 3 Build the new node, and put the reference to the new data item in it.

. headsize tail 3 Make the new node point at the current front node

. headsize tail 4 Reset head so that it points at the new node, and increment size of the linked list

void InsertRear(linkedlist *ll) { if (ll->head == NULL && ll- >nodeCount == 0) //means empty list { person *p; p = (person*) malloc(sizeof(person)); getData(p); ll->head = p; ll->tail = p; ll->nodeCount++; }else{ person *p; p = (person*) malloc(sizeof(person)); getData(p); p->Next_Person = NULL; //rear insertion... hence points to NULL. ll->tail->Next_Person = p; //now point tail of second last element to last ll->tail = p;//yes tail is now the new element inserted ll->nodeCount++; //increment counter }}

. headsize tail 3 Build the new node, and put the reference to the new data item in it. null reference

headsize tail 3 Make the current tail node point to the new node.

headsize tail 4 Reset tail so that it points to the new node, and increment the list size.

void RemoveFront(linkedlist *ll) { if (ll->nodeCount > 0 ) //means not empty list { person *temp; temp = ll->head ; ll->head =ll->head->Next_Person if (ll->head==NULL) //if list has only single item ll->tail =NULL ll->tail =NULL delete temp; ll->nodeCount --; }}

headsize tail 4 Save the value of head, so that we will be able to retrieve the data item to be deleted.

headsize tail 4 Replace the value in head with a reference to the second node on the list.

headsize tail 4 Save a reference to the data item in the first node.

headsize tail 3 Get rid of the (original) first node, decrement the size of the list, and return the reference to the removed item.

headsize tail 1 Save a reference to the node, and a reference to the data item found in the first node.

headsize tail 0 Set head and tail to NULL, and decrement the size of the list...

headsize tail 0 Get rid of the node, and return the reference to the data item...

void DeleteRear(linkedlist *ll) { person *tempNext; person *tempPrevious; tempNext = ll->head ; if (ll->nodeCount > 0 ) {if (ll->nodeCount ==1){ only single item in the linkedlist ll->head = NULL; ll->tail=NULL; } else{//more than one items in the linked list while (tempNext->Next_Person != NULL) { tempPrevious = tempNext; tempNext = tempNext->Next_Person; } ll->tail=tempPrevious; tempPrevious->Next_Person = NULL; } delete tempNext; ll->nodeCount --; }}

headsize tail 4 Save the value of tail, so that we will be able to retrieve the data item to be deleted.

headsize tail 4 Beginning at head, traverse the list until we reach the node that points at the tail node. 1 23

headsize tail 3 Set the link reference in this node to NULL, make tail point at this node, and decrement the list size..

headsize tail 3 Save a reference to the removed data object, get rid of the removed node object, and return the reference to the data object..

 Algorithms that traverse a list start at the first node and examine each node in succession until the last node has been processed.  Traversal logic is used by several different types of algorithms, such as changing a value in each node, printing the list, summing a field in the list, or calculating the average of a field. Etc etc etc.. ( so many requirements)  Any application that requires that the entire list be processed uses a traversal.

 To traverse the list we need a walking (navigator) pointer.  This pointer is used to move from node to node as each element is processed.  Example is our print function….

 A search list is used by several algorithms to locate data in a list:  To insert data, we need to know the logical predecessor to the new data.  To delete data, we need to find the node to be deleted and identify its logical predecessor.  To retrieve data from a list, we need to search the list and find the data.  Worst case is that the data to be retrieved is at the last of the list.  E.g Find the employee who’s name is “Salman”, if salman is present then the record (node) having salman will be returned else return null node.

 First let us consider searching a list in which the elements are sorted based on a field value.  Given a target key, the ordered list search attempts to locate the requested node in the linked list.  If a node in the list matches the target, the search returns true, else we return false. True can also be returning the specific node  E.g Find the employee who’s name is “Salman”, It will be faster if the list was in alphabetical order