Linked-list.

Slides:



Advertisements
Similar presentations
EENG212 Algorithms and Data Structures
Advertisements

Linear Lists – Linked List Representation
Linked Lists Linked Lists Representation Traversing a Linked List
Data Structures Using C++
CHP-5 LinkedList.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
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.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India.
Data Strcutures.
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.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
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,
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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:
Link List Submitted by Submitted to Mukesh (5765) Er.Dheeraj Maam Vijender(5755) Lect. In Comp. sc. BCA 2 nd Year.
Part 2. Deletion from a linked list Let LIST be a linked list with a node N between nodes A and B. suppose node N is to be deleted from the linked list.
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.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
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.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Doubly / Two Way Linked List It is linear collection of data elements, called nodes, where each node N is divided into three parts: – An information field.
Part 2. Deletion from a linked list Let LIST be a linked list with a node N between nodes A and B. suppose node N is to be deleted from the linked list.
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.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Data Structures Using C, 2e
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Linked List :: Basic Concepts
5.13 Recursion Recursive functions Functions that call themselves
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Review Deleting an Element from a Linked List Deletion involves:
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Lecture - 6 On Data Structures
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
Linked lists.
EEL 4854 IT Data Structures Linked Lists
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.
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.
Chapter 4 Linked Lists
Algorithms and Data Structures
LINKED LISTS CSCD Linked Lists.
INSERTION INTO A LINEAR ARRAY Set J = N Repeat step 3 and 4 while J>= K Set LA[ J+1] = LA [ J ] Set J = J-1 Set LA [K] = ITEM Set N = N+1 Exit.
Chapter 4 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.
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
Linked lists.
Chapter 9 Linked Lists.
LINEAR DATA STRUCTURES
Presentation transcript:

Linked-list

Introduction Linked list Use a linked list instead of an array when Linear collection of self-referential class objects, called nodes Connected by pointer links Accessed via a pointer to the first node of the list Link pointer in the last node is set to null to mark the list’s end Use a linked list instead of an array when You have an unpredictable number of data elements You want to insert and delete quickly.

Linked-list

Traversing linked-list Set Ptr := Start [Initializes pointer Ptr] Repeat step 3 and 4 while Ptr != NULL Apply Process to Info[Ptr] Set Ptr := Link[Ptr] [Ptr now points to next node] [End of step 2 loop] 5. Exit

Searching a linked-list Set Ptr := Start [Initializes pointer Ptr] Repeat step 3 while Ptr != NULL If ITEM = Info[Ptr], then Set Loc := Ptr and Exit Else Set Ptr := Link[Ptr] [Ptr now points to next node] [End of If structure] [End of step 2 loop] [Search is unsuccessful] Set Loc := NULL Exit

Adding an Element to the front of a Linked List

Adding an Element to the front of a Linked List

Adding an Element to the front of a Linked List

Adding an Element to the front of a Linked List

Adding an Element to the front of a Linked List

Adding an Element to the front of a Linked List

Insert at beginning inFirst(info, link, Start, avail, item) This algo. Inserts item as first node in list. [Overflow?] If Avail = NULL, then Write: Overflow and Exit [Remove first node from avail list] Set New := Avail and Avail := Link[Avail] Set Info[New] := item [copies new data into new node] Set Link[New] := Start [new node points to original first node] Set Start := New [changes Start so it points to new node] Exit

Inserting after a given node insLOC(info, link, Start, avail, loc, item) This algo. inserts ITEM so that ITEM follows node with location Loc or inserts ITEM as first node when Loc=NULL [Overflow?] If Avail := NULL, then Write: Overflow and Exit [Remove first node from avail list] Set New := Avail and Avail := Link[Avail] Set Info[New] := ITEM [copies new data into new node] If Loc = NULL, then [Insert as first node when list is empty] Set Link[New] := Start and Start := New Else [insert after node with location Loc] Set Link[New] := Link[Loc] and Link[Loc] := New [End of If structure] 5. Exit.

Removing an Element from the front of a Linked List

Removing an Element from the front of a Linked List

Removing an Element from the front of a Linked List

Removing an Element from the front of a Linked List

Removing an Element from the front of a Linked List

Removing an Element from the front of a Linked List

Delete at beginning Delete() If Start = NULL, then Write: list is empty and Exit Set item = info[Start] Set Loc := Start 3. Set Start := Link[Start] [Deletes 1st node] 4. [Return deleted node to Avail list] Set Link[Loc] := Avail and Avail := Loc 5. Exit

Deleting an item x from a list pointed to by Loc LocP Loc LocP

