提问人:ahnherin092 提问时间:11/12/2023 最后编辑:Some programmer dudeahnherin092 更新时间:11/12/2023 访问量:66
在 C 中使用链表实现堆栈
Implement Stack using Linked List in C
问:
我制作了这段代码,通过链表实现堆栈,并执行一些操作,如添加元素、删除等。我的输出有一些问题,这是出乎意料的。如果您可以修改 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
答:
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
评论
struct StackNode
init
s
init
malloc
s
&
StackNode
Node