Circular singly linked list Implementation
Circular Linked List Implementation
#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
struct Node
{
int item;
struct Node *next;
};
void insertAtLast(struct Node **l, int data)
{
struct Node *n;
n = (struct Node*) malloc(sizeof(struct Node));
n-> item = data;
if(*l==NULL)
{
*l = n;
n->next = n;
}
else
{
n->next = (*l)->next;
(*l)->next = n;
*l = n;
}
}
// * function to insert at First
void insertAtFirst(struct Node **l,int data)
{
struct Node *n;
n = (struct Node*) malloc(sizeof(struct Node));
n-> item = data;
if(*l==NULL)
{
*l = n;
n->next = n;
}
else
{
n->next = (*l)->next;
(*l)->next = n;
}
}
//* to insert after given node
void insertAfterNode(struct Node **l,struct Node *temp,int data)
{
struct Node *n,*r;// this node will have address of first node
n = (struct Node*) malloc(sizeof(struct Node));
n-> item = data;
r = (*l)->next; // have address of first node
if(*l == NULL)
{
printf("Insert After not possible , list is empty, try to add some values first\n");
}
else if(r == temp)
{
insertAtFirst(l,data);
}
else if(temp==*l)
{
insertAtLast(l,data);
}
else
{
while(r->next != temp)
r = r->next;
n->next = temp->next;
temp->next = n;
}
}
struct Node* findElement(struct Node *l,int element)
{
struct Node *t;
t = (l)->next; // first node address
do
{
if(t->item == element)
return t;
t = t->next;
}while((t!= (l)->next));
return NULL;
}
void deleteAtFirst(struct Node **l)
{
struct Node *t;
t = (*l)->next ; // first node address
if(t==*l)
{
free(t);
*l = NULL;
}
else if(*l == NULL)
printf("List is empty\n");
else
{
(*l)->next= t->next;
free(t);
}
}
void deleteAtLast(struct Node **l)
{
struct Node *t,*r;
t=*l;
if(*l ==NULL)
printf("List is empty");
else if(t->next == *l)
{
free(*l);
*l =NULL;
}
else
{
while(t->next!=*l)
t=t->next;
r = t->next;
t->next = r->next;
*l = t;
free(r);
}
}
void deleteNode(struct Node *temp,struct Node **l)
{
struct Node *r;
r = (*l)->next ; // first node address
if(*l == NULL)
printf("Deletion Error: Node not found\n");
else if(temp == r)
deleteAtFirst(l);
else
{
while(r->next!=temp)
r = r->next;
if(r->next == *l)
{
r->next = (*l)->next;
*l = r;
}
else
r->next = temp->next;
free(temp);
}
}
void viewList(struct Node *last)
{
struct Node *t;
if(last!=NULL)
{
t = last;
do
{
t = t->next;
printf("%d ",t->item);
}
while(t->next != last->next);
}
}
int menu()
{
int choice;
printf("\n\nCircular linked list implementation\n");
printf("\n1.Insert node at first ");
printf("\n2.Insert node at last");
printf("\n3.Insert Node after given node");
printf("\n4.Delete first node");
printf("\n5.Delete last node");
printf("\n6.Delete particular node");
printf("\n7. Exit...");
printf("\nEnter your choice\n");
scanf("%d",&choice);
return choice;
}
int main()
{
struct Node *last = NULL;
struct Node *temp;
int data,element;
while(1)
{
printf("Data : ");
viewList(last);
system("clear");
switch(menu())
{
case 1:
printf("\nEnter the value to be inserted\n");
scanf("%d",&data);
insertAtFirst(&last,data);
break;
case 2:
printf("\nEnter the value to be inserted after\n");
scanf("%d",&data);
insertAtLast(&last,data);
break;
case 3:
printf("\nEnter the value after which new data is to be inserted\n");
scanf("%d",&element);
temp = findElement(last,element);
printf("\nEnter data to be inserted now\n");
scanf("%d",&data);
if(temp!=NULL)
insertAfterNode(&last,temp,data);
else
printf("\nElement not found\n");
break;
case 4:
printf("\nDeleting first node\n");
deleteAtFirst(&last);
break;
case 5:
printf("\nDeleting last node\n");
deleteAtLast(&last);
break;
case 6:
printf("\nEnter the node you want to delete\n");
scanf("%d",&element);
temp = findElement(last,element);
if(temp!=NULL)
deleteNode(temp,&last);
else
printf("\n Node not found\n");
break;
case 7:
exit(0);
default:
printf("\nEnter a valid choice");
}
}
return 0;
}
Comments
Post a Comment