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

Linked List

test

Uploaded by

pjeraids
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)
19 views

Linked List

test

Uploaded by

pjeraids
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/ 27

Linked List

A linked list is a linear collection of data elements


called nodes in which linear representation is given by
links from one node to the next node.

Linked list is a data structure which in turn can be used


to implement other data structures. Thus, it acts as
building block to implement data structures like
stacks, queues and their variations.
Linked List
A linked list can be perceived as a train or a sequence
of nodes in which each node contains one or more
data fields and a pointer to the next node.
START

1 2 3 4 5 6 7 X

START

1 M 2 F 3 F 4 M X
Linked List
START

1 2 3 4 5 6 7 X

START

1 M 2 F 3 F 4 M X

• The left part of the node which contains data

• The right part of the node contains a pointer to the next

node
The last node will have no next node connected to it,

so it will store a special value called NULL.


Linked List
NODE :
struct node
{
int data;
struct node *next;
};
struct node
START {
int i;
1 2 3 4 5 6 7 X
struct node *next;
};

struct node
START
{
int i;
1 M 2 F 3 F 4 M X
char ch;
struct node *next;
};
Linked List
START

1 2 3 4 5 6 7 X

We can traverse the entire linked list using a single pointer


variable called START
The START node contains the address of the first node
The next part of the first node in turn stores the address of
its succeeding node
If START = NULL, this means that the linked list is empty
and contains no nodes
If AVAIL = NULL, this means that the memory not available
so overflow condition
Singly Linked List (SLL)

A singly linked list is the simplest type of linked list in


which every node contains some data and a pointer to the
next node of the same data type

START

1 2 3 4 5 6 7 X
SLL - TRAVERSING A LINKED LIST
START

1 2 3 4 5 6 7 X

ALGORITHM FOR TRAVERSING A LINKED LIST

Step 1: [INITIALIZE] SET PTR = START


Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply Process to PTR -> DATA
Step 4: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 5: EXIT
SLL - INSERTING A NODE AT THE BEGINNING
1 7 3 4 2 6 5 X

START

9 1 7 3 4 2 6 5 X

START

ALGORITHM TO INSERT A NEW NODE IN THE BEGINNING OF THE LINKED LIST

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 6
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET New_Node -> DATA = VAL
Step 4: SET New_Node -> Next = START
Step 5: SET START = New_Node
Step 6: EXIT
SLL - INSERTING A NODE AT THE END
1 7 3 4 2 6 5 X

START, PTR

1 7 3 4 2 6 5 9 X

START

ALGORITHM TO INSERT A NEW NODE AT THE END OF THE LINKED LIST

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 9
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET New_Node -> DATA = VAL
Step 4: SET New_Node -> Next = NULL
Step 5: SET PTR = START
Step 6: Repeat Step 7 while PTR -> NEXT != NULL
Step 7: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 8: SET PTR -> NEXT = New_Node
Step 9: EXIT
SLL - INSERTING A NODE AFTER NODE THAT
HAS VALUE NUM
1 7 3 4 5 X

START, PREPTR, PTR

1 7 3 4 5 X

PREPTR PTR
START

1 7 3 4 5 X

START 6

NEW NODE

1 7 3 4 6 5 X
SLL - INSERTING A NODE AFTER NODE THAT
HAS VALUE NUM

ALGORITHM TO INSERT A NEW NODE AFTER A NODE THAT HAS VALUE NUM

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET New_Node - > DATA = VAL
Step 4: SET PTR = START
Step 5: SET PREPTR = PTR
Step 6: Repeat Steps 8 and 9 while PREPTR -> DATA != NUM
Step 7: SET PREPTR = PTR
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: PREPTR -> NEXT = New_Node
Step 10: SET New_Node -> NEXT = PTR
Step 11: EXIT
SLL - DELETING THE FIRST NODE

Algorithm to delete the first node from the linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START -> NEXT
Step 4: FREE PTR
Step 5: EXIT

1 7 3 4 2 6 5 X

START, PTR

1 7 3 4 2 6 5 X

PTR START

7 3 4 2 6 5 X

START
SLL - DELETING THE LAST NODE

ALGORITHM TO DELETE THE LAST NODE OF THE LINKED LIST

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT

1 7 3 4 2 6 5 X

START, PREPTR, PTR

1 7 3 4 2 6 X 5 X

PREPTR PTR
START
SLL - DELETING THE NODE WHOSE VALUE NUM

1 7 3 4 2 6 5 X

START, PREPTR, PTR

1 7 3 4 2 6 5 X

START PREPTR PTR

1 7 3 4 2 6 5 X

START

1 7 3 4 6 5 X
SLL - DELETING THE NODE AFTER A GIVEN NODE

ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE LINKED LIST

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 10
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PREPTR -> DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 7: SET PREPTR -> NEXT = PTR -> NEXT
Step 8: FREE PTR
Step 9: EXIT
SLL – C Program
SINGLY LINKED LIST
#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *next;
};

struct node *start = NULL;


SINGLY LINKED LIST
void create();
void display();
void insert_beg();
void insert_end();
void insert_middle();
void delete_beg();
void delete_end();
void delete_middle();
SINGLY LINKED LIST
int main()
{ case 2: display();
int option; break;
do case 3: insert_beg( );
{ break;
printf("\n\n *****MAIN MENU *****"); case 4: insert_end( );
printf("\n 1: Create a list"); break;
printf("\n 2: Display the list"); case 5: insert_ middle( );
printf("\n 3: Add a node at the beginning"); break;
printf("\n 4: Add a node at the end"); case 6: delete_beg( );
printf("\n 5: Add a node in the middle"); break;
printf("\n 6: Delete a node from the beginning"); case 7: delete_end( );
printf("\n 7: Delete a node from the end"); break;
printf("\n 8: Delete a node in the middle"); case 8: delete_ middle( );
printf("\n 9: EXIT"); break;
printf("\n\n Enter your option : "); case 9: exit(0);
scanf("%d", &option); }
switch(option) }while(option < 9);
{ return 0;
case 1: create(); }
break;
SINGLY LINKED LIST
void create()
{ else
struct node *new_node; {
struct node *ptr; ptr=start;
int val; while(ptr -> next != NULL)
printf("\n Enter –1 to end"); ptr=ptr->next;
printf("\n Enter the data : "); ptr -> next = new_node;
scanf("%d", &val); }
while(val != -1) printf("\n Enter the data : ");
{ scanf("%d", &val);
new_node = (struct node*) }
malloc(sizeof(struct node));
}
new_node -> data = val;
new_node -> next = NULL;
if(start == NULL)
{
start = new_node;
}
SINGLY LINKED LIST
void display()
{
struct node *ptr;
ptr = start;
while(ptr != NULL)
{
printf("\t %d", ptr -> data);
ptr = ptr -> next;
}
}
SINGLY LINKED LIST
void insert_beg()
{
struct node *new_node;
int val;
printf("\n Enter the data : ");
scanf("%d", &val);
new_node = (struct node *) malloc(sizeof(struct node));
new_node -> data = val;
new_node -> next = start;
start = new_node;
}
SINGLY LINKED LIST
void insert_end()
{
struct node *ptr, *new_node;
int num;
printf("\n Enter the data : ");
scanf("%d", &val);
new_node = (struct node *) malloc(sizeof(struct node));
new_node -> data = val;
new_node -> next = NULL;
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = new_node;
}
SINGLY LINKED LIST
void insert_middle()
{
struct node *new_node, *ptr, *preptr;
int num, val;
printf("\n Enter the data : ");
scanf("%d", &val);
printf("\n Enter the value after which the node has to be inserted : ");
scanf("%d", &num);
new_node = (struct node *) malloc(sizeof(struct node));
new_node -> data = val;
ptr = start;
preptr = start;
while(preptr -> data != num)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = new_node;
new_node -> next = ptr;
}
SINGLY LINKED LIST
void delete_beg()
{
struct node *ptr;
ptr = start;
start = start -> next;
free(ptr);
}
SINGLY LINKED LIST
void delete_end()
{
struct node *ptr, *preptr;
ptr = start;
while(ptr -> next != NULL)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = NULL;
free(ptr);
}
SINGLY LINKED LIST
void delete_middle()
{
struct node *ptr, *preptr;
int num;
printf("\n Enter the number after which the node has to be deleted : ");
scanf("%d", &num);
ptr = start;
preptr = ptr;
while(preptr -> data != num)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = ptr -> next;
free(ptr);
}

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