C_DS_AIgo/linked_list_stack.c

44 lines
747 B
C
Raw Permalink Normal View History

2025-03-25 18:09:23 +08:00
#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>ܣ<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>ܣ<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;
}