在C中使用链表实现堆栈

问题描述 投票:0回答:1

我编写了这段代码来通过链表实现堆栈,并进行一些添加元素、删除等操作。我的输出存在一些问题,这是意料之外的。如果您可以修改 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 linked-list stack output
1个回答
0
投票

编译器不知道

struct StackNode
是什么。你需要:

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

© www.soinside.com 2019 - 2024. All rights reserved.