You are on page 1of 5

#include<stdio.

h>
#include <stdlib.h>

/*declaring a structure to create a node*/


struct node {
int info;
struct node *lpt;
struct node *rpt;
};
struct node *first;

/*main program*/
void main() {
void create();
void ftraverse();
void btraverse();
void insert_beginning();
void insert_end();
void insert_after_node();
void delete_beginning();
void delete_end();
void delete_given_node();
//clrscr();
create();
ftraverse();
btraverse();
insert_beginning();

ftraverse();
btraverse();
insert_end();
ftraverse();
btraverse();
insert_after_node();
ftraverse();
btraverse();
delete_beginning();
ftraverse();
btraverse();
delete_end();
ftraverse();
btraverse();
delete_given_node();
ftraverse();
btraverse();
// getch();
}
void create() {
struct node *ptr, *cpt;
char ch = 'Y';
ptr = (struct node*) malloc(sizeof (struct node));
printf("----Input first node information:");
scanf("%d", &ptr->info);
ptr->lpt = NULL;
ptr->rpt = NULL;
first = ptr;
do {
cpt = (struct node*) malloc(sizeof (struct node));
printf("\nenter next input node information:");
scanf("%d", & cpt->info);
cpt->lpt = NULL;
cpt->rpt = NULL;
ptr->rpt = cpt;
cpt->lpt = ptr;
ptr = cpt;
printf("\npress <Y/N > for more nodes:");
scanf("%c", &ch);

} while (ch == 'Y');


ptr->rpt = NULL;
}

void ftraverse() {
struct node *ptr;
printf("\nforward traversing:");
ptr = first;
while (ptr != NULL) {
printf("%d\t", ptr->info);
ptr = ptr->rpt;
}
}

void btraverse() {
struct node *ptr;
printf("\nbackward traversing:");
ptr = first;
while (ptr->rpt != NULL) {
ptr = ptr -> rpt;
}
while (ptr != NULL) {
printf("%d \t", ptr->info);
ptr = ptr->lpt;
}
}
/* inserting nodes into the list*/
/*function to insert values from beginning of the the double linked list*/
void insert_beginning() {
struct node *ptr;
/*allocating implicit memory to the node*/
ptr = (struct node*) malloc(sizeof (struct node));
if (ptr == NULL) /*checking if List is empty*/ {
printf("overflow ");
return;
}
printf("\ninput new node:");
scanf("%d", &ptr->info);
ptr->lpt = NULL;
ptr->rpt = NULL;

ptr->rpt = first;
first->lpt = ptr;
first = ptr;
printf("new node is inserted");
}

/*function to insert values from the end of the linked list*/


void insert_end() {
struct node *ptr, *cpt;
ptr = (struct node *) malloc(sizeof (struct node));
if (ptr == NULL) {
printf("overflow");
return;
}
printf("\ninput new node information");
scanf("%d", &ptr->info);
cpt = first;
/* assigning first node pointer to next nod pointer to delete a data from the starting of the node*/
while (cpt->rpt != NULL)
cpt = cpt->rpt;
cpt->rpt = ptr;
ptr->lpt = cpt;
ptr->rpt = NULL;
printf("insertion is done\n");
}

/*function to insert values from the middle of the linked list*/


void insert_after_node() {
struct node *ptr, *cpt, *tpt;
int m;
/*allocating implicit memory to the node*/
ptr = (struct node *) malloc(sizeof (struct node));
if (ptr == NULL) {
printf("overflow");
return;
}
printf("\ninput new node information");
scanf("%d", &ptr->info);
printf("\ninput node information after which insertion");
scanf("%d", &m);
cpt = first;
while (cpt->info != m) {
cpt = cpt->rpt;
tpt = cpt->rpt;
}
cpt->rpt = ptr;
ptr->lpt = cpt;
ptr->rpt = tpt;
tpt->lpt = ptr;
printf("\ninsertion is done");
}
/*end of insertion operation*/

/*deletion operation*/
void delete_beginning() {
struct node * ptr, *cpt;
if (first == NULL) {
printf("underflow");
return;
}
ptr = first;
first = ptr->rpt;
first->lpt = NULL;
free(ptr);
printf("\ndeletion is done\n");
}

void delete_end() {
struct node *ptr, *cpt;
if (first == NULL) {
printf("underflow");
return;
}
ptr = first;
while (ptr->rpt != NULL) {
cpt = ptr;
ptr = ptr->rpt;
}
cpt->rpt = NULL;
free(ptr);
printf("deletion is done \n");
}

void delete_given_node() {
struct node *ptr, *cpt, *tpt;
int m;
if (first == NULL) {
printf("underflow");
return;
}
printf("insertion of the node to be deleted:");
scanf("%d", & m);
ptr = first;
while (ptr->info != m) {
ptr = ptr->rpt;
}
cpt = ptr->lpt;
tpt = ptr->rpt;
cpt->rpt = tpt;
tpt->lpt = cpt;
free(ptr);
printf("deletion is done\n");
}

You might also like