Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists.
Linked Lists Linked Lists Representation Traversing a Linked List
CHP-5 LinkedList.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
M180: Data Structures & Algorithms in Java
Lecture - 9 On Queues. Prepared by, Jesmin Akhter, Lecturer, IIT,JU QUEUES A Queue is a linear list of elements in which deletions can take place only.
Data Structures: A Pseudocode Approach with C
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
Data Strcutures.
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.
Lecture -3 on Data structures Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Array Data structures are classified as either linear or nonlinear.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
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,
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Data Structure & Algorithms
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
LIST Unsorted 1.Set PTR := START 2.Repeat Step 3 while PTR=NULL 3.If ITEM = INFO[PTR], then : Set LOC := PTR and Exit else Set PTR:=LINK[PTR] [PTR points.
1 Linked list. 1 A linked list, or one-way list, is a linear collection of data elements, called nodes Each node is divided into two parts: * first part.
STACKS & QUEUES for CLASS XII ( C++).
Lecture 6 of Computer Science II
COSC160: Data Structures Linked Lists
Chapter 4 Linked Structures.
Data Structures Using C, 2e
Dynamic Allocation Review Structure and list processing
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Linked Lists Chapter 6 Section 6.4 – 6.6
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
Data Structure Interview Question and Answers
Review Deleting an Element from a Linked List Deletion involves:
Lecture - 6 On Data Structures
UNIT-3 LINKED LIST.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Linked-list.
Linked lists.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Data Structures Interview / VIVA Questions and Answers
Linked Lists A linked list or one way list is a linear collection of data elements called nodes where the order is given by means of pointers It is divided.
QUEUE.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists.
DATA STRUCTURE QUEUE.
Arrays and Linked Lists
Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
LINKED LIST.
Chapter 17: Linked Lists.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Data Structures & Algorithms
LINKED LIST Dr. T. Kokilavani Assistant Professor
Linked lists.
LINEAR DATA STRUCTURES
Presentation transcript:

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Linked List outline Why linked lists Linked lists Representation of Linked lists in memory Traversing a linked lists Memory allocation: Garbage collection Overflow and Underflow Basic operations of linked lists Insert, find, delete, print, etc. Variations of linked lists Circular linked lists Doubly linked lists Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Lecture – 5 on Data Structure Linked Lists

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Why linked lists Disadvantages of arrays as storage data structures: slow insertion in ordered array Fixed size Linked lists solve some of these problems Linked lists are general purpose storage data structures. Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Linked lists A linked list, or one way list, is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. Each node is divided into two parts: The first part contains the information of the element, and The second part, called the link field or next pointer field, contains the address of the next node in the list. The pointer of the last node contains a special value,called the null pointer. A pointer variable – called START which contains the address of the first node. A special case is the list that has no nodes, such a list is called the null list or empty list and is denoted by the null pointer in the variable START. Linked list with 3 nodes Start Data Next node node A data pointer Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Prepared by, Jesmin Akhter, Lecturer, IIT, JU linked lists A linked list organizes a collection of data items (elements ) such that elements can easily be added to and deleted from any position in the list. Only references To next elements are updated in insertion and deletion operations. There is no need to copy or move large blocks of data to facilitate insertion and deletion of elements. Lists grow dynamically. Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Representation of Linked lists in memory INFO LINK 1 2 3 4 5 6 7 8 67 45 80 75 90 5 2 7 4 START START=3, INFO[3]=45 LINK[3]=2, INFO[2]=67 LINK[2]=5, INFO[5]=75 LINK[5]=4, INFO[4]=80 LINK[4]=7, INFO[7]=90 LINK[7]=0, NULL value, So the list has ended 3 Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Traversing a linked lists LIST be a linked list in memory stored in linear arrays INFO and LINK with START pointing to the first element and NULL indicating the end of LIST. We want to traverse LIST in order to process each node exactly once. Pointer variable PTR points to the node that is currently being processed. LINK[PTR] points to the next node to be processed. Thus update PTR by the assignment PTR : =LINK[PTR] START X INFO LINK PTR Fig : PTR : = LINK[PTR] Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Traversing a linked lists For printing the information at each node of a linked list, must traverse the list. Algorithm 5.1 : PRINT(INFO, LINK, START) Algorithm Prints the information at each node of the list. Set PTR : =START. Repeat steps 3 and 4 while PTR : ≠ NULL: Write : INFO[PTR]. Set PTR : =LINK[PTR]. Exit. Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Traversing a linked lists For Finding the number NUM of elements in a linked list, must traverse the list. Algorithm 5.1 : COUNT(INFO, LINK, START, NUM) Set NUM: =0. . Set PTR : =START. Repeat steps 4 and 5 while PTR : ≠ NULL: Set NUM : =NUM+1. Set PTR : =LINK[PTR]. Exit. Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Memory allocation: Garbage collection Memory space can be reused if a node is deleted from a list i.e deleted node can be made available for future use The operating system of a computer may periodically collect all the deleted space on to the free storage list. Any technique which does this collection called garbage collection Garbage (Deleted Space) Computer programs Periodic Collector … Avail List (Free space) Takes space avail list Sent to avail list Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Overflow and Underflow Sometimes data are inserted into a data structure but there is no available space. This situation is called overflow Example: In linked list overflow occurs when AVAIL= NULL and There is an insertion operation Underflow: Situation: Want to delete data from data structure that is empty. START = NULL and There is an deletion operation Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Insertion into a linked list Node N is to be inserted in to the list between nodes A and B Three pointer fields are changed as follows: The next pointer field of node A now points to the new node N, to which AVAIL previously pointed. AVAIL now point to the second node in the free pool, to which node N previously pointed. The next pointer field of node N now points to node B, to which node A previously pointed. START Node A Node B (a) Before insertion Node N (a) After insertion AVAIL Free storage list Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Inserting a new node Possible cases of Insert Node Insert into an empty list Insert in front Insert at back Insert in middle But, in fact, only need to handle two cases: Insert as the first node (Case 1 and Case 2) Insert in the middle or at the end of the list (Case 3 and Case 4) Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Check for available memory Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit Set NEW= AVAIL and AVAIL=LINK[AVAIL] Set INFO[NEW]= ITEM IF LOC = NULL then [Insert as first Node] Set LINK[NEW]= START and START=NEW. Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW Exit. Check for available memory Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit Set NEW= AVAIL and AVAIL=LINK[AVAIL] Set INFO[NEW]= ITEM IF LOC = NULL then [Insert as first Node] Set LINK[]= START and START=NEW. Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW Exit. Create a new node Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit Set NEW= AVAIL and AVAIL=LINK[AVAIL] Set INFO[NEW]= ITEM IF LOC = NULL then [Insert as first Node] Set LINK[NEW]= START and START=NEW. Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW Exit. Insert as first element START NEW Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit Set NEW= AVAIL and AVAIL=LINK[AVAIL] Set INFO[NEW]= ITEM IF LOC = NULL then [Insert as first Node] Set LINK[NEW]= START and START=NEW. Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW Exit. Insert after currNode LOC NEW Prepared by, Jesmin Akhter, Lecturer, IIT, JU