fork download
  1. };
  2.  
  3. /* The structure works like this: Create a structure of a node,
  4.  * allocate memory for the node to be put into memory, put and
  5.  * point previous node to new node. Point new node to NULL and
  6.  * add integer value as needed
  7.  *
  8.  * root node:1 node:2 root
  9.  * [*pointer]----->[(value)|*pointer]----->[(value)|*pointer]----->[NULL]
  10.  *
  11.  * It should be noted that *pointer points to the address holding the data
  12.  * structure and does NOT point to the (value) address nor does it
  13.  * point to the value of node:#.
  14.  */
  15.  
  16. struct NODE *insert(struct NODE *list, int n);
  17. struct NODE *prints(struct NODE *list);
  18.  
  19. int main(){
  20.  
  21. //declare the root node and traversal node
  22. struct NODE *initial = NULL; /* Root node. Notice it points to NULL; is currently
  23. * an empty list of nodes.
  24. */
  25. //control variables for terminal
  26. int input, value;
  27.  
  28. //terminal to control linked list program
  29. for(;;){
  30. printf("Linked List\nCommands:\n1: Add node\n2: View list\n\n");
  31. scanf("%d", &input); /* command input to control while loop and switch statement */
  32.  
  33. switch(input){
  34. case 1: printf("Input value for node: ");
  35. scanf("%d", &value);
  36. printf("\n");
  37. initial = insert(initial, value);
  38. printf("Done\n\n");
  39. break;
  40. case 2: prints(initial);
  41. break;
  42. default: return(0);
  43. };
  44.  
  45. };
  46.  
  47. }//end main
  48.  
  49. /* Start function definitions */
  50.  
  51. //insert: insert a new node into linked list
  52. // struct NODE *list: passes the root of the linked list
  53. struct NODE *insert(struct NODE *list, int n){
  54.  
  55. //create and allocate new node
  56. struct NODE *new; /* New node */
  57.  
  58. if((new = malloc(sizeof(struct NODE))) == NULL){ /* Allocate space for new struture */
  59. printf("Allocation Error: malloc returned the NULL pointer. Terminating.\n");
  60. exit(EXIT_FAILURE);
  61. };
  62.  
  63. //begin adding values to new node
  64. new->value = n; /* Input value into data structure new NODE*/
  65. new->link = list; /* new will point to list argument passed */
  66.  
  67. return(new); /* return new node to list */
  68.  
  69. }//end *insert
  70.  
  71. //prints: prints the values for each node in the linked list
  72. struct NODE *prints(struct NODE *list){
  73.  
  74. for(;list != NULL; list = list->link){
  75. printf("%d ", list->value); /* derencerences node pointer and accesses value */
  76. }
  77.  
  78. printf("\n");
  79.  
  80. }//end *prints
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:1: error: expected identifier or '(' before '}' token
 };
 ^
prog.c: In function 'main':
prog.c:22:25: error: 'NULL' undeclared (first use in this function)
  struct NODE *initial = NULL; /* Root node. Notice it points to NULL; is currently
                         ^
prog.c:22:25: note: each undeclared identifier is reported only once for each function it appears in
prog.c:30:3: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
   printf("Linked List\nCommands:\n1: Add node\n2: View list\n\n");
   ^
prog.c:30:3: warning: incompatible implicit declaration of built-in function 'printf'
prog.c:30:3: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:31:3: warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]
   scanf("%d", &input); /* command input to control while loop and switch statement */
   ^
prog.c:31:3: warning: incompatible implicit declaration of built-in function 'scanf'
prog.c:31:3: note: include '<stdio.h>' or provide a declaration of 'scanf'
prog.c: In function 'insert':
prog.c:58:12: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
  if((new = malloc(sizeof(struct NODE))) == NULL){ /* Allocate space for new struture */
            ^
prog.c:58:12: warning: incompatible implicit declaration of built-in function 'malloc'
prog.c:58:12: note: include '<stdlib.h>' or provide a declaration of 'malloc'
prog.c:58:26: error: invalid application of 'sizeof' to incomplete type 'struct NODE'
  if((new = malloc(sizeof(struct NODE))) == NULL){ /* Allocate space for new struture */
                          ^
prog.c:58:44: error: 'NULL' undeclared (first use in this function)
  if((new = malloc(sizeof(struct NODE))) == NULL){ /* Allocate space for new struture */
                                            ^
prog.c:59:3: warning: incompatible implicit declaration of built-in function 'printf'
   printf("Allocation Error: malloc returned the NULL pointer. Terminating.\n");
   ^
prog.c:59:3: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:60:3: warning: implicit declaration of function 'exit' [-Wimplicit-function-declaration]
   exit(EXIT_FAILURE); 
   ^
prog.c:60:3: warning: incompatible implicit declaration of built-in function 'exit'
prog.c:60:3: note: include '<stdlib.h>' or provide a declaration of 'exit'
prog.c:60:8: error: 'EXIT_FAILURE' undeclared (first use in this function)
   exit(EXIT_FAILURE); 
        ^
prog.c:64:5: error: dereferencing pointer to incomplete type 'struct NODE'
  new->value = n; /* Input value into data structure new NODE*/
     ^
prog.c: In function 'prints':
prog.c:74:15: error: 'NULL' undeclared (first use in this function)
  for(;list != NULL; list = list->link){
               ^
prog.c:75:3: warning: incompatible implicit declaration of built-in function 'printf'
   printf("%d ", list->value); /* derencerences node pointer and accesses value */
   ^
prog.c:75:3: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:78:2: warning: incompatible implicit declaration of built-in function 'printf'
  printf("\n");
  ^
prog.c:78:2: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:80:1: warning: control reaches end of non-void function [-Wreturn-type]
 }//end *prints
 ^
stdout
Standard output is empty