Docsity

Linked list and it's summary, Summaries of Engineering

Partial preview of the text

Download Linked list and it's summary and more Summaries Engineering in PDF only on Docsity!

LINKED LIST

Linked List

  • A linked list is a collection of elements called nodes , where each node is connected to the next node using a pointer.
  • Nodes do not need to be stored in consecutive memory locations.
  • Each node contains:
    • Data field
    • Link field / Pointer field
  • The link field stores the address of the next node.
  • Linked lists are useful when the number of elements is unknown or changes dynamically.

Why Linked Lists?

  • Consider a situation where we need to store a list whose size is not known in advance.
  • With arrays:
  • We may allocate a large array → memory wastage.
  • We may allocate a small array → insufficient space.
  • Insertion/deletion may require movement of elements.
  • Therefore, an alternative representation is required. Solution: Linked List
  • A linked list allows elements to be stored anywhere in memory and connects them using links.

Structure of a Node

A Node in a linked list holds the data value and a pointer to the location of the next node in the linked list. Each node of a linked list has at least two fields:

1. Data Field

  • Stores the actual value/data. 2. Link Field
  • Stores the address of the next node.

How Linked Lists are Stored in Memory

Unlike arrays, linked-list nodes do not need contiguous memory locations. For example: Memory Location 1000 → [10 | 2500] 2500 → [20 | 1800] 1800 → [30 | NULL]

  • First node is located at address 1000.
  • It stores the address 2500 of the second node.
  • Second node stores the address 1800 of the third node.
  • The last node contains NULL. Thus, the link field connects the nodes.

Array vs Linked List

Array Linked List Elements stored contiguously Nodes can be anywhere in memory Fixed size Dynamic size Insertion/deletion can require shifting Insertion/deletion can be done by changing links May waste memory Memory allocated as required Direct/random access possible Sequential access Does not require pointer field Requires pointer/link field

Advantages of Linked Lists

  • Linked lists provide several benefits:
  • Dynamic size – size can grow or shrink during execution.
  • Efficient insertion and deletion – no shifting of elements is normally required.
  • Better memory utilization – memory is allocated as needed.
  • Nodes can be stored at different memory locations.
  • Useful for implementing:
    • Stacks
    • Queues
    • Trees
    • Graphs
    • Other dynamic data structures.

CREATION of FIRST NODE Step 1: SET NEW_NODE = new Node(VAL) Step 2: SET NEW_NODE → DATA = VAL Step 3: SET NEW_NODE → NEXT = NULL Step 4: SET HEAD = NEW_NODE Step 5: SET TAIL = NEW_NODE Step 6: EXIT

Insertion at the End Step 1: SET NEW_NODE = new Node(VAL) Step 2: SET TAIL → NEXT = NEW_NODE Step 3: SET TAIL = NEW_NODE Step 4: EXIT

Insertion at the Specific Position Step 1: SET NEW_NODE = new Node(VAL) Step 2: IF POSITION = 1 (position for node to be inserted) SET NEW_NODE → NEXT = HEAD SET HEAD = NEW_NODE Go to Step 9 Step 3: SET TEMP = HEAD Step 4: SET CNT = 1 Step 5: Repeat while CNT < POSITION - 1 SET TEMP = TEMP → NEXT SET CNT = CNT + 1 Step 6: IF TEMP → NEXT = NULL SET TAIL → NEXT = NEW_NODE SET TAIL = NEW_NODE Go to Step 9 Step 7: SET NEW_NODE → NEXT = TEMP → NEXT Step 8: SET TEMP → NEXT = NEW_NODE Step 9: EXIT

Deletion of

Node

Step 1: IF POSITION = 1 SET TEMP = HEAD SET HEAD = HEAD → NEXT SET TEMP → NEXT = NULL DELETE TEMP Go to Step 8 Step 2: SET CURR = HEAD Step 3: SET PREV = NULL Step 4: SET CNT = 1 Repeat while CNT < POSITION SET PREV = CURR SET CURR = CURR → NEXT SET CNT = CNT + 1 Step 5: SET PREV → NEXT = CURR → NEXT Step 6: SET CURR → NEXT = NULL Step 7: DELETE CURR Step 8: IF PREV → NEXT == NULL SET TAIL = PREV Step 9: EXIT

SEARCHING IN LINKED LIST Step 1: SET TEMP = HEAD Step 2: SET POS = 1 Step 3: Repeat while TEMP ≠ NULL IF TEMP → DATA == VAL Write ELEMENT FOUND Write POS Go to Step 4 SET TEMP = TEMP → NEXT SET POS = POS + 1 Step 4: EXIT

Insertion in STACK using Linked List Step 1: SET NEW_NODE = new Node(VAL) Step 2: SET NEW_NODE → NEXT = TOP Step 3: SET TOP = NEW_NODE Step 4: INCREMENT SIZE Step 5: EXIT

Deletion in

STACK

using Linked

List

Step 1: IF TOP = NULL DISPLAY "Stack is empty" and EXIT Step 2: SET TEMP = TOP Step 3: SET TOP = TOP → NEXT Step 4: DELETE TEMP Step 5: DECREMENT SIZE Step 6: EXIT