Linked List
Linked List
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:
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.
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 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.
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 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
}
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.