0% found this document useful (0 votes)
2 views15 pages

Doubly Linked List Program

The document provides a series of C programs for creating, displaying, inserting, and deleting nodes in a doubly linked list. It includes functionalities for adding nodes at the beginning, end, and specific positions, as well as deleting nodes from both ends and at specified positions. Each section is accompanied by example outputs demonstrating the operations performed on the linked list.

Uploaded by

Ashmita Maharana
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)
2 views15 pages

Doubly Linked List Program

The document provides a series of C programs for creating, displaying, inserting, and deleting nodes in a doubly linked list. It includes functionalities for adding nodes at the beginning, end, and specific positions, as well as deleting nodes from both ends and at specified positions. Each section is accompanied by example outputs demonstrating the operations performed on the linked list.

Uploaded by

Ashmita Maharana
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/ 15

Program to create and display a doubly linked list

#include <stdio.h>
struct node{
int data;
struct node *previous;
struct node *next;
};

struct node *head, *tail = NULL;


void addNode(int data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;

if(head == NULL)
{
head = tail = newNode;
head->previous = NULL;
tail->next = NULL;
}
else
{
tail->next = newNode;
newNode->previous = tail;
tail = newNode;
tail->next = NULL;
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of doubly linked list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
}

Swetanjali Maharana(Asst. prof)


int main()
{
addNode(30);
addNode(22);
addNode(3);
addNode(54);
addNode(20);
display();
return 0;
}
Output:
Nodes of doubly linked list:
30 22 3 54 20

Insertion in doubly linked list at beginning


#include <stdio.h>
#include <stdlib.h>

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

struct node *head, *tail = NULL;


void addNode(int data) {
struct node *newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL) {
head = tail = newNode;
head->prev = NULL;
tail->next = NULL;
}
else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
tail->next = NULL;
}
}

Swetanjali Maharana(Asst. prof)


void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of doubly linked list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
}
void insertbeginning(int item)
{

struct node *ptr = (struct node *)malloc(sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
}

}
int main()
{

Swetanjali Maharana(Asst. prof)


int item;
addNode(30);
addNode(22);
addNode(3);
addNode(54);
addNode(20);
display();
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
insertbeginning(item);
display();
return 0;
}

Output:
Nodes of doubly linked list:
30 22 3 54 20
Enter the item which you want to insert?
26
Nodes of doubly linked list:
26 30 22 3 54 20

Insertion in doubly linked list at End


#include <stdio.h>
#include <stdlib.h>

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

struct node *head, *tail = NULL;


void addNode(int data) {
struct node *newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL) {
head = tail = newNode;
head->prev = NULL;
tail->next = NULL;
}

Swetanjali Maharana(Asst. prof)


else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
tail->next = NULL;
}
}

void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of doubly linked list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
}
void insertlast(int item)
{

struct node *ptr = (struct node *) malloc(sizeof(struct node));


struct node *temp;
if(ptr == NULL)
{
printf("\nOVERFLOW");

}
else
{

ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{

Swetanjali Maharana(Asst. prof)


temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
printf("\nNode Inserted\n");

}
}
int main()
{
int item;
addNode(30);
addNode(22);
addNode(3);
addNode(54);
addNode(20);
display();
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
insertlast(item);
display();
return 0;
}

Output:
Nodes of doubly linked list:
30 22 3 54 20
Enter the item which you want to insert?
56
Node Inserted
Nodes of doubly linked list:
30 22 3 54 20 56

Swetanjali Maharana(Asst. prof)


Deletion in doubly linked list at beginning

#include <stdio.h>
#include <stdlib.h>

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

struct node *head, *tail = NULL;


void addNode(int data) {
struct node *newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL) {
head = tail = newNode;
head->prev = NULL;
tail->next = NULL;
}
else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
tail->next = NULL;
}
}

void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of doubly linked list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
}
void beginning_delete()

Swetanjali Maharana(Asst. prof)


{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW\n");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nNode Deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nNode Deleted\n");
}
}
int main()
{
int item;
addNode(30);
addNode(22);
addNode(3);
addNode(54);
addNode(20);
display();
beginning_delete();
display();
return 0;
}
Output:
Nodes of doubly linked list:
30 22 3 54 20
Node Deleted
Nodes of doubly linked list:
22 3 54 20

Swetanjali Maharana(Asst. prof)


Deletion in doubly linked list at end

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* head = NULL;
void insertAtEnd(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
head = newNode;
return;
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
void deleteFromEnd() {
if (head == NULL) {
printf("List is empty. Cannot delete.\n");
return;
}
struct Node* temp = head;
if (head->next == NULL) {
free(head);
head = NULL;
return;
}

Swetanjali Maharana(Asst. prof)


while (temp->next != NULL) {
temp = temp->next;
}
temp->prev->next = NULL;
free(temp);
}
void printList() {
struct Node* temp = head;
if (head == NULL) {
printf("List is empty\n");
return;
}
printf("Doubly Linked List: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
insertAtEnd(10);
insertAtEnd(20);
insertAtEnd(30);
insertAtEnd(40);
printf("Original Linked List:\n");
printList();
deleteFromEnd(); // Delete last node
printf("Linked List after deleting last node:\n");
printList();
return 0;
}

Output:
Original Linked List:
Doubly Linked List: 10 20 30 40 NULL

Linked List after deleting last node:


Doubly Linked List: 10 20 30 NULL
Insert a node at any position using double linked list
#include <stdio.h>
#include <stdlib.h>
Swetanjali Maharana(Asst. prof)
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* head = NULL;
void insertAtEnd(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
head = newNode;
return;
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
void insertAtPosition(int data, int position) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = data;
if (position == 1) {
newNode->next = head;
newNode->prev = NULL;
if (head != NULL) {
head->prev = newNode;

Swetanjali Maharana(Asst. prof)


}
head = newNode;
return;
}

struct Node* temp = head;


int currentPosition = 1;
while (temp != NULL && currentPosition < position - 1) {
temp = temp->next;
currentPosition++;
}
if (temp == NULL) {
printf("Position %d is out of range\n", position);
free(newNode);
return;
}
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != NULL) {
temp->next->prev = newNode;
}
temp->next = newNode;
}
void printList() {
struct Node* temp = head;
if (head == NULL) {
printf("List is empty\n");
return;
}
printf("Doubly Linked List: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
insertAtEnd(10);

Swetanjali Maharana(Asst. prof)


insertAtEnd(20);
insertAtEnd(40);
insertAtEnd(50);
printf("Original Linked List:\n");
printList();
int data, position;
printf("Enter data and position to insert: ");
scanf("%d %d", &data, &position);

insertAtPosition(data, position);
printf("Linked List after insertion:\n");
printList();
return 0;
}
Output
Doubly Linked List after insertions:
10 50 20 30 40 NULL

Delete a node at any position using double linked list


#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* head = NULL;
void insertAtEnd(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
head = newNode;

Swetanjali Maharana(Asst. prof)


return;
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}

temp->next = newNode;
newNode->prev = temp;
}
void deleteAtPosition(int position) {
if (head == NULL) {
printf("List is empty. Cannot delete.\n");
return;
}
struct Node* temp = head;
int currentPosition = 1;
while (temp != NULL && currentPosition < position) {
temp = temp->next;
currentPosition++;
}
if (temp == NULL) {
printf("Position %d is out of range\n", position);
return;
}
if (temp->prev == NULL) {
head = temp->next;
if (head != NULL) {
head->prev = NULL;
}
} else {
temp->prev->next = temp->next;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
}

Swetanjali Maharana(Asst. prof)


void printList() {
struct Node* temp = head;
if (head == NULL) {
printf("List is empty\n");
return;
}
printf("Doubly Linked List: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
insertAtEnd(10);
insertAtEnd(20);
insertAtEnd(30);
insertAtEnd(40);
insertAtEnd(50);
printf("Original Linked List:\n");
printList();
int position;
printf("Enter position to delete: ");
scanf("%d", &position);
deleteAtPosition(position);
printf("Linked List after deletion:\n");
printList();
return 0;
}
Output
Original Linked List:
Doubly Linked List: 10 20 40 50 NULL

Enter data and position to insert: 30 3

Linked List after insertion:


Doubly Linked List: 10 20 30 40 50 NULL

Swetanjali Maharana(Asst. prof)

You might also like