0% found this document useful (0 votes)
32 views

linkedlist

Uploaded by

f.t.tasnoo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

linkedlist

Uploaded by

f.t.tasnoo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Linked list

Fariha Tasneem
Roll:BBH2211023F
• Linked list is a linear data structure.

• In which elements are not stored at a contiguous location, rather


they are linked using pointers.

• Linked List forms a series of connected nodes, where each node


stores the data and the address of the next node.
Node Structure: A node in a linked list typically consists of two
components:

Data: It holds the actual value or data associated with the node.

Next Pointer: It stores the memory address (reference) of the next node
in the sequence.

Head and Tail: The linked list is accessed through the head node, which
points to the first node in the list. The last node in the list points to NULL
or nullptr, indicating the end of the list. This node is known as the tail
node
Why linked list data structure needed?

Dynamic Data structure: The size of memory can be allocated


or de-allocated at run time based on the operation insertion or deletion
Ease of Insertion/Deletion: The insertion and deletion of elements are
simpler than arrays since no elements need to be shifted after insertion and
deletion, Just the address needed to be updated
Efficient Memory Utilization: As we know Linked List is a dynamic data
structure the size increases or decreases as per the requirement so this
avoids the wastage of memory.
Implementation: Various advanced data structures can be
implemented using a linked list like a stack, queue, graph, hash maps, etc.
Example:

In a system, if we maintain a sorted list of IDs in an array


id[] = [1000, 1010, 1050, 2000, 2040].

If we want to insert a new ID 1005, then to maintain the sorted order, we


have to move all the elements after 1000 (excluding 1000).

Deletion is also expensive with arrays until unless some special techniques
are used. For example, to delete 1010 in id[], everything after 1010 has to be
moved due to this so much work is being done which affects the efficiency of
the code.
There are mainly three types of linked lists

Single- Double Circular


linked list linked list linked list
Single-linked list:

In a singly linked list, each node contains a reference to the next node in the sequence.
Traversing a singly linked list is done in a forward direction.
Double-linked list:
In a doubly linked list, each node contains references to both the next
and previous nodes. This allows for traversal in both forward and
backward directions, but it requires additional memory for the
backward reference.
Circular linked list:
In a circular linked list, the last node points back to the head node,
creating a circular structure. It can be either singly or doubly linked.
Create a single line linked list:
algorithm
1.Create a class Node which has two attributes: data and next. Next is a pointer to the
next node.
2.Create another class which has two attributes: head and tail.
3.addNode() will add a new node to the list:
a)Create a new node.
b) It first checks, whether the head is equal to null which means the list is empty.
c)If the list is empty, both head and tail will point to the newly added node.
d)If the list is not empty, the new node will be added to end of the list such that tail’s
next will point to the newly added node. This new node will become the new tail of
the list.
4)display() will display the nodes present in the list:
a)Define a node current which initially points to the head of the list.
b)Traverse through the list till current points to null.
c)Display each node by making current to point to node next to it in each iteration.
linked list operation
Traversal - access each element of the linked list

Insertion - adds a new element to the linked list

Deletion - removes the existing elements

Search - find a node in the linked listSort - sort the nodes


of the linked list
Insertion operation
Adding a new node in linked list is a more than one step activity. We shall learn
this with diagrams here. First, create a node using the same structure and find
the location where it has to be inserted.

Imagine that we are inserting a node B (NewNode), between


A (LeftNode) and C (RightNode). Then point B.next to C −
it should look like this

now the next node should point to the right node


This will put the new node in the middle of the two. The new list
should look like this −
Algorithm
Step 1: IF PTR = NULL
Write OVERFLOW Go to Step 7
[END OF IF
Step2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR → NEXT
Step 4: SET NEW_NODE → DATA = VAL
Step 5: SET NEW_NODE → NEXT = HEAD
Step 6: SET HEAD = NEW_NODE
Step 7: EXIT
Deletion in single linked list
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8 [END OF IF
]Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!= NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT[END OF LOOP]ADVERTISEMENT
Step 6: SET PREPTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
Traversal of Singly Linked List
Algorithm
• First, we will initialize a pointer to the head node of the singly linked
list.
• Second, we will check that the pointer is null or not null, if it is null
then return.
• While the pointer is not null, we will access the data or element
present on that node and store it in some variable to print side by side,
then we move the pointer to next node.
• we will keep on checking the pointer is null or not null, until it reaches
to the end of the list.
Aplication of linked list

• Polynomial Manipulation representation


• Addition of long positive integers
• Representation of sparse matrices
• Addition of long positive integers
• Symbol table creation
• Mailing list
• Memory management
• Linked allocation of files
• Multiple precision arithmetic etc
Thank you

You might also like