Write c program to perform following functions on Doubly Linked List 1) Create Doubly Linked List 2) Display Doubly Linked List

Doubly Linked List

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

//To create structure dll
struct dll
{
int data;
   struct dll *next, *prev;
};
typedef struct dll node;

//To define functions
node * getnode();
node * createdll(node * head);
void displaydll(node * head);

void main()
{
   node * head;
   int ch;
   do
   {
    printf("\n1.Create DLL.\n2.Display DLL\nOther than above to EXIT.");
      printf("\nEnter your choice: ");
      scanf("%d",&ch);
      switch (ch)
      {
      case 1:
         head=createdll(head);
         break;
         case 2:
         displaydll(head);
         break;
         default:
         printf("\nYou have chosen to EXIT. Press any key to exit.");
      }
   }while(ch==1||ch==2);
   getch();
}

//To create node
node * getnode()
{
node * temp=NULL;
   temp=(node *) malloc(sizeof(node));
   printf("\nEnter the data: ");
   scanf("%d",&temp->data);
   temp->next=NULL;
   temp->prev=NULL;
   return temp;
}

//To create Linked List
node * createdll(node * head)
{
node * temp=NULL, *ptr=NULL;
   int i,n;
   printf("\nEnter the number of nodes: ");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   {
    temp=getnode();
      if(head==NULL)
      {
      head=temp;
      }
      else
      {
      ptr=head;
         while(ptr->next!=NULL)
         ptr=ptr->next;
         ptr->next=temp;
         temp->prev=ptr;
      }
   }
   return head;
}

//To display elements in Linked List
void displaydll(node * head)
{
node *ptr=NULL;
   if(head==NULL)
   printf("\nDLL EMPTY.");
   else
   {
    ptr=head;
      printf("\nContent is: ");
      while(ptr!=NULL)
      {
      printf(" %d",ptr->data);
         ptr=ptr->next;
      }
   }
}

1 Comments

  1. Best description ever..if you want to know about bit coin and getnode you can come in this link Getnode

    ReplyDelete