/////// Created By ///////////////////////////////
/// ///
/// ///
/// MUHAMMED ISMAIL PP ///
/// ///
/// ///
/////////////////////////////////////////////////////////////
#include<stdio.h>
#include<stdlib.h>
void create(void);
void sorted_create(void);
void display(void);
void insert_first(void);
void insert_btwn(int x);
void delete(int data);
void reverse(void);
char s[5];
struct clist
{
int data;
struct clist *next;
}*start=NULL;
///////////////////Main////////////////////////////////////////
int main()
{
while(1)
{
system("clear");
printf("\n");
printf(" ---------------------Circular Link List Data Structure------------- \n\n");
printf(" 1--Create \n\n");
printf(" 2--view \n\n");
printf(" 3--insert\n\n");
printf(" 4--Delete\n\n");
printf(" 5--Reverse\n\n");
printf(" 6--Exit\n\n");
printf(" ------------------------------------------------------------------- \n\n");
int x;
int i;
printf("Enter choice :");
scanf("%d",&x);
switch(x)
{
case 1:
{
system("clear");
printf("\nSelect your choice :\n");
printf("\n--------------------------------------------\n");
printf("\n 1)---Create Normal Structure :\n");
printf("\n 2)---Create Sorted Structure :\n");
printf("\n--------------------------------------------\n");
scanf("%d",&x);
switch (x)
{
case 1:
{
printf("\n\n\nEnter the number of elements :");
scanf("%d",&x);
for(i=0;i<x;i++)
{
printf("\nEnter the data %d = ",i+1);
create();
}
printf("\nYour Data Structure Successfully Created");
printf("\n-----------------------------------\n");
sleep(1);
break;
}
case 2:
{
printf("\n\n\nEnter the number of elements :");
scanf("%d",&x);
for(i=0;i<x;i++)
{
printf("\nEnter the data %d = ",i+1);
sorted_create();
}
printf("\nYour Data Structure Successfully Created");
printf("\n-----------------------------------\n");
sleep(1);
break;
}
}
break;
}
case 2:
{
gets(s);
display();
break;
}
case 3:
{
printf("\nEnter the position :");
scanf("%d",&x);
if(x==1)
{
insert_first();
}
else
{
insert_btwn(x);
}
break;
}
case 4:
{
printf("\nEnter the data for delete : ");
scanf("%d",&x);
gets(s);
delete(x);
break;
}
case 5:
{
reverse();
printf("\nYour Data Structure Successfully Reversed\n");
printf("-------------------------------------------\n");
sleep(2);
break;
}
case 6:
{
return;
break;
}
default:
{
gets(s);
printf("\n\nEnter valid Choice : Press Any Key to Return : ");
gets(s);
break;
}
}
}
return ;
}
///////////////////////Create//////////////////////////////////
void create(void)
{
struct clist *temp,*tr;
temp=malloc(sizeof(struct clist));
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
{
start=temp;
temp->next=temp;
}
else
{
tr=start;
while(tr->next !=start)
{
tr=tr->next;
}
tr->next=temp;
temp->next=start;
}
}
/////////////////////////////////////////////////////////////////////
void display(void)
{
char s[5];
int ct=0;
printf("\n\n\n\n\n\n\n START\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" ");
struct clist *tr;
tr=start;
if(start==NULL)
{
printf("--> No Data");
}
else
{
while (tr->next!=start)
{
printf("--> %d ",tr->data);
tr=tr->next;
ct=ct+7;
}
printf("--> %d",tr->data); //to print last element
ct=ct+7;
}
///////////
if(start!=NULL)
{
printf("\n");
printf("%*c",15,'|');
printf("%*c\n",ct,'|');
printf("%*c",15,'|');
printf("%*c\n",ct,'|');
printf("%*c",15,'|');
printf("%*c\n",ct,'|');
printf("%*c",15,'<');
int i;
for(i=0;i<ct;i++)
{printf("-");}
}
//////////////////
printf("\n\n\n\n\n\n\nPRESS ENTER KEY");
gets(s);
}
////////////////////////////////////////////////////////////////////////
void insert_first(void)
{
struct clist *temp,*tr;
temp=malloc(sizeof(struct clist));
printf("\nEnter the data : ");
scanf("%d",&temp->data);
tr=start;
temp->next=start;
while(tr->next!=start)
{
tr=tr->next;
}
tr->next=temp;
start=temp;
printf("\nYour Data Successfully Saved in First Position");
printf("\n-----------------------------------\n");
sleep(1);
}
///////////////////////////////////////////////////////////////////////
void insert_btwn(int x)
{
int i;
struct clist *tr,*temp;
tr=start;
for(i=0;i<x-2;i++)
{
tr=tr->next;
}
temp=malloc(sizeof(struct clist));
printf("\nEnter the data : ");
scanf("%d",&temp->data);
temp->next=tr->next;
tr->next=temp;
printf("\nYour Data Successfully Saved in Position %d ",x);
printf("\n-----------------------------------\n");
sleep(1);
}
////////////////////////////////////////////////////////////////////////
void delete(int data)
{
struct clist *temp,*tr;
////////// if only one data /////////////
if(start->next==start&&start->data==data)
{
temp=start;
start=NULL;
free(temp);
printf("\nYour Data1 %d is Successfully Deleted ",data);
printf("\n-----------------------------------\n");
sleep(2);
return;
}
tr=start->next;
////////// data in starting positoin ///////
if(tr->data==data)
{
temp=tr;
start->next=temp->next;
free(temp);
printf("\nYour Data2 %d is Successfully Deleted ",data);
printf("\n-----------------------------------\n");
sleep(2);
return;
}
////////// if middle ////////////////////////////////
while(tr->next!=start)
{
if(tr->next->data==data)
{
temp=tr->next;
tr->next=temp->next;
free(temp);
printf("\nYour Data3 %d is Successfully Deleted",data );
printf("\n----------------------------------\n");
sleep(2);
return ;
}
tr=tr->next;
}
///////// Last //////////////////////////////////////
if(tr->next->data==data)
{
temp=tr->next;
tr->next=temp->next;
free(temp);
start=tr;
printf("\nYour Data4 %d is Successfully Deleted",data);
printf("\n----------------------------------\n");
sleep(2);
}
else
{
printf("\nThis data not exist");
printf("\n\n\n\n\n\n\n\n\npress any key ");
gets(s);
}
}
//////////////Reverse /////////////////////////////////////////////////
void reverse(void)
{
struct clist *temp,*tr0,*tr;
temp=malloc(sizeof(struct clist));
tr=tr0=start;
temp=start->next;
start->next=NULL;
while(temp!=start)
{
tr=temp;
temp=temp->next;
tr->next=tr0;
tr0=tr;
}
start->next=tr;
start=tr;
}
////////////////////////////////////////////////////////////////////////
void sorted_create(void)
{
struct clist *temp,*tr;
temp=malloc(sizeof(struct clist));
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL||start->data>temp->data)
{
if(start==NULL)
{
start=temp;
start->next=start;
}
start->next=temp;
temp->next=start;
start=temp;
return;
}
else
{
tr=start;
while(tr->next!=start&&tr->next->data<temp->data)
{
tr=tr->next;
}
temp->next=tr->next;
tr->next=temp;
}
}
No comments:
Post a Comment