0% found this document useful (0 votes)
8 views

Doublelinklist PRGM

This C program implements a doubly linked list with functions to insert nodes at the beginning, end or after a specified key. It also includes functions to delete nodes from the beginning, end or by key. The main function provides a menu to call these functions and display the list.

Uploaded by

shehin8345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Doublelinklist PRGM

This C program implements a doubly linked list with functions to insert nodes at the beginning, end or after a specified key. It also includes functions to delete nodes from the beginning, end or by key. The main function provides a menu to call these functions and display the list.

Uploaded by

shehin8345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<stdio.

h>
#include<stdlib.h>
struct node{
int data;
struct node* llink;
struct node* rlink;
}*head = NULL, *ptr = NULL, *temp = NULL;
int data, key;
void display(){
if(head == NULL){
printf("List empty\n");
}
else{
ptr = head;
while(ptr != NULL){
printf("|%d", ptr->data);
ptr = ptr->rlink;
}
printf("|\n");
}
}
void insertFirst(){
temp = (struct node*)malloc(sizeof(struct node));
if(temp == NULL){
printf("Memory underflow\n");
}
else{
printf("Enter value to insert: ");
scanf("%d", &data);
temp->data = data;
temp->rlink = head;
temp->llink = NULL;
if(head != NULL){
head->llink = temp;
}
head = temp;
display();
}
}
void insertLast(){
temp = malloc(sizeof(struct node));
if(temp == NULL){
printf("Memory underflow\n");
}
else{
printf("Enter value to insert: ");
scanf("%d", &data);
temp->data = data;
temp->rlink = NULL;
if(head == NULL){
head = temp;
temp->llink = NULL;
}
else{
ptr = head;
while(ptr->rlink != NULL){
ptr = ptr->rlink;
}
temp->llink = ptr;
ptr->rlink = temp;
}
display();
}
}
void insertKey(){
temp = malloc(sizeof(struct node));
if(temp == NULL){
printf("Memory underflow\n");
}
else{
printf("Enter key to insert it after: ");
scanf("%d", &key);
ptr = head;
while(ptr != NULL && ptr->data != key){
ptr = ptr->rlink;
}
if(ptr == NULL){
printf("Key not in list\n");
}
else{
printf("Enter value to insert: ");
scanf("%d", &data);
temp->data = data;
temp->llink = ptr;
temp->rlink = ptr->rlink;
if(ptr->rlink != NULL){
ptr->rlink->llink = temp;
}
ptr->rlink = temp;
display();
}
}
}
void deleteFirst(){
if(head == NULL){
printf("List empty\n");
}
else{
printf("%d is deleted\n", head->data);
ptr = head;
head = head->rlink;
if(head != NULL){
head->llink = NULL;
}
free(ptr);
ptr = NULL;
display();
}
}
void deleteLast(){
if(head == NULL){
printf("List empty\n");
}
else{
ptr = head;
while(ptr->rlink != NULL){
ptr = ptr->rlink;
}
printf("%d is deleted\n", ptr->data);
if(ptr == head){
free(ptr);
head = ptr = NULL;
}
else{
ptr->llink->rlink = NULL;
free(ptr);
ptr = NULL;
}
display();
}
}
void deleteKey(){
if(head == NULL){
printf("List empty\n");
}
else{
printf("Enter key to delete: ");
scanf("%d", &key);
ptr = head;
while(ptr != NULL && ptr->data != key){
ptr = ptr->rlink;
}
if(ptr == NULL){
printf("Key not in list\n");
}
else{
printf("%d is deleted\n", ptr->data);
if(ptr == head){
head = head->rlink;
free(ptr);
ptr = NULL;
}
else{
ptr->llink->rlink = ptr->rlink;
if(ptr->rlink != NULL){
ptr->rlink->llink = ptr->llink;
}
free(ptr);
ptr = NULL;
}
display();
}
}
}
int main(void){
int ch = 0;
printf("1. Insert at first\n2. Insert at last\n3. Insert after key\n4. Delete fir
st\n5. Delete last\n6. Delete key\n7. Display\n8. Exit\n");
while(ch != 8){
printf("\nChoose(1-8): ");
scanf("%d", &ch);
switch(ch){
case 1: insertFirst();
break;
case 2: insertLast();
break;
case 3: insertKey();
break;
case 4: deleteFirst();
break;
case 5: deleteLast();
break;
case 6: deleteKey();
break;
case 7: display();
break;
case 8: printf("Exiting program...\n");
break;
default:printf("Invalid input\n\n");
break;
}
}
return(0);
}

You might also like