<分区>
我目前正在尝试将用户输入的项目推送到 C 中的堆栈(链表结构)中,但我希望能够将各种不同类型的内容输入到堆栈中。现在我的堆栈只能接受 int,但我希望它能够接受其他类型,如 char、double、float 等。
到目前为止我的代码:
#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);
}
}