Doubly Non Circular Linked List

Doubly Non Circular linked list is another type of linked list.

The doubly Non Circular linked list is a type of linked list where a node contains 3 field one contains data in node and two pointers which stores the address of previous and next node as shown below.

Doubly Non Circular Linked List

As we seeing the non circular list, Which means the end of the doubly linked list does not store any address(which means it contain NULL) as shown below.

Doubly Non Circular Linked List 1

As node added we stored previous and next address of node.

At initial stage that is there is only one node the previous and next contains null after as we add node we store previous and next address in node as shown.

Program

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220#include<stdio.h>#include<alloc.h>struct node{    int data;    struct node *next;    struct node *prev;}struct node *start;void insert_beg(void);void insert_end(void);void insert_before(void);void display(void);void deleteion(void);void destroy(void);#define pf printf#de4fine sc scanfvoid main(void){int ch;start=NULL;do{pf(“\n 1. insert_beg\n 2. insert_end\n 3. insert_before\n 4. Display\n 5. Deletion\n 6.Destroy\n7.Exit”);sf(“%d”,&ch);switch(ch){case 1:   insert_beg();  break;case 2:insert_end();break;case 3:insert_before();break;case 4:display();break;case 5:deletion();break;case 6:destroy();break;case 7:exit(0);break;}}while(ch!=7);}void insert_beg(void){struct node *nn;nn=(strcut node *)malloc(sizeof(struct node));pf(“Enter data”);sf(“%d”,&nn->data);if(start==NULL){nn->next=NULL;nn->prev=NULL;start=nn;}else{nn->prev=NULL;nn->next=start;start->prev=nn;start=nn;}}void insert_end(void){struct node *nn ,*temp;nn=(strcut node *)malloc(sizeof(struct node));pf(“Enter data”);sf(“%d”,&nn->data);if(start==NULL){nn->prev=NULL;nn->next=NULL;start=nn;}else{temp=start;while(temp->next!=NULL){temp=temp->next;}temp->next=nn;nn->next=NULL;nn->prev=temp;}}void insert_before(void){struct node *nn ,*temp,*ptemp;int x;if(start==NULL){pf(“DLL is empty”);return;}pf(“Enter data which you want to insert the node”);sf(“%d”,&x);if(x==start->data){insert_beg();            return;}temp=start;while(temp!=NULL && temp->data!=x){ptemp=temp;temp=temp->next;}if(temp==NULL){pf(“%d does not exist:”,x);}else{nn=(strcut node *)malloc(sizeof(struct node));pf(“Enter data”);sf(“%d”,&nn->data);nn->prev=ptemp;nn->next=temp;ptemp->next=nn;temp->prev=nn;}}void display(void){struct node *temp*ptemp;if(start==NULL){pf(“\n DLL is empty”);return;}pf(“Forward order”);temp=start;while(temp!=NULL){pf(“%d”,temp->data);ptemp=temp;temp=temp->next;}pf(“reverse order”);while(ptemp!=NULL){pf(“%d”,ptemp->data);ptemp=ptemp->prev;}}void deletion(void)   {struct node *ptemp,*temp,*ntemp;int x;if(start==NULL){pf(“\n DLL is empty”);return;}pf(“Enter  data to delete”);sf(“%d”,&x);if(x==start->data){{temp=start;start=start->next;free(temp);if(start!=NULL){start->prev=NULL;}return;}temp=start;while(temp!=NULL && temp ->data!=x){ptemp=temp;temp=temp->next;}if(temp==NULL){pf(“%d does not exist”);}else{ptemp->next=temp->next;if(temp->next!=NULL){ntemp=temp->next;ntemp->prev=ptemp;}free(temp);}}void destroy(void)   {struct node *temp,*dp;if(start==NULL){pf(“\n DLL is empty”);return;}temp=start;while(temp!=NULL){dp=temp;temp=temp->next;free(dp);}start=NULL;}