Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4.  
  5. typedef struct node {
  6.     int checkNumber;
  7.     char date[9], to[41], description[81]; //arrays one size larger to account for terminating char
  8.     double amount;
  9.     struct node *next;
  10.  
  11. } node;
  12.  
  13. struct node *head = NULL;
  14.  
  15. node *createCheck(int checkNumber, char *date, char *to, char *description, double amount, node *next) {
  16.     node *checkNode = (node *) malloc(sizeof(node));
  17.    
  18.     if (checkNode == NULL) {
  19.         printf("Error creating a new node.\n");
  20.         exit(0);
  21.     }
  22.    
  23.     checkNode->checkNumber = checkNumber;
  24.     strcpy(checkNode->date, date);
  25.     strcpy(checkNode->to, to);
  26.     strcpy(checkNode->description, description);
  27.     checkNode->amount = amount;
  28.     checkNode->next = next;
  29.  
  30.     return checkNode;
  31. }
  32.  
  33. void add(node *head, node *node) {
  34.  
  35.     if (head == NULL) {
  36.         printf("\nEntering head == NULL check");
  37.         head = malloc(sizeof(node));
  38.         *head = *node;
  39.         printf("*head = *node");
  40.         node->next = NULL;
  41.  
  42.     } else {
  43.         struct node *currentNode = head;
  44.         while (head->next != NULL) {
  45.             currentNode = currentNode->next;
  46.         }
  47.         currentNode->next = node;
  48.     }
  49.  
  50. }
  51.  
  52. void printAllNodes(struct node *head) {
  53.     struct node *currentNode = head;
  54.     while (currentNode != NULL) {
  55.         printf("\n%i", currentNode->checkNumber);
  56.     }
  57.  
  58. }
  59.  
  60. int main() {
  61.  
  62.     struct node *firstNode = createCheck(1, "123456", "someone", "Rent", 123.23, NULL);
  63.     struct node *secondNode = createCheck(2, "123456", "someone", "Rent", 123.23, NULL);
  64.     struct node *thirdNode = createCheck(3, "123456", "someone", "Rent", 123.23, NULL);
  65.  
  66.     printf("All nodes created");
  67.     add(head, firstNode);
  68.     printf("\nFirst node added");
  69.     add(head, secondNode);
  70.     printf("\nsecond node added");
  71.     add(head, thirdNode);
  72.     printf("\nthird node added");
  73.     printAllNodes(head);
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement