Page 115 - DCAP407_DATA_STRUCTURE
P. 115

Data Structure




                                          The following program inserts a node in a circular linked list.

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

                                          #include<process.h>
                                          struct list
                                          {
                                          int value;
                                          struct list *link;
                                          };
                                          typedef struct list* node;
                                          node getnode()

                                          {
                                           node p;
                                           p= (node) malloc(sizeof(struct list));
                                           if(p==NULL)
                                          {
                                                  printf("Out of memory\n");

                                                  exit(0);
                                           }
                                           return p;
                                          }
                                          node insert_beginning(int num, node last)
                                          {
                                          node tmp;

                                          tmp=getnode();
                                          tmp->value=num;
                                          if(last==NULL)
                                          last=tmp;
                                          else
                                          tmp->link=last->link;

                                          last->link=tmp;
                                          return last;
                                          }
                                          void display(node last)
                                          {





                          108                     LOVELY PROFESSIONAL UNIVERSITY
   110   111   112   113   114   115   116   117   118   119   120