C 制作多个堆栈
我想在C中创建一个堆栈数组,在这里我应该能够保留单个堆栈及其各自的信息。我目前有以下实现,它只适用于一个堆栈。如何修改push和pop函数,以实现使用同一函数的多个堆栈。 (我很容易在Java中实现这一点,因为我可以创建一个类,但我不知道如何使用C)C 制作多个堆栈,c,stack,C,Stack,我想在C中创建一个堆栈数组,在这里我应该能够保留单个堆栈及其各自的信息。我目前有以下实现,它只适用于一个堆栈。如何修改push和pop函数,以实现使用同一函数的多个堆栈。 (我很容易在Java中实现这一点,因为我可以创建一个类,但我不知道如何使用C) #包括 #包括 结构节点{ int数据; 结构节点*下一步; }; 结构节点*first=NULL; 无效推送(整数x){ 结构节点*newnode=malloc(sizeof(结构节点)); newnode->data=x; 新建节点->下一步=
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
结构节点*first=NULL;
无效推送(整数x){
结构节点*newnode=malloc(sizeof(结构节点));
newnode->data=x;
新建节点->下一步=第一步;
第一个=新节点;
}
int-pop(){
int temp=第一个->数据;
第一个=第一个->下一个;
返回温度;
}
在pop()
函数中,代码内存泄漏。你应该释放你拥有的内存
从@Jongware在您问题下方的评论中获取建议
下面是push()
和pop()函数的新版本
#include <stdlib.h>
struct node {
int data;
struct node *prev;
};
void push(struct node **stack, int x) {
if (stack != NULL)
{
struct node *newnode = malloc(sizeof(struct node));
newnode->data = x;
newnode->prev = *stack;
*stack = newnode;
} else
{
// You didn't give me a valid pointer to a stack, so I'm ignoring you!
}
}
int pop(struct node **stack) {
int temp = 0; // This is the default value that is returned when there is an error.
struct node *oldnode;
if (stack != NULL)
{
if (*stack != NULL)
{
oldnode= *stack;
temp = oldnode->data;
(*stack) = oldnode->prev;
free(oldnode);
} else
{
// The stack is empty. I will just ignore you and return the default value for temp.
}
} else
{
// You didn't give me a valid pointer to a stack so I'm ignoring you and returning the default value of 0 for temp!
}
return temp;
}
#包括
结构节点{
int数据;
结构节点*prev;
};
无效推送(结构节点**堆栈,int x){
如果(堆栈!=NULL)
{
结构节点*newnode=malloc(sizeof(结构节点));
newnode->data=x;
newnode->prev=*堆栈;
*stack=newnode;
}否则
{
//你没有给我一个指向堆栈的有效指针,所以我忽略了你!
}
}
int pop(结构节点**堆栈){
int temp=0;//这是出现错误时返回的默认值。
结构节点*oldnode;
如果(堆栈!=NULL)
{
如果(*堆栈!=NULL)
{
oldnode=*堆栈;
temp=oldnode->data;
(*堆栈)=oldnode->prev;
自由(oldnode);
}否则
{
//堆栈为空。我将忽略您并返回temp的默认值。
}
}否则
{
//您没有给我一个指向堆栈的有效指针,因此我将忽略您并返回temp的默认值0!
}
返回温度;
}
下面是一个如何使用它们的示例:
#include <stdio.h>
int main()
{
struct node *stack1 = NULL, *stack2 = NULL;
int value;
// Push some values onto the stacks
printf("Pushing 7 and then 8 onto stack1\n");
push(&stack1, 7);
push(&stack1, 8);
printf("Pushing 3 onto stack2\n");
push(&stack2, 3);
// Pop and print both stacks
value = pop(&stack2);
printf("Popped %d from stack2\n", value);
value = pop(&stack1);
printf("Popped %d from stack1\n", value);
value = pop(&stack1);
printf("Popped %d from stack1\n", value);
return 0;
}
#包括
int main()
{
结构节点*stack1=NULL,*stack2=NULL;
int值;
//将一些值推送到堆栈上
printf(“将7和8推到堆栈1上\n”);
推送(&stack1,7);
推送(和堆叠1,8);
printf(“将3推到堆栈2上\n”);
推送(和堆叠2、3);
//弹出并打印两个堆栈
值=pop(&stack2);
printf(“从堆栈中弹出%d\n”,值);
值=pop(&stack1);
printf(“从堆栈1中弹出%d”,值);
值=pop(&stack1);
printf(“从堆栈1中弹出%d”,值);
返回0;
}
至于您应该在哪里声明真正由您决定的堆栈指针,以及您打算如何使用它们
阅读一些选项以及如何使用它们
此外,我还必须在函数中声明这些指针时包含警告。无论在哪个函数中声明指针,都必须确保在退出该函数之前将所有内容弹出堆栈,否则将丢失指针并泄漏所有分配的内存。如果这不是您想要的,或者您希望指针比函数寿命长,那么您可以全局声明或传递指针,确保在程序存在或丢失指针之前,所有内容都从堆栈中弹出
<> P>另一个你可能要考虑的问题是,当你在空栈上使用<代码> POP()/<代码>时会发生什么?我给您的实现只是返回0
并忽略您。您可能希望更好地处理这个问题。在pop()
函数中的代码中存在内存泄漏。你应该释放你拥有的内存
从@Jongware在您问题下方的评论中获取建议
下面是push()
和pop()函数的新版本
#include <stdlib.h>
struct node {
int data;
struct node *prev;
};
void push(struct node **stack, int x) {
if (stack != NULL)
{
struct node *newnode = malloc(sizeof(struct node));
newnode->data = x;
newnode->prev = *stack;
*stack = newnode;
} else
{
// You didn't give me a valid pointer to a stack, so I'm ignoring you!
}
}
int pop(struct node **stack) {
int temp = 0; // This is the default value that is returned when there is an error.
struct node *oldnode;
if (stack != NULL)
{
if (*stack != NULL)
{
oldnode= *stack;
temp = oldnode->data;
(*stack) = oldnode->prev;
free(oldnode);
} else
{
// The stack is empty. I will just ignore you and return the default value for temp.
}
} else
{
// You didn't give me a valid pointer to a stack so I'm ignoring you and returning the default value of 0 for temp!
}
return temp;
}
#包括
结构节点{
int数据;
结构节点*prev;
};
无效推送(结构节点**堆栈,int x){
如果(堆栈!=NULL)
{
结构节点*newnode=malloc(sizeof(结构节点));
newnode->data=x;
newnode->prev=*堆栈;
*stack=newnode;
}否则
{
//你没有给我一个指向堆栈的有效指针,所以我忽略了你!
}
}
int pop(结构节点**堆栈){
int temp=0;//这是出现错误时返回的默认值。
结构节点*oldnode;
如果(堆栈!=NULL)
{
如果(*堆栈!=NULL)
{
oldnode=*堆栈;
temp=oldnode->data;
(*堆栈)=oldnode->prev;
自由(oldnode);
}否则
{
//堆栈为空。我将忽略您并返回temp的默认值。
}
}否则
{
//您没有给我一个指向堆栈的有效指针,因此我将忽略您并返回temp的默认值0!
}
返回温度;
}
下面是一个如何使用它们的示例:
#include <stdio.h>
int main()
{
struct node *stack1 = NULL, *stack2 = NULL;
int value;
// Push some values onto the stacks
printf("Pushing 7 and then 8 onto stack1\n");
push(&stack1, 7);
push(&stack1, 8);
printf("Pushing 3 onto stack2\n");
push(&stack2, 3);
// Pop and print both stacks
value = pop(&stack2);
printf("Popped %d from stack2\n", value);
value = pop(&stack1);
printf("Popped %d from stack1\n", value);
value = pop(&stack1);
printf("Popped %d from stack1\n", value);
return 0;
}
#包括
int main()
{
结构节点*stack1=NULL,*stack2=NULL;
int值;
//将一些值推送到堆栈上
printf(“将7和8推到堆栈1上\n”);
推送(&stack1,7);
推送(和堆叠1,8);
printf(“将3推到堆栈2上\n”);
推送(和堆叠2、3);
//弹出并打印两个堆栈
值=pop(&stack2);
printf(“从堆栈中弹出%d\n”,值);
值=pop(&stack1);
printf(“从堆栈1中弹出%d”,值);
值=pop(&stack1);
printf(“从堆栈1中弹出%d”,值);
返回0;
}
至于您应该在哪里声明真正由您决定的堆栈指针,以及您打算如何使用它们
阅读一些选项以及如何使用它们
此外,我还必须在函数中声明这些指针时包含警告。一时兴起
struct stack {
struct node *first;
};
void stack_init(struct stack *);
void stack_cleanup(struct stack *);
struct stack *stack_alloc(void); /* also calls stack_init on new stack */
void stack_free(struct stack *); /* calls stack_cleanup, then frees */
{
struct stack temp_stack;
stack_init(&temp_stack); /* stack is good to go! */
/* ... use stack ... */
stack_cleanup(&temp_stack); /* don't forget to clean up */
}
struct stack array_of_stacks[42];
int i;
for (i = 0; i < 42; i++)
stack_init(&array_of_stacks[i]); /* no memory allocation taking place */
/* Alternative "A" */
typedef struct node *stack_t; /* a stack_t type is a pointer to a node */
/* Alternative "B" */
typedef struct stack {
struct node *top;
} stack_t; /* stack_t is a structure containing a pointer to a node */
void stack_init(stack *s);
int stack_push(stack *s, int item);
/* Alternative "A" */
typedef struct node *stack_t; /* a stack_t type is a pointer to a node */
#define stack_top(s) (*(s)) /* dereference stack s to obtain the top pointer */
/* Alternative "B" */
typedef struct stack {
struct node *top;
} stack_t; /* stack_t is a structure containing a pointer to a node */
#define stack_top(s) ((s)->top) /* dereference struct pointer to get top pointer */
/* push new_node onto stack */
new_node->next = stack_top(s);
stack_top(s) = new_node;
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int Item;
#define ItemFormat "%d"
struct node {
Item data;
struct node *next;
};
typedef struct node *Stack;
void push(Stack *st, Item x){
struct node *newnode = malloc(sizeof(struct node));
newnode->data = x;
newnode->next = *st;
*st = newnode;
}
bool isEmpty(Stack st){
return st == NULL;
}
Item pop(Stack *st) {
if(!isEmpty(*st)){
struct node *p = *st;
Item value = p->data;
*st = p->next;
free(p);
return value;
}
fprintf(stderr, "Stack is Empty!\n");
return (Item)0;
}
bool inputItem(Item *x){
int stat;
if(1==(stat=scanf(ItemFormat, x)))
return true;
if(stat == EOF)
return false;
scanf("%*[^\n]");
return false;
}
void printItem(Item x){
printf(ItemFormat, x);
}
int main(void){
Stack st = NULL, array[5] = { NULL };
Item x;
while(inputItem(&x)){
push(&array[1], x);
}
while(!isEmpty(array[1])){
x = pop(&array[1]);
printItem(x);
printf("\n");
}
/*
while(inputItem(&x)){
push(&st, x);
}
while(!isEmpty(st)){
x = pop(&st);
printItem(x);
printf("\n");
}
*/
return 0;
}
struct stack
{
int data[MAX];
int top1,top2;
}s;
if((s->top1)+1==s->top2)
printf("Stack 1 overflow\n");
if((s->top2)-1==s->top1)
printf("Stack 2 overflow\n");