Link-List

Link-List :: Doubly

#include<stdio.h>
#include<malloc.h>
#include<process.h>

void create(int);
void display();
void insert_front();
void insert_last();
void insert_after();
void del_front();
void del_last();
void del_any();
void count();
void search();

struct linklist
{
struct linklist *llink;
int data;
struct linklist *rlink;
}*head;
typedef struct linklist node;

void main()
{
head=NULL;
int choice;
while(1)
{
printf(“\n1.Create\n2.Front\n3.Last\n4.Add After\n5.Del_First\n6.Del_Last\n7.Del_Any\n8.Count\n9.Search\n10.Display\n11.Exit\n”);
printf(“Enter your Choice:”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
int n;
printf(“Enter no. of nodes to be created:”);
scanf(“%d”,&n);
create(n);
break;
case 2:
insert_front();
break;
case 3:
insert_last();
break;
case 4:
insert_after();
break;
case 5:
del_front();
break;
case 6:
del_last();
break;
case 7:
del_any();
break;
case 8:
count();
break;
case 9:
search();
break;
case 10:
display();
break;
case 11:
abort();
default:
printf(“Invalid Selection\n”);
}
}
}

void create(int n)
{

for(int i=0;i<n;i++)
{
node *newnode,*temp;
newnode=(node *)malloc(sizeof(node));
printf(“Enter value %d:”,i+1);
scanf(“%d”,&newnode->data);
newnode->rlink=NULL;
if(head==NULL)
{
newnode->llink=NULL;
head=newnode;
}
else
{
temp=head;
while(temp->rlink != NULL)
temp=temp->rlink;
newnode->llink=temp;
temp->rlink=newnode;

}
}
}

void display()
{
node *temp;
temp=head;
if(head==NULL)
{
printf(“List is Empty\n”);
return;
}
while(temp != NULL)
{
printf(“%d\n”,temp->data);
temp=temp->rlink;
}
}

void insert_front()
{
node *newnode;
newnode=(node *)malloc(sizeof(node));
printf(“Enter the value to be inserted:”);
scanf(“%d”,&newnode->data);
newnode->llink=NULL;
newnode->rlink=head;
head=newnode;
}

void insert_last()
{
if(head == NULL)
{
insert_front();
return;
}
node *newnode,*temp;
newnode=(node *)malloc(sizeof(node));
printf(“Enter the value to be inserted:”);
scanf(“%d”,&newnode->data);

temp=head;
while(temp->rlink != NULL)
temp=temp->rlink;
newnode->llink=temp;
temp->rlink=newnode;
newnode->rlink=NULL;
}

void insert_after()
{

if(head==NULL)
{
insert_front();
return;
}
int pos;
printf(“Enter the position where insertion has to be done:”);
scanf(“%d”,&pos);
if(pos==1)
{
insert_front();
return;
}
node *newnode,*current,*previous;

previous=NULL;
current=head;
for(int i=0;i<pos-1;i++)
{

if(current->rlink==NULL)
{
printf(“There are less than %d elements in the list\n\n”,pos);
return;
}
previous=current;
current=current->rlink;
}
newnode=(node *)malloc(sizeof(node));
printf(“Enter the value to be inserted:”);
scanf(“%d”,&newnode->data);
previous->rlink=newnode;
newnode->llink=previous;
newnode->rlink=current;
current->llink=newnode;
}

void del_front()
{
node *temp;
if(head==NULL)
printf(“List is Empty\n”);
else
{
temp=head;
head=head->rlink;
free(temp);
}
}

void del_last()
{
node *current,*previous;
current=head;
previous=NULL;
if(head==NULL)
{
printf(“List is Empty\n”);
return;
}
while(current->rlink != NULL)
{
previous=current;
current=current->rlink;
}
previous->rlink=NULL;
free(current);
}

void del_any()
{
if(head==NULL)
{
printf(“The List is Empty:\n”);
return;
}
int key;
node *current,*previous,*next;
current=head;
previous=NULL;
printf(“Enter the element to be deleted:”);
scanf(“%d”,&key);
if(current->data == key)
{
del_front();
return;
}
while(current->rlink != NULL && current->data != key)
{
previous=current;
current=current->rlink;
}
if(current == NULL)
{
printf(“Element Not Found:\n”);
return;
}

else
{
if(current->rlink==NULL)
del_last();
else
{
//previous->rlink=current->rlink;
next=current->rlink;
previous->rlink=next;
next->llink=previous;
free(current);
}
}
}

void count()
{
if(head==NULL)
{
printf(“The list is empty\n”);
return;
}
node *temp=head;
int cnt=0;
while(temp != NULL)
{
cnt++;
temp=temp->rlink;
}
printf(“No. of Nodes in the list are %d”,cnt);
}

void search()
{
if(head==NULL)
{
printf(“The list is empty\n”);
return;
}
node *temp=head;
int pos=1,key;
printf(“Enter the element to be searched:”);
scanf(“%d”,&key);
while(temp != NULL && temp->data != key)
{
pos++;
temp=temp->rlink;

}
if(temp != NULL)
printf(“Search Successful\nElement %d is present at %d location\n”,key,pos);
else
printf(“Search Unsuccessful\n”);
}

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.