this is my implimentation :
#include
#include
struct node{
struct node *prev;
struct node *next;
int info;
}*start;
int length()
{
if(start==NULL)
return 0;
int count=1;
struct node *q;
q=start;
while(q->next!=NULL)
{
count++;
q=q->next;
}
return count;
}
void add_beg(int num)
{
struct node *tmp;
if(start==NULL)
{
tmp->next=NULL;
tmp->prev=NULL;
tmp->info=num;
start=tmp;
}
else{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->next=start;
start->prev=tmp;
tmp->prev=NULL;
start=tmp;
}
}
void display()
{
struct node *q;
if(start==NULL)
{
printf("List is empty\n");
return;
}
q=start;
printf("List is :\n");
while(q!=NULL)
{
printf("%d ", q->info);
q=q->next;
}
printf("\n");
}/*End of display() */
void add_after(int num,int loc)
{
struct node *q,*tmp;
int i;
q=start;
if(loc==length())
{
while(q->next!=NULL)
q=q->next;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->next=NULL;
tmp->prev=q;
q->next=tmp;
return;
}
printf("hi");
for(i=0;inext;
if(q==NULL)
{
printf("list does not have that many elements");
return;
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->prev=q;
q->next->prev=tmp;
tmp->next=q->next;
q->next=tmp;
}
void del(int num)
{
struct node *q;
q=start;
while(q->next!=NULL)
q=q->next;
if(q->info==num)
{
q->prev->next=NULL;
free(q);
return;
}
else
{q=start;}
while(q!=NULL)
{
if(q->info==num)
{
//delete q
q->prev->next=q->next;
q->next->prev=q->prev;
free(q);
return;
}
q=q->next;
}
printf("element not in in list");
}
void rev()
{
struct node *p1,*p2;
p1=start;
p2=p1->next;
p1->next=NULL;
p1->prev=p2;
while(p2!=NULL)
{
p2->prev=p2->next;
p2->next=p1;
p1=p2;
p2=p2->prev; /*next of p2 changed to prev */
}
start=p1;
}/*End of rev()*/
int main()
{
int choice,n,m,po,i;
start=NULL;
while(1)
{
printf("1.Add at begining\n");
printf("2.Display\n");
printf("3.exit\n");
printf("4.add after\n");
printf("5.delete\n");
printf("6.length\n");
printf("7.reverse\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the element : ");
scanf("%d",&m);
add_beg(m);
break;
case 2:display();
break;
case 3:return 0;
case 4:printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position after which this element is inserted : ");
scanf("%d",&po);
add_after(m,po);
break;
case 5:printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break;
case 6:printf("%d\n",length());
break;
case 7:rev();
break;
default:printf("hello option");
}
}
return 0;
}
#include
#include
struct node{
struct node *prev;
struct node *next;
int info;
}*start;
int length()
{
if(start==NULL)
return 0;
int count=1;
struct node *q;
q=start;
while(q->next!=NULL)
{
count++;
q=q->next;
}
return count;
}
void add_beg(int num)
{
struct node *tmp;
if(start==NULL)
{
tmp->next=NULL;
tmp->prev=NULL;
tmp->info=num;
start=tmp;
}
else{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->next=start;
start->prev=tmp;
tmp->prev=NULL;
start=tmp;
}
}
void display()
{
struct node *q;
if(start==NULL)
{
printf("List is empty\n");
return;
}
q=start;
printf("List is :\n");
while(q!=NULL)
{
printf("%d ", q->info);
q=q->next;
}
printf("\n");
}/*End of display() */
void add_after(int num,int loc)
{
struct node *q,*tmp;
int i;
q=start;
if(loc==length())
{
while(q->next!=NULL)
q=q->next;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->next=NULL;
tmp->prev=q;
q->next=tmp;
return;
}
printf("hi");
for(i=0;i
if(q==NULL)
{
printf("list does not have that many elements");
return;
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->prev=q;
q->next->prev=tmp;
tmp->next=q->next;
q->next=tmp;
}
void del(int num)
{
struct node *q;
q=start;
while(q->next!=NULL)
q=q->next;
if(q->info==num)
{
q->prev->next=NULL;
free(q);
return;
}
else
{q=start;}
while(q!=NULL)
{
if(q->info==num)
{
//delete q
q->prev->next=q->next;
q->next->prev=q->prev;
free(q);
return;
}
q=q->next;
}
printf("element not in in list");
}
void rev()
{
struct node *p1,*p2;
p1=start;
p2=p1->next;
p1->next=NULL;
p1->prev=p2;
while(p2!=NULL)
{
p2->prev=p2->next;
p2->next=p1;
p1=p2;
p2=p2->prev; /*next of p2 changed to prev */
}
start=p1;
}/*End of rev()*/
int main()
{
int choice,n,m,po,i;
start=NULL;
while(1)
{
printf("1.Add at begining\n");
printf("2.Display\n");
printf("3.exit\n");
printf("4.add after\n");
printf("5.delete\n");
printf("6.length\n");
printf("7.reverse\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the element : ");
scanf("%d",&m);
add_beg(m);
break;
case 2:display();
break;
case 3:return 0;
case 4:printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position after which this element is inserted : ");
scanf("%d",&po);
add_after(m,po);
break;
case 5:printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break;
case 6:printf("%d\n",length());
break;
case 7:rev();
break;
default:printf("hello option");
}
}
return 0;
}