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

Experiment No-7

The document outlines an experiment for implementing a circular linked list in C, detailing the problem statement, objectives, and theory behind circular linked lists. It provides a complete program code that includes functionalities for creating, displaying, inserting, and deleting nodes in the list. The conclusion confirms the successful implementation of the circular linked list and its operations.
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)
7 views10 pages

Experiment No-7

The document outlines an experiment for implementing a circular linked list in C, detailing the problem statement, objectives, and theory behind circular linked lists. It provides a complete program code that includes functionalities for creating, displaying, inserting, and deleting nodes in the list. The conclusion confirms the successful implementation of the circular linked list and its operations.
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

Department of Computer Engineering

SE: SEM III Subject: DS

Experiment No. 7
AIM: Implementation of Circular Linked List

PROBLEM STATEMENT: Given a set of integer values as data items, write a C


program to implement a circular linked list and perform operations of insertion,
deletion and display.

OBJECTIVE: The objective of the program is to design, develop and implement a


menu driven Program in C for implementing a circular linked list and performing
insertion and deletion operations at beginning and end.

THEORY:
Circular Linked List is a variation of Linked list in which the first element points to
the last element and the last element points to the first element. Both Singly
Linked List and Doubly Linked List can be made into a circular linked list.

Circular Singly Linked List


In circular singly linked list, the next pointer of the last node points to the first
node.

​ Circular Doubly Linked List


In circular doubly linked list, the next pointer of the last node points to the first
node and the previous pointer of the first node points to the last node making the
circular in both directions.
Program Code:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *createLL(struct node *);
struct node *insert_beg(struct node *);
struct node *insert_end(struct node *);
struct node *delete_beg(struct node *);
struct node *delete_end(struct node *);
struct node *display(struct node *);
int main(){
int option;
do{
printf("\n* * * MAIN MENU * * *");
printf("\n1. Create a LinkedList");
printf("\n2. Display the LinkedList");
printf("\n3. Insert Node at Beginning");
printf("\n4. Insert Node at End");
printf("\n5. Delete Node at Beginning");
printf("\n6. Delete Node at End");
printf("\n7. EXIT");
printf("\nEnter your Option - ");
scanf("%d", &option);
switch(option){
case 1:
start = createLL(start);
printf("\n\tCircular Singly LinkedList created.\n");
break;
case 2:
start = display(start);
break;
case 3:
start = insert_beg(start);
break;
case 4:
start = insert_end(start);
break;
case 5:
start = delete_beg(start);
break;
case 6:
start = delete_end(start);
break;
}
}while(option != 7);
return 0;
}
struct node *createLL(struct node *start){
struct node *newnode, *ptr;
int val;
printf("\n Enter -1 to End");
printf("\n Enter the data - ");
scanf("%d", &val);
while(val != -1){
newnode = (struct node *)malloc(sizeof(struct node *));
newnode->data = val;
if (start == NULL){
newnode->next = newnode;
start = newnode;
}
else{
ptr = start;
while(ptr->next != start){
ptr = ptr->next;
}
ptr->next = newnode;
newnode->next = start;
}
printf("\n Enter the data - ");
scanf("%d", &val);
}
return start;
}
struct node *insert_beg(struct node *start){
struct node *newnode, *ptr;
int val;
printf("\nEnter the data - ");
scanf("%d", &val);
newnode = (struct node *)malloc(sizeof(struct node *));
newnode->data = val;
ptr = start;
while(ptr->next != start){
ptr = ptr->next;
}
ptr->next = newnode;
newnode->next = start;
start = newnode;
return start;
}
struct node *insert_end(struct node *start){
struct node *newnode, *ptr;
int val;
printf("\nEnter the data - ");
scanf("%d", &val);
newnode = (struct node *)malloc(sizeof(struct node *));
newnode->data = val;
ptr = start;
while (ptr->next != start){
ptr = ptr->next;
}
ptr->next = newnode;
newnode->next = start;
return start;
}
struct node *delete_beg(struct node *start){
struct node *ptr;
if (start == NULL){
printf("\n\tUNDERFLOW.\n");
return start;
}
ptr = start;
while(ptr->next != start){
ptr = ptr->next;
}
ptr->next = start->next;
printf("\nElement Deleted - %d\n", start->data);
free(start);
start = ptr->next;
return start;
}
struct node *delete_end(struct node *start){
struct node *ptr, *preptr;
if (start == NULL){
printf("\n\tUNDERFLOW.\n");
return start;
}
ptr = start;
while(ptr->next != start){
preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr->next;
printf("\nElement Deleted - %d\n", ptr->data);
free(ptr);
return start;
}
struct node *display(struct node *start){
struct node *ptr;
if (start == NULL){
printf("\n\tUNDERFLOW.\n");
return start;
}
ptr = start;
printf("\n");
while(ptr->next != start){
printf("\t %d", ptr->data);
ptr = ptr->next;
}
printf("\t %d\n", ptr->data);
return start;
}

Program Output:
CONCLUSION: A circular linked list is implemented and operations of insertion,
deletion and display are performed on it.

You might also like