Hi, a doubly linked list is a special kind of list where each node is connected to the one before it and the one after it. This means you can move forward and backward easily, like flipping pages in a book both ways. Each node holds some data and two links, one pointing to the previous node and the other to the next. If you need to add or remove something, you can do it quickly because you don't have to start from the beginning. It’s like a two-way street where traffic can flow in both directions. I prefer this type because it makes it easy to travel through the list.
Q Create a function to reverse a linked list.
struct node
{
int data;
struct node*prev,*next;
};
struct node *head;
void reverst(){
struct node *prevnode,*currentnode,*nextnode;
prevnode=0;
currentnode=nextnode=head;
while(nextnode!=0){
nextnode=nextnode->next;
currentnode->next=prevnode;
prevnode=currentnode;
currentnode=nextnode;
}
}
Q Write a program to create a doubly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node*next;
struct node*prev;
};
struct node *head=NULL,*newnode,*temp;
int a=0;
void create(){
struct node* newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&newnode->data);
newnode->prev=0;
newnode->next=0;
if(head==0){
head=temp=newnode;
}
else
{
temp->next=newnode;
newnode->prev=temp;
temp=newnode;
}
printf("Enter 1 to cont and 2 to stop:");
scanf("%d",&a);
}
void printt(){
temp=head;
printf("List:");
while(temp!=NULL){
printf("[%d|%d|%d] ",temp->prev,temp->data,temp->next);
temp=temp->next;
}
}
int main(){
printf("Enter 1 to cont and 2 to stop:");
scanf("%d",&a);
while(a!=2){
create();
}
printt();
}
Q. Insertion in the beginning of a linked list, another function to insert in the end and in the given position.
//Beginning
struct node
{
int data;
struct node*next;
struct node*prev;
};
struct node *head=NULL,*newnode,*temp,*tail;
void Insert_In_Begenning(){
struct node*newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data:");
scanf("%d",&newnode->data);
newnode->prev=0;
newnode->next=0;
head->prev=newnode;
newnode->next=head;
head=newnode;
}
// End
void Insert_In_End(){
struct node*newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data:");
scanf("%d",&newnode->data);
newnode->prev=0;
newnode->next=0;
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
}
// Position
void Insert_pos(){
int pos,i;
printf("Enter position:");
scanf("%d",&pos);
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&newnode->data);
temp=head;
while(i<pos-1){
temp=temp->next;
i++;
}
newnode->prev=temp;
newnode->next=temp->next;
temp->next=newnode;
temp=newnode->next;
temp->prev=newnode;
printt();
}
Q Write a program to do all the 3 add in the beginning, mid and end.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node*next;
struct node*prev;
};
struct node *head=NULL,*newnode,*temp,*tail=NULL,*nnode=NULL;
int a=0;
void printt(){
temp=head;
printf("List:");
while(temp!=NULL){
printf("[%d|%d|%d] ",temp->prev,temp->data,temp->next);
temp=temp->next;
}
}
void Insert_pos(){
int pos,i=0;
printf("Enter position:");
scanf("%d",&pos);
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&newnode->data);
temp=head;
while(i<pos-1){
temp=temp->next;
i++;
}
newnode->prev=temp;
newnode->next=temp->next;
temp->next=newnode;
temp=newnode->next;
temp->prev=newnode;
printt();
}
void Del_pos(){
int pos,i;
printf("Enter position:");
scanf("%d",&pos);
temp=head;
while(i<pos-1){
temp=temp->next;
i++;
}
nnode=temp->next;
temp->next=nnode->next;
temp=nnode->next;
temp->prev=nnode->prev;
free(nnode);
printt();
}
void Insert_In_Begenning(){
struct node*newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data:");
scanf("%d",&newnode->data);
newnode->prev=0;
newnode->next=0;
head->prev=newnode;
newnode->next=head;
head=newnode;
printt();
}
void Insert_In_End(){
struct node*newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data:");
scanf("%d",&newnode->data);
newnode->prev=0;
newnode->next=0;
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
printt();
}
void create(){
struct node* newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&newnode->data);
newnode->prev=0;
newnode->next=0;
if(head==0){
head=temp=newnode;
}
else
{
temp->next=newnode;
newnode->prev=temp;
temp=newnode;
tail=newnode;
}
}
int main(){
for(int i=0;i<3;i++){
create();
}
int p;
printf("Enter which pos u wanna add 1=start,2=mid,3=end");
scanf("%d",&p);
if(p==1){
Insert_In_Begenning();
}
else if(p==2){
Insert_pos();
}
else if(p==3){
Insert_In_End();
}
else if(p==4){
Del_pos();
}
}
Hope you learnt something new about linked list.
Happy Coding …