#include <stdio.
h>
#include <stdlib.h>
int count;
struct Node
{
struct Node *prev;
int data;
struct Node *next;
};
void countItems(struct Node *head)
{
struct Node * temp=head;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
printf("No of nodes=%d",count);
}
struct Node* insertAtBeginning(struct Node * head,int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value;
if(head == NULL)
{
newNode -> next = NULL;
newNode->prev=NULL;
head=newNode;
}
else
{
head->prev=newNode;
newNode -> next = head;
head = newNode;
}
printf("\nInsertion success!!!");
return head;
}
struct Node* insertAtEnd(struct Node * head,int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value;
newNode -> next = NULL;
if(head == NULL)
{
newNode->prev=NULL;
head=newNode;
}
else
{
struct Node *temp = head;
while(temp->next!=NULL)
temp=temp->next;
temp -> next = newNode;
newNode->prev=temp;
}
printf("\nInsertion success!!!");
return head;
}
struct Node* insertAtSpe(struct Node * head,int value,int pos)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value;
countItems(head);
if(pos>1&& pos<=count){
int i=1;
struct Node *temp=head;
while(i++<pos-1)
temp=temp->next;
newNode->next=temp->next;
temp->next->prev=newNode;
newNode->prev=temp;
temp->next=newNode;
}
printf("\nInsertion success!!!");
return head;
}
struct Node * delAtBeginning(struct Node *head)
{
struct Node *temp=head;
if(head==NULL)
printf("list is empty");
else if(head->next==NULL)
{
head=NULL;
free(temp);
}
else
{
head=head->next;
head->prev=NULL;
free(temp);
}
return head;
}
struct Node * delAtEnd(struct Node *head)
{
struct Node *temp=head;
if(head==NULL)
printf("list is empty");
else if(head->next==NULL)
{
head=NULL;
free(temp);
}
else
{ struct Node *temp1;
while(temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
temp->prev=NULL;
temp1->next=NULL;
free(temp);
}
return head;
}
struct Node * delAtPos(struct Node *head,int pos)
{
if(head==NULL)
printf("list is empty");
else
{ struct Node *temp1=head,*temp2;
int i=1;
while(i++<pos-1)
temp1=temp1->next;
temp2=temp1->next;
temp1 -> next = temp1->next->next;
temp2->next->prev=temp1;
free(temp2);
}
return head;
}
void display(struct Node *head)
{
struct Node * temp=head;
printf("NULL<-->");
while(temp!=NULL)
{
printf("%d<-->",temp->data);
temp=temp->next;
}
printf("NULL");
}
void sort(struct Node *head){
struct Node *temp1,*temp2;
temp1=head;
while(temp1!=NULL)
{
temp2=head;
while(temp2->next!=NULL)
{
if(temp2->data>temp2->next->data)
{
int t=temp2->data;
temp2->data=temp2->next->data;
temp2->next->data=t;
}
temp2=temp2->next;
}
temp1=temp1->next;
}
}
void search(struct Node *head,int val){
int pos=0,flag=0;
struct Node *t=head;
while(t!=NULL)
{
pos++;
if(t->data==val)
{
flag=1;
break;
}
t=t->next;
}
if(flag)
printf("element found %d at %d",val,pos);
else
printf("Element not found");
}
int main(){
struct Node *head=NULL;
int op,pos;
while(1){
int val;
printf("\n\n [Link]\n [Link]\n [Link] \n [Link]\n [Link] \n [Link] \n
[Link]\[Link] \n [Link]\n [Link]\n [Link]\n\n ");
printf("choose option");
scanf("%d",&op);
switch(op){
case 1:
printf("enter val");
scanf("%d",&val);
head=insertAtBeginning(head,val);break;
case 2:
printf("enter val");
scanf("%d",&val);
head=insertAtEnd(head,val);break;
case 3:
printf("enter val and pos");
scanf("%d%d",&val,&pos);
head=insertAtSpe(head,val,pos);break;
case 4: display(head);
break;
case 5: head=delAtBeginning(head);break;
case 6: head=delAtEnd(head);break;
case 7: printf("enter pos");
scanf("%d",&pos);
head=delAtPos(head,pos);break;
case 8: countItems(head);break;
case 9: printf("enter val");
scanf("%d",&val);
search(head,val);
break;
case 10:sort(head);break;
case 11 :exit(0);
default:printf("choose correct option");
}
}
}