0% found this document useful (0 votes)
19 views10 pages

Programs On Doubbly Linked List

Programs on doubbly linked list

Uploaded by

ijantkartanishka
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 views10 pages

Programs On Doubbly Linked List

Programs on doubbly linked list

Uploaded by

ijantkartanishka
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/ 10

Doubbly Linked List

1.Create a Doubly linked List ,count the node and display the contents of the node
,search the element in the list.
#include <stdio.h>
#include <stdlib.h>
struct node{
struct node *prev;
int data;
struct node *next;
}*new,*head=NULL,*temp;

int main()
{
int val,in,count=0,flag=0,key;
do{
new=(struct node *)malloc(sizeof(struct node));
new->prev=NULL;
printf("enter the value\n");
scanf("%d",&val);
new->data=val;
new->next=NULL;
if(head==NULL){
head=new;
temp=new;
}
else{
temp->next=new;
new->prev=temp;
temp=new;
}
printf("Do you want to add one more node 1-Yes/0-No\n");
scanf("%d",&in);
}while(in);
count=0;
temp=head;
printf("\n Elements in doubly linked list are :\n");
while(temp!=NULL){
count=count+1;
printf("%d<->",temp->data);
temp=temp->next;
}
printf("\nTotal number of nodes in the list are=%d\n",count);
//search the Key element in the list
printf("enter the Key element\n");
scanf("%d",&key);
temp=head;
while(temp!=NULL && temp->data!=key)
{
temp=temp->next;
}
if(temp->data==key)
printf("Key element is found\n");
else
printf("key element is not found\n");
}
2. Traverse the list in forward direction and reverse direction
Function:
if (head == NULL) {
printf("The list is empty.\n");
return;
}
temp=head;
printf("Forward traversal:\n");
while (temp != NULL) {
printf("%d<->", temp->data);
curr = temp; // Save the last node in curr
temp = temp->next;
}
printf("NULL\n");

// Reverse traversal from the last node


printf("Reverse traversal:\n");
while (curr != NULL) {
printf("%d<->", curr->data);
curr = curr->prev;
}
printf("NULL\n");
}
3.Perform following operations on Doubly linked list
a. Insert at Beginning
b. Insert at End
c. Insert at Specified Position
d. Delete from beginning
e. Delete from End.
f. Delete from Specified Position.
g. Display the list and count the number of nodes in the list.
Solution Code:
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node in a doubly linked list


struct node {
struct node *prev;
int data;
struct node *next;
} *head = NULL, *new, *temp,*d;

int val; //global variable


// Function to insert a node at the beginning of the doubly linked list
void Insert_Begin() {
new = (struct node *)malloc(sizeof(struct node));
new->prev = NULL;
printf("Enter the value\n");
scanf("%d", &val);
new->data = val;
new->next = head;

if (head != NULL)
head->prev = new;
head = new;
//Traverse the list
temp=head;
while(temp!=NULL)
{
printf("%d<->",temp->data);
temp=temp->next;
}
}

// Function to insert a node at the end of the doubly linked list


void Insert_End() {
new = (struct node *)malloc(sizeof(struct node));
printf("Enter the value\n");
scanf("%d", &val);
new->data = val;
new->next = NULL;

// If the list is empty, make the new node the head


if (head == NULL) {
new->prev = NULL;
head = new;
return;
}

// Traverse to the last node


temp = head;
while (temp->next != NULL) {
temp = temp->next;
}

// Update the last node's next pointer and set the new node's previous pointer
temp->next = new;
new->prev = temp;

// Display the list after insertion


temp = head;
while (temp != NULL) {
printf("%d<->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
void Insert_SpecifiedPos(){
int pos, val, i;
temp=head;
printf("Enter the position to insert the node:\n");
scanf("%d", &pos);

// Validate the position


if (pos <= 0) {
printf("Invalid position. Position should be greater than 0.\n");
return;
}

// Allocate memory for the new node


new = (struct node *)malloc(sizeof(struct node));
if (new == NULL) {
printf("Memory allocation failed.\n");
return;
}

// Get the value to be inserted in the new node


printf("Enter the value to be inserted:\n");
scanf("%d", &val);
new->data = val;

// Case 1: Insert at the beginning


if (pos == 1) {
new->next = head;
new->prev = NULL;
if (head != NULL)
head->prev = new;
head = new;
printf("Node inserted at the beginning of the list.\n");
} else {
// Traverse the list to find the position to insert
for (i = 1; i < pos - 1 && temp!=NULL; i++) {
temp = temp->next;
}

// If the position is out of range


if (temp == NULL) {
printf("Invalid position. List has fewer elements than the specified position.\n");
free(new); // Free the allocated memory
return;
}

// Case 2: Insert at the specified position (middle or end)


new->next = temp->next;
new->prev = temp;

if (temp->next != NULL) {
temp->next->prev = new;
}

temp->next = new;
printf("Node inserted at position %d.\n", pos);
}

// Display the list after insertion


printf("Updated list: ");
temp = head;
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
void Delete_Begin(){
if(head==NULL)
{
printf("List is empty,Deletion not possible\n");
return;
}
temp=head;
printf("Deleted node is %d\n",temp->data);
head=head->next;
head->prev=NULL;
temp->next=NULL;
free(temp);
temp = head;
while (temp != NULL) {
printf("%d<->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
void Delete_End(){
temp=head;
if(head==NULL)
{
printf("List is empty,Deletion not possible\n");
return;
}
if (head->next == NULL) {
printf("Deleted node is %d\n", head->data);
free(head);
head = NULL;
return;
}
else{
while(temp->next->next!=NULL)
temp=temp->next;
d=temp->next;
printf("Deleted node is %d\n",d->data);
temp->next=NULL;
d->prev=NULL;
free(d);
}
temp = head;
if (temp == NULL) {
printf("List is now empty.\n");
} else {
while (temp != NULL) {
printf("%d<->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
}
void Delet_SpecifiedPos(){
temp=head;
int pos;
printf("enter the node's position to be deleted\n ");
scanf("%d",&pos);
// Case 1: If the list is empty
if(head==NULL)
{
printf("List is empty,Deletion not possible\n");
return;
}
// Case 2: If the position is less than or equal to 0 (invalid position)
if (pos <= 0) {
printf("Invalid position. Position should be greater than 0.\n");
return;
}
// Case 3: Deleting the first node (position 1)
if (pos == 1) {
d = head;
printf("Deleted node is %d\n", d->data);
// Update head to the next node
head = head->next;

// If the new head is not NULL, update its previous pointer


if (head != NULL)
head->prev = NULL;

// Free the deleted node


free(d);
} else {
// Traverse the list to find the node before the one to be deleted
for (int i = 1; i < pos - 1 && temp != NULL; i++) {
temp = temp->next;
}

// If the position is out of range


if (temp == NULL || temp->next == NULL) {
printf("Invalid position. No node exists at the specified position.\n");
return;
}

// Case 4: Deleting a node other than the first one


d = temp->next;
printf("Deleted node is %d\n", d->data);

// Update pointers to bypass the deleted node


temp->next = d->next;

if(d->next!=NULL){
d->next->prev = temp;
}
d->prev=NULL;
// Free the deleted node
free(d);
}

temp = head;
while (temp != NULL) {
printf("%d<->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Function to display all the elements in the doubly linked list
void Display() {
int count=0;
temp = head;
if (head == NULL) {
printf("List is empty\n");
return;
}

printf("Elements in DLL are:\n");


while (temp != NULL) {
count++;
printf("%d<->", temp->data);
temp = temp->next;
}
printf("NULL\n");
printf("Total number of elements in the doubly Linked List are:%d",count);
}
// Main function to control the flow
int main() {
int ch;
while (1) {

printf("\n1-Insert_Begin\t2-Insert_End\t3-Display\n4-Insert_SpecifiedPos\t5-Delete_Begin\t6
-Delete_End\t7-Delete_SpecifiedPos\t8-Exit");
printf("\nEnter your choice\n");
scanf("%d", &ch);

switch (ch) {
case 1:Insert_Begin();
break;
case 2:Insert_End();
break;
case 3:Display();
break;
case 4:Insert_SpecifiedPos();
break;
case 5:Delete_Begin();
break;
case 6:Delete_End();
break;
case 7:Delet_SpecifiedPos();
break;
case 8:exit(0);
break;
default:printf("Invalid choice\n");
break;
}
}
return 0;
}

You might also like