Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "llist.h"
- #include <stdlib.h>
- #include <stdio.h>
- //create a new node with the specified value
- Node *LLMakeNode(int iData, Node *pNext)
- {
- Node *newNode;
- newNode = (Node*)malloc(sizeof(*newNode));
- if (newNode != NULL)
- {
- newNode->iData = iData;
- newNode->pNext = pNext;
- return newNode;
- }
- printf("Null pointer - could not create node.");
- return;
- }
- //add the specified value to the end of the list
- Node *LLAddToEnd(Node *pHead, int iData)
- {
- Node *scan = pHead; //initialize scanning node to head node
- while (scan->pNext != NULL) scan = scan->pNext; //
- 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
- scan->pNext = newNode;
- return pHead;
- }
- //add the specified value to the appropriate position to maintain ascending order in the list
- Node *LLAddInOrder(Node *pHead, int iData)
- {
- }
- //show the node count of the linked list
- int LLCount(Node *pHead)
- {
- int count = 0;
- Node *current = pHead;
- while (current != NULL)
- {
- count++;
- current = current->pNext;
- }
- return count;
- }
- //show the list to the console, comma separated, no extra or leading commas
- ///we pass the head of the linked list to this function and iterate through it by using the pNext member
- void LLShowList(Node *pHead)
- {
- Node *current = pHead; //save current node
- if (pHead == NULL) printf("<empty>");
- while (current != NULL) //pHead is valid
- {
- if (current->pNext == NULL) //we're at last node
- {
- printf("%d", current->iData); //so don't print a comma
- break;
- }
- printf("%d, ", current->iData); //print node's data
- current = current->pNext; //grab next node
- }
- }
- //remove the first node
- Node *LLDeleteFirst(Node *pHead)
- {
- /*if (pHead != NULL)
- {
- Node *front = pHead; //save head node
- pHead = pHead->pNext; //move head down 1 node
- front->pNext = NULL; //sever front's connection to the list
- //if (front == pHead) pHead = NULL;
- free(front);
- return pHead;
- }*/
- /*if (pHead == NULL) return NULL;
- Node *front = pHead;
- pHead = pHead->pNext;
- front->pNext = NULL;
- if (front == NULL) pHead = NULL;
- free(front);
- return pHead;*/
- int ret = -1;
- Node *next = NULL;
- if (pHead == NULL) return -1;
- next = pHead->pNext;
- ret = pHead->iData;
- free(pHead);
- pHead = next;
- return ret;
- }
- //free the entire list
- void LLDeleteList(Node *pHead)
- {
- }
- //show the list in reverse order using tail recursion
- void LLShowListReverseRec(Node *pHead)
- {
- if (pHead == NULL)
- return;
- LLShowListReverseRec(pHead->pNext);
- printf("%d, ", pHead->iData);
- }
- //delete all the nodes in the linked list recursively
- void LLDeleteListRec(Node *pHead)
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement