#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define pf printf
struct node
{
int info;
struct node* next;
struct node* previous;
}*temp,*start,*ptr;
static int cnt;
void create()
{
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
scanf("%d",&temp->info);
temp->next=NULL;
temp->previous=NULL;
start=temp;
cnt=1;
}
int check()
{
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
scanf("%d",&temp->info);
if(start==NULL)
{
temp->next=NULL;
temp->previous=NULL;
start=temp;
cnt++;
return 0;//true
}
return 1;
}
int delcheck()
{
if(start==NULL)
{
pf("\nList Empty..");
getch();
return 0;
}
return 1;
}
void firstinsert()
{
if(check())
{
temp->previous=NULL;
temp->next=start;
start=temp;
cnt++;
}
}
void lastinsert()
{
if(check())
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
temp->previous=ptr;
temp->next=NULL;
cnt++;
}
}
void locinsert()
{
int loc,i;
if(check())
{
pf("\nEnter the location");
scanf("%d",&loc);
if(loc>cnt || loc<0)
{
pf("Invalid location");
getch();
}
else if(loc==0)
{
firstinsert();
}
else if(loc==cnt)
{
lastinsert();
}
else
{
ptr=start;
for(i=1;i<loc;i++)
{
ptr=ptr->next;
}
temp->next=ptr->next;
temp->previous=ptr;
ptr->next=temp;
}
}
}
void firstdelete()
{
if(delcheck())
{
ptr=start;
start=start->next;
start->previous=NULL;
free(ptr);
cnt--;
}
}
void lastdelete()
{
if(delcheck())
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->previous->next=NULL;
free(ptr);
cnt--;
}
}
void numdelete()
{
int num,i=1;
pf("\nEnter no. to delete: ");
scanf("%d",&num);
ptr=start;
while(ptr->info!=num && cnt>= (i++))
{
ptr=ptr->next;
}
if(i>cnt)
{
pf("\nNumber not found..");
getch();
return;
}
else if(i==1)
{
firstdelete();
return;
}
else if(i==cnt)
{
lastdelete();
return;
}
else
{
ptr->previous->next=ptr->next;
ptr->next->previous=ptr->previous;
free(ptr);
cnt--;
}
}
void display()
{
if(start==NULL)
{
pf("List Empty");
getch();
return;
}
else
{
ptr=start;
while(ptr!=NULL)
{
pf("%d\t",ptr->info);
ptr=ptr->next;
}
pf("\nNo of items: %d",cnt);
getch();
}
}
void main()
{
int ch;
clrscr();
do
{
clrscr();
pf("\n1.Create LL");
pf("\n2.Insert at Begin");
pf("\n3.Insert at last");
pf("\n4.Insert at location");
pf("\n5.Delete from start");
pf("\n6.Delete from last");
pf("\n7.Delete a no.");
pf("\n8.display");
pf("\n9.Exit");
pf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: create(); break;
case 2: firstinsert(); break;
case 3: lastinsert(); break;
case 4: locinsert(); break;
case 5: firstdelete(); break;
case 6: lastdelete(); break;
case 7: numdelete(); break;
case 8: display(); break;
case 9: exit(0); break;
default: pf("Wrong input");
}
}while(ch!=9);
}
2 comments:
Nice one :)
is their any BUG in this program
Post a Comment