C_DS_AIgo/linked_list_stack.c
jdh f7a523b8ca up
Co-Authored-By: Jdhggg <111557398+Jdhggg@users.noreply.github.com>
2025-04-29 18:01:09 +08:00

52 lines
929 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}
void print_linked(stack_linked* s) {
stack_node* L = s->top;
while (L != NULL) {
printf("%d", L->value);
L = L->next;
}
printf("\n");
}