Linked Lists Linked Lists Representation Traversing a Linked List

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

JAVA & Linked List Implementation
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Data Structures Using C++
CHP-5 LinkedList.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Data Structures: A Pseudocode Approach with C
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Data Structures & Algorithms
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Data Structures Using Java1 Chapter 4 Linked Lists.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Linked List by Chapter 5 Linked List by
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
Subject Name : Data Structure Using C Title : 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,
Ceng-112 Data Structures ISerap ATAY, Ph. D. 1 Chapter 3 – Part 2 Linear Lists.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
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?
 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.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Link List Submitted by Submitted to Mukesh (5765) Er.Dheeraj Maam Vijender(5755) Lect. In Comp. sc. BCA 2 nd Year.
Data Structure & Algorithms
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.
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.
  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
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.
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.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
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.
Linked-list.
Linked lists.
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.
LINKED LISTS CSCD 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.
Data Structures: Searching
Linked lists.
LINEAR DATA STRUCTURES
Presentation transcript:

Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists Two – Way Lists

Introduction It consists of a sequence of nodes, each containing arbitrary data field and one or two reference (links) pointing to the next and/or previous nodes. Linked List consist of nodes . A node, the building block of a linked list, contains two parts: – A data element representing the information in the current position of the list. – A pointer/link/address to the next node in the list • The START pointer points to the first node of the list. The last node of Linked List has its Link portion null indicating end of Linked List No node in Linked List has its data portion empty

Representation in Memory Introduction Cont…. Start Data Link 1 4 8 10 Representation in Memory It requires two linear arrays as INFO and LINK Info Link 1 2 3 4 5 6 5 Example 5.3, 5.4,5.5 4 H Start G A 2

Arrays and Linked Lists • An array is a list store in contiguous memory. – Any element of an array can be accessed quickly. – Insertion and deletion in the middle of an array requires the movement of many elements. – The size of an array is fixed. • A linked list is a list scattered throughout memory and connected with pointers/Links of next element. – The elements of a linked list must be accessed in order. Linear Access Mechanism – Insertion and deletion only requires re-assignment of a few pointers. – The length of the list can change at any time, making it a dynamic data structure.

Traversing a Linked List 1 4 8 10 Start Data Link PTR:= LINK [PTR] PTR 1. Set PTR := START 2. Repeat Steps 3-4 while PTR ≠ NULL 3. Apply process to INFO[PTR] 4. Set PTR:= LINK[PTR] [end of loop] 5. Exit

Searching in a Linked List LIST => Link List, LOC => ITEM Location or LOC => NULL Search (LINK, START, ITEM, LOC) Set PTR := START, LOC:= NULL Repeat Steps 3-4 while PTR ≠ NULL If INFO[PTR] = ITEM Set LOC:=PTR [end of If Structure] Set PTR:= LINK[PTR] [end of loop] Return LOC

Insertion in a Linked List 1. Create a new Node and store data in that node. 2. Find the correct position in the list. Assign the pointers to include the new node. Algorithm: InsertFirst(START, ITEM) Set New:= Create Node() Set INFO [New] := ITEM and LINK [New] := NULL Set START := New Exit

Insertion in a Linked List 1. Create a new Node and store data in that node. 2. Find the correct position in the list. Assign the pointers to include the new node. Algorithm: InsertAtLoc( START, LOC, ITEM) If LOC=NULL then InsertFirst (START, ITEM) Else Set New:= Create Node() Set INFO [New] := ITEM and LINK [New] := NULL Set LINK [NEW] := LINK [LOC] Set LINK [LOC] := New [End of if Structure] Exit

Insertion In Sorted Linked List Steps: Find the Location of the node Use Insertion method to insert into the Linked List Find the Location of the node: Search the Item after which insertion needs to be made. As the Linked List is sorted searching will result in the location of element => Given ITEM As Insertion can not be made before a Node therefore another pointer will keep track of the previous node i.e. before moving the PTR , Save := PTR PTR points to current node Element which is => ITEM while SAVE points to Element before PTR. Insertion will be made after SAVE

Algorithm: FindLoc (START,ITEM,LOC) If START = NULL, then Set LOC := NULL and Return Else if ITEM < INFO [START] [ITEM is not in List] [ End of if ] Set SAVE := START and PTR := LINK [ START ] Repeat Steps 3 - 4 while PTR ≠ NULL If ITEM < INFO [ PTR ] then Set LOC := SAVE and Return Else Set SAVE := PTR and PTR := LINK [ PTR ] [ End of Step 4 loop ] Set LOC := PTR and Return “Find Location in a Sorted Linked List”

Algorithm: FindAndInsert (START, ITEM) LOC:= FindLoc (START,ITEM,LOC) Call InsetAtLoc (Start, ITEM,LOC) Exit

Header Linked Lists A Header Linked List always Contains a Special Node called Header Node 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

Graphical Representations 4 8 10 Start Header Node Data Link Grounded Header Link List LINK [START] = NULL 4 8 10 Start Header Node Data Link Circular Header Link List LINK [START] = START

Header Linked Lists One way to simplify insertion and deletion is never to insert an item before the first or after the last item and never to delete the first node You can set a header node at the beginning of the list containing a value smaller than the smallest value in the data set You can set a trailer node at the end of the list containing a value larger than the largest value in the data set. These two nodes, header and trailer, serve merely to simplify the insertion and deletion algorithms and are not part of the actual list. The actual list is between these two nodes.

Doubly Linked Lists A Doubly Linked List is List in which every Node has a Next Pointer and a Back Pointer 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 Doubly Linked List can be Traversed in Either Direction

Definition and Graphical Representations The Node is Divided into 3 parts 1) Information Field 2) Forward Link which Points to the Next Node 3) 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 Doubly LL from End. This Pointer is Called END or LAST 4 X 2 10 Start Last INFO Field FORE Pointer BACK Pointer

Reading Assignments Complexity in Traversing a Linked List Complexity in Searching a Linked List Complexity in Sorted Linked List Garbage Collection in Linked List Overflow and Underflow in Linked Lists Complexities of Insertion in a Linked List