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

CLL C Program

This C program implements a circular linked list with functionalities to insert, delete, search, and display nodes. It provides a menu-driven interface for users to choose operations such as inserting at the beginning or end, deleting from either end, searching for an element, and displaying the list. The program handles memory allocation and checks for overflow and underflow conditions during operations.
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)
4 views6 pages

CLL C Program

This C program implements a circular linked list with functionalities to insert, delete, search, and display nodes. It provides a menu-driven interface for users to choose operations such as inserting at the beginning or end, deleting from either end, searching for an element, and displaying the list. The program handles memory allocation and checks for overflow and underflow conditions during operations.
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

// CLL C program

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert (); void lastinsert (); void randominsert();


void begin_delete(); void last_delete();
void random_delete(); void display();
void search(); void main ()
{
int choice =0;
while(choice != 7)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n======================\n");
printf("\n [Link] in beginning\n [Link] at last \n");
printf("[Link] from Beginning \n [Link] from last \n");
printf("[Link] for an element \n [Link]\n [Link] \n");
printf("\n Enter your choice?\n");
scanf("\n %d", &choice); switch(choice)
{
case 1: beginsert(); break;
case 2: lastinsert(); break;
case 3: begin_delete(); break;
case 4: last_delete(); break;
case 5: search(); break;
case 6: display(); break;
case 7: exit(0); break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr,*temp; int item;
ptr = (struct node *)malloc(sizeof(struct node));

if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
printf("\n Enter the node data?"); scanf("%d", &item);
ptr -> data = item; if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head) temp = temp->next;
ptr->next = head; temp -> next = ptr; head = ptr;
}
printf("\n node inserted \n");
}
}

void lastinsert()
{
struct node *ptr,*temp; int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW \n");
}

else
{
printf("\n Enter Data?"); scanf("%d", &item);
ptr->data = item; if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr; ptr -> next = head;
}

printf("\n node inserted\n");


}
}

void begin_delete()
{
struct node *ptr; if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == head)
{
head = NULL; free(head);
printf("\n node deleted \n");
}
else
{
ptr = head;
while(ptr -> next != head) ptr = ptr -> next;
ptr->next = head->next; free(head);
head = ptr->next; printf("\n node deleted \n");
}
}

void last_delete()
{
struct node *ptr, *preptr; if(head==NULL)
{
printf("\n UNDERFLOW");
}
else if (head ->next == head)
{
head = NULL; free(head);
printf("\n node deleted \n");
}
else
{
ptr = head;
while(ptr ->next != head)
{
preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next; free(ptr);
printf("\n node deleted \n");
}
}

void search()
{
struct node *ptr;
int item, i=0, flag=1; ptr = head;
if(ptr == NULL)
{
printf("\n Empty List \n");
}
else
{
printf("Enter item which you want to search? ");
scanf("%d", &item);
if(head ->data == item)
{
printf("item found at location %d",i+1); flag=0;
}
else
{
while (ptr->next != head)
{

if(ptr->data == item)
{

printf("item found at location %d ",i+1); flag=0;


break;
}
else
{
flag=1;
} i++;
ptr = ptr -> next;
}
}
if(flag != 0)
{
printf("Item not found\n");
}
}
}

void display()
{

struct node *ptr; ptr=head; if(head == NULL)


{
printf("\n nothing to print");
}
else
{
printf("\n printing values ... \n");

while(ptr -> next != head)


{
printf("%d\n", ptr -> data); ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}

You might also like