Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct TinCan
- {
- int label;
- } TinCan;
- typedef struct LinkedListNode
- {
- TinCan *data;
- struct LinkedListNode *next;
- } LinkedListNode;
- typedef struct LinkedList
- {
- LinkedListNode *head;
- } LinkedList;
- LinkedList* createList() /*creates empty linked list*/
- {
- LinkedList* myList;
- myList = (LinkedList*)malloc(sizeof(LinkedList));
- myList->head = NULL;
- return myList;
- }
- void insertLast(LinkedList* list, TinCan *newData)
- {
- int ii = 1;
- LinkedListNode* newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
- newNode->data = newData;
- //check if queue empty
- if(list->head == NULL)
- {
- list->head = newNode;
- newNode->next=NULL;
- }
- else
- {
- LinkedListNode* current = list->head;
- while (current->next != NULL)
- {
- current = current->next;
- }
- current->next = newNode;
- printf("%d\n", ii);
- ii++;
- }
- }
- void testLinkedList(LinkedList* list)
- {
- int count = 1;
- LinkedListNode* current = list->head;
- while (current != NULL)
- {
- printf("%d: Label is is %d\n", count, current->data->label);
- current = current->next;
- count++;
- }
- }
- int main () {
- LinkedList* canQueue=createList();
- TinCan* testCan = (TinCan*) malloc(sizeof(TinCan));
- testCan->label=69;
- insertLast(canQueue, testCan);
- TinCan* testCan2 = (TinCan*) malloc(sizeof(TinCan));
- testCan2->label=42;
- insertLast(canQueue, testCan2);
- testLinkedList(canQueue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement