I hope the details below help, but to summarize... I am trying to implement a stack data structure using structs. I receive only one error about incompatible pointer types when assigning however they are both single pointers to my struct. However, I do have an issue which I can't understand, the pointer type I am trying to assign (show below) has a type of 'StackNode * (*)(int)'
, which I cannot seem to understand why, it should just be StackNode *
.
Does anyone have an answer? Thank you.
My error given is this...
Problem1.c:63:14: warning: assignment to ‘StackNode *’ from incompatible pointer type ‘StackNode * (*)(int)’ [-Wincompatible-pointer-types]
63 | root = newNode;//inserting at the front of the stack, so make sure to set our root node to point to newNode
My code block which I am having trouble with is this function:
//This is a function that will create a new StackNode structure given some data and return pointer to the node
StackNode* newNode(int data){
StackNode* newNode = (StackNode*)malloc(sizeof(StackNode)); //allocate memory for the newly made stacknode
newNode->data = data; //set the data
newNode->next = NULL; //set the next node to null
return newNode;
}
The full code which I have written is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
//type definition for each node in the queue
typedef struct StackNode{
int data;
struct StackNode* next;
}StackNode;
//function prototype definitions
int isEmpty(StackNode*);
int peek(StackNode*);
int pop(StackNode*);
void push(StackNode*, int);
StackNode* newNode(int data);
int main(){
int userInput = 0;
int validInput = 1;
StackNode *root = NULL;
while(validInput){
printf("Please enter a value to push into the stack");
scanf("%d", &userInput);
if(userInput>=0){
push(root, userInput);
printf("%d pushed to stack\n", userInput);//print the data we to the stack
}else{
break;
}
}
while(root!=NULL){
printf("%d was popped from the stack\n", root->data);
}
return 0;
}
int isEmpty(StackNode* root){
return !root; //NULL evaluates to false, so this returns whether it isnt null or not
}
//pushes a new node to the top of the stack by taking in data and using our function newNode to make a new node from the data.
//takes in a pointer to the root node since we want to adjust the stack.
void push(StackNode* root, int data){
StackNode* node = newNode(data); //create a new node from our data.
node->next = root;//insert at the front of the stack, so save the node root is pointing at and make newNode point there
root = newNode;//inserting at the front of the stack, so make sure to set our root node to point to newNode
}
//removes the first node and returns the value stored by it, makes sure to free the root node.
//It takes in a pointer to the root node since we want to free the space taken by the root node.
int pop(StackNode *root){
if(isEmpty(root)){
return INT_MIN; //return smallest possible integer since root doesnt exist
}
StackNode* temp = root; //sets a temporary node equal to the root node in order to free it after we are done
int poppedData = temp->data;//get the data from the root node
root = root->next;//set the root node to the next node in line
free(temp); //since we we no longer want the root node in memory, free it by freeing our temp node.
return poppedData; //return the data the root is storing.
}
//show the data stored at the top of the stack without removing the node itself, takes in pointer to root node
int peek(StackNode *root){
if(isEmpty(root)){
return INT_MIN; //return smallest possible integer since root doesnt exist
}
return root->data; //return the data the root is storing.
}
//This is a function that will create a new StackNode structure given some data and return pointer to the node
StackNode* newNode(int data){
StackNode* newNode = (StackNode*)malloc(sizeof(StackNode)); //allocate memory for the newly made stacknode
newNode->data = data; //set the data
newNode->next = NULL; //set the next node to null
return newNode;
}
CodePudding user response:
in push
root = newNode;
newNode
is a function!
you probably meant
root=node;