3a) Implementation of Singly Linked List
AIM: To implement the singly linked list operations to insert, Delete, Count
and Display.
ALGORITHM:
Inserting
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, set newNode→next = NULL and head = newNode.
Step 4 - If it is Not Empty then, set newNode→next = head and head = newNode.
Deleting
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize
with head.
Step 4 - Check whether list is having only one node (temp → next == NULL)
Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list
conditions)
Step 6 - If it is FALSE then set head = temp → next, and delete temp.
Display
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize
with head.
Step 4 - Keep displaying temp → data with an arrow (--->) until temp reaches to the
last node
Step 5 - Finally display temp → data with arrow pointing to NULL (temp → data
---> NULL).
Program
//Singly Linked List
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void insert();
void display();
void delete();
int count();
typedef struct node DATA_NODE;
DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;
int data;
int main() {
int option = 0;
printf("Singly Linked List Example - All Operations\n");
while (option < 5) {
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");
printf("3 : Display Linked List\n");
printf("4 : Count Linked List\n");
printf("Others : Exit()\n");
printf("Enter your option:");
scanf("%d", &option);
switch (option) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
count();
break;
default:
break;
}
}
return 0;
}
void insert() {
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);
temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));
temp_node->value = data;
if (first_node == 0) {
first_node = temp_node;
} else {
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
fflush(stdin);
}
void delete() {
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
printf("\nDisplay Linked List : \n");
printf("\nEnter Position for Delete Element : \n");
scanf("%d", &pos);
if (pos > 0 && pos <= countvalue) {
if (pos == 1) {
temp_node = temp_node -> next;
first_node = temp_node;
printf("\nDeleted Successfully \n\n");
} else {
while (temp_node != 0) {
if (i == (pos - 1)) {
prev_node->next = temp_node->next;
if(i == (countvalue - 1))
{
head_node = prev_node;
}
printf("\nDeleted Successfully \n\n");
break;
} else {
i++;
prev_node = temp_node;
temp_node = temp_node -> next;
}
}
}
} else
printf("\nInvalid Position \n\n");
}
void display() {
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
}
int count() {
int count = 0;
temp_node = first_node;
while (temp_node != 0) {
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}
Output
RESULT: Thus the C program is implemented and verified the output for Singly Linked list
to insert Delete and display.
3b). To implement Doubly Linked List.
AIM: Write a C program to implement Doubly linked List
Algorithm: insert
Step 1: IF ptr = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
Step 2: SET NEW_NODE = ptr
Step 3: SET ptr = ptr -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> PREV = NULL
Step 6: SET NEW_NODE -> NEXT = START
Step 7: SET head -> PREV = NEW_NODE
Step 8: SET head = NEW_NODE
Step 9: EXIT
Algorithm: Delete
STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 6
STEP 2: SET PTR = HEAD
STEP 3: SET HEAD = HEAD → NEXT
STEP 4: SET HEAD → PREV = NULL
STEP 5: FREE PTR
STEP 6: EXIT
Program
//Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
/* structure representing a node of the doubly linked list */
struct dnode
{
struct dnode *prev;
int data;
struct dnode *next;
};
struct dnode *start = NULL;
void append(int);
void addatbeg(int);
void remov(int);
void display();
int main()
{
int n, ch;
do
{
printf("\n\nOperations on doubly linked list");
printf("\n1. Append \n2. Add at beginning \n3. Remove\n4. Display\n0.
Exit\n");
printf("\nEnter Choice 0-4? : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter number: ");
scanf("%d", &n);
append(n);
break;
case 2:
printf("\nEnter number: ");
scanf("%d", &n);
addatbeg(n);
break;
case 3:
printf("\nEnter number to delete: ");
scanf("%d", &n);
remov(n);
break;
case 4:
display();
break;
}
}while (ch != 0);
}
/* adds a new node at the end of the doubly linked list */
void append(int num)
{
struct dnode *nptr, *temp = start;
/*create a new node */
nptr = malloc(sizeof(struct dnode));
nptr->data = num;
nptr->next = NULL;
nptr->prev = NULL;
/* if the linked list is empty */
if (start == NULL)
{
start = nptr;
}
else
{
/* traverse the linked list till the last node is reached */
while (temp->next != NULL)
temp = temp->next;
nptr->prev = temp;
temp->next = nptr;
}
}
/* adds a new node at the begining of the linked list */
void addatbeg(int num)
{
struct dnode *nptr;
/* create a new node */
nptr = malloc(sizeof(struct dnode));
/* assign data and pointer to the new node */
nptr->prev = NULL;
nptr->data = num;
nptr->next = start;
if (start != NULL)
start->prev = nptr;
start = nptr;
}
/* deletes the specified node from the doubly linked list */
void remov(int num)
{
struct dnode *temp = start;
/* traverse the entire linked list */
while (temp != NULL)
{
/* if node to be deleted is found */
if (temp->data == num)
{
/* if node to be deleted is the first node */
if (temp == start)
{
start = start->next;
start->prev = NULL;
}
else
{
/* if node to be deleted is the last node */
if (temp->next == NULL)
temp->prev->next = NULL;
else
/* if node to be deleted is any intermediate node */
{
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
}
free(temp);
}
return ; /* return back after deletion */
}
temp = temp->next; /* go to next node */
}
printf("\n%d not found.", num);
}
/* displays the contents of the linked list */
void display()
{
struct dnode *temp = start;
printf("\n");
/* traverse the entire linked list */
while (temp != NULL)
{
printf("%d\t", temp->data);
temp = temp->next;
}
}
Output
RESULT: Thus the C program is implemented and verified the output for Doubly Linked list
to Append, Add at beginning, Delete and Display.