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