0% found this document useful (0 votes)
23 views6 pages

DS LESSON 2 - Removed

Uploaded by

mr.dhanush.j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

DS LESSON 2 - Removed

Uploaded by

mr.dhanush.j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT 2

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

Types of linked list,

1.singly linked list


2.doubly linked list
3.circular linked list
4. Circular doubly 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.

OPERATION ON SINGLE LINKED LIST:


1. Insertion:

Insertion in a singly linked list can be performed in the following ways,

✓ 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.

void insertAtStart(Node newNode, Node head){


newNode.data = 10;
newNode.next = head;
head.next = newNode;
}

✓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

void insertAfterTargetNode(Node newNode, Node head, int target){


newNode.data = 10;
Node temp = head;
while(temp.data != target){
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
}

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.

void insertAtEnd(Node newNode, Node head){


newNode.data = 10;
Node temp = head;
while(temp.next != null){
temp = temp.next;
}
temp.next = newNode;
newNode.next = null;
}

2.Deletion:

Deletion in a singly linked list can be performed in the following ways,

✓Deletion at the start


The first node of the singly linked list can be deleted as follows,

• Make the HEAD point to its next element.


• Deleting the first node of a singly linked list is an O(1) operation.

void deleteAtFirst(Node head){


head = head.next;
}

Deletion at the middle


The deletion after a specific node can be formed in the following way,

• Reach the desired node after which the node is to be deleted.


• Make the current node point to the next of next element.
•Deleting a node after a specific node is an O(N) operation.

void deleteAfterTarget(Node head, int target){


Node temp = head;
while(temp.data != target){
temp = temp.next;
}
temp.next= temp.next.next;
}

Deletion at last
The deletion of the last node is performed in the following manner,

• Reach the second last node of th singly linke list.


• Make the second last node point null.
• Deleting the last node is an O(N) operation.

void deleteLast(Node head){


Node temp = head;
while(temp.next.next != null){
temp = temp.next;
}
temp.next = null;
}

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

void display(Node head){


Node temp = head;
while(temp != null){
System.out.println(temp.data);
temp = temp.next;
}

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.

Node search(Node head, int target){


Node temp = head;
while(temp != null && temp.data != target){
temp = temp.next;
}
return temp;
}

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.

There are generally two types of circular linked lists:

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.

Properties of Multi-Linked List:

The properties of a multi-linked list are mentioned below.

• It is an integrated list of related structures.


• All the nodes are integrated using links of pointers.
• Linked nodes are connected with related data.
• Nodes contain pointers from one structure to the other.

Structure of Multi-linked list:

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.

typedef struct node {


int data;

vector<struct node*> pointers;


} Node;

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct scorel

You might also like