Doubly Linked List Program
Doubly Linked List Program
#include <stdio.h>
struct node{
int data;
struct node *previous;
struct node *next;
};
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;
}
}
struct node{
int data;
struct node *prev;
struct node *next;
};
}
int main()
{
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
struct node{
int data;
struct node *prev;
struct node *next;
};
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)
{
}
else
{
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
}
}
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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *prev;
struct node *next;
};
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()
#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;
}
Output:
Original Linked List:
Doubly Linked List: 10 20 30 40 NULL
insertAtPosition(data, position);
printf("Linked List after insertion:\n");
printList();
return 0;
}
Output
Doubly Linked List after insertions:
10 50 20 30 40 NULL
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);
}