C_DS_AIgo/linked_list_stack.c

52 lines
929 B
C
Raw Permalink Normal View History

#include <stdio.h>
#include <stdlib.h>
#include "linked_list_stack.h"
// <20><>ʼջ
stack_linked* init_stack_linked(void)
{
stack_linked* s = (stack_linked*)malloc(sizeof(stack_linked));
if (s == NULL)
{
printf("<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>\n");
return NULL;
}
s->top = NULL;
s->size = 0;
return s;
}
// <20><>ջ
void push_stack_linked(stack_linked* s, elem_type value)
{
stack_node* node = (stack_node*)malloc(sizeof(stack_node));
if (node == NULL)
{
printf("<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>\n");
return NULL;
}
node->value = value;
node->next = s->top;
s->top = node;
s->size++;
}
// <20><>ջ
int pop_stack_linked(stack_linked* s)
{
int flog = 0;
flog = s->top->value;
stack_node* tmp = s->top;
s->top = s->top->next;
free(tmp);
tmp = NULL;
return flog;
2025-04-17 17:34:18 +08:00
}
void print_linked(stack_linked* s) {
stack_node* L = s->top;
while (L != NULL) {
printf("%d", L->value);
L = L->next;
}
printf("\n");
}