Double Linklist Creation
Double Linklist Creation
h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
int choice,n,m,position,i;
head=NULL;
while(1)
{
printf("1. Create list\n");
printf("2. Add at begining\n");
printf("3.Add at a position\n");
printf("4. ADD at end\n");
printf("5. Display \n");
printf("Enter your choice : ");
scanf("%d",&choice);
fflush(stdin);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
fflush(stdin);
for(i=0;i<n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
fflush(stdin);
create_list(m,i);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position at which this element is inserted : ");
scanf("%d",&position);
addafter(m,position);
break;
case 4:
printf("Enter the element : ");
scanf("%d",&m);
addatend(m);
break;
case 5:
display();
break;
case 6:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
getch();
return 0;
}/*End of main()*/
else
{
temp=(list) malloc(sizeof(node));
temp->data=n;
temp->next=NULL; /*Element inserted at the end */
tail->next =temp;
temp->prev=tail;
tail=tail->next;
}
}/*End of create_list()*/
void addatbeg(int n)
{
list temp;
temp=(list)malloc(sizeof(node));
temp->data=n;
temp->next=head;
head->prev=temp;
head=temp;
}
void addatend(int n)
{
list temp;
temp=(list)malloc(sizeof(node));
temp->data=n;
temp->next=NULL;
tail->next=temp;
temp->prev=tail;
tail=tail->next;
}
void addafter(int n,int pos)
{
list temp,q;
int i;
q=head;
for(i=0;i<pos-1;i++)
{
q=q->next;
if(q==NULL)
{
printf("There are less than %d elements",pos);
return;
}
}/*End of for*/
temp=(list)malloc(sizeof(node) );
temp->data=n;
temp->next=q->next;
q->next=temp;
temp->prev=q;
}
void display()
{
temp=head;
while(temp!=NULL)
{
printf("% d",temp->data);
temp=temp->next;
}
printf("\n");
}