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

Linked List

The document discusses different types of linked lists including singly linked lists. It describes the basic concepts and operations involved with linked lists such as insertion, deletion and traversal. Linked lists allow dynamic memory allocation and easy insertion/deletion unlike arrays but have disadvantages related to random access and extra memory for pointers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Linked List

The document discusses different types of linked lists including singly linked lists. It describes the basic concepts and operations involved with linked lists such as insertion, deletion and traversal. Linked lists allow dynamic memory allocation and easy insertion/deletion unlike arrays but have disadvantages related to random access and extra memory for pointers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

UNIT 1

INTRODUCTION
 INTRODUCTION TO DATA STRUCTURES:
 Concept of Data:
A data structure is a specialized format for organizing, processing, retrieving and
storing data.. There are several basic and advanced types of data structures, all designe
to arrange data to suit a specific purpose. Data structures make it easy for users to acce
and work with the data they need in appropriate ways.
ways

 Data Object:
A data object is a region of storage that contains a value or group of values.
values Each
value can be accessed using its identifier or a more complex expression that refers to th
object. In addition, each object has a unique data type.
 Data structure:
A data structure is a specialized format for organizing, processing, retrieving and
storing data. There are several basic and advanced types of data structures, all
designed to arrange data to suit a specific purpose. Data structures make it easy for
users to access and work with the data they need in appropriate ways. Most
importantly, data structures frame the organization of information so that machines
and humans can better understand it.
 Classification of data Structure
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.
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list
elements are not stored at a contiguous location; the elements are linked using
pointers. They include a series of connected nodes. Here, each node stores the
data and the address of the next node.
Why Linked List?
 Arrays can be used to store linear data of similar types, but arrays have the
following limitations:

 The size of the arrays is fixed:: So we must know the upper limit on the number of
elements in advance. Also, generally, the allocated memory is equal to the
upper limit irrespective of the usage.

 Insertion of a new element / Deletion of a existing element in an array of


elements is expensive: The room has to be created for the new elements and to
create room existing elements have to be shifted but in Linked list if we have the
head node then we can traverse to any node through it and insert new node at
the required position.
Advantages of Linked Lists over arrays:

 Dynamic Array.
 Ease of Insertion/Deletion.
Drawbacks of Linked Lists:
 Random access is not allowed. We have to access elements sequentially
starting from the first node(head node).

 Extra memory space for a pointer is required with each element of the list.

 Not cache friendly. Since array elements are contiguous locations, there is
locality of reference which is not there in case of linked lists.
Types of Linked Lists:
 Simple Linked List – In this type of linked list, one can move or traverse the
linked list in only one direction

 Doubly Linked List – In this type of linked list, one can move or traverse the
linked list in both directions (Forward and Backward)

 Circular Linked List – In this type of linked list, the last node of the linked list
contains the link of the first/head node of the linked list in its next pointer
and the first/head node contains the link of the last node of the linked list in
its prev pointer
What is a Singly Linked List?

A singly linked list is a unidirectional linked list. So, you can only traverse it in one
direction, i.e., from head node to tail node.
node

The way to represent a linear list to expand each node to contain a link or pointer to the
next node.This representation is called a one way chain or singly linked list.

In singly link list, we can tranverse only in one direction.

In this type of linked list two nodes are linked with each other in sequential linear
manner.
In the singly link list each node has two field one for data and other is for link
reference of the next node. The last node’s link field point to NULL.

The node structure is---


struct node
{
int data;
struct node *next;
}
Advantages of singly linked list:

 1 Accessibility of a node in the forword direction is easier.


2 Inserction and deletion of two node are easier.

Disadvantages of singly linked list:


1. Accessing the preceding node of a current node is not possible as there is no
backward traversal.
2. Accessing a node is time consuming.
Opretions on singly linked list:
 1. Create a Linked List:
 2. Insert a node:
 3. Delet a node:
 4. Display/Traversing a linked list:
 5. Searching in Linked List:
 6. Sort the element of Linked List:
 7. Merge the element of Linked List:
List
 8. Reverse a Linked List:
 9. Concatenation of Linked List:
 10. Length of Linked List:
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.
 Inserting a new node at the start is an O(1) operation.
operation

operations-on-singly-linked-list-1024x446.webp
void insertAtStart(Node newNode,
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
(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 followin 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.
Head

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;
}
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.


element

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

void deleteAtFirst(Node
(Node head)
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.
deleted

 Make the current node point to the next of next element.


element

 Deleting a node after a specific node is an O(N) operation.


void deleteAfterTarget(Node
(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;
}
Display/Traversing
Traversing:
 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;
}

}
CREATE A LINKED LIST:
 To Create a linked list, we have to create a node one by one and append
(insert at end) it to the list.
Initialize head = NULL;
 It is important to note that head a node,rather the pointer variable which
store address of the first node of the list.
 1.Create a new node: To create a node following steaps are required:
 Allocate memory for the new node.
 Store data in the new node’s data field.
 Set next field to NULL.

 2.Append: Add newly created node at the end of linked list.


Syntax:
 Case1: /*if empty list*/
head = NULL
Set head = new node

 Case2: /*if list is not empty */


Head != NULL
Set last-> next = new node
Last = newnode

You might also like