Sie sind auf Seite 1von 6

#include<stdio.

h>

#include<stdlib.h>

struct node

struct node* left;

int data;

struct node* right;

};

struct node *root=NULL;

void append()

struct node *temp;

temp=(struct node*)malloc(sizeof(struct node));

temp->left=NULL;

temp->right=NULL;

printf("enter node data");

scanf("%d",&temp->data);

if(root==NULL)

root=temp;

else

{ struct node *p;

p=root;

while(p->right!=NULL)

p=p->right;

p->right=temp;

temp->left=p;

}
}

void addatloc()

int i=1,loc;

printf("enter the location\n");

scanf("%d",&loc);

struct node* temp;

temp=(struct node*)malloc(sizeof(struct node));

printf("enter the node data\n");

scanf("%d",&temp->data);

temp->left=NULL;

temp->right=NULL;

struct node *p;

p=root;

while(i<loc)

p=p->right;

i++;

temp->right=p;

temp->left=p->left;

p->left->right=temp;

p->left=temp;

void display()

struct node* p=root;

while(p!=NULL)

{
printf("%d\t",p->data);

p=p->right;

int length()

int count=0;

struct node *temp;

temp=root;

while(temp!=NULL)

count++;

temp=temp->right;

return(count);

void Delete()

int loc;

printf("enter the location to be deleted\n");

scanf("%d",&loc);

if(loc>length())

printf("invalid location");

else if(loc==1)
{

root=root->right;

root->left=NULL;

else if(loc==length())

struct node *p,*q;

p=root;

while(p->right!=NULL)

p=p->right;

q=p->left;

q->right=NULL;

p->left=NULL;

free(p);

else

int i=1;

struct node* k=root;

while(i<loc)

k=k->right;

i++;

k->left->right=k->right;

k->right->left=k->left;
k->right=NULL;

k->left=NULL;

free(k);

void addatorigin()

struct node* temp;

temp=(struct node*)malloc(sizeof(struct node));

printf("enter node data");

scanf("%d",&temp->data);

temp->left=NULL;

temp->right=NULL;

temp->right=root;

root->left=temp;

root=temp;

int main()

int i=1,num;

printf("enter the nodes to be added\n");

scanf("%d",&num);

while(i<=num){

append();

i++;}

display();

printf("%d\n",length());
addatorigin();

display();

printf("%d\n",length());

addatloc();

display();printf("\n");

printf("%d\n",length());

Delete();

display();

return(0);

Das könnte Ihnen auch gefallen