如何在C编程中使用Linked List将堆栈链接到其他堆栈?

时间:2021-04-08 07:18:32

I made my own stack using linked list. But I think this is wrong. my push method is linking Stack1 to other stacks. So, I think it is like...

我使用链表创建了自己的堆栈。但我认为这是错误的。我的推送方法是将Stack1链接到其他堆栈。所以,我认为这就像......

In my main function,

push(stack1, 10);
push(stack1, 20);

[Stack1] -> [nextStack]
[Stack1] -> [nextStack] (new address from first nextStack)

So, It's like... I am repeating to link stack1 to other stacks again and again...

所以,就像......我正在重复将stack1连连到其他堆栈......

this is my stack using linked list code below.

这是我使用下面链接列表代码的堆栈。

#include <stdio.h>
#include <stdlib.h>

typedef struct{
    int data;
   struct stack *top;
}stack;

void push(stack *currentStack, int data){

    if (currentStack->top == NULL)
        fprintf(stderr, "Stack is emtpy");

    else{
        stack *nextStack = (stack*)malloc(sizeof(stack));
        currentStack->data = data;
        currentStack->top = nextStack;

        printf("currentStack is %d\n", currentStack->data);
    }
}

int main(){

    stack* stack1;
    stack1 = (stack*)malloc(sizeof(stack));

    push(stack1, 10);
    push(stack1, 20);

    return 1;
}

and this is the result of my code.

这是我的代码的结果。

currentStack is 10
currentStack is 20

1 个解决方案

#1


1  

#include <stdio.h>
#include <stdlib.h>

struct stack
{
    int data;
    struct stack *top;
}  *head = NULL;


void push(int data)
{
    if (head == NULL)   //that means stack is empty
    {
        head =(struct node *)malloc(1*sizeof(struct node));
        head->top = NULL;
        head->data = data;
    }
    else
    {
        temp =(struct node *)malloc(1*sizeof(struct node));
        temp->top = head;
        temp->data = data;
        head = temp;
    }

}

Your push() function is incomplete. It should consider two cases one when stack is empty and one when it is not.

你的push()函数不完整。当堆栈为空时应考虑两种情况,而当堆栈为空时应考虑一种情况。

Also there is no need to pass a pointer-to-stack in push() function because push() function by default pushes the new element on the topmost node and there is only one stack.

此外,不需要在push()函数中传递指针到堆栈,因为默认情况下push()函数会在最顶层节点上推送新元素,并且只有一个堆栈。

Also you have not initialised your stack pointer with NULL. This might give you undefined behaviour during program run.

您还没有使用NULL初始化堆栈指针。这可能会在程序运行期间为您提供未定义的行为。

#1


1  

#include <stdio.h>
#include <stdlib.h>

struct stack
{
    int data;
    struct stack *top;
}  *head = NULL;


void push(int data)
{
    if (head == NULL)   //that means stack is empty
    {
        head =(struct node *)malloc(1*sizeof(struct node));
        head->top = NULL;
        head->data = data;
    }
    else
    {
        temp =(struct node *)malloc(1*sizeof(struct node));
        temp->top = head;
        temp->data = data;
        head = temp;
    }

}

Your push() function is incomplete. It should consider two cases one when stack is empty and one when it is not.

你的push()函数不完整。当堆栈为空时应考虑两种情况,而当堆栈为空时应考虑一种情况。

Also there is no need to pass a pointer-to-stack in push() function because push() function by default pushes the new element on the topmost node and there is only one stack.

此外,不需要在push()函数中传递指针到堆栈,因为默认情况下push()函数会在最顶层节点上推送新元素,并且只有一个堆栈。

Also you have not initialised your stack pointer with NULL. This might give you undefined behaviour during program run.

您还没有使用NULL初始化堆栈指针。这可能会在程序运行期间为您提供未定义的行为。