#include stdio.h
#include conio.h
#include malloc.h
struct node
{
struct node *left;
int data;
struct node *right;
}*start=NULL;
void create(int);
void addatbeg(int);
void addinbet(int,int);
void del(int);
void search(int);
void display();
void main()
{
int x,pos,n,i,ch;
clrscr();
printf("\nDOUBLE LINKED LIST\n");
do
{
printf("\n\n1. CREATE LIST \
\n2. ADD ELEMENT AT BEGINNING \
\n3. ADD ELEMENT IN BETWEEN \
\n4. DELETE AN ELEMENT \
\n5. SEARCH AN ELEMENT \
\n6. COUNT \
\n7. DISPLAY \
\n8. EXIT \
\nEnter Your Choice...");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("\nEnter No. of Elements in List: ");
scanf("%d",&n);
for(i=1;i<=n;i++) { printf("\nEnter Element %d: ",i); scanf("%d",&x); create(x); } break; } case 2: { printf("\nEnter The Element: "); scanf("%d",&x); addatbeg(x); break; } case 3: { printf("Enter The Element: "); scanf("%d",&x); printf("Enter The Position: "); scanf("%d",&pos); addinbet(x,pos); break; } case 4: { printf("Enter The Element to be Deleted: "); scanf("%d",&x); del(x); break; } case 7: { display(); break; } case 8: { exit(); } } } while(ch!=8); getch(); } void create(int a) { struct node *tmp,*q; tmp=malloc(sizeof(struct node *)); q=malloc(sizeof(struct node *)); tmp->data=a;
tmp->right=NULL;
if(start==NULL)
{
tmp->left=NULL;
start=tmp;
return;
}
q=start;
while(q->right != NULL)
{
q=q->right;
}
tmp->left=q;
q->right=tmp;
}
void addatbeg(int a)
{
struct node *tmp;
tmp=malloc(sizeof(struct node *));
tmp->data=a;
tmp->left=NULL;
start->left=tmp;
tmp->right=start;
start=tmp;
}
void addinbet(int a,int p)
{
struct node *tmp,*q;
int i;
if(p==1)
{
addatbeg(a);
return;
}
q=start;
for(i=1;iright;
}
tmp=malloc(sizeof(struct node *));
tmp->data=a;
tmp->right=q->right;
tmp->left=q;
q->right=tmp;
q->right->left=tmp;
}
void del(int a)
{
struct node *tmp,*q;
if(start->data==a)
{
tmp=start;
start=start->right;
start->left=NULL;
free(tmp);
return;
}
q=start;
while(q->right != NULL)
{
if(q->right->data == a)
{
tmp=q->right;
q->right=tmp->right;
q->right->left=q;
free(tmp);
return;
}
else
{
q=q->right;
}
}
printf("\nElement Not Found!\n");
}
void display()
{
struct node *q;
if(start==NULL)
{
printf("\nLIST EMPTY!!\n");
return;
}
q=start;
printf("\nNULL <-> ");
while(q != NULL)
{
printf("%d <-> ",q->data);
q=q->right;
}
printf("NULL");
}
No comments:
Post a Comment