I want to create a program in C that differentiates between a number or a character and put them into a linked list, then output the list. The issue is that when I input anything into it, it only outputs 10. In the results down below 1 2 3, I need it to separate the char and the nums. Help will be greatly appreciated
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// define a union to overlay an integer with a char
union DataType{
int operand;
char operate;
};
// typedef to rename DataType
typedef union DataType Data;
// create a structure for a node
typedef struct NodeType{
Data data;
struct NodeType* next;
}Node;
Node* top = NULL;
// typedef to rename NodeType
void displayNode();
void addNode() {
Node * newNode = malloc(sizeof(Node));
char expression[64];
// get the stuff
printf("Enter expression: ");
fgets(expression, 64, stdin);
//char array , int size, stdin
for (int i = 0; i < strlen(expression); i ) { //checks individual character
if (isdigit((expression[i]))){ // if digit add it to operate
(*newNode).data.operate = (expression[i]); // how
}else //else add it to operand
newNode->data.operand = (expression[i]); //how
}
if (top == NULL)
top = newNode; // update headPtr in main
else {
Node *currPtr = top;
// find last Node
while (currPtr->next != NULL)
currPtr = currPtr->next;
// insert new Node
currPtr->next = newNode;
}
}
void popNode()
{
if(top != NULL)
{
Node * temp = top;
top = top->next;
free(temp);
}
}
void displayNode()
{
if(top == NULL)
{
printf("Error");
return;
}
Node * curr = top;
// while(curr -> next != NULL){
printf("\n --------------------------------- ");
printf("\nOperator stack: ");
printf("%c ", curr->data.operate);
printf("\nOperand stack: ");
printf("%d ", curr->data.operand);
printf("\n --------------------------------- ");
curr = curr->next;
// }
}
int main()
{
int menu = 1;
do {
//call an array in main
addNode(&top);
displayNode(&top);
char cont;
printf("\nDo you want to enter another expression (Y/N)?");
scanf(" %c", &cont);
if (cont == 'n'|| cont == 'N') { // if not close menu
menu = 0;
}
}while(menu == 1);
return 0;
}
Enter expression:1 2 3
---------------------------------
Operator stack:
Operand stack: 10
---------------------------------
Do you want to enter another expression (Y/N)?
CodePudding user response:
Testing out your code quickly indicated that the addNode function was only creating one initial node. That is why you only see one operator stack output. With that I moved the node creation line of code along with the node linkage loop inside the for loop.
Following is a revised version of your code with the noted changes in the addNode function.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// define a union to overlay an integer with a char
union DataType
{
int operand;
char operate;
};
// typedef to rename DataType
typedef union DataType Data;
// create a structure for a node
typedef struct NodeType
{
Data data;
struct NodeType* next;
} Node;
Node* top = NULL;
// typedef to rename NodeType
void displayNode();
void addNode()
{
char expression[64];
// get the stuff
printf("Enter expression: ");
fgets(expression, 64, stdin);
//char array , int size, stdin
for (int i = 0; i < strlen(expression); i ) //checks individual character
{
Node * newNode = malloc(sizeof(Node)); /* Moved this line of code here to build multiple nodes */
if (isdigit((expression[i]))) // if digit add it to operate
{
(*newNode).data.operate = (expression[i]); // how
}
else //else add it to operand
newNode->data.operand = (expression[i]); //how
/* Moved this block of code inside the for loop to create the linked list */
if (top == NULL)
top = newNode; // update headPtr in main
else
{
Node *currPtr = top;
// find last Node
while (currPtr->next != NULL)
currPtr = currPtr->next;
// insert new Node
currPtr->next = newNode;
}
}
}
void popNode()
{
if(top != NULL)
{
Node * temp = top;
top = top->next;
free(temp);
}
}
void displayNode()
{
if(top == NULL)
{
printf("Error");
return;
}
Node * curr = top;
while(curr -> next != NULL){
printf("\n --------------------------------- ");
printf("\nOperator stack: ");
printf("%c ", curr->data.operate);
printf("\nOperand stack: ");
printf("%d ", curr->data.operand);
printf("\n --------------------------------- ");
curr = curr->next;
}
}
int main()
{
int menu = 1;
do
{
//call an array in main
addNode();
displayNode();
char cont;
printf("\nDo you want to enter another expression (Y/N)?");
scanf(" %c", &cont);
if (cont == 'n'|| cont == 'N') // if not close menu
{
menu = 0;
}
}
while(menu == 1);
return 0;
}
Following was the output at the terminal utilizing your test expression.
@Dev:~/C_Programs/Console/Diff/bin/Release$ ./Diff
Enter expression: 1 2 3
---------------------------------
Operator stack: 1
Operand stack: 49
---------------------------------
---------------------------------
Operator stack:
Operand stack: 43
---------------------------------
---------------------------------
Operator stack: 2
Operand stack: 50
---------------------------------
---------------------------------
Operator stack:
Operand stack: 43
---------------------------------
---------------------------------
Operator stack: 3
Operand stack: 51
---------------------------------
Do you want to enter another expression (Y/N)?
I suspect that there is some more refinement that you would probably do but this gets your code to the point of creating multiple nodes and linking them. Give that a try and see if it meets the spirit of your project.