0% found this document useful (0 votes)
22 views9 pages

CLL

ciruclar linked list

Uploaded by

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

CLL

ciruclar linked list

Uploaded by

gosalacharvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* head = NULL;

void create();

void insert_begin();

void insert_last();

void insert_after();

void delete_begin();

void delete_last();

void delete_info();

void display();

void main() {

int ch = 0;

while (1) {

printf("\n_____________________________________");

printf("\n CIRCULAR LINKED LIST ADT OPERATIONS ARE:\n");

printf("_____________________________________");

printf("\n\[Link]");

printf("\n\[Link] AT THE BEGINNING");

printf("\n\[Link] AFTER THE GIVEN INFO:");

printf("\n\[Link] AT THE END");

printf("\n\[Link] AT THE BEGINNING");


printf("\n\[Link] AT THE END:");

printf("\n\[Link] AT GIVEN INFO");

printf("\n\[Link]");

printf("\n\[Link]");

printf("\n Enter ur choice:");

scanf("\n%d", &ch);

switch (ch) {

case 1:

create();

break;

case 2:

insert_begin();

break;

case 3:

insert_last();

break;

case 4:

insert_after();

break;

case 5:

delete_begin();

break;

case 6:

delete_last();

break;

case 7:

delete_info();

break;

case 8:

display();
break;

case 9:exit(0);

printf("Exiting...\n");

break;

default:

printf("Invalid choice. Please try again.\n");

void create() {

int ch;

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

printf("Enter data for the first node: ");

scanf("%d", &newNode->data);

head = newNode;

newNode->next = head;

struct Node* temp = head;

printf("Enter 1 to add more nodes, 0 to stop: ");

scanf("%d", &ch);

while (ch == 1) {

newNode = (struct Node*)malloc(sizeof(struct Node));

printf("Enter data for the next node: ");

scanf("%d", &newNode->data);

newNode->next = head;

temp->next = newNode;
temp = newNode;

printf("Enter 1 to add more nodes, 0 to stop: ");

scanf("%d", &ch);

void insert_begin() {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

printf("Enter data to insert at beginning: ");

scanf("%d", &newNode->data);

if (head == NULL) {

head = newNode;

newNode->next = head;

} else {

struct Node* temp = head;

while (temp->next != head) {

temp = temp->next;

newNode->next = head;

temp->next = newNode;

head = newNode;

printf("\nNode inserted");

void insert_last() {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

printf("Enter data to insert at last: ");

scanf("%d", &newNode->data);
if (head == NULL) {

head = newNode;

newNode->next = head;

} else {

struct Node* temp = head;

while (temp->next != head) {

temp = temp->next;

temp->next = newNode;

newNode->next = head;

printf("\nNode inserted");

void insert_after()

int loc;

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

printf("Enter node information to insert: ");

scanf("%d", &newNode->data);

printf("Enter location: ");

scanf("%d", &loc);

if(loc == 1) {

newNode->next = head;

struct Node* temp = head;

while (temp->next != head) {

temp = temp->next;

temp->next = newNode;

head = newNode;
} else {

struct Node* temp = head;

for (int i = 1; i < loc; i++) {

if (temp->next == head) {

printf("Position out of range.\n");

free(newNode);

return;

temp = temp->next;

newNode->next = temp->next;

temp->next = newNode;

printf("\nNode inserted");

void delete_begin() {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = head;

if (temp->next == head) {

free(head);

head = NULL;

} else {

struct Node* last = head;

while (last->next != head) {

last = last->next;
}

head = head->next;

last->next = head;

printf("\n deleted element is :%d",temp->data);

free(temp);

void delete_last() {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = head;

if (temp->next == head) {

free(head);

head = NULL;

} else {

struct Node* prev;

while (temp->next != head) {

prev = temp;

temp = temp->next;

prev->next = head;

printf("\n deleted element is :%d",temp->data);

free(temp);

}
void delete_info() {

int position;

printf("Enter position to delete: ");

scanf("%d", &position);

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = head;

if (position == 1) {

struct Node* last = head;

while (last->next != head) {

last = last->next;

head = head->next;

last->next = head;

free(temp);

} else {

struct Node* prev;

for (int i = 1; i < position; i++) {

prev = temp;

temp = temp->next;

if (temp == head) {

printf("cannot delete.\n");

return;

prev->next = temp->next;
printf("\n deleted element is :%d",temp->data);

free(temp);

void display() {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = head;

do {

printf("%d -> ", temp->data);

temp = temp->next;

} while (temp != head);

printf("(head)\n");

You might also like