Advertisement
Guest User

f

a guest
Sep 17th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct node
  4. {
  5.     int data;
  6.     struct node *next;
  7. };
  8.  
  9.  
  10.  
  11. int main()
  12. {
  13.     struct node *ptr, *newNode;
  14.     int i = 1;
  15.     int num;
  16.     struct node * headptr;
  17.     headptr = NULL;
  18.    
  19.     printf("Enter 1 to make a head-appended linked list.\nEnter 2 to make a tail-appended linked list.\n");
  20.     scanf("%d", &num);
  21.  
  22.     while (i != 0)
  23.     {
  24.         ptr = (struct node *)malloc(sizeof(struct node));
  25.         printf("Enter a Value: ");
  26.         scanf("%d", &ptr -> data);
  27.        
  28.         if (headptr == NULL)
  29.         {
  30.             headptr = ptr;
  31.             headptr -> next = NULL;
  32.            
  33.            
  34.         }
  35.         else if (headptr != NULL && num == 1)
  36.         {
  37.             ptr -> next = headptr;
  38.             headptr = ptr;
  39.         }
  40.        
  41.        
  42.         else if (headptr != NULL && num == 2)
  43.         {            
  44.             newNode = (struct node *)malloc(sizeof(struct node));
  45.            
  46.             while(ptr -> next != NULL) {
  47.                 ptr = ptr -> next; }
  48.            
  49.             newNode -> next = NULL;
  50.             ptr -> next = newNode;
  51.         }
  52.  
  53.         printf("Do you want to add another number?\n 1 = yes  0 = no || ");
  54.         scanf("%d", &i);
  55.        
  56.     }
  57.    
  58.     ptr = headptr;
  59.    
  60.     while (ptr)
  61.     {
  62.         printf("%d ", ptr->data);
  63.         ptr = ptr->next;
  64.     }
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement