Link-List

Link-List :: Circular

#include<stdio.h>
#include<malloc.h>
#include<process.h>
#include<conio.h>

void create(int);
void display();
void insert_front();
void insert_last();
void insert_after();
void del_front();
void del_last();
void del_any();
void count();
void search();

struct linklist
{
int data;
struct linklist *link;
};
typedef struct linklist node;
node *head = (node *)malloc(sizeof(node));
void main()
{
head->data=NULL;
head->link=head;
int choice;
while(1)
{

printf(“\n1.Create\n2.Front\n3.Last\n4.Add After\n5.Del_First\n6.Del_Last\n7.Del_Any\n8.Count\n9.Search\n10.Display\n11.Exit\n”);
printf(“Enter your Choice:”);
scanf(“%d”,&choice);
clrscr();
switch(choice)
{
case 1:
int n;
printf(“Enter no. of nodes to be created:”);
scanf(“%d”,&n);
create(n);
break;
case 2:
insert_front();
break;
case 3:
insert_last();
break;
case 4:
insert_after();
break;
case 5:
del_front();
break;
case 6:
del_last();
break;
case 7:
del_any();
break;
case 8:
count();
break;
case 9:
search();
break;
case 10:
display();
break;
case 11:
abort();
default:
printf(“Invalid Selection\n”);
}
}
}

void create(int n)
{

for(int i=0;i<n;i++)
{
node *newnode,*temp;
newnode=(node *)malloc(sizeof(node));
printf(“Enter the value %d:”,i+1);
scanf(“%d”,&newnode->data);
temp=head;
while(temp->link != head)
temp=temp->link;
temp->link=newnode;
newnode->link=head;
}
}

void display()
{
if(head->link==head)
{
printf(“The list is empty\n”);
return;
}
node *temp=head->link;
while(temp != head)
{
printf(“%d\n”,temp->data);
temp=temp->link;
}
}

void insert_front()
{
node *temp,*newnode;
newnode=(node *)malloc(sizeof(node));
printf(“Enter the element to be inserted:”);
scanf(“%d”,&newnode->data);
newnode->link=head->link;
head->link=newnode;
}

void insert_last()
{
node *temp,*newnode;
newnode=(node *)malloc(sizeof(node));
printf(“Enter the element to be inserted:”);
scanf(“%d”,&newnode->data);
newnode->link=head;
temp=head;
while(temp->link != head)
temp=temp->link;
temp->link=newnode;
}

void del_front()
{
node *temp;
if(head->link == head)
{
printf(“The List is Empty\n”);
return;
}
temp=head->link;
head->link=temp->link;
free(temp);
}

void del_last()
{
node *next,*previous;
if(head->link == head)
{
printf(“The List is Empty\n”);
return;
}
previous=head;
next=head->link;
while(next->link != head)
{
previous=next;
next=next->link;
}
previous->link=head;
free(next);
}

void insert_after()
{
node *next,*previous,*newnode;
int key;
newnode=(node *)malloc(sizeof(node));
printf(“Enter the key after which you want to insert:”);
scanf(“%d”,&key);
printf(“Enter the element to be inserted:”);
scanf(“%d”,&newnode->data);
if(head->link==head)
{
head->link=newnode;
newnode->link=head;
}
else
{
previous=head;
next=head->link;
while(previous->link != head && previous->data !=key)
{
previous=next;
next=next->link;
}
if(previous->link == head)
{
previous->link=newnode;
newnode->link=head;
}
else
{
previous->link=newnode;
newnode->link=next;
}
}
}

void del_any()
{
if(head->link==head)
{
printf(“The list is empty\n”);
return;
}
node *previous,*next,*current;
previous=NULL;
current=head;
int key;
printf(“Enter the element to be deleted:”);
scanf(“%d”,&key);
while(current->link != head && current->data != key)
{
previous=current;
current=current->link;

}
if(current->link == head)
printf(“The key is not present in the list\n”);
else
{
previous->link=current->link;
free(current);
}
}

void count()
{
if(head->link==head)
{
printf(“The list is empty\n”);
return;
}
node *temp=head;
int counter=0;
while(temp->link != head)
{
counter++;
temp=temp->link;
}
printf(“There are %d nodes in the list\n”,counter);
}

void search()
{
if(head->link==head)
{
printf(“The list is empty\n”);
return;
}
int key,pos=0;
printf(“Enter the element to be searched:”);
scanf(“%d”,&key);
node *temp=head;
while(temp->link != head && temp->data != key)
{
pos++;
temp=temp->link;
}
if(temp->link==head)
printf(“Element not found\n”);
else
printf(“Specified Element is at %d position in the list\n”,pos);
}

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.