#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
struct node *lpt;
int info;
struct node *rpt;
}*first;
void create()
{
struct node *ptr, *cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node)); //Allocating memory to ptr
printf("Enter the information of first node: ");
scanf("%d",&ptr->info);
ptr->lpt=NULL;
first=ptr; //Initializing 'first'
do
{
cpt=(struct node*)malloc(sizeof(struct node)); //Allocating memory to cpt
printf("\nEnter the information of next node: ");
scanf("%d",&(cpt->info));
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress <Y> for more nodes\n");
ch=getch();
}
while(ch=='y'||ch=='Y');
ptr->rpt=NULL;
}
void ftraverse()
{
struct node *ptr;
ptr=first;
if(ptr==NULL) printf("List is empty");
else
{
printf("\nThe Elements in the list are: ");
while(ptr->rpt!=NULL)
{
printf("\t%d",ptr->info);
ptr=ptr->rpt;
}
printf("\t%d",ptr->info);
}
}
void rtraverse()
{
struct node *ptr;
ptr=first;
if(ptr==NULL) printf("List is empty");
else
{
while(ptr->rpt!=NULL)
{
ptr=ptr->rpt;
}
printf("\nThe Elements in the list are: ");
while(ptr->lpt!=NULL)
{
printf("\t%d",ptr->info);
ptr=ptr->lpt;
}
printf("\t%d",ptr->info);
}
}
void binsert()
{
struct node *ptr;
ptr=(struct node*)malloc(sizeof(struct node)); //Allocating memory to ptr
printf("Enter the information of node: ");
scanf("%d",&ptr->info);
ptr->lpt=NULL;
ptr->rpt=first;
first->lpt=ptr;
first=ptr;
ftraverse();
}
void einsert()
{
struct node *ptr, *cpt;
cpt=first;
ptr=(struct node*)malloc(sizeof(struct node)); //Allocating memory to ptr
printf("Enter the information of node: ");
scanf("%d",&ptr->info);
while(cpt->rpt!=NULL)
{
cpt=cpt->rpt;
}
cpt->rpt=ptr;
ptr->lpt=cpt;
cpt=ptr;
cpt->rpt=NULL;
ftraverse();
}
void dinsert()
{
struct node *ptr, *cpt, *tpt;
int data;
cpt=first;
printf("Enter the node information after which you want to insert: ");
scanf("%d",&data);
while(cpt->info!=data)
{
cpt=cpt->rpt;
}
tpt=cpt->rpt;
ptr=(struct node*)malloc(sizeof(struct node)); //Allocating memory to ptr
printf("Enter information of node to be inserted: ");
scanf("%d",&ptr->info);
cpt->rpt=ptr;
ptr->lpt=cpt;
ptr->rpt=tpt;
tpt->lpt=ptr;
ftraverse();
}
void bdel()
{
struct node *ptr;
ptr=first;
if(ptr==NULL) printf("The list is empty");
else
{
first=first->rpt;
free(ptr);
ftraverse();
}
}
void edel()
{
struct node *ptr, *cpt;
ptr=first;
cpt=first;
if(ptr==NULL) printf("The list is empty");
else
{
while(ptr->rpt!=NULL)
{
cpt=ptr;
ptr=ptr->rpt;
}
if(ptr==first)
{
bdel();
}
else
{
cpt->rpt=NULL;
ptr->lpt=NULL;
free(ptr);
ftraverse();
}
}
}
void ddel()
{
struct node *ptr, *cpt, *tpt;
int data;
ptr=first;
if(ptr==NULL) printf("The list is empty");
else
{
printf("Enter the node information to which you want to delete: ");
scanf("%d",&data);
while(ptr->info!=data)
{
cpt=ptr;
ptr=ptr->rpt;
}
tpt=ptr->rpt;
if(ptr==first)
{
first=tpt;
free(ptr);
ftraverse();
}
else
{
cpt->rpt=tpt;
tpt->lpt=cpt;
free(ptr);
ftraverse();
}
}
}
void main()
{
clrscr();
char ch=1;
char choice;
printf("Working with Data Strucrure\n\n");
printf("Menu\n\n1. Create\n2. Insert at Beginning\n3. Insert After Given Info Node\n");
printf("4. Insert at End\n5. Delete Node From Beginning\n6. Delete Given Info Node\n");
printf("7. Delete Node From End\n8. Farward Traverse\n9. Reverse Traverse\n0. Exit");
while(ch)
{
printf("\n\nEnter your choice: \n");
choice=getch();
switch(choice)
{
case '1': create();
break;
case '2': binsert();
break;
case '3': dinsert();
break;
case '4': einsert();
break;
case '5': bdel();
break;
case '6': ddel();
break;
case '7': edel();
break;
case '8': ftraverse();
break;
case '9': rtraverse();
break;
case '0': return;
default: printf("Wrong choice");
}
}
}
#include<conio.h>
#include<alloc.h>
struct node
{
struct node *lpt;
int info;
struct node *rpt;
}*first;
void create()
{
struct node *ptr, *cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node)); //Allocating memory to ptr
printf("Enter the information of first node: ");
scanf("%d",&ptr->info);
ptr->lpt=NULL;
first=ptr; //Initializing 'first'
do
{
cpt=(struct node*)malloc(sizeof(struct node)); //Allocating memory to cpt
printf("\nEnter the information of next node: ");
scanf("%d",&(cpt->info));
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress <Y> for more nodes\n");
ch=getch();
}
while(ch=='y'||ch=='Y');
ptr->rpt=NULL;
}
void ftraverse()
{
struct node *ptr;
ptr=first;
if(ptr==NULL) printf("List is empty");
else
{
printf("\nThe Elements in the list are: ");
while(ptr->rpt!=NULL)
{
printf("\t%d",ptr->info);
ptr=ptr->rpt;
}
printf("\t%d",ptr->info);
}
}
void rtraverse()
{
struct node *ptr;
ptr=first;
if(ptr==NULL) printf("List is empty");
else
{
while(ptr->rpt!=NULL)
{
ptr=ptr->rpt;
}
printf("\nThe Elements in the list are: ");
while(ptr->lpt!=NULL)
{
printf("\t%d",ptr->info);
ptr=ptr->lpt;
}
printf("\t%d",ptr->info);
}
}
void binsert()
{
struct node *ptr;
ptr=(struct node*)malloc(sizeof(struct node)); //Allocating memory to ptr
printf("Enter the information of node: ");
scanf("%d",&ptr->info);
ptr->lpt=NULL;
ptr->rpt=first;
first->lpt=ptr;
first=ptr;
ftraverse();
}
void einsert()
{
struct node *ptr, *cpt;
cpt=first;
ptr=(struct node*)malloc(sizeof(struct node)); //Allocating memory to ptr
printf("Enter the information of node: ");
scanf("%d",&ptr->info);
while(cpt->rpt!=NULL)
{
cpt=cpt->rpt;
}
cpt->rpt=ptr;
ptr->lpt=cpt;
cpt=ptr;
cpt->rpt=NULL;
ftraverse();
}
void dinsert()
{
struct node *ptr, *cpt, *tpt;
int data;
cpt=first;
printf("Enter the node information after which you want to insert: ");
scanf("%d",&data);
while(cpt->info!=data)
{
cpt=cpt->rpt;
}
tpt=cpt->rpt;
ptr=(struct node*)malloc(sizeof(struct node)); //Allocating memory to ptr
printf("Enter information of node to be inserted: ");
scanf("%d",&ptr->info);
cpt->rpt=ptr;
ptr->lpt=cpt;
ptr->rpt=tpt;
tpt->lpt=ptr;
ftraverse();
}
void bdel()
{
struct node *ptr;
ptr=first;
if(ptr==NULL) printf("The list is empty");
else
{
first=first->rpt;
free(ptr);
ftraverse();
}
}
void edel()
{
struct node *ptr, *cpt;
ptr=first;
cpt=first;
if(ptr==NULL) printf("The list is empty");
else
{
while(ptr->rpt!=NULL)
{
cpt=ptr;
ptr=ptr->rpt;
}
if(ptr==first)
{
bdel();
}
else
{
cpt->rpt=NULL;
ptr->lpt=NULL;
free(ptr);
ftraverse();
}
}
}
void ddel()
{
struct node *ptr, *cpt, *tpt;
int data;
ptr=first;
if(ptr==NULL) printf("The list is empty");
else
{
printf("Enter the node information to which you want to delete: ");
scanf("%d",&data);
while(ptr->info!=data)
{
cpt=ptr;
ptr=ptr->rpt;
}
tpt=ptr->rpt;
if(ptr==first)
{
first=tpt;
free(ptr);
ftraverse();
}
else
{
cpt->rpt=tpt;
tpt->lpt=cpt;
free(ptr);
ftraverse();
}
}
}
void main()
{
clrscr();
char ch=1;
char choice;
printf("Working with Data Strucrure\n\n");
printf("Menu\n\n1. Create\n2. Insert at Beginning\n3. Insert After Given Info Node\n");
printf("4. Insert at End\n5. Delete Node From Beginning\n6. Delete Given Info Node\n");
printf("7. Delete Node From End\n8. Farward Traverse\n9. Reverse Traverse\n0. Exit");
while(ch)
{
printf("\n\nEnter your choice: \n");
choice=getch();
switch(choice)
{
case '1': create();
break;
case '2': binsert();
break;
case '3': dinsert();
break;
case '4': einsert();
break;
case '5': bdel();
break;
case '6': ddel();
break;
case '7': edel();
break;
case '8': ftraverse();
break;
case '9': rtraverse();
break;
case '0': return;
default: printf("Wrong choice");
}
}
}
0 comments:
Post a Comment