[go: up one dir, main page]

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

CLLMID C

The document is a C program that implements a circular singly linked list with functionalities to create a list, insert a node in the middle, delete a node from the middle, and display the list. It includes a menu-driven interface for user interaction and utilizes dynamic memory allocation for node creation. The program also handles edge cases such as empty lists and invalid positions for insertion and deletion.
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)
22 views4 pages

CLLMID C

The document is a C program that implements a circular singly linked list with functionalities to create a list, insert a node in the middle, delete a node from the middle, and display the list. It includes a menu-driven interface for user interaction and utilizes dynamic memory allocation for node creation. The program also handles edge cases such as empty lists and invalid positions for insertion and deletion.
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 <conio.h>
# include <stdlib.h>
struct cslinklist
{
int data;
struct cslinklist *next;
};
typedef struct cslinklist node;
node *start = NULL;
int nodectr;
node* getnode()
{
node * newnode;
newnode = (node *) malloc(sizeof(node));
printf("\n Enter data: ");
scanf("%d", &newnode -> data);
newnode -> next = NULL;
return newnode;
}int menu()
{
int ch;
clrscr();
printf("\n 1. Create a list ");
printf("\n\n--------------------------");
printf("\n 2. Insert a node at middle ");
printf("\n\n--------------------------");
printf("\n 3. Delete a node from middle");
printf("\n\n--------------------------");
printf("\n 4. Display the list");
printf("\n 5. Exit");
printf("\n\n--------------------------");
printf("\n Enter your choice: ");
scanf("%d", &ch);
return ch;
}
void createlist(int n)
{
int i;
node *newnode;
node *temp;
nodectr = n;
for(i = 0; i < n ; i++)
{
newnode = getnode();
if(start == NULL)
{
start = newnode;
}
else
{
temp = start;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;
}
}
newnode ->next = start; /* last node is pointing to starting node */
}
void display()
{
node *temp;
temp = start;
printf("\n The contents of List (Left to Right): ");
if(start == NULL )
printf("\n Empty List");
else
{
do
{
printf("\t %d ", temp -> data);
temp = temp -> next;
} while(temp !=
start); printf(" X ");
}
}
void cll_insert_mid()
{
node *newnode, *temp, *prev;
int i, pos ;
newnode = getnode();
printf("\n Enter the position: ");
scanf("%d", &pos);
if(pos > 1 && pos < nodectr)
{
temp = start;
prev = temp;
i = 1;
while(i < pos)
{
prev = temp;
temp = temp -> next;
i++;
}
prev -> next = newnode;
newnode -> next = temp;nodectr++;
printf("\n Node inserted at middle..");
}
else
{
printf("position %d of list is not a middle position ", pos);
}
}
void cll_delete_mid()
{
int i = 0, pos;
node *temp, *prev;
if(start == NULL)
{
printf("\n No nodes exist..");
getch();
return ;
}
else
{
printf("\n Which node to delete: ");
scanf("%d", &pos);
if(pos > nodectr)
{
printf("\nThis node does not exist");
getch();
return;
}
if(pos > 1 && pos < nodectr)
{
temp=start;
prev = start;
i = 0;
while(i < pos - 1)
{
prev = temp;
temp = temp -> next ;
i++;
}
prev -> next = temp -> next;
free(temp);
nodectr--;
printf("\n Node Deleted..");
}
else
{
printf("\n It is not a middle position..");
getch();
}
}
}
void main(void)
{
int result;
int ch, n;
clrscr();
while(1)
{
ch = menu();
switch(ch)
{
case 1 :
if(start == NULL)
{
printf("\n Enter Number of nodes to create: ");
scanf("%d", &n);
createlist(n);
printf("\nList created..");
}else
printf("\n List is already Exist..");
break;
case 2 :
cll_insert_mid();
break;
case 3 :
cll_delete_mid();
break;
case 4 :
display();
break;
case 5 :
exit(0);
}
getch();
}
}

You might also like