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

Weekend Homework Anjali Sharma 19MCA0210: Q1. Write A Program To Implement A Circular Doubly Linked List Where The Head

The document contains C code to implement a circular doubly linked list where the head pointer points to the last node. It includes functions to insert nodes at the beginning, middle, and end of the list as well as delete nodes from the beginning, middle, and end of the list. It also contains a display function to print out the nodes in the list. The main function uses a switch case to call the various functions based on a user's choice.

Uploaded by

Anjji Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Weekend Homework Anjali Sharma 19MCA0210: Q1. Write A Program To Implement A Circular Doubly Linked List Where The Head

The document contains C code to implement a circular doubly linked list where the head pointer points to the last node. It includes functions to insert nodes at the beginning, middle, and end of the list as well as delete nodes from the beginning, middle, and end of the list. It also contains a display function to print out the nodes in the list. The main function uses a switch case to call the various functions based on a user's choice.

Uploaded by

Anjji Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Weekend Homework

Anjali Sharma
19MCA0210

Q1. Write a program to implement a circular doubly linked list where the head
pointer points to the last node.

#include <stdio.h>

#include <stdlib.h>

struct stu

int rno;

struct stu *next, *prev;

};

struct stu *head = NULL;

void insertb(int rno)

struct stu *curr = (struct stu*)malloc(sizeof(struct stu));

curr->rno = rno;

curr->prev = curr;
curr->next = curr;

if(head==NULL)

head=curr;

else

(head->next)->prev= curr;

curr->next=head->next;

curr->prev=head;

head->next=curr;

printf("inserted successfully\n");

void midi(int rno)

int sno;

struct stu *temp=head;

struct stu *curr = (struct stu*)malloc(sizeof(struct stu));

curr->rno = rno;

curr->prev = curr;

curr->next = curr;
if(head==NULL)

head=curr;

printf(" Inserted as first node");

else

printf("enter rno after which you wanna insert the node \n");

scanf("%d",&rno);

while(temp->rno!=sno && temp->next!=head)

temp=temp->next;

if(temp->next==head && temp->rno!=sno)

printf("inserting in end as element is not found\n");

curr->prev=head;

curr->next=head->next;

(head->next)->prev=curr;

head->next=curr;

head=curr;

else{

curr->next=temp->next;

(temp->next)->prev=curr;

curr->prev=temp;

temp->next=curr;

}
}

printf(" inserted successfully\n");

void inserte(int rno)

struct stu *curr = (struct stu*)malloc(sizeof(struct stu));

curr->rno = rno;

curr->prev = curr;

curr->next = curr;

if(head==NULL)

head=curr;

else

curr->next=head->next;

(head->next)->prev=curr;

head->next=curr;

curr->prev=head;

head=curr;

printf("inserted successfully");

void deleteb()
{

struct stu *temp;

if(head==NULL)

printf("Empty list");

else

temp=head->next;

head->next=(head->next)->next;

(temp->next)->prev=head;

printf(" deleted successfully");

free(temp);

void midd(int rno_del)

struct stu *temp=head;

if(head==NULL)

printf("list is empty");

return;

else

while(temp->rno!=rno_del && temp->next!=head)

temp=temp->next;
if(temp->next==head && temp->rno!=rno_del)

printf("Element not found\n");

else

(temp->prev)->next=temp->next;

(temp->next)->prev=temp->prev;

printf(" deleted successfully\n");

free(temp);

void end_delete()

struct stu *temp=head,*temp2;

if(head==NULL)

printf("empty list");

else

(head->next)->prev=head->prev;

(head->prev)->next=head->next;

head=head->prev;

printf("\nRecord with employee number: %d deleted successfully\n",temp->rno);

free(temp);

}
}

void display()

struct stu *temp= head;

if(head==NULL)

printf("Nothing to delete");

else{

printf("\nRecords in list are:\n \n");

do

printf("roll number %d", (temp->next)->rno);

printf("\n");

temp=temp->next;

}while(temp!=head);

void main()

int rno,choice,rno_del;
do

printf("Enter 1 for insertion in beginning, \n 2 for insertion in middle,\n 3 for insertion in end, \n 4
for delete from beginning, \n 5 for delete from middle, \n 6 for delete from end, \n 7 for display \n 8 for
exit \n");

scanf("%d", &choice);

switch(choice)

case 1: printf("Enter roll number\n");

scanf("%d",&rno);

insertb(rno);

break;

case 2: printf("Enter roll number \n");

scanf("%d",&rno);

midi(rno);

break;

case 3: printf("Enter roll number \n");

scanf("%d ",&rno);

inserte(rno);

break;

case 4: deleteb();

break;

case 5: printf("Enter roll number of stu you want to delete \n");


scanf("%d",&rno_del);

midd(rno_del);

break;

case 6: end_delete();

break;

case 7: display();

break;

case 8: printf("exit");

}while(choice!=8);
}

You might also like