DOUBLE LINKED LIST
DOUBLE LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *rightlink;
struct node *leftlink;
};
struct node* root;
void appened()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter node data\n");
scanf("%d",&temp->data);
temp->rightlink=NULL;
temp->leftlink=NULL;
if(root==NULL)
{
printf("node is create\n");
root=temp;
}
else
{
struct node* p;
p=root;
while(p->rightlink!=NULL)
{
p=p->rightlink;
}
p->rightlink=temp;
temp->leftlink=p;
}
}
void atbegin()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter node data\n");
scanf("%d",&temp->data);
temp->rightlink=NULL;
temp->leftlink=NULL;
if(root==NULL)
{
printf("node is create\n");
root=temp;
}
else
{
temp->rightlink=root;
root->leftlink=temp;
root=temp;
}
}
void display()
{
struct node* temp;
temp=root;
if(temp==NULL)
{
printf("no elements\n");
}
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->rightlink;
}
}
int length()
{
int len=0;
struct node* temp;
temp=root;
if(temp==NULL)
{
printf("no elements\n");
}
while(temp!=NULL)
{
len++;
temp=temp->rightlink;
}
printf("node od nodes %d\n",len);
}
void middle()
{
struct node* temp;
int location;
temp=root;
printf("enter your location\n");
scanf("%d",&location);
int len=length();
if(len<location)
{
printf("your enter location exit");
exit(0);
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
printf("enter new node data\n");
scanf("%d",&temp->data);
temp->rightlink=NULL;
temp->leftlink=NULL;
if(root==NULL)
{
temp=root;
}
else
{
struct node* p;int i=1;
p=root;
while(i>location)
{
p=p->rightlink;
i++;
}
temp->rightlink=p->rightlink;
p->rightlink=temp;
temp->leftlink=p->leftlink;
p->leftlink=temp;
}
}
}
void delete()
{
struct node* p,*q;
int i=1,location;
printf("enter which location you want delete\n");
scanf("%d",&location);
p=root;
if(root==NULL)
{
printf("sorry no elements present right now\n");
}
else if(location==1)
{
}
}
int main()
{
appened();
appened();
appened();
length();
atbegin();
middle();
delete();
display();
}
struct node
{
int data;
struct node *rightlink;
struct node *leftlink;
};
struct node* root;
void appened()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter node data\n");
scanf("%d",&temp->data);
temp->rightlink=NULL;
temp->leftlink=NULL;
if(root==NULL)
{
printf("node is create\n");
root=temp;
}
else
{
struct node* p;
p=root;
while(p->rightlink!=NULL)
{
p=p->rightlink;
}
p->rightlink=temp;
temp->leftlink=p;
}
}
void atbegin()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter node data\n");
scanf("%d",&temp->data);
temp->rightlink=NULL;
temp->leftlink=NULL;
if(root==NULL)
{
printf("node is create\n");
root=temp;
}
else
{
temp->rightlink=root;
root->leftlink=temp;
root=temp;
}
}
void display()
{
struct node* temp;
temp=root;
if(temp==NULL)
{
printf("no elements\n");
}
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->rightlink;
}
}
int length()
{
int len=0;
struct node* temp;
temp=root;
if(temp==NULL)
{
printf("no elements\n");
}
while(temp!=NULL)
{
len++;
temp=temp->rightlink;
}
printf("node od nodes %d\n",len);
}
void middle()
{
struct node* temp;
int location;
temp=root;
printf("enter your location\n");
scanf("%d",&location);
int len=length();
if(len<location)
{
printf("your enter location exit");
exit(0);
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
printf("enter new node data\n");
scanf("%d",&temp->data);
temp->rightlink=NULL;
temp->leftlink=NULL;
if(root==NULL)
{
temp=root;
}
else
{
struct node* p;int i=1;
p=root;
while(i>location)
{
p=p->rightlink;
i++;
}
temp->rightlink=p->rightlink;
p->rightlink=temp;
temp->leftlink=p->leftlink;
p->leftlink=temp;
}
}
}
void delete()
{
struct node* p,*q;
int i=1,location;
printf("enter which location you want delete\n");
scanf("%d",&location);
p=root;
if(root==NULL)
{
printf("sorry no elements present right now\n");
}
else if(location==1)
{
}
}
int main()
{
appened();
appened();
appened();
length();
atbegin();
middle();
delete();
display();
}
Comments
Post a Comment