programminginc.net

C Language Programming

  • Home
  • C Examples
Home > Data Structure C Programming Examples > Insertion in doubly linked list data structure Using C Program

Insertion in doubly linked list data structure Using C Program

Insertion in doubly linked list data structure Using pointer in C Program. The program shows delete operation of a node from beginning, middle and end

 //insertion in doubly linked list # dllins.c # //
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct doublelist
{ int info;
struct doublelist *next, *prev;
};
typedef struct doublelist node;
node* makenode(int);
node* insertbeg(node*);
void insertmid(node*);
void insertend(node*);
void display(node*);
main()
{
node *head, *p, *z;
int x;
char c='y';
clrscr();
printf("Enter the number");
scanf("%d", &x);
head=makenode(x);
p=head;
printf("Do you wnat to continue");
fflush(stdin);
scanf("%c", &c);
while(c!='n')
{
printf("Enter the number");
scanf("%d", &x);
z=makenode(x);
p->next=z;
z->prev=p;
p=z;
printf("\n\nDo you want to continue");
fflush(stdin);
scanf("%c", &c);
}
p->next=head;
head->prev=p;
printf("\n\nThe linked list is ");
display(head);
printf("\n\nThe following function insert beg");
head=insertbeg(head);
printf("\n\nThe list after insert beg is ");
display(head);
printf("\n\nThe following function insert mid");
insertmid(head);
printf("\n\nThe list after insert mid is");
display (head);
printf("\n\nThe following function insert end");
insertend(head);
printf("\n\nThe list after insertend is ");
display(head);
getch();
return 0;
}
node* makenode(int x)
{
node *z;
z=(node*)malloc(sizeof(node));
z->info=x;
z->prev=NULL;
z->next=NULL;
return z;
}
void display(node *head)
{node *p, *z;
p=head;
printf("\n\n");
while(p->next!=head)
{
printf("\t%d", p->info);
p=p->next;
z=p;
}
printf("\t%d", p->info);
printf("\n\n");
while(z->prev!=p)
{printf("\t%d", z->info);
z=z->prev;
}
printf("\t%d", z->info);
}
void insertend(node *head)
{
node *p, *q;
int x;
for(p=head; p->next!=head; p=p->next);
printf("\nEnter the new value to insert at end");
scanf("%d", &x);
q=makenode(x);
p->next=q;
q->next=head;
q->prev=head->prev;
head->prev=q;
}
node* insertbeg(node *head)
{
node *p;
int x;
printf("\nInsert Number to insert beg");
scanf("%d", &x);
p=makenode(x);
p->next=head;
p->prev=head->prev;
head->prev->next=p;
head->prev=p;
return p;
}
void insertmid (node *head)
{
node *p, *q;
int x,y;
printf("Enter the previous Number");
scanf("%d", &x);
for(p=head; p->info!=x; p=p->next);
printf("Enter the information to insert mid");
scanf("%d", &y);
q=makenode(y);
p->next->prev=q;
q->next=p->next;
p->next=q;
q->prev=p;
}
//end of program//
  • Singly Linked List Data Structure Program C
  • C program to insert a node in singly linked list
  • Doubly linked list in data structure using C Program
  • Insertion in doubly linked list data structure Using C Program
  • Deletion in doubly linked list data structure Using C Program

Copyright © 2021  programminginc.net