Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include "llist.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. //create a new node with the specified value
  6. Node *LLMakeNode(int iData, Node *pNext)
  7. {
  8.     Node *newNode;
  9.     newNode = (Node*)malloc(sizeof(*newNode));
  10.     if (newNode != NULL)
  11.     {
  12.         newNode->iData = iData;
  13.         newNode->pNext = pNext;
  14.         return newNode;
  15.  
  16.     }
  17.     printf("Null pointer - could not create node.");
  18.     return;
  19. }
  20.  
  21. //add the specified value to the end of the list
  22. Node *LLAddToEnd(Node *pHead, int iData)
  23. {
  24.     Node *scan = pHead; //initialize scanning node to head node
  25.     while (scan->pNext != NULL) scan = scan->pNext; //
  26.  
  27.     Node *newNode = LLMakeNode(iData, NULL); //create new node, give it the data and set its next value to null because it is the new tail node
  28.     scan->pNext = newNode;
  29.  
  30.     return pHead;
  31. }
  32.  
  33. //add the specified value to the appropriate position to maintain ascending order in the list
  34. Node *LLAddInOrder(Node *pHead, int iData)
  35. {
  36.  
  37. }
  38.  
  39. //show the node count of the linked list
  40. int LLCount(Node *pHead)
  41. {
  42.     int count = 0;
  43.     Node *current = pHead;
  44.     while (current != NULL)
  45.     {
  46.         count++;
  47.         current = current->pNext;
  48.     }
  49.     return count;
  50. }
  51.  
  52. //show the list to the console, comma separated, no extra or leading commas
  53. ///we pass the head of the linked list to this function and iterate through it by using the pNext member
  54. void LLShowList(Node *pHead)
  55. {
  56.     Node *current = pHead; //save current node
  57.     if (pHead == NULL) printf("<empty>");
  58.     while (current != NULL) //pHead is valid
  59.     {
  60.         if (current->pNext == NULL) //we're at last node
  61.         {
  62.             printf("%d", current->iData); //so don't print a comma
  63.             break;
  64.         }
  65.         printf("%d, ", current->iData); //print node's data
  66.         current = current->pNext; //grab next node
  67.     }
  68. }
  69.  
  70. //remove the first node
  71. Node *LLDeleteFirst(Node *pHead)
  72. {
  73.     /*if (pHead != NULL)
  74.     {
  75.         Node *front = pHead; //save head node
  76.         pHead = pHead->pNext; //move head down 1 node
  77.         front->pNext = NULL; //sever front's connection to the list
  78.         //if (front == pHead) pHead = NULL;
  79.         free(front);
  80.         return pHead;
  81.     }*/
  82.  
  83.     /*if (pHead == NULL) return NULL;
  84.     Node *front = pHead;
  85.     pHead = pHead->pNext;
  86.     front->pNext = NULL;
  87.     if (front == NULL) pHead = NULL;
  88.     free(front);
  89.     return pHead;*/
  90.  
  91.     int ret = -1;
  92.     Node *next = NULL;
  93.  
  94.     if (pHead == NULL) return -1;
  95.  
  96.     next = pHead->pNext;
  97.     ret = pHead->iData;
  98.     free(pHead);
  99.     pHead = next;
  100.  
  101.     return ret;
  102.  
  103. }
  104.  
  105. //free the entire list
  106. void LLDeleteList(Node *pHead)
  107. {
  108.  
  109. }
  110.  
  111. //show the list in reverse order using tail recursion
  112. void LLShowListReverseRec(Node *pHead)
  113. {
  114.     if (pHead == NULL)
  115.         return;
  116.     LLShowListReverseRec(pHead->pNext);
  117.     printf("%d, ", pHead->iData);
  118. }
  119.  
  120. //delete all the nodes in the linked list recursively
  121. void LLDeleteListRec(Node *pHead)
  122. {
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement