Reputation: 107
I have a Stack (implemented using a linked list structure) that contains two types: char * (holds values of the strings) & double (holds values of the numbers). I have a Pop function which is void that pops the top value off the stack. But I want to also have access to that value.
But I can't figure out how to do that since I cant do a function overload in C which will allow me to have two Pop functions (one of type char * & the other of type double) that return their respective types. Does anybody know of another way to go about this?
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
typedef struct stack
{
char *valC;
double valNum;
struct stack *next;
}node;
node *head = NULL;
int isnumber(char *str)
{
int i;
for (i = 0; str[i] != '\0'; i++)
{
if (!isdigit(str[i]))
return (0);
}
return (1);
}
node *get_node(void *item)
{
node *temp;
temp = (node*)malloc(sizeof(node));
if (temp == NULL) printf("Memory Cannot Be Allocated");
temp->valC = item;
temp->next = NULL;
return (temp);
}
void Push (char *Item, double num, node **head)
{
node *New = (node*)malloc(sizeof(node));
if (New == NULL) printf("Memory Cannot Be Allocated");
New->valNum = num;
New->valC = Item;
//node *get_node(void*);
//New = get_node(Item);
New->next = *head;
*head = New;
}
int Sempty (node *temp)
{
if(temp == NULL)
return 1;
else
return 0;
}
void Pop (node **head)
{
char *item;
double itemN;
node *temp;
node *prev = (node*)malloc(sizeof(node));
//item = (*head)->valC;
//itemN = (*head)->valNum;
temp = *head;
prev->next = temp;
*head = (*head)->next;
free(temp);
if (prev->valC != NULL)
{
return (prev->valC);
}
else if (prev->valNum > 0)
{
return (prev->valNum);
}
}
double PopN (node **head)
{
double itemN;
node *temp;
itemN = (*head)->valNum;
temp = *head;
*head = (*head)->next;
free(temp);
return(itemN);
}
void Display (node **head)
{
node *temp;
temp = *head;
char *error;
if(Sempty(temp)) {
printf("Empty Stack!\n");
error = "::error::\n";
Push(error, 0, &temp);
}
else
{
while (temp != NULL)
{
if (temp->valC != NULL) printf("%s\n", temp->valC);
if (temp->valNum > 0) printf("%f\n", temp->valNum);
temp = temp->next;
}
}
}
void main()
{
node *inp = (node*)malloc(sizeof(node));;
while(1)
{
printf("repl> ");
char *storage [30];
char *tok;
double my_double;
char buffer[50];
int pos = 0;
fgets(buffer,sizeof(buffer),stdin);
tok = strtok(buffer," ");
while (tok != NULL)
{
if (strcmp(tok, "add") == 0) printf("YES!!");
else if (strcmp(tok, "add\n") == 0) printf("YELZZ!!!");
if (strcmp(tok, "quit") == 0) exit(1);
else if (strcmp(tok, "quit\n") == 0) exit(1);
if (strcmp(tok, "pop") == 0)
{
Pop(&head);
break;
}
else if (strcmp(tok, "pop\n") == 0)
{
Pop(&head);
break;
}
if (sscanf(tok, "%lf", &my_double) > 0)
{
Push(NULL, my_double, &head);
}
else
Push(strdup(tok), 0, &head);
//if(Sempty(head)) Push("::error::", 0, &head);
tok = strtok(NULL," ");
}
Display(&head);
}
}
Upvotes: 0
Views: 87
Reputation: 3162
one thing you can do is to written a pointer, of type void * then you can type cast it to your required type later.
i'll give an example, you can get idea from it to implement pop()
void *check(int x)
{
static void *p;
int a=5;
char *c="abcd";
if(x==1)
return p=&a;
else
return p=c;
}
int main()
{
int * a;
char * b;
a=(int *)check(1);
b=(char *)check(2);
printf("%d %s",*a,b);
}
for your pop() you can even add as flag as parameter to know the type of returned pointer to type cast in calling function. as
void * pop(node **head, int &flag)
set flag to indicate type of returned pointer.
Upvotes: 1
Reputation: 50775
You cannot overload functions in C. You already have a Pop function that pops the topmost node from the stack. The node can either contain a number or a string. Your Display function displays the content of the node and tests correctly what kind of data is in the node.
You should write a function that takes a node as argument and return say false if it's a number and true if it's a string:
bool IsString(node *inp)
{
return temp->valC != NULL ;
}
But even if you could overload functions in C, you don't know what type of node (number or string) is on the top of the stack. What would happen if the you call Pop function that returns a string and the topmost node contains a number ?
Just do the pop, and then find out if the node is a string or a number and act accordingly.
Upvotes: 1