Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

By using typedef a new type is created. This also might decrease the possibility of future errors by forgetting to put the struct in at some point. This stackoverflow question stackoverflow question discusses why it might be good to use typedef.

Generally the use of global variables are frowned upon. When creating, reading and debugging code global variables can be affected by side affects and it can be very difficult to find where the problem is actually occurring. This stackoverflow question stackoverflow question talks about when it is proper to use global variables.

The C programming language was originally created to implement operating systems and in some cases is still used for that purpose. While C doesn't have the exception throwing capabilities of C++, Java, C# and other more modern languages errors can be handled, either by returning error codes or using setjmp() and longjmp() setjmp() and longjmp().

By using typedef a new type is created. This also might decrease the possibility of future errors by forgetting to put the struct in at some point. This stackoverflow question discusses why it might be good to use typedef.

Generally the use of global variables are frowned upon. When creating, reading and debugging code global variables can be affected by side affects and it can be very difficult to find where the problem is actually occurring. This stackoverflow question talks about when it is proper to use global variables.

The C programming language was originally created to implement operating systems and in some cases is still used for that purpose. While C doesn't have the exception throwing capabilities of C++, Java, C# and other more modern languages errors can be handled, either by returning error codes or using setjmp() and longjmp().

By using typedef a new type is created. This also might decrease the possibility of future errors by forgetting to put the struct in at some point. This stackoverflow question discusses why it might be good to use typedef.

Generally the use of global variables are frowned upon. When creating, reading and debugging code global variables can be affected by side affects and it can be very difficult to find where the problem is actually occurring. This stackoverflow question talks about when it is proper to use global variables.

The C programming language was originally created to implement operating systems and in some cases is still used for that purpose. While C doesn't have the exception throwing capabilities of C++, Java, C# and other more modern languages errors can be handled, either by returning error codes or using setjmp() and longjmp().

Corrected a typo in code that had commented out part of the code.
Source Link
pacmaninbw
  • 26.2k
  • 13
  • 47
  • 113
 case 'p':
 /*+>*>*+>*/ print_nodes(head); /* Pass by value */
 break;
 while(token != NULL){
 node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
 if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid input*/
 fprintf(stderr, "Invalid number: %c", *ptr);
 exit(EXIT_FAILURE);
 }
 /* ++> */ add_node(node_value, &head); /* Pass by reference */
 token = strtok(NULL, s);
 }
void add_node(int data, Node **head){
 Node* temp;
 
 if(*head == NULL){
 *head = malloc(sizeof(struct node));
 (*head)->value = data;
 (*head)->next_ptr = NULL;
 return;
 }
 
 temp = *head;
 while(temp->next_ptr != NULL){
 temp = temp->next_ptr;
 }
 
 if((temp->next_ptr = malloc(sizeof(struct node))) == NULL){
 fprintf(stderr, "Out of memory");
 exit(EXIT_FAILURE);
 }
 
 temp = temp->next_ptr;
 temp->value = data;
 temp->next_ptr = NULL;
}
void print_nodes(Node* head){
 Node* temp = head;
 if(temp == NULL){
 printf("Linked list is empty\n");
 }
 for(temp = head; temp != NULL; temp = temp->next_ptr){
 printf("%i\n", temp->value);
 }
}
 case 'p':
 /*+>*> print_nodes(head); /* Pass by value */
 break;
 while(token != NULL){
 node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
 if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid input*/
 fprintf(stderr, "Invalid number: %c", *ptr);
 exit(EXIT_FAILURE);
 }
 /* ++> */ add_node(node_value, &head); /* Pass by reference */
 token = strtok(NULL, s);
 }
void add_node(int data, Node **head){
 Node* temp;
 
 if(*head == NULL){
 *head = malloc(sizeof(struct node));
 (*head)->value = data;
 (*head)->next_ptr = NULL;
 return;
 }
 
 temp = *head;
 while(temp->next_ptr != NULL){
 temp = temp->next_ptr;
 }
 
 if((temp->next_ptr = malloc(sizeof(struct node))) == NULL){
 fprintf(stderr, "Out of memory");
 exit(EXIT_FAILURE);
 }
 
 temp = temp->next_ptr;
 temp->value = data;
 temp->next_ptr = NULL;
}
void print_nodes(Node* head){
 Node* temp = head;
 if(temp == NULL){
 printf("Linked list is empty\n");
 }
 for(temp = head; temp != NULL; temp = temp->next_ptr){
 printf("%i\n", temp->value);
 }
}
 case 'p':
 /*+>*/ print_nodes(head); /* Pass by value */
 break;
 while(token != NULL){
 node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
 if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid input*/
 fprintf(stderr, "Invalid number: %c", *ptr);
 exit(EXIT_FAILURE);
 }
 /* ++> */ add_node(node_value, &head); /* Pass by reference */
 token = strtok(NULL, s);
 }
void add_node(int data, Node **head){
 Node* temp;
 
 if(*head == NULL){
 *head = malloc(sizeof(struct node));
 (*head)->value = data;
 (*head)->next_ptr = NULL;
 return;
 }
 
 temp = *head;
 while(temp->next_ptr != NULL){
 temp = temp->next_ptr;
 }
 
 if((temp->next_ptr = malloc(sizeof(struct node))) == NULL){
 fprintf(stderr, "Out of memory");
 exit(EXIT_FAILURE);
 }
 
 temp = temp->next_ptr;
 temp->value = data;
 temp->next_ptr = NULL;
}
void print_nodes(Node* head){
 Node* temp = head;
 if(temp == NULL){
 printf("Linked list is empty\n");
 }
 for(temp = head; temp != NULL; temp = temp->next_ptr){
 printf("%i\n", temp->value);
 }
}
Based on chux's comment, removed NodePointer type.
Source Link
pacmaninbw
  • 26.2k
  • 13
  • 47
  • 113
typedef struct node{
 int value;
 struct node *next_ptr;
} Node;
typedef Node* NodePointer;

NodePointerNode *head;
void add_node(int data){
 NodePointerNode* temp = head;
 /* ... */
}
void print_nodes(void){
 NodePointerNode* temp = head;
 /* ... */
}
 case 'p':
 /*+>*> print_nodes(head); /* Pass by value */
 break;
 while(token != NULL){
 node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
 if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid input*/
 fprintf(stderr, "Invalid number: %c", *ptr);
 exit(EXIT_FAILURE);
 }
 /* ++> */ add_node(node_value, &head); /* Pass by reference */
 token = strtok(NULL, s);
 }
void add_node(int data, NodePointerNode *head**head){
 NodePointerNode* temp;
 
 if(*head == NULL){
 *head = malloc(sizeof(struct node));
 (*head)->value = data;
 (*head)->next_ptr = NULL;
 return;
 }
 
 temp = *head;
 while(temp->next_ptr != NULL){
 temp = temp->next_ptr;
 }
 
 if((temp->next_ptr = malloc(sizeof(struct node))) == NULL){
 fprintf(stderr, "Out of memory");
 exit(EXIT_FAILURE);
 }
 
 temp = temp->next_ptr;
 temp->value = data;
 temp->next_ptr = NULL;
}
void print_nodes(NodePointerNode* head){
 NodePointerNode* temp = head;
 if(temp == NULL){
 printf("Linked list is empty\n");
 }
 for(temp = head; temp != NULL; temp = temp->next_ptr){
 printf("%i\n", temp->value);
 }
}
typedef struct node{
 int value;
 struct node *next_ptr;
} Node;
typedef Node* NodePointer;

NodePointer *head;
void add_node(int data){
 NodePointer temp = head;
 /* ... */
}
void print_nodes(void){
 NodePointer temp = head;
 /* ... */
}
 case 'p':
 /*+>*> print_nodes(head); /* Pass by value */
 break;
 while(token != NULL){
 node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
 if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid input*/
 fprintf(stderr, "Invalid number: %c", *ptr);
 exit(EXIT_FAILURE);
 }
 /* ++> */ add_node(node_value, &head); /* Pass by reference */
 token = strtok(NULL, s);
 }
void add_node(int data, NodePointer *head){
 NodePointer temp;
 
 if(*head == NULL){
 *head = malloc(sizeof(struct node));
 (*head)->value = data;
 (*head)->next_ptr = NULL;
 return;
 }
 
 temp = *head;
 while(temp->next_ptr != NULL){
 temp = temp->next_ptr;
 }
 
 if((temp->next_ptr = malloc(sizeof(struct node))) == NULL){
 fprintf(stderr, "Out of memory");
 exit(EXIT_FAILURE);
 }
 
 temp = temp->next_ptr;
 temp->value = data;
 temp->next_ptr = NULL;
}
void print_nodes(NodePointer head){
 NodePointer temp = head;
 if(temp == NULL){
 printf("Linked list is empty\n");
 }
 for(temp = head; temp != NULL; temp = temp->next_ptr){
 printf("%i\n", temp->value);
 }
}
typedef struct node{
 int value;
 struct node *next_ptr;
} Node;
Node *head;
void add_node(int data){
 Node* temp = head;
 /* ... */
}
void print_nodes(void){
 Node* temp = head;
 /* ... */
}
 case 'p':
 /*+>*> print_nodes(head); /* Pass by value */
 break;
 while(token != NULL){
 node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
 if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid input*/
 fprintf(stderr, "Invalid number: %c", *ptr);
 exit(EXIT_FAILURE);
 }
 /* ++> */ add_node(node_value, &head); /* Pass by reference */
 token = strtok(NULL, s);
 }
void add_node(int data, Node **head){
 Node* temp;
 
 if(*head == NULL){
 *head = malloc(sizeof(struct node));
 (*head)->value = data;
 (*head)->next_ptr = NULL;
 return;
 }
 
 temp = *head;
 while(temp->next_ptr != NULL){
 temp = temp->next_ptr;
 }
 
 if((temp->next_ptr = malloc(sizeof(struct node))) == NULL){
 fprintf(stderr, "Out of memory");
 exit(EXIT_FAILURE);
 }
 
 temp = temp->next_ptr;
 temp->value = data;
 temp->next_ptr = NULL;
}
void print_nodes(Node* head){
 Node* temp = head;
 if(temp == NULL){
 printf("Linked list is empty\n");
 }
 for(temp = head; temp != NULL; temp = temp->next_ptr){
 printf("%i\n", temp->value);
 }
}
Based on Vorac's comment the typedef NODE was changed to Node.
Source Link
pacmaninbw
  • 26.2k
  • 13
  • 47
  • 113
Loading
Source Link
pacmaninbw
  • 26.2k
  • 13
  • 47
  • 113
Loading
lang-c

AltStyle によって変換されたページ (->オリジナル) /