0% found this document useful (0 votes)
21 views14 pages

Linkedddd Listtt

A linked list is a data structure consisting of nodes that contain data and a reference to the next node, with the first node known as the head and the last as the tail. It supports various operations such as insertion, deletion, searching, and traversal, making it suitable for dynamic memory allocation and implementing other data structures like stacks and queues. While linked lists offer advantages like efficient insertions and deletions, they also have drawbacks including slow access times and higher memory overhead.

Uploaded by

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

Linkedddd Listtt

A linked list is a data structure consisting of nodes that contain data and a reference to the next node, with the first node known as the head and the last as the tail. It supports various operations such as insertion, deletion, searching, and traversal, making it suitable for dynamic memory allocation and implementing other data structures like stacks and queues. While linked lists offer advantages like efficient insertions and deletions, they also have drawbacks including slow access times and higher memory overhead.

Uploaded by

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

Linked List

A linked list is a data structure that stores a sequence of elements. Each


element in the list is called a node, and each node has a reference to
the next node in the list. The first node in the list is called the head, and
the last node in the list is called the tail.

Concept:
Representation of linked lists
We can visualize a linked list as a chain of nodes; where every node
contains a data field and a reference link to the next node in the list.
The first node in the sequence is termed as ‘head’. And we can identify
the last node as the one which points to ‘Null’.
Each node consists of -
 A data field
 Reference to the next node
In c programming language
/*using structures in c creating node*/
struct node {
int data;
Node* next;
};

/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;

/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));

/* Assign data values */


one->data = 1;
two->data = 2;
three->data=3;
/* Connect nodes */
one->next = two;
two->next = three;
three->next = NULL;

/* Save address of first node in head */


head = one;

Operations of Singly Linked List


Singly Linked List is good for dynamically allocating memory. It provides
all the basic operations of the linked list, i.e., insertion, deletion,
searching, updating, merging two linked lists, traversing, etc.
Here we’ll discuss the following operation of the linked list:
 Inserting at head
 Inserting at tail
 Inserting after a node
 Delete the head node
 Delete the tail node
 Search and Delete a node
 Traversing the Linked List
 Searching a node
Insertion at the head of a Singly Linked List
This is a simple operation. Generally, it’s known as pushing a singly
linked list. You need to create a new node and place this at the head
of the linked list.
To perform this operation, we need to follow two important
conditions. They’re
1. If the list is empty, then the newly created node will be the head
node, and the next node of the head will be ”NULL”.
2. If the list is not empty, the new node will be the head node, and
the next will point to the previous head node.
Insertion at the end of a Singly Linked List
Inserting a node at the end of a linked list has some similarities with
inserting in the head. All you need to do is, traverse to the end node or
the tail node. Then point the “next” node of the end node to the newly
created node. If the head node is null, the new node will be the head
node.
Here’re the steps:
Step 1) Traverse until the “next” node of the current node becomes
null.
Step 2) Create a new node with the specified value.
Step 3) Assign the new node as the next node of the tail node.

Insertion after a node in a Singly Linked List


Inserting after a node has two parts: Search for the node and attach
after the searched node. We need to traverse all the nodes. For each
node, we need to match with the search element. If there’s a match,
then we will add the new node after the searched node.
Here’re the steps:
Step 1) Traverse the next node until the value of the current node
equals the search item.
Step 2) New node’s next pointer will be the current node’s next pointer.
Step 3) Current node’s next node will be the new node.

Delete the head of the singly linked list


In every function of the linked list, the head pointer is provided as the
parameter. You need to delete the head node and make the next node
of the head node as the new head of the linked list. We also need to
free the memory of the deleted node. Otherwise, the memory will be
marked as occupied when another program tries to access it.
Here’re the steps for deleting the head of the singly linked list:
Step 1) Assign the next node of the head node as the new head.
Step 2) Free the allocated memory by the previous head node.
Step 3) Return the new head node.

Delete the tail of the singly linked list


Deleting the tail node is more familiar with deleting the head node. The
difference is that we need to traverse to the end of the linked list and
delete it. In the singly linked list, the node with the next node as “NULL”
is the tail node.
Here’re the steps for deleting the tail node of the linked list:
Step 1) Traverse before the tail node. Save the current node.
Step 2) Free the memory of the next node of the current node.
Step 3) Set the next node of the current node as NULL.
Search and delete a node from a singly linked list
This function has two tasks, search and delete. We need to search until
the end of the singly linked lists. If we find any similar node, we will
delete that one. After that, we need to merge or link the left and right
nodes of the deleted node. Here’re the steps for doing this:
Step 1) Traverse until the end of the linked list. Check if the current
node is equal to the search node or not.
Step 2) If any node matches, store the node pointer to the current
node.
Step 3) The “next” of the previous node will be the next node of the
current node.
Step 4) Delete and free the memory of the current node.

Traverse a singly linked list


Singly linked list only has the functionality for traversing head to tail.
There are no pointer points to the previous node; that’s why we can’t
traverse the Singly Linked List in reverse order. We will traverse each
node and print the current node’s value until we get null.
Here’re the steps:
Step 1) Traverse each node until we get null as the next node.
Step 2) Print the value of the current node.
Search a node from a singly linked list
This function has one task, search . We need to search until the end of
the singly linked lists. If we find any similar node, we will print that one.
Step 1) Traverse until the end of the linked list. Check if the current
node is equal to the search node or not.
Step 2) If any node matches, store the node pointer to the current node
and print it

Applications of Linked Lists:


 Linked Lists can be used to implement stacks, queue,
deque, sparse matrices and adjacency list representation of
graphs.
 Dynamic memory allocation in operating systems and compilers
(linked list of free blocks).
 Manipulation of polynomials
 Arithmetic operations on long integers.
 In operating systems, they can be used in Memory management,
process scheduling (for example circular linked list for round robin
scheduling) and file system.
 Algorithms that need to frequently insert or delete items from
large collections of data.
 LRU cache, which uses a doubly linked list to keep track of the
most recently used items in a cache.
Applications of Linked Lists in real world:
 The list of songs in the music player are linked to the previous and
next songs.
 In a web browser, previous and next web page URLs can be linked
through the previous and next buttons (Doubly Linked List)
 In image viewer, the previous and next images can be linked with
the help of the previous and next buttons (Doubly Linked List)
 Circular Linked Lists can be used to implement things in round
manner where we go to every element one by one.
 Linked List are preferred over arrays for implementations of
Queue and Deque data structures because of fast deletions (or
insertions) from the front of the linked lists.
Advantages of Linked Lists (or Most Common Use Cases):
 Linked Lists are mostly used because of their effective insertion
and deletion. We only need to change few pointers (or
references) to insert (or delete) an item in the middle
 Insertion and deletion at any point in a linked list take O(1) time.
Whereas in an array data structure, insertion / deletion in the
middle takes O(n) time.
 This data structure is simple and can be also used to implement a
stack, queues, and other abstract data structures.
 Implementation of Queue and Deque data structures : Simple
array implementation is not efficient at all. We must use circular
array to efficiently implement which is complex. But with linked
list, it is easy and straightforward. That is why most of the
language libraries use Linked List internally to implement these
data structures..
 Linked List might turn out to be more space efficient compare to
arrays in cases where we cannot guess the number of elements in
advance. In case of arrays, the whole memory for items is
allocated together. Even with dynamic sized arrays like vector in C+
+ or list in Python or ArrayList in Java. the internal working
involves de-allocation of whole memory and allocation of a bigger
chunk when insertions happen beyond the current capacity.
Disadvantages of Linked Lists:
Linked lists are a popular data structure in computer science, but like
any other data structure, they have certain disadvantages as well. Some
of the key disadvantages of linked lists are:
 Slow Access Time: Accessing elements in a linked list can be slow,
as you need to traverse the linked list to find the element you are
looking for, which is an O(n) operation. This makes linked lists a
poor choice for situations where you need to access elements
quickly.
 Pointers or References: Linked lists use pointers or references to
access the next node, which can make them more complex to
understand and use compared to arrays. This complexity can make
linked lists more difficult to debug and maintain.
 Higher overhead: Linked lists have a higher overhead compared to
arrays, as each node in a linked list requires extra memory to store
the reference to the next node.
 Cache Inefficiency: Linked lists are cache-inefficient because the
memory is not contiguous. This means that when you traverse a
linked list, you are not likely to get the data you need in the cache,
leading to cache misses and slow performance.
 Easy to use : Arrays are relatively very easy to use and are
available as core of programming languages
Extra notes for students who want to explore more

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy