ahmedraza

SLL

Dec 21st, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //struct node.
  4.  struct node
  5. {
  6.     int data;
  7.     struct node* next;
  8. };
  9. //prototype of create node function
  10. void create();
  11. //Global variable of startlist.
  12. struct node* startlist;
  13. int main (void)
  14. {
  15.     //initially startlist pointer value is null
  16.     startlist = NULL;
  17.     create();
  18.    
  19.     return 0;
  20. }
  21.  
  22.  
  23.  
  24. void create()
  25. {
  26.  
  27.  struct node *newnode, *temp;
  28.  //variable c for checking if you want to increase the list, initial value is "y"
  29.   char c = 'y';
  30.      
  31.   while (c=='y' || c=='Y')
  32.   {
  33.       //if it is start of list
  34.       if (startlist == NULL)
  35.       {
  36.           newnode = (struct node *)
  37.           malloc (sizeof (struct node));
  38.           printf ("please enter Int\n");
  39.           scanf ("%d", &newnode->data);
  40.           newnode -> next = NULL;
  41.           startlist = newnode;
  42.           temp = newnode;
  43.         }
  44.         // If list has already created
  45.         else
  46.         {
  47.             newnode = (struct node*)
  48.           malloc (sizeof (struct node));
  49.           printf ("please enter Int\n");
  50.           scanf ("%d", &newnode->data);
  51.           newnode-> next = NULL;
  52.           temp ->next = newnode;
  53.           temp = newnode;
  54.         }
  55.         // if want to increase the list reply with yes or no
  56.         printf ("Want to enter more y/n?\n");
  57.         scanf ("%c", &c);
  58.   }
  59.   printf ("Node is created\n");
  60. }
Add Comment
Please, Sign In to add comment