#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
int m,n;
struct node
{
int data;
struct node *next,*pre;
}*start;
void create_list(int);
void display();
void insert_at_beg(int);
void insert_at_end(int);
void insert_at_pos(int,int);
void selection(int);
void count();
void main()
{
int i,choice;
clrscr();
start=NULL;
while(1)
{
clrscr();
printf("\n press 1 for list creation");
printf("\n press 2 for displaying the list");
printf("\n press 3 to insert a data at beginning");
printf("\n press 4 to insert a data at end");
printf("\n press 5 to insert at a position");
printf("\n press 6 for selection");
printf("\n press 7 for counting");
printf("\n press 8 for exit");
printf("\n enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(start!=NULL)
{
printf("\n list already created");
break;
}
printf("\n enter total no. of lists to be created:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the data:");
scanf("%d",&m);
create_list(m);
}
printf("\n list created");
break;
case 2:
if(start==NULL)
{
printf("\n no lists created till now");
break;
}
display();
break;
case 3:
if(start==NULL)
{
printf("\n list not created......create list first");
break;
}
printf("\n enter data to be inserted:");
scanf("%d",&m);
insert_at_beg(m);
printf("\n data inserted");
break;
case 4:
if(start==NULL)
{
printf("\n list not created......create list first");
break;
}
printf("\n enter data to be inserted:");
scanf("%d",&m);
insert_at_end(m);
printf("\n data inserted");
break;
case 5:
if(start==NULL)
{
printf("\n no list created.....error");
break;
}
printf("\n enter the data:");
scanf("%d",&m);
printf("\n enter the position:");
scanf("%d",&n);
insert_at_pos(m,n);
break;
case 6:
if(start==NULL)
{
printf("\n no list created....error");
break;
}
printf("\n enter the data for selection");
scanf("%d",&m);
selection(m);
break;
case 7:
if(start==NULL)
{
printf("\n no list created......error");
break;
}
count();
break;
case 8:
exit(1);
break;
default:
printf("\n wrong choice");
}
}
getch();
}
void create_list(int item)
{
struct node *tmp,*p;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=item;
if(start==NULL)
{
start=tmp;
start->next=start;
start->pre=start;
return;
}
p=start;
while(p->next!=start)
p=p->next;
p->next=tmp;
tmp->pre=p;
tmp->next=start;
start->pre=tmp;
}
void display()
{
struct node *p;
p=start;
do
{
printf("\n data= %d",p->data);
p=p->next;
} while(p!=start);
getch();
}
void insert_at_beg(int item)
{
struct node *tmp,*p;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=item;
p=start;
while(p->next!=start)
{
p=p->next;
}
p->next=tmp;
tmp->pre=p;
tmp->next=start;
start=tmp;
}
void insert_at_end(int item)
{
struct node *tmp,*p;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=item;
tmp->next=start;
p=start;
while(p->next!=start)
{
p=p->next;
}
p->next=tmp;
tmp->pre=p;
start->pre=tmp;
}
void count()
{
int i=1,j;
struct node *p;
p=start;
while(p->next!=start)
{
p=p->next;
i++;
}
printf("\n total no. of lists created is %d",i);
getch();
}
void insert_at_pos(int item,int pos)
{
int i;
struct node *tmp,*p;
tmp=(struct node*)malloc(sizeof(struct node));
tmp->data=item;
if(pos==1)
{
insert_at_beg(item);
return;
}
p=start;
for(i=1;i<pos;i++)
{
p=p->next;
}
tmp->next=p->next;
p->next->pre=tmp;
tmp->pre=p;
p->next=tmp;
if(p==NULL)
{
printf("\n position not in range");
return;
}
printf("\n list inserted at %d position",i);
}
void selection(int item)
{
int i;
struct node *p,*q;
if(item==start->data)
{
p=start;
start=start->next;
start->pre->next=p->next;
start->next->pre=start->pre;
free(p);
p=start;
while(p->next!=start)
{
p=p->next;
}
start->pre=p;
p->next=start;
printf("\n node freed");
return;
}
q=start;
while(q->next->data!=item)
{
q=q->next;
i++;
}
p=q->next;
q->next=q->next->next;
q->next->next->pre=q;
free(p);
if(q==NULL)
{
printf("\n wrong value");
return;
}
printf("\n data found at %d position",i);
printf("\n node deleted");
}
#include<conio.h>
#include<alloc.h>
#include<dos.h>
int m,n;
struct node
{
int data;
struct node *next,*pre;
}*start;
void create_list(int);
void display();
void insert_at_beg(int);
void insert_at_end(int);
void insert_at_pos(int,int);
void selection(int);
void count();
void main()
{
int i,choice;
clrscr();
start=NULL;
while(1)
{
clrscr();
printf("\n press 1 for list creation");
printf("\n press 2 for displaying the list");
printf("\n press 3 to insert a data at beginning");
printf("\n press 4 to insert a data at end");
printf("\n press 5 to insert at a position");
printf("\n press 6 for selection");
printf("\n press 7 for counting");
printf("\n press 8 for exit");
printf("\n enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(start!=NULL)
{
printf("\n list already created");
break;
}
printf("\n enter total no. of lists to be created:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the data:");
scanf("%d",&m);
create_list(m);
}
printf("\n list created");
break;
case 2:
if(start==NULL)
{
printf("\n no lists created till now");
break;
}
display();
break;
case 3:
if(start==NULL)
{
printf("\n list not created......create list first");
break;
}
printf("\n enter data to be inserted:");
scanf("%d",&m);
insert_at_beg(m);
printf("\n data inserted");
break;
case 4:
if(start==NULL)
{
printf("\n list not created......create list first");
break;
}
printf("\n enter data to be inserted:");
scanf("%d",&m);
insert_at_end(m);
printf("\n data inserted");
break;
case 5:
if(start==NULL)
{
printf("\n no list created.....error");
break;
}
printf("\n enter the data:");
scanf("%d",&m);
printf("\n enter the position:");
scanf("%d",&n);
insert_at_pos(m,n);
break;
case 6:
if(start==NULL)
{
printf("\n no list created....error");
break;
}
printf("\n enter the data for selection");
scanf("%d",&m);
selection(m);
break;
case 7:
if(start==NULL)
{
printf("\n no list created......error");
break;
}
count();
break;
case 8:
exit(1);
break;
default:
printf("\n wrong choice");
}
}
getch();
}
void create_list(int item)
{
struct node *tmp,*p;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=item;
if(start==NULL)
{
start=tmp;
start->next=start;
start->pre=start;
return;
}
p=start;
while(p->next!=start)
p=p->next;
p->next=tmp;
tmp->pre=p;
tmp->next=start;
start->pre=tmp;
}
void display()
{
struct node *p;
p=start;
do
{
printf("\n data= %d",p->data);
p=p->next;
} while(p!=start);
getch();
}
void insert_at_beg(int item)
{
struct node *tmp,*p;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=item;
p=start;
while(p->next!=start)
{
p=p->next;
}
p->next=tmp;
tmp->pre=p;
tmp->next=start;
start=tmp;
}
void insert_at_end(int item)
{
struct node *tmp,*p;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=item;
tmp->next=start;
p=start;
while(p->next!=start)
{
p=p->next;
}
p->next=tmp;
tmp->pre=p;
start->pre=tmp;
}
void count()
{
int i=1,j;
struct node *p;
p=start;
while(p->next!=start)
{
p=p->next;
i++;
}
printf("\n total no. of lists created is %d",i);
getch();
}
void insert_at_pos(int item,int pos)
{
int i;
struct node *tmp,*p;
tmp=(struct node*)malloc(sizeof(struct node));
tmp->data=item;
if(pos==1)
{
insert_at_beg(item);
return;
}
p=start;
for(i=1;i<pos;i++)
{
p=p->next;
}
tmp->next=p->next;
p->next->pre=tmp;
tmp->pre=p;
p->next=tmp;
if(p==NULL)
{
printf("\n position not in range");
return;
}
printf("\n list inserted at %d position",i);
}
void selection(int item)
{
int i;
struct node *p,*q;
if(item==start->data)
{
p=start;
start=start->next;
start->pre->next=p->next;
start->next->pre=start->pre;
free(p);
p=start;
while(p->next!=start)
{
p=p->next;
}
start->pre=p;
p->next=start;
printf("\n node freed");
return;
}
q=start;
while(q->next->data!=item)
{
q=q->next;
i++;
}
p=q->next;
q->next=q->next->next;
q->next->next->pre=q;
free(p);
if(q==NULL)
{
printf("\n wrong value");
return;
}
printf("\n data found at %d position",i);
printf("\n node deleted");
}
No comments:
Post a Comment