DS LESSON 2 - Removed
DS LESSON 2 - Removed
LISTS
LINKED LIST:
A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations. The elements in a linked list are linked using pointers as shown in the below image
In simple words, a linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the 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.
✓ Insertion at the start Insertion of a new node at the start of a singly linked list is carried out in the
following manner.
• Make the new node point to HEAD.
• Make the HEAD point to the new node.
✓Insertion after some Node Insertion of a new node after some node in a singly linked list is carried out
in the following manner,
• Reach the desired node after which the new node is to be inserted.
• Make the new node point to the next element of the current node.
• Make the current node point to the new node. Inserting a new node after some node is an O(N)
operation
Insertion at the end Insertion of a new node at the end of a singly linked list isperformed in te following
way,
• Taverse the list from start and reach the last node.
• Make the last node point to the new node.
• Make the new node point to null, marking the end of the list. Inserting a new node at the end is an O(N)
operation.
2.Deletion:
Deletion at last
The deletion of the last node is performed in the following manner,
3.Display
To display the entire singly linked list, we need to traverse it from first to last.
In contrast to arrays, linked list nodes cannot be accessed randomly. Hence to reach the n-th element,
we are bound to traverse through all (n-1) elements.
Since the entire linked list is traversed, the operation costs us O(N) time complexity. The following JAVA
snippet shows how the entire singly linked list can be displayed
4. Search
To search an element in the singly linked list, we need to traverse the linked list right from the start.
At each node, we perform a lookup to determine if the target has been found, if yes, then we return the
target node else we move to the next element.
In the worst case, we could end up visiting all the nodes in the list and hence searching an element in the
singly linked list cost us O(N) operational time.
PROGRAM:
#include<stdio.h>
CIRCULAR LINKED LIST:
The circular linked list is a linked list where all nodes are connected to form a circle. In a circular linked
list, the first node and the last node are connected to each other which forms a circle. There is no NULL
at the end.
1 . Circular singly linked list: In a circular Singly linked list, the last node of the list contains a pointer to
the first node of the list. We traverse the circular singly linked list until we reach the same node where we
started. The circular singly linked list has no beginning or end. No null value is present in the next part of
any of the nodes.
2 . Circular Doubly linked list: Circular Doubly Linked List has properties of both doubly linked list and
circular linked list in which two consecutive elements are linked or connected by the previous and next
pointer and the last node points to the first node by the next pointer and also the first node points to the
last node by the previous pointer.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
In a multi-linked list, each node can have N number of pointers to other nodes. A multi-linked list is
generally used to organize multiple orders of one set of elements.
The structure of a multi-linked list depends on the structure of a node. A single node generally contains
two things:
1. A list of pointers
2. All the relevant data.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct scorel