在 C 中使用链表实现堆栈

Implement Stack using Linked List in C

提问人:ahnherin092 提问时间:11/12/2023 最后编辑:Some programmer dudeahnherin092 更新时间:11/12/2023 访问量:66

问:

我制作了这段代码,通过链表实现堆栈,并执行一些操作,如添加元素、删除等。我的输出有一些问题,这是出乎意料的。如果您可以修改 freeStack()、pop() 和 push() 函数,请随意修改它们。谢谢

// C program for linked list implementation of stack 
#include <stdio.h> 
#include <stdlib.h>

// Implement a stack as a linked list
typedef struct Node { 
    int data; 
    struct StackNode* next; 
}Node; 

typedef struct Stack {
    int size;
    Node *pTop;
}Stack;

// Initialize the stack
void init (Stack *s) {
    s = (Stack*)malloc(sizeof(Stack));
    s->size = 0;
    s->pTop = NULL;
}

// Check if a stack is empty
int isEmpty (Stack st) {
    return (st.size == 0);
}

// Push operation to add new element
int push (int newData, Stack *st) 
{
// put newData in a stack node -> this node becomes the top element
    Node *p = (Node*)malloc(sizeof(Node));
    if (p == NULL) // check if memory allocation for the new node is successful
        return 0; // return 0 to indicate failure
    p->data = newData;
    p->next = st->pTop;
    st->pTop = p;
    st->size++;
    return 1;
}

// Pop operation to delete the first top element
int pop (Stack *st) 
{
    Node *p;
    if (isEmpty(*st))
        return 0; // fail to pop
    p = st->pTop;
    st->pTop = st->pTop->next; // change the pointer of the top element to the second top element
    st->size--;
    free(p);
    return 1;
}

// Top operation to return the top element
int top(Stack st) {
    if (isEmpty(st))
        return 1;
    return st.pTop->data;
}

// Display elements of the stack
void display(Stack *s)
{
    Node *p = s->pTop;
    printf("Stack: ");
    for (; p != NULL; p = p->next) {
        printf("%d ", p->data);
    }
    printf("\n");
}

// Free all memory used by the stack
void freeStack(Stack* st) {
    Node* current = st->pTop;
    Node* next;

    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }

    st->pTop = NULL;
    st->size = 0;
}

int main() 
{ 
    // Memory allocation
    Stack st;
    init(&st);

    // Push elements
    push(10, &st); 
    push(20, &st); 
    push(30, &st); 
    display(&st);

    printf("30 is popped from stack\n", pop(&st)); 
    display(&st);

    printf("Top element is %d\n", top(st)); 

    freeStack(&st);

    return 0; 
} 

输出如下:

linked_list_stack.c: In function 'push':
linked_list_stack.c:36:17: warning: assignment to 'struct StackNode *' from incompatible pointer type 'Node *' [-Wincompatible-pointer-types]
   36 |         p->next = st->pTop;
      |                 ^
linked_list_stack.c: In function 'pop':
linked_list_stack.c:50:14: warning: assignment to 'Node *' from incompatible pointer type 'struct StackNode *' [-Wincompatible-pointer-types]
   50 |     st->pTop = st->pTop->next; // change the pointer of the top element to the second top element
      |              ^
linked_list_stack.c: In function 'display':
linked_list_stack.c:68:25: warning: assignment to 'Node *' from incompatible pointer type 'struct StackNode *' [-Wincompatible-pointer-types]
   68 |     for (; p != NULL; p = p->next) {
      |                         ^
linked_list_stack.c: In function 'freeStack':
linked_list_stack.c:80:14: warning: assignment to 'Node *' from incompatible pointer type 'struct StackNode *' [-Wincompatible-pointer-types]
   80 |         next = current->next;
      |              ^
Stack: 30 20 10 -489483407
30 is popped from stack
Stack: 20 10 -489483407
Top element is 20
c 链接列表 堆栈 输出

评论

1赞 0___________ 11/12/2023
什么?没有声明struct StackNode
1赞 Some programmer dude 11/12/2023
你的一个问题是初学者的一个常见问题:你忘记了函数参数是按值传递的。这意味着,调用中使用的值将复制到函数局部参数变量中。这些局部参数变量的生存期与任何其他局部函数变量一样,仅持续到函数结束。当函数返回时,对这些变量所做的所有修改都将丢失。因此,请考虑一下,以及例如函数和赋值中会发生什么。inits
1赞 Some programmer dude 11/12/2023
您确实应该将任何和所有警告消息视为错误。你在代码中做了一些坏事,这是 C 标准允许的,但并不好,编译器试图告诉你这一点。
0赞 Some programmer dude 11/12/2023
关于函数,当我查看它是如何调用时,您不需要调用。指针已指向已分配的结构对象。请花一些时间刷新初学者书籍中有关指针和动态分配以及指针运算符的章节。initmallocs&
1赞 Amit 11/13/2023
@0____ ,应该是 。这很可能是一个错别字。StackNodeNode

答:

1赞 0___________ 11/12/2023 #1

编译器不知道它是什么。您需要:struct StackNode

typedef struct StackNode { 
    int data; 
    struct StackNode* next; 
}Node; 

另外,我不知道您试图存档什么。返回对已分配结构的引用。

// Initialize the stack
Stack *init (void) {
    Stack *s = malloc(sizeof(*s));
    if(s)
    {
       s->size = 0;
       s->pTop = NULL;
    }
    return s;
}

在功能上:main

    Stack *st;
    st = init();
    if(st)
    {
         push(10, st); 
         /* ... */
    }
0赞 chqrlie 11/12/2023 #2

该函数应返回顶部节点的数据字段,而不仅仅是发布的 1。pop