栈
栈的基本概念:
栈的顺序结构:
栈是一种操作受限于线性表,它也有两种存储方式。 我们本次的操作是基于顺序表的结构来实现栈的基本功能。
栈的存储结构:
typedef struct Stack {
STDataType* data;
int top;
int capacity;
} ST;
栈的初始化:
void StackInit ( ST* ps) {
assert ( ps) ;
ps-> data = NULL ;
ps-> top = 0 ;
ps-> capacity = 0 ;
}
栈的销毁:
void StackDestroy ( ST* ps) {
assert ( ps) ;
free ( ps-> data) ;
ps-> data = NULL ;
ps-> top = ps-> capacity = 0 ;
}
进栈:
void StackPush ( ST* ps, STDataType x) {
assert ( ps) ;
if ( ps-> top == ps-> capacity) {
int newCapacity = ps-> capacity == 0 ? 4 : ps-> capacity* 2 ;
STDataType* tmp = realloc ( ps-> data, sizeof ( STDataType) * newCapacity) ;
if ( tmp == NULL ) {
printf ( "realloc fail.\n" ) ;
exit ( - 1 ) ;
}
ps-> data = tmp;
ps-> capacity = newCapacity;
}
ps-> data[ ps-> top] = x;
ps-> top++ ;
}
出栈:
void StackPop ( ST* ps) {
assert ( ps) ;
assert ( ! StackEmpty ( ps) ) ;
ps-> top-- ;
}
读出栈顶的元素
STDataType StackTop ( ST* ps) {
assert ( ps) ;
assert ( ! StackEmpty ( ps) ) ;
return ps-> data[ ps-> top - 1 ] ;
}
计算栈中的元素个数:
int StackSize ( ST* ps) {
assert ( ps) ;
return ps-> top;
}
判断栈是否为空:
bool StackEmpty ( ST* ps) {
assert ( ps) ;
return ps-> top == 0 ;
}
完整源码:
头文件:Stack.h
# pragma once
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# include <stdbool.h>
typedef int STDataType;
typedef struct Stack {
STDataType* data;
int top;
int capacity;
} ST;
void StackInit ( ST* ps) ;
void StackDestroy ( ST* ps) ;
void StackPush ( ST* ps, STDataType x) ;
void StackPop ( ST* ps) ;
STDataType StackTop ( ST* ps) ;
bool StackEmpty ( ST* ps) ;
int StackSize ( ST* ps) ;
源文件:Stack.c
# include "Stack.h"
void StackInit ( ST* ps) {
assert ( ps) ;
ps-> data = NULL ;
ps-> top = 0 ;
ps-> capacity = 0 ;
}
void StackDestroy ( ST* ps) {
assert ( ps) ;
free ( ps-> data) ;
ps-> data = NULL ;
ps-> top = ps-> capacity = 0 ;
}
void StackPush ( ST* ps, STDataType x) {
assert ( ps) ;
if ( ps-> top == ps-> capacity) {
int newCapacity = ps-> capacity == 0 ? 4 : ps-> capacity* 2 ;
STDataType* tmp = realloc ( ps-> data, sizeof ( STDataType) * newCapacity) ;
if ( tmp == NULL ) {
printf ( "realloc fail.\n" ) ;
exit ( - 1 ) ;
}
ps-> data = tmp;
ps-> capacity = newCapacity;
}
ps-> data[ ps-> top] = x;
ps-> top++ ;
}
void StackPop ( ST* ps) {
assert ( ps) ;
assert ( ! StackEmpty ( ps) ) ;
ps-> top-- ;
}
STDataType StackTop ( ST* ps) {
assert ( ps) ;
assert ( ! StackEmpty ( ps) ) ;
return ps-> data[ ps-> top - 1 ] ;
}
int StackSize ( ST* ps) {
assert ( ps) ;
return ps-> top;
}
bool StackEmpty ( ST* ps) {
assert ( ps) ;
return ps-> top == 0 ;
}
源文件:main.c
# include "Stack.h"
void TestStack1 ( ) {
ST st;
StackInit ( & st) ;
StackPush ( & st, 1 ) ;
StackPush ( & st, 2 ) ;
StackPush ( & st, 3 ) ;
StackPush ( & st, 4 ) ;
StackPop ( & st) ;
StackPop ( & st) ;
StackPop ( & st) ;
printf ( "%d" , StackTop ( & st) ) ;
StackDestroy ( & st) ;
}
void TestStack2 ( )
{
ST st;
StackInit ( & st) ;
StackPush ( & st, 1 ) ;
StackPush ( & st, 2 ) ;
StackPush ( & st, 3 ) ;
StackPush ( & st, 4 ) ;
printf ( "%d " , StackTop ( & st) ) ;
StackPop ( & st) ;
printf ( "%d " , StackTop ( & st) ) ;
StackPop ( & st) ;
StackPush ( & st, 5 ) ;
StackPush ( & st, 6 ) ;
while ( ! StackEmpty ( & st) )
{
printf ( "%d " , StackTop ( & st) ) ;
StackPop ( & st) ;
}
StackDestroy ( & st) ;
}
int main ( )
{
TestStack1 ( ) ;
return 0 ;
}