Advertisement
Guest User

Untitled

a guest
May 2nd, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. typedef struct TinCan
  5.     {
  6.     int label;
  7.     } TinCan;
  8.  
  9. typedef struct LinkedListNode
  10.     {
  11.     TinCan *data;
  12.     struct LinkedListNode *next;
  13.     } LinkedListNode;
  14.  
  15. typedef struct LinkedList
  16.     {
  17.     LinkedListNode *head;
  18.     } LinkedList;
  19.  
  20. LinkedList* createList() /*creates empty linked list*/
  21.     {
  22.     LinkedList* myList;
  23.     myList = (LinkedList*)malloc(sizeof(LinkedList));
  24.     myList->head = NULL;
  25.     return myList;
  26.     }
  27.  
  28.  
  29.  
  30. void insertLast(LinkedList* list, TinCan *newData)
  31.     {
  32.         int ii = 1;
  33.     LinkedListNode* newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
  34.     newNode->data = newData;
  35.  
  36.     //check if queue empty
  37.     if(list->head == NULL)
  38.         {
  39.         list->head = newNode;
  40.         newNode->next=NULL;
  41.         }
  42.     else
  43.         {
  44.         LinkedListNode* current = list->head;
  45.         while (current->next != NULL)
  46.             {
  47.             current = current->next;
  48.             }
  49.         current->next = newNode;
  50.         printf("%d\n", ii);
  51.         ii++;
  52.         }
  53. }
  54.  
  55.  
  56.  
  57. void testLinkedList(LinkedList* list)
  58.     {
  59.         int count = 1;
  60.         LinkedListNode* current = list->head;
  61.         while (current != NULL)
  62.             {
  63.             printf("%d: Label is is %d\n", count, current->data->label);
  64.  
  65.  
  66.  
  67.             current = current->next;
  68.             count++;
  69.             }
  70.     }
  71.  
  72.  
  73. int main () {
  74.     LinkedList* canQueue=createList();
  75.     TinCan* testCan = (TinCan*) malloc(sizeof(TinCan));
  76.     testCan->label=69;
  77.     insertLast(canQueue, testCan);
  78.  
  79.     TinCan* testCan2 = (TinCan*) malloc(sizeof(TinCan));
  80.     testCan2->label=42;
  81.     insertLast(canQueue, testCan2);
  82.  
  83.    
  84.     testLinkedList(canQueue);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement