Sunteți pe pagina 1din 22

LINKED LIST

Created by:

Mehak
UID: 23837
Introduction
WHAT IS LINKED LIST?
– Linear collection of data elements, called nodes.
– Connected by links, called pointers.

Each node is divided into 2 parts:


• 1st part: information of the element.
• 2nd part: contains the address of next node in the list,
called link field or next pointer field.
– Address of 1st node of the list is stored in List Pointer
Variable-NAME or START.
– Pointer in the last node is set to null(0 or negative
number) 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 REPRESENTATION:

List with 6 nodes


Practical Example
TRAVERSING LINKED-LIST
• Consider linked list LIST, with two linear arrays INFO and LINK with
START pointing to the first node and NULL indicating end of the LIST.
• We want to traverse LIST so that every node is visited exactly once.
• Pointer PTR is used , which refers to every node which is being
traversed/processed.
• LINK[PTR]: points to the next node to be processed.
Thus, PTR = LINK[PTR]
depicted as:

PTR moves the pointer to the next node in the LIST.


Algorithm

Complexity: O(n)
Example
Algorithm to count the number NUM of elements in
the list.
MEMORY ALLOCATION

Suppose insertion-deletion is needed to be performed on the list. Now,


unused memory cells will be linked together to form a linked list using
AVAIL as its pointer variable. It is denoted by:
GARBAGE COLLECTION
INSERTION IN LINKED LIST
• Insertion at the beginning
• Insertion after the given node
Insertion at the beginning

**Complexity: O(1) because insertion is occurring at first place.


Example

Original lis
Inserting after a given node
Suppose an ITEM needs to be inserted at a given location LOC after
the node. If LOC=NULL, then insert the ITEM at the beginning of the
LIST.

Earlier Node A pointed to Node B; after inserting the Node N, Node A


points to Node N and Node N points to Node B.
Algorithm
INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)
This algorithm 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 = LINK[AVAIL]
3. Set INFO[NEW] = ITEM [copies new data into new node]
4. 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.

**Complexity: O(n)
DELETION IN LINKED LIST
• Deleting from the beginning
• Deleting from the given location
• Deleting node with given ITEM information
Deletion at the beginning of Linked List

Let’s say we want to delete the first node of the following


list.

That means, “start” should point to the 2nd node as:


But still, 1st node has reserved some memory. In order to delete it, 1st
node is set free and added to AVAIL list. So the new list will be:

Algorithm:
DELETE(START,ITEM,LINK,AVAIL,LOC)

1. If START = NULL, then Write: “list is empty” and Exit


2. Set ITEM = LINK[START]
3. Set LOC = START
4. Set START = LINK[START] [Deletes 1st node]
5. [Return deleted node to Avail list]
Set LINK[LOC] = AVAIL and AVAIL = LOC
6. Exit.

**Complexity: O(1)
Delete a node at given location

DEL(INFO, LINK, START, AVAIL, LOC, LOCP)


This algorithm deletes node N with location LOC. LOCP is location of node which
precedes N or when N is first node then LOCP = NULL

1. If LOCP = NULL then:


Set START = LINK[START] [deletes 1st node]
Else
Set LINK[LOCP] = LINK[LOC] [deletes node N]
2. [Return deleted node to avail list]
Set LINK[LOC] = AVAIL and AVAIL = LOC
3. Exit.
Deleting node with given ITEM information
Difference between Array and Linked List
• Array Size has to be defined and is limited to the defined size, while
linked list can grow to limit of memory.
• Continuous allocation is required for array, while this is not the
case with linked list.
• Arrays compute address of data elements by arithmetic operations,
while in Linked List addresses are in structure itself.
• Arrays can be accessed backward and forward but we have to use
special linked lists like doubly linked list for backward access.
• Arrays definition is part of the language construct but linked list we
have to create.
• Merging two arrays is very difficult while merging linked lists is
easy
• With large records, moving pointers is easier and faster in linked list
than moving the items themselves in arrays.

S-ar putea să vă placă și