You are on page 1of 6

1) WACP to create a doubly linked list and display it.

 #include <stdio.h>
#include <stdlib.h>
struct node
{struct node* prev;
int data;
struct node* next;
};
struct node* createNode(int data)
{struct node* newNode=(struct node*)malloc(sizeof(struct
node));
newNode->data=data;
newNode->next=NULL;
newNode->prev=NULL;
return newNode;
}
void addElement(struct node** head,int data)
{struct node* ptr=createNode(data);
if(*head==NULL)
{*head=ptr;
}
else
{ struct node* pt=*head;
while(pt->next!=NULL)
{
pt=pt->next;
}
ptr->prev=pt;
pt->next=ptr;
}
}
void displayList(struct node* head)
{struct node* ptr=head;
printf("THE LINKED LIST IS:: \n");
while(ptr!=NULL)
{ printf("%d ",ptr->data);
ptr=ptr->next;
}
}
void displayRevList(struct node* head)
{
struct node* ptr=head;
printf("\nTHE LINKED LIST IS:: \n");
while(ptr->next!=NULL)
{ ptr=ptr->next;
}
while(ptr!=NULL)
{ printf("%d ",ptr->data);
ptr=ptr->prev;
}
}
void main()
{ struct node* start=NULL;
int d,ch,n,index;
printf("ENTER THE NUMBER OF ELEMENTS: ");
scanf("%d",&n);
int nn=n;
while(nn!=0)
{ printf("ENTER THE VALUE: ");
scanf("%d",&d);
addElement(&start,d);
nn--;
}
displayList(start);
displayRevList(start);
}

2) WACP to insert a element at the beginning,end or at


any index of a doubly linked list.

 #include <stdio.h>
#include <stdlib.h>
struct node {
struct node* prev;
int data;
struct node* next;
};
struct node* createNode(int data)
{ struct node* newNode=(struct node*)malloc(sizeof(struct
node));
newNode-> data=data;
newNode->next= NULL;
newNode->prev= NULL;
return newNode;
}
struct node* addAtBeginning(struct node* head,int data)
{ struct node* ptr=createNode(data);
ptr->next=head;
head->prev=ptr;
return ptr;
}
void addAtIndex(struct node** head,int data,int index)
{ struct node* ptr=createNode(data);
struct node* temp=*head;
int i=1;
for(i=1;i<index-1&&temp!=NULL;i++)
{
temp=temp->next;
}
ptr->prev=temp;
struct node* h=temp->next;
ptr->next=temp->next;
temp->next=ptr;
h->prev=ptr;
}
void addAtLast(struct node** head,int data)
{ struct node* ptr=createNode(data);
if(*head==NULL)
{ *head=ptr;
}
else
{ struct node* pt=*head;
while(pt->next!=NULL)
{ pt=pt->next;
}
ptr->prev=pt;
pt->next=ptr;
}
}
void displayList(struct node* head)
{ struct node* ptr=head;
printf("THE LINKED LIST IS :: \n");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
void displayRevList(struct node* head)
{ struct node* ptr=head;
printf("\nTHE LINKED LIST IS:: \n");
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
while(ptr!=NULL)
{ printf("%d ",ptr->data);
ptr=ptr->prev;
}
}
void main()
{ struct node *start=NULL,*new_head=NULL;
int n,count,d,in;
printf("ENTER NUMBER OF ELEMENTS:: ");
scanf("%d",&n);
count=n;
while(n!=0)
{ printf("ENTER THE VALUE: ");
scanf("%d",&d);
addAtLast(&start,d);
n-=1;
}
printf("ENTER THE VALUE TO BE ADDED: ");
scanf("%d",&d);
new_head=addAtBeginning(start,d);
printf("ENTER THE VALUE TO BE ADDED WITH INDEX: ");
scanf("%d %d",&d,&in);
addAtIndex(&new_head,d,in+1);
displayList(new_head);
displayRevList(new_head);
}

You might also like