Search This Blog

Tuesday, March 4, 2014

Doubly Linked List



Doubly Linked List:



#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};

struct node *create_node(int);
void insert_node_first();
void insert_node_last();
void insert_node_pos();
void reverse();
void delete_pos();
void search();
void display();
void sort();
struct node *head;

void main()
{
int ch;
char ans='Y';

while(ans=='Y'||ans=='y')
{
printf("\n---------------------------------\n");
printf("\nOperations on Doubly linked list\n");
printf("\n---------------------------------\n");
printf("\n1.Insert node at first");
printf("\n2.Insert node at last");
printf("\n3.Insert node at position");
printf("\n4.Delete Node from any Position");
printf("\n5.Search Element in the linked list");
printf("\n6.Display List from Beginning to end");
printf("\n7.Reverse the List");
printf("\n8.Sort the List");
printf("\n9.Exit\n");
printf("\nEnter your choice");
scanf("%d",&ch);

switch(ch)
{
case 1:
printf("\n...Inserting node at first...\n");
insert_node_first();
break;
case 2:
printf("\n...Inserting node at last...\n");
insert_node_last();
break;
case 3:
printf("\n...Inserting node at position...\n");
insert_node_pos();
break;
case 4:
printf("\n...Deleting Node from any Position...\n");
delete_pos();
break;
case 5:
printf("\n...Searching Element in the List...\n");
search();
break;
case 6:
printf("\n...Displaying List From Beginning to End...\n");
display();
break;
case 7:
printf("\n Reversing the List");
reverse();
break;
case 8:
printf("\n Sorting the List");
sort();
break;
case 9:
exit(0);
printf("\n...Exiting...\n");
break;
default:
printf("\n...Invalid Choice...\n");
break;
}
printf("\nYOU WANT TO CONTINUE (Y/N)");
scanf(" %c",&ans);
}
}

struct node *create_node(int ele)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));

temp->data =ele;
temp->next = NULL;
temp->prev=NULL;
return temp;

}

void insert_node_first()
{
int  ele;
struct node *temp;
printf("\nEnter the value for the node:");
scanf("%d",&ele);
temp=create_node(ele);
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
temp->prev=NULL;
head->prev=temp;
head=temp;
}
printf("\n----INSERTED----");
}

void insert_node_last()
{
int  ele;
struct node *temp,*r;
printf("\nEnter the value for the Node:");
scanf("%d",&ele);
temp=create_node(ele);
if(head==NULL)
{
head=temp;
}
else
{
r=head;
while(r->next!=NULL)
{
r=r->next;
}
 r->next=temp;
temp->prev=r;
temp->next=NULL;
temp->data=ele;
}
printf("\n----INSERTED----");
}

void insert_node_pos()
{
int pos,ele,i;
struct node *temp,*r,*t;
printf("\nEnter the value for the Node:");
scanf("%d",&ele);
temp=create_node(ele);
printf("\nEnter the position ");
scanf("%d",&pos);
if(pos==1)
{
if(head==NULL)
{
head=temp;
}
}
else
{
r=head;

for(i=1;i<pos;i++)
{
 t=r;
r=r->next;
}
t->next=temp;
temp->prev=t;
r->prev=temp;
temp->next=r;
}
printf("\nInserted");
}

void delete_pos()
{
int pos,i;
struct node *r,*t;
if(head==NULL)
{
printf(":No node to delete\n");
}
else
{
printf("\nEnter the position of value to be deleted:");
scanf("%d",&pos);
if(pos==1)
{
head=head->next;
printf("\nElement deleted");
}
else
{
 r=head;
for(i=1;i<pos;i++)
{
t=r;
r=r->next;
}
t->next=r->next;
r->next->prev=t;
free(r);
}
}
}
void search()
{
struct node *r;
int ele;
if(head==NULL)
{
 printf(":No nodes in the list\n");
}
else
{
printf("\nEnter the value to search");
scanf("%d",&ele);
r=head;
while(r!=NULL)
{
 if(r->data==ele)
{
printf("found");
return;
}
else
{
r=r->next;
}
}
printf("not found");
}}

void display()
{
 struct node *r;
if(head==NULL)
{
printf("No nodes in the list to display\n");
}
else
{
r=head;
while(r!=NULL)
{
printf("%d\t",r->data);
r=r->next;
}
}
}
void reverse()
{
 struct node *t;
 t=head;
 if(t==NULL)
 {
  return;
 }
 while(t->next!=NULL)
 {
  t=t->next;
 }
 printf("\nReversed LIST is\n");
 while(t!=NULL)
 {
 printf("%d",t->data);
 t=t->prev;
}
}
void sort()
{
 struct node *t,*r;
 for(t=head;t!=NULL;t=t->next)
 {
  for(r=t->next;r!=NULL;r=r->next)
  {
   if(t->data>r->data)
   {
    int x=t->data;
    t->data=r->data;
    r->data=x;
   }
  }
 }
 printf("\nsorted list is\n");
 display();
}




No comments:

Post a Comment