Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. #include "llist.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. typedef struct Node {
  6.     int iData;
  7.     struct Node *pNext;
  8. } Node;
  9.  
  10. Node *LLMakeNode(int iData, Node *pNext);
  11.  
  12. Node *LLAddToEnd(Node *pHead, int iData);
  13.  
  14. Node *LLAddInOrder(Node *pHead, int iData);
  15.  
  16. int LLCount(Node *pHead);
  17.  
  18. void LLShowList(Node *pHead);
  19.  
  20. Node *LLDeleteFirst(Node *pHead);
  21.  
  22. void LLDeleteList(Node *pHead);
  23.  
  24. void LLShowListReverseRec(Node *pHead);
  25.  
  26. void LLDeleteListRec(Node *pHead);
  27.  
  28. //create a new node with the specified value
  29. Node *LLMakeNode(int iData, Node *pNext)
  30. {
  31.     Node *newNode;
  32.     newNode = (Node*)malloc(sizeof(*newNode));
  33.     if (newNode != NULL)
  34.     {
  35.         newNode->iData = iData;
  36.         newNode->pNext = pNext;
  37.         return newNode;
  38.  
  39.     }
  40.     printf("Null pointer - could not create node.");
  41.     return;
  42. }
  43.  
  44. //add the specified value to the end of the list
  45. Node *LLAddToEnd(Node *pHead, int iData)
  46. {
  47.     Node *scan = pHead; //initialize scanning node to head node
  48.     while (scan->pNext != NULL) scan = scan->pNext; //
  49.  
  50.     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
  51.     scan->pNext = newNode;
  52.  
  53.     return pHead;
  54. }
  55.  
  56. //add the specified value to the appropriate position to maintain ascending order in the list
  57. Node *LLAddInOrder(Node *pHead, int iData)
  58. {
  59.  
  60. }
  61.  
  62. //show the node count of the linked list
  63. int LLCount(Node *pHead)
  64. {
  65.     int count = 0;
  66.     Node *current = pHead;
  67.     while (current != NULL)
  68.     {
  69.         count++;
  70.         current = current->pNext;
  71.     }
  72.     return count;
  73. }
  74.  
  75. //show the list to the console, comma separated, no extra or leading commas
  76. ///we pass the head of the linked list to this function and iterate through it by using the pNext member
  77. void LLShowList(Node *pHead)
  78. {
  79.     Node *current = pHead; //save current node
  80.     if (pHead == NULL) printf("<empty>");
  81.     while (current != NULL) //pHead is valid
  82.     {
  83.         if (current->pNext == NULL) //we're at last node
  84.         {
  85.             printf("%d", current->iData); //so don't print a comma
  86.             break;
  87.         }
  88.         printf("%d, ", current->iData); //print node's data
  89.         current = current->pNext; //grab next node
  90.     }
  91. }
  92.  
  93. //remove the first node
  94. Node *LLDeleteFirst(Node *pHead)
  95. {
  96.     /*if (pHead != NULL)
  97.     {
  98.         Node *front = pHead; //save head node
  99.         pHead = pHead->pNext; //move head down 1 node
  100.         front->pNext = NULL; //sever front's connection to the list
  101.         //if (front == pHead) pHead = NULL;
  102.         free(front);
  103.         return pHead;
  104.     }*/
  105.  
  106.     /*if (pHead == NULL) return NULL;
  107.     Node *front = pHead;
  108.     pHead = pHead->pNext;
  109.     front->pNext = NULL;
  110.     if (front == NULL) pHead = NULL;
  111.     free(front);
  112.     return pHead;*/
  113.  
  114.     int ret = -1;
  115.     Node *next = NULL;
  116.  
  117.     if (pHead == NULL) return -1;
  118.  
  119.     next = pHead->pNext;
  120.     ret = pHead->iData;
  121.     free(pHead);
  122.     pHead = next;
  123.  
  124.     return ret;
  125.  
  126. }
  127.  
  128. //free the entire list
  129. void LLDeleteList(Node *pHead)
  130. {
  131.  
  132. }
  133.  
  134. //show the list in reverse order using tail recursion
  135. void LLShowListReverseRec(Node *pHead)
  136. {
  137.     if (pHead == NULL)
  138.         return;
  139.     LLShowListReverseRec(pHead->pNext);
  140.     printf("%d, ", pHead->iData);
  141. }
  142.  
  143. //delete all the nodes in the linked list recursively
  144. void LLDeleteListRec(Node *pHead)
  145. {
  146.  
  147. }
  148.  
  149. void main()
  150. {
  151.     Node *head = NULL;
  152.     while (LLCount(head) < 25)
  153.         head = LLAddToEnd(head, rand() % 100);
  154.     //head = LLMakeNode(NULL, head);
  155.     LLShowList(head);
  156.     printf("\n\nCount: %d\n", LLCount(head));
  157.  
  158.  
  159.     /*int i = 1;
  160.     while (i < 300)
  161.     {
  162.         LLAddToEnd(head, i++);
  163.     }
  164.  
  165.     //LLAddToEnd(head, 1000);
  166.     //head = LLDeleteFirst(head);
  167.     LLShowList(head);
  168.  
  169.     printf("\n\n\n");
  170.  
  171.     LLShowListReverseRec(head);
  172.     */
  173.  
  174.  
  175.  
  176.     getchar();
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement