l_stack.c

来自「c和指针 学习c语言必须阅读的书籍之一 提高对C语言的掌握理解能力」· C语言 代码 · 共 100 行

C
100
字号
/*
** A stack implemented with a linked list.  This stack has no size
** limit.
*/
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>

#define	FALSE 0

/*
**	Define a structure to hold one value.  The link field will
**	point to the next value on the stack.
*/
typedef	struct	STACK_NODE {
	STACK_TYPE	value;
	struct STACK_NODE *next;
} StackNode;

/*
**	A pointer to the topmost node on the stack.
*/
static	StackNode	*stack;

/*
**	create_stack
*/
void
create_stack( size_t size )
{
}

/*
**	destroy_stack
*/
void
destroy_stack( void )
{
	while( !is_empty() )
		pop();
}

/*
**	push
*/
void
push( STACK_TYPE value )
{
	StackNode	*new_node;

	new_node = malloc( sizeof( StackNode ) );
	assert( new_node != NULL );
	new_node->value = value;
	new_node->next = stack;
	stack = new_node;
}

/*
**	pop
*/
void
pop( void )
{
	StackNode	*first_node;

	assert( !is_empty() );
	first_node = stack;
	stack = first_node->next;
	free( first_node );
}

/*
**	top
*/
STACK_TYPE top( void )
{
	assert( !is_empty() );
	return stack->value;
}

/*
**	is_empty
*/
int
is_empty( void )
{
	return stack == NULL;
}

/*
**	is_full
*/
int
is_full( void )
{
	return FALSE;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?