Docsity

Linked List: Data Structures and Algorithms, Lecture notes of Software Engineering

Partial preview of the text

Download Linked List: Data Structures and Algorithms and more Lecture notes Software Engineering in PDF only on Docsity!

Linked-list

Introduction

  • (^) Linked list
    • (^) 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.

Traversing linked-list

  1. Set Ptr = Start [Initializes pointer Ptr]
  2. Repeat step 3 and 4 while Ptr != NULL
  3. Apply Process to Ptrinfo
  4. Set Ptr = Ptrlink [Ptr now points to next

node]

[End of step 2 loop]

  1. Exit

Q.1 What is the time complexity of Traversing operation in single linked list? A.O(1) B.O(n^2) C.O(n^3) D.O(n)

Q.2 What is the time complexity of Searching operation in single linked list? A.O(1) B.O(n^2) C.O(n^3) D.O(n)

Q.3 What type of searching is

applicable on single linked list?

A.Binary

B.Linear

C.None

D.Both

Adding an Element to the front of a

Linked List

info next list info next info next 3 8 info next p (^) 6

info(p) = 6

null

Adding an Element to the front of a Linked List 5 info next info next info next 3 8 info next p 6 list next(p) = list null

Adding an Element to the front of a Linked List 5 info next info next info next 3 8 info next list (^) 6 null

Insert at beginning

inFirst(info, link, Start, avail, item)

This algo. Inserts item as first node in list.

1. [Overflow?] If Avail == NULL, then

Write: Overflow and Exit

2. [Remove first node from avail list]

Making new node from avail

Set New = Avail and Avail = Availlink [shifting privious link of avail to new

avail list]

3. Set Newinfo = item [copies new data into new node]

4. Set Newlink = Start [new node points to original first node]

5. Set Start = New [changes Start so it points to new node]

6. Exit

Inserting after a given node location 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

1. [Overflow?] If Avail == NULL, then Write: Overflow and Exit 2. [Remove first node from avail list] Set New = Avail and Avail = Availlink

  1. Set Newinfo = ITEM [copies new data into new node]
  2. If Loc == NULL, then [Insert as first node when list is empty] Set head =New Loc (add. Part of node) and Start = New Else [insert after node with location Loc] Set Loclink= New and Newlink =NextLoc [add. Of next node] [End of If structure]
  3. Exit.

Q.4 What is the time complexity of Insertion operation(at beg, at middle and at end) in single linked list? A.O(1) B.O(n^2) C.O(n^3) D.O(n)

Removing an Element from the front of a Linked List 5 info next info next info next 3 8 info next list (^) 6 null

Removing an Element from the front of a Linked List 5 info next info next info next 3 8 info next 6 list p p = list null