/////// 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 dlist
{
int data;
struct dlist *next;
struct dlist *previous;
}*start=NULL;
///////////////////Main////////////////////////////////////////
int main()
{
while(1)
{
system("clear");
printf("\n");
printf(" ---------------------Double 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(2);
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(2);
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();
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 dlist *temp,*tr;
temp=malloc(sizeof(struct dlist));
scanf("%d",&temp->data);
temp->next=NULL;
temp->previous=NULL;
if(start==NULL)
{
start=temp;
}
else
{
tr=start;
while(tr->next !=NULL)
{
tr=tr->next;
}
tr->next=temp;
temp->previous=tr;
}
}
/////////////////////////////////////////////////////////////////////
void display(void)
{
char s[5];
printf("\n\n\n\n\n\n\n START\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" ");
struct dlist *tr;
tr=start;
if(start==NULL)
{
printf("--> No Data");
}
while (tr!=NULL)
{
printf("--> %d",tr->data);
tr=tr->next;
}
printf("\n\n\n\n\n\n\nPRESS ENTER KEY");
gets(s);
}
////////////////////////////////////////////////////////////////////////
void insert_first(void)
{
struct dlist *temp;
temp=malloc(sizeof(struct dlist));
printf("\nEnter the data : ");
scanf("%d",&temp->data);
temp->next=start;
start->previous=temp;
start=temp;
printf("\nYour Data Successfully Saved in First Position");
printf("\n-----------------------------------\n");
sleep(2);
}
///////////////////////////////////////////////////////////////////////
void insert_btwn(int x)
{
int i;
struct dlist *tr,*temp;
tr=start;
for(i=0;i<x-2;i++)
{
tr=tr->next;
}
temp=malloc(sizeof(struct dlist));
printf("\nEnter the data : ");
scanf("%d",&temp->data);
temp->next=tr->next;
tr->next=temp;
temp->previous=tr;
if(temp->next!=0)
{temp->next->previous=temp;}
printf("\nYour Data Successfully Saved in Position %d ",x);
printf("\n-----------------------------------\n");
sleep(2);
}
////////////////////////////////////////////////////////////////////////
void delete(int data)
{
struct dlist *temp,*tr;
////////// if data in Starting position /////////////
if(start->data==data)
{
if(start->next!=NULL)
{
temp=start;
start=start->next;
start->previous=NULL;
free(temp);
}
else
{
start=NULL;
}
printf("\nYour Data %d is Successfully Deleted ",data);
printf("\n-----------------------------------\n");
sleep(2);
}
////////// if middle ////////////////////////////////
else
{
tr=start;
while(tr->next->next!=NULL)
{
if(tr->next->data==data)
{
temp=tr->next;
tr->next=temp->next;
temp->next->previous=tr;
free(temp);
printf("\nYour Data %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=NULL;
free(temp);
printf("\nYour Data %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 dlist *temp,*tr0,*tr;
temp=malloc(sizeof(struct dlist));
tr=tr0=start;
temp=start->next;
start->next=NULL;
while(temp!=NULL)
{
tr=temp;
tr0->previous=tr;
temp=temp->next;
tr->next=tr0;
tr->previous=temp;
tr0=tr;
}
start=tr;
}
////////////////////////////////////////////////////////////////////////
void sorted_create(void)
{
struct dlist *temp,*tr;
temp=malloc(sizeof(struct dlist));
scanf("%d",&temp->data);
temp->next=NULL;
temp->previous=NULL;
if(start==NULL||start->data>temp->data)
{
if(start!=NULL)
{start->previous=temp;}
temp->next=start;
start=temp;
return;
}
else
{
tr=start;
while(tr->next!=NULL&&tr->next->data<temp->data)
{
tr=tr->next;
}
temp->previous=tr;
temp->next=tr->next;
tr->next=temp;
if(temp->next!=NULL)
{
temp->next->previous=temp;
}
}
}
No comments:
Post a Comment