Monday, 2 April 2012

Program : Doubly Linked List – Create


Program : Doubly Linked List – Create

// create and traverse doubly linked list

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

struct link
{
          int no;
          struct link *next;
          struct link *pre;
};

struct link *node,*start;

void create_link(struct link *node);
void display(struct link *node);

void main()
{
          clrscr();
          printf("\n creating doubly linked list");

          node=(struct link *)malloc(sizeof(struct link));
          create_link(node);
          printf("\n Output\n");
          display(node);
          getch();

}

void create_link(struct link *node)
{
          char ans;
          start->next=NULL;
          start->pre=NULL;
          node=start;
          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node->next->pre=node;
                   node=node->next;
                   printf("\n Enter data for node:-");
                   scanf("%d",&node->no);
                   node->next=NULL;
                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
          }
}
void display(struct link *node)
{
          node=start->next;
          do
          {
                   printf("\n  %d",node->no);
                   node=node->next;
          }while(node->next);

          printf("\n Backward direction");
          do
          {
                   printf("\n  %d",node->no);
                   node=node->pre;
          }while(node);

}

Posted By : Ruchita Pandya

No comments:

Post a Comment