1

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);
    }

}
Manny O
  • 97
  • 2
  • 14
  • 1
    just use a pointer as your "item" type. then you can store pointers to pretty much anything. a pointer's a pointer... it'd be up to the code using your stack to figure out what those pointers are. – Marc B Mar 03 '14 at 18:24

2 Answers2

3

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;
Community
  • 1
  • 1
Jean
  • 368
  • 2
  • 13
  • Ok so whenever i want to push the value unto the stack I have to use a if statement of switch case to specify the type? Can you show an example please @Phibonacci – Manny O Mar 03 '14 at 19:14
  • I edited my post. 'root' is the first element of the chained list. – Jean Mar 03 '14 at 19:38
  • To be honest im so confused now lol. I want to push user values into a stack but be able to distinguish which types they are so when I call a function like "add" ill be able to check if the 2 previous values are integers. – Manny O Mar 03 '14 at 20:01
  • ' while(1) { printf("repl>"); char *storage [30]; char *tok; char g; char buffer[20]; int pos = 0, i; fgets(buffer,sizeof(buffer),stdin); tok = strtok(buffer," "); while (tok) { strcpy(g,tok); Push(g, &head); Display(&head); tok = strtok(NULL," "); } }' – Manny O Mar 03 '14 at 20:01
  • Thats the code I have to enter the token into the stack^^ (dont know how to format it) @Phibonacci – Manny O Mar 03 '14 at 20:10
  • You need some parsing for that. You have to be able to tell if the inputed string is an int (only digits), a float (one dot in it) or a simple text string (anything else). Depending of what you detected you will call a different function that will set your 'type' variable, allocate memory at you void *value and store the converted value. sscanf libc function can do most of the job for you. – Jean Mar 03 '14 at 20:16
  • The code you show me there is wrong at many level (pointer confusion, undeclared variable, and unexpected behavior). I think you expect it to send all the substring of a string divided by spaces to the push function. At first I would suggest you to simply take the standard input as one string and only then you could improve your parsing. – Jean Mar 03 '14 at 20:35
  • Yea I just figured that, im a beginner in C so its very confusing. Can you please help point out whats wrong with this code and how to fix it? – Manny O Mar 03 '14 at 20:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48905/discussion-between-manny-o-and-phibonacci) – Manny O Mar 03 '14 at 20:42
1
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.

user376507
  • 2,004
  • 1
  • 19
  • 31
  • Ok so whenever i want to push the value unto the stack I have to use a if statement of switch case to specify the type? Can you show an example please @user376507 – Manny O Mar 03 '14 at 19:15
  • Sorry for delayed response, looks like someone already posted an example. Hope that helped. – user376507 Mar 04 '14 at 11:21
  • Yes it did, thanks anyway. Quick question tho. Im trying to pop out the first element of the stack but I dont know how to do it when I have 2 different data types (Char * & double). Do you have an idea? @user376507 – Manny O Mar 04 '14 at 14:07
  • Once you decide to pop an element check the type of element and access the element based on type. – user376507 Mar 04 '14 at 23:47