Question

I'm currently trying to push items of a user input into a stack (linked list structure) in C, but I want to be able to enter in various different types into the stack. Right now my stack can only take in int, but I want it to be able to take in other types like char, double, float, etc.

My Code Thus Far:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>


typedef struct stack
{
    int val;
    struct stack *next;
}node;

node *head = NULL;

void Push (int Item, node **head)
{
    node *New;
    node *get_node(int);
    New = get_node(Item);
    New->next = *head;
    *head = New;
}

node *get_node(int item)
{
    node *temp;
    temp = (node*)malloc(sizeof(node));
    if (temp == NULL) printf("Memory Cannot Be Allocated");
    temp->val = item;
    temp->next = NULL;
    return (temp);
}

int Sempty (node *temp)
{
    if(temp == NULL)
        return 1;
    else
        return 0;
}

int Pop (node **head)
{
    int item;
    node *temp;
    item = (*head)->val;
    temp = *head;
    *head = (*head)->next;
    free(temp);
    return(item);
}

void Display (node **head)
{
    node *temp;
    temp = *head;
    if(Sempty(temp)) printf("The stack is empty\n");
    else
    {
        while (temp != NULL)
        {
            printf("%s", temp->val);
            temp = temp->next;
        }
    }
}

void main()
{
    char* in;
    int data, item, i;
    char length[5];
    for(i = 0; i <= sizeof(length); i++)
    {
    printf("Enter a value: ");
    scanf("%c", &in);
    strcpy(in, in);
    Push(in, &head);
    Display(&head);
    }

}
Was it helpful?

Solution

I would use a void pointer and cast it whenever I need it. You can't directly store the type of it but you still can use an int variable to access the right function in an array of function pointer that will use the right cast.

typedef struct stack
{
    void *val;
    int type;
    struct stack *next;
}node;

With type matching one function of your array of function pointer. How can I use an array of function pointers?

You can also make a simple switch case on your "type" (definitely appropriated).

Edit: simple example:

while (root != NULL)
    {
        switch (root->type) {
        case 0:
          printf("%d\n", *(int *)(root->val));
          break;
        case 1:
          printf("%c\n", *(char *)(root->val));
          break;
        default:
          printf("unexpected type\n");
        }
        root = root->next;
    }

It would probably make more sens with a char instead of an int so you could just do case 'c', case 'i'.

Be careful, you have a void *, it's a pointer to your variable, don't forget to allocate it.

root->val = malloc(sizeof(int));
*(int *)(root->val) = 2;

OTHER TIPS

typedef struct stack
{
    int type;
    void *ptr;
    struct stack *next;
}node;

Based on what type of element you want to store you can do an appropriate malloc pointed by ptr.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top