Delete a node at given location/position Del(info, link, Start, avail, loc, locP) This algo deletes node N with location Loc. LocP is location of node which precedes N or when N is first node then LocP = NULL If LocP = NULL then, Set Start := Link[Start] [deletes 1st node] Else Set Link[LocP] := Link[Loc] [deletes node N] [Return deleted node to avail list] Set Link[Loc] := Avail and Avail := Loc 4. Exit.

Deleting an item from list FindB() If Start = NULL, then [List empty?] Set Loc := NULL and LocP := NULL and Return [End of If structure] If Info[Start] = ITEM, then [ITEM in first node?] Set Loc := Start and LocP := NULL and Return Set PtrP := Start and Ptr := Link[Start] [Initializes pointer] Repeat steps 5 and 6 while Ptr != NULL If Info[Ptr] = ITEM, then Set Loc := Ptr and LocP := PtrP and Return Set PrtP := Ptr and Ptr := Link[Ptr] [Updates pointers] [End of step 4 loop] Set Loc := NULL [Search unsuccessful] Return

Deleting an item from list Delete() Call FindB() If Loc = NULL, then Write: ITEM not in list and Exit [Delete node] If LocP = NULL, then Set Start := Link[Start] [Deletes 1st node] Else Set Link[LocP] := Link[Loc] [End of If Structure] [Return deleted node to Avail list] Set Link[Loc] := Avail and Avail := Loc 5. Exit

Header Linked Lists Header linked list is a linked list which always contains a special node called the Header Node, at the beginning of the list. It has two types: a) Grounded Header List Last Node Contains the NULL Pointer b) Circular Header List Last Node Points Back to the Header Node Header linked list Grounded Header linked list Circular Header linked list

Grounded Header Link List A grounded header list is a header list where the last node contains the null pointer. The term “grounded” comes from the fact that many texts use the electrical ground symbol to indicate the null pointer. Start Header Node Figure: Grounded Header Link List

Circular Header Linked List A circular header Link list is a header list where the last node points back to the header node. The chains do not indicate the last node and first node of the link list. In this case, external pointers provide a frame reference because last node of a circular link list does not contain null pointer. Start Header Node Figure: Circular Linked List with header node

Circular Linked Lists In a circular linked list there are two methods to know if a node is the first node or not. Either a external pointer, list, points the first node or A header node is placed as the first node of the circular list. The header node can be separated from the others by either heaving a sentinel value as the info part or having a dedicated flag variable to specify if the node is a header node or not.

Circular Linked Lists In linear linked lists if a list is traversed (all the elements visited) an external pointer to the list must be preserved in order to be able to reference the list again. Circular linked lists can be used to help the traverse the same list again and again if needed. A circular list is very similar to the linear list where in the circular list the pointer of the last node points not NULL but the first node.

Traverse Circular list Set Ptr := Link[Start] [initializes pointer Ptr] Repeat steps 3 & 4 while Ptr != Start Apply Process to Info[Ptr] Set Ptr := Link[Ptr] [Ptr now points to next node] [End of Step 2 loop] 5. Exit.

Two-way lists A two-way list is a linear collection of data elements, called nodes, where each node N is divided into three parts: Information field Forward Link which points to the next node Backward Link which points to the previous node The starting address or the address of first node is stored in START / FIRST pointer . Another pointer can be used to traverse list from end. This pointer is called END or LAST. 10 70 20 55 40 Head Cur Cur->next Cur->prev

Two-way lists(cont…) Every node (except the last node) contains the address of the next node, and every node (except the first node) contains the address of the previous node. A two-way list (doubly linked list) can be traversed in either direction.

Representations of Two-way lists X X 4 2 10 Start Last BACK Pointer FORE Pointer INFO Field

Search in circular list SRCHHL(info, link, Start, item, loc) list is circular header list in memory. This algo. Finds location of loc of node where item first appers in list or sets loc=null Set ptr = link[start] Repeat while info[ptr] != item and ptr != Start Set ptr = link[ptr] //[ptr now points to next node] If info[ptr] = item, then Set loc = ptr else Set loc = null 4. Exit

Deletion in 2-way list DelTwl(info, forw, back, start, avail, loc) [Delete node] Set forw[ back[loc] ] = forw[loc] and back[ forw[loc] ] = back[loc] [Return node to avail list] Set forw[loc] = avail and avail = loc 3. Exit 10 20 40 55 70 Head loc

Insertion in 2-way list INSTwl(info, forw, back, start, avail, locA, locB, item) [Overflow?] If avail = null, then Write: Overflow and Exit [Remove node from avail list and copy new data into node] Set new = avail, avail = forw[avail], info[new] = item [Insert node into list] Set forw[locA] = new, forw[new] = locB back[locB] = new, back[new] = locA 4. Exit. 10 20 55 70 40 Head LocA LocB New