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

linked-lists

The document provides an overview of linked lists as a type of linear data structure, detailing their definition, operations, and comparisons with arrays. It explains the structure of singly and doubly linked lists, including basic operations such as insertion, deletion, and searching. Additionally, it highlights applications of linked lists in various programming scenarios.

Uploaded by

Dipayan Ghose
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

linked-lists

The document provides an overview of linked lists as a type of linear data structure, detailing their definition, operations, and comparisons with arrays. It explains the structure of singly and doubly linked lists, including basic operations such as insertion, deletion, and searching. Additionally, it highlights applications of linked lists in various programming scenarios.

Uploaded by

Dipayan Ghose
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Linked List

Linear Data Structures


Definition
A data structure is said to be linear if its elements form a sequence or a linear list.
• Examples:

• Array
• Linked List
• Stacks
• Queues

• Operations on linear Data Structures


– Traversal : Visit every part of the data structure
– Search : Traversal through the data structure for a given element
– Insertion : Adding new elements to the data structure
– Deletion : Removing an element from the data structure.
– Sorting : Rearranging the elements in some type of order(e.g Increasing or Decreasing)
– Merging : Combining two similar data structures into one
What are Linked Lists

• A linked list is a linear data structure.


• A chain of structures make up linked lists
called Node.
• Nodes are made up of data and a pointer
to another node.
• Usually the pointer points to a structure
of the same type as itself
Arrays Vs Linked Lists
Arrays Linked list

Fixed size: Resizing is expensive Dynamic size

Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No shifting
Elements are usually shifted

Random access i.e., efficient indexing No random access


 Not suitable for operations requiring
accessing elements by index such as sorting

No memory waste if the array is full or almost Since memory is allocated dynamically(acc. to our
full; otherwise may result in much memory need) there is no waste of memory.
waste.

Sequential access is faster [Reason: Elements Sequential access is slow [Reason: Elements not
in contiguous memory locations] in contiguous memory locations]
Representation of linked list in memory
Types of lists
There are two basic types of linked list
– Singly Linked list
– Doubly linked list
– Circular linked list
Singly Linked List
• Each node has only one link part
• Each link part contains the address of the next
node in the list
• Link part of the last node contains NULL value
which signifies the end of the node
Schematic representation
Here is a singly-linked list (SLL):

myList

a b c d

• Each node contains a value(data) and a pointer to the next


node in the list

• myList is the header pointer which points at the first node


in the list
Basic Operations on a list
• Creating a List
• Inserting an element in a list
• Deleting an element from a list
• Searching a list
• Reversing a list
Creating a node
typedef struct {
int val; // A simple node of a linked list
struct node * next; // pointer to the next address
} node;
void create()
{
item * curr, * head,*tail;
head = NULL;
// beginning of loop
curr = (item *)malloc(sizeof(item));
curr-> val =read value;
curr->next = NULL;

if (head == NULL)
{
head = curr;
tail = curr;
}
else {
tail->next = curr;
tail = curr;
}
// end of loop;
}
To display
void display()
{
curr = head;
printf( "\n");
while(curr != NULL) {
printf(" %d---> ", curr->val);

curr = curr->next ;
} }
Traversing a Linked List
Traversing linked list means visiting each and every node of the Singly
linked list. Following steps are involved while traversing the singly
linked list –
 Firstly move to the first node
 Fetch the data from the node and perform the operations such as
arithmetic operation or any operation depending on data type.
 After performing operation, advance pointer to next node and
perform all above steps on Visited node.
Inserting the node in a SLL
There are 3 cases here:-

 Insertion at the beginning


 Insertion at the end
 Insertion after a particular node
Insertion at the beginning

There are two steps to be followed:-


a) Make the next pointer of the new node point towards the
first node of the list
b) Make the start pointer point towards this new node
 If the list is empty simply make the start pointer point
towards the new node;
Insertion at the beginning
Inserting at the end

Here we simply need to make the next pointer


of the last node point to the new node
void insert_at_end() {
struct node *newnode,*current,*temp;
newnode=(struct node *)malloc(sizeof(struct node));
scanf("%d",&new_node->data);
newnode->next=NULL;
if(start==NULL) {
start=newnode;
current=newnode; }
else {
temp = start;
while(temp->next!=NULL) {
temp = temp->next;
}
temp->next = newnode; } }
Inserting after an element
Here we again need to do 2 steps :-

 Make the next pointer of the node to be inserted


point to the next node of the node after which you
want to insert the node

 Make the next pointer of the node after which the


node is to be inserted, point to the node to be
inserted
void insert_mid() {
int pos,i; struct node *new_node,*current,*temp,*temp1;
new_node=(struct node *)malloc(sizeof(struct node));
printf("nEnter the data : "); scanf("%d",&new_node->data);
new_node->next=NULL;
printf("nEnter the position : "); scanf("%d",&pos);

if(start==NULL) { start=new_node; current=new_node; }


else { temp = start;
for(i=1;i< pos-1;i++) {
temp1 = temp;
temp=temp->next; }
temp1->next = newnode;
newnode->next=temp;
}
Deleting a node in SLL
Here also we have three cases:-
 Deleting the first node
 Deleting the last node
 Deleting the intermediate node
Deleting the first node
Here we apply 2 steps:-

 Making the start pointer point towards the 2nd node

 Deleting the first node using delete keyword

start

one two three


void del_beg()
{
struct node *temp;
temp = start;
start = start->next;
free(temp);
}
Deleting the last node
Here we apply 2 steps:-

 Making the second last node’s next pointer point to


NULL

 Deleting the last node via delete keyword

start

node1 node2 node3


void delete_at_end()
{
struct node *temp,*temp1;
if(start!=NULL) {
temp = start;
while(temp->next!=NULL) {
temp1 = temp;
temp = temp->next;
}
temp1->next = NULL;
delete(temp);
}
}
Deleting a particular node

Here we make the next pointer of the node previous to


the node being deleted ,point to the successor node of
the node to be deleted and then delete the node using
delete keyword

node1 node2 node3

To be deleted
void delete_any()
{
struct node *temp,*temp1;
int key = data to be deleted;
if(start!=NULL) {
temp = start;
while(temp->next!=NULL and temp->data != key) {
temp1 = temp;
temp = temp->next;
}
if (temp->data == key){
temp1->next = temp->next;
delete(temp);
}
}
}
Searching a SLL
• Searching involves finding the required element in
the list
• We can use various techniques of searching like
linear search or binary search where binary search
is more efficient in case of Arrays
• But in case of linked list since random access is
not available it would become complex to do
binary search in it
• We can perform simple linear search traversal
In linear search each node is traversed till the data in
the node matches with the required value

void search(int x)
{
node*temp=start;
while(temp!=NULL)
{
if(temp->data==x)
{
cout<<“FOUND ”<<temp->data;
break;
}
temp=temp->next;
}
}
Reversing a linked list
• We can reverse a linked list by reversing the direction
of the links between 2 nodes
Doubly Linked List
1. Doubly linked list is a linked data structure that consists of a set of
sequentially linked records called nodes.

2. Each node contains three fields ::


-: one is data part which contain data only.
-:two other field is links part that are point
or references to the previous or to the next
node in the sequence of nodes.

3. The beginning and ending nodes' previous and next


links, respectively, point to some kind of terminator,
typically a sentinel node or null to facilitate traversal
of the list.
NODE
previous data next

NULL A
11 786 200 B
656 400 786 777 CNULL
200 786 400

A doubly linked list contain three fields: an integer


value, the link to the next node, and the link to the
DLL’s compared to SLL’s
Advantages:
We can traverse in both Disadvantages:
directions i.e. from starting Requires more space
to end and as well as from List manipulations are
end to starting. slower (because more links
It is easy to reverse the
must be changed)
linked list. Greater chance of having
If we are at a node, then
bugs (because more links
we can go to any node. But must be manipulated)
in linear linked list, it is not
possible to reach the
previous node.
Structure of DLL

struct node
{
int data;
node*next;
node*previous; //holds the address of previous node
};

previous.i .Data .next


nf
Inserting at beginning
void insert_beg(node *p)
{
if(start==NULL)
{
start=p;

}
else
{
node* temp=start;
start=p;
temp->previous=p; //making 1st node’s previous point to the new node
p->next=temp; //making next of the new node point to the 1st node

}
Inserting at the end
void insert_end(node* p)
{
if(start==NULL)
{
start=p;

}
else
{
node* temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p->previous=temp;

}
}
Inserting after a node

Making next and previous pointer of the node to be


inserted point accordingly

Adjusting the next and previous pointers of the nodes b/w which the
new node accordingly
void insert_after(int c,node* p)
{
temp=start;
for(int i=1;i<c-1;i++)
{
temp1 = temp;
temp=temp->next;
}
p->next=temp;
temp->previous=p;
temp1->next=p;
p->previous=temp1;
}
Deleting a node

• Node deletion from a DLL involves changing two links


• In this example, we will delete node b
myDLL

a b c

• We don’t have to do anything about the links in node b


• Garbage collection will take care of deleted nodes
• Deletion of the first node or the last node is a special case
void del_at(int c)
{
node*s=start;
{
for(int i=1;i<c-1;i++)
{
s1= s;
s=s->next;
}
node* p=s;
s1->next=p->next;
p->next->previous=s1;
delete p;
}
}
}
APPLICATIONS OF LINKED LIST
1. Applications that have an MRU(most recently used)
list (a linked list of file names)

2. The cache in your browser that allows you to hit the


BACK button (a linked list of URLs)

3. Undo functionality in Photoshop or Word (a linked list


of state)

4. A stack, hash table, and binary tree can be


implemented using linked list.
THANK YOU

You might also like