Facebook
From Whipped Bison, 1 Year ago, written in C.
This paste is a reply to Untitled from Mustard Camel - view diff
Embed
Download Paste or View Raw
Hits: 120
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Node
  5. {
  6.     int data;
  7.     struct Node* next;
  8. } Node;
  9.  
  10. Node* head;
  11.  
  12. void PrintLinkedList()
  13. {
  14.     Node* temp;
  15.     temp = head;
  16.     while(temp !=NULL){
  17.         printf("%d, ", temp->data);
  18.         temp = temp->next;
  19.     }
  20.     printf("\n\n");
  21. }
  22.  
  23. void InsertAtLast(int userData)
  24. {
  25.     Node* temp = (Node*) malloc(sizeof(Node));
  26.     temp->data = userData;
  27.     temp->next = NULL;
  28.     if(head==NULL)
  29.     {
  30.         head=temp;
  31.         return;
  32.     }
  33.  
  34.     Node* scout = head;
  35.     while(scout->next != NULL)
  36.     {
  37.         scout = scout->next;
  38.     }
  39.     scout->next = temp;
  40. }
  41.  
  42. int main()
  43. {
  44.     head = NULL;
  45.     InsertAtLast(5);
  46.     InsertAtLast(7);
  47.     InsertAtLast(2);
  48.     PrintLinkedList();
  49.  
  50.     return 0;
  51. }
  52.