0% found this document useful (0 votes)
6 views6 pages

Program - 6 (D) Rajat

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)
6 views6 pages

Program - 6 (D) Rajat

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/ 6

Program 6(d)

Program Name – Doubly linked list Name – Rajat Sharma


Domain – Linked list Roll no - 2300320130187
Problem statement – Write a program for Do linkedlist implementation
Code :-

#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
struct node *prev;
};
struct node *start = NULL;
struct node *last = NULL;
void create();
void traverse_forward();
void traverse_backward();
void insert_begin();
void insert_last();
void insert_after_specific();
void delete_first();
void delete_last();
void delete_specific();
void count_node();
int main() {
int ch;
while (1) {
printf("\n1. Create a List\n2. Display Forward\n3. Display Backward\n4. Insert Element at First
Position\n5. Insert Element at Last Position\n6. Insert Element After Specific Location\n7. Delete
First Element\n8. Delete Last Element\n9. Delete Specific Node\n10. Exit\n");
printf("Enter Your Choice: ");
scanf("%d", &ch);
switch (ch) {
case 1: create(); break;
case 2: traverse_forward(); break;
case 3: traverse_backward(); break;
case 4: insert_begin(); break;
case 5: insert_last(); break;
case 6: insert_after_specific(); break;
case 7: delete_first(); break;
case 8: delete_last(); break;
case 9: delete_specific(); break;
case 10: exit(0); break;
default: printf("\nEnter a Valid Choice\n");
}
Program 6(d)
Program Name – Doubly linked list Name - Rajat Sharma
Domain – Linked list Roll no - 2300320130187
Problem statement – Write a program for Doubly linkedlist implementation
Code :-

}
}
void create() {
int ch;
struct node *temp = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the data: ");
scanf("%d", &temp->data);
temp->next = temp->prev = NULL;
start = last = temp;
do {
struct node *temp1 = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the data: ");
scanf("%d", &temp1->data);
temp1->next = NULL;
temp1->prev = last;
last->next = temp1;
last = temp1;
printf("Do you want to add more data? [1/0]: ");
scanf("%d", &ch);
} while (ch == 1);
}
void traverse_forward() {
if (start == NULL) {
printf("Doubly Linked List is Empty\n");
} else {
struct node *temp = start;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
}
void traverse_backward() {
if (start == NULL) {
printf("Doubly Linked List is Empty\n");
} else {
struct node *temp = last;
while (temp != NULL) {
printf("%d ", temp->data);
Program 6(d)
Program Name – Doubly linked list Name - Rajat Sharma
Domain – Linked list Roll no- 2300320130187
Problem statement – Write a program for Doubly linkedlist implementation
Code :-

temp = temp->prev;
}
printf("\n");
}
}
void insert_begin() {
struct node *temp = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the data: ");
scanf("%d", &temp->data);
temp->prev = NULL;
if (start == NULL) {
temp->next = NULL;
start = last = temp;
} else {
temp->next = start;
start->prev = temp;
start = temp;
}
}
void insert_last() {
struct node *temp = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the data: ");
scanf("%d", &temp->data);
temp->next = NULL;
if (start == NULL) {
temp->prev = NULL;
start = last = temp;
} else {
temp->prev = last;
last->next = temp;
last = temp;
}
}
void insert_after_specific() {
struct node *temp1 = start;
struct node *temp = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the data: ");
scanf("%d", &temp->data);
int x;
printf("\nEnter the node after which you want to insert: ");
Program 6(d)
Program Name – Doubly linked list Name - Rajat Sharma
Domain – Linked list Roll no - 2300320130187
Problem statement – Write a program for Doubly linkedlist implementation
Code :-

scanf("%d", &x);
while (temp1 != NULL && temp1->data != x) {
temp1 = temp1->next;
}
if (temp1 == NULL) {
printf("\nNode not found\n");
free(temp);
return;
}
temp->next = temp1->next;
temp->prev = temp1;
if (temp1->next != NULL) {
temp1->next->prev = temp;
} else {
last = temp;
}
temp1->next = temp;
}
void delete_first() {
if (start == NULL) {
printf("\nList is empty\n");
} else {
struct node *temp = start;
start = start->next;
if (start != NULL) {
start->prev = NULL;
} else {
last = NULL;
}
free(temp);
}
}
void delete_last() {
if (start == NULL) {
printf("\nList is empty\n");
} else {
struct node *temp = last;
last = last->prev;
if (last != NULL) {
last->next = NULL;
Program 6(d)
Program Name – Doubly linked list Name - Rajat Sharma
Domain – Linked list Roll no- 2300320130187
Problem statement – Write a program for Doubly linkedlist implementation
Code :-

} else {
start = NULL;
}
free(temp);
}
}
void delete_specific() {
if (start == NULL) {
printf("\nList is empty\n");
} else {
int x;
printf("\nEnter the node value to delete: ");
scanf("%d", &x);
struct node *temp = start;
while (temp != NULL && temp->data != x) {
temp = temp->next;
}
if (temp == NULL) {
printf("\nNode not found\n");
return;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
} else {
start = temp->next;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
} else {
last = temp->prev;
}
free(temp);
}
}
void count_node() {
int count = 0;
struct node *temp = start;
while (temp != NULL) {
count++;
temp = temp->next;
}
printf("Total nodes: %d\n", count);
}

Time complexity :- O(n)


Space complexity :- O(n)

Program 6(d)
Program Name – Doubly linked list Name - Rajat Sharma
Domain – Linked list Roll no- 2300320130187
Problem statement – Write a program for Doubly linkedlist implementation
Output :-

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