C language stack sequence structure implementation code
- 2020-04-02 01:55:29
- OfStack
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
typedef struct Point2D
{
int x;
int y;
}ElemType; //Stack element structure
typedef struct
{
ElemType *btm; //The bottom of the stack
ElemType *top; //To the top of the stack
int height; //The stack is high
int size; //Total size of the stack
}ArrStack; //The stack structure
//Stack method declaration
ArrStack *CreateStack( int nSize ); /// create a stack of size nSize
void DestroyStack( ArrStack *pStack ); /// destroy stack pStack
void ClearStack( ArrStack *pStack ); /// empty the elements in the stack pStack
int GetHeight( ArrStack *pStack ); //Gets the height of the stack pStack
int GetSize( ArrStack *pStack ); //Gets the total capacity of stack pStack
int IsEmpty( ArrStack *pStack ); /// checks if stack pStack is empty
int Push( ArrStack *pStack, ElemType *pt ); /// pushes element pt onto pStack
int Pop( ArrStack *pStack, ElemType *pt ); /// will To the top of the stack Elements are pushed to pt
int GetTop( ArrStack *pStack, ElemType *pt ); /// To obtain To the top of the stack Elements to pt
void ForEachStack( ArrStack *pStack, void (*func)(ElemType *pt) ); /// from The bottom of the stack to To the top of the stack Each element of func function
void ReForEachStack( ArrStack *pStack, void (*func)(ElemType *pt) ); /// from To the top of the stack to The bottom of the stack Each element of func function
//Stack method implementation
ArrStack *CreateStack( int nSize )
{
// According to the The stack structure Create a stack
ArrStack *pStack = (ArrStack *)malloc( sizeof(ArrStack) );
//Application stack initial space
pStack->btm = (ElemType *)calloc( nSize, sizeof(ElemType) );
// make To the top of the stack Point to the The bottom of the stack The element
pStack->top = &pStack->btm[0];
// Initialize the The stack is high Degree of 0
pStack->height = 0;
//Initialize the stack size to the initial size
pStack->size = nSize;
return pStack;
}
void DestroyStack( ArrStack *pStack )
{
//Release the stack elements
free( pStack->btm );
//The release of the stack
free( pStack );
}
void ClearStack( ArrStack *pStack )
{
// make To the top of the stack Point to the The bottom of the stack
pStack->top = &pStack->btm[0];
// will The stack is high Degree to 0
pStack->height = 0;
}
int GetHeight( ArrStack *pStack )
{
return pStack->height;
}
int GetSize( ArrStack *pStack )
{
return pStack->size;
}
int IsEmpty( ArrStack *pStack )
{
return pStack->height == 0 ? TRUE : FALSE;
}
int Push( ArrStack *pStack, ElemType *pt )
{
/// check if capacity expansion is needed
if( pStack->height == pStack->size )
{ //Need to increase
//Reapply stack space twice the original stack size
ElemType *pe = (ElemType *)calloc( pStack->size * 2, sizeof(ElemType) );
//Copy the old stack contents to the new stack contents
memcpy( pe, pStack->btm, pStack->size * sizeof(ElemType) );
//Resets the total stack capacity
pStack->size = pStack->size * 2;
//Free up the old stack space
free( pStack->btm );
// will The bottom of the stack Points to the newly created stack space
pStack->btm = pe;
//To the top of the stack Points to the last element of the new stack
pStack->top = &pe[pStack->height-1];
}
//Push the new element onto the stack
pStack->btm[pStack->height].x = pt->x;
pStack->btm[pStack->height].y = pt->y;
//The stack is high Degrees from one
++pStack->height;
//To the top of the stack Points to the latest stack element
pStack->top = &pStack->btm[pStack->height-1];
return pStack->height;
}
int Pop( ArrStack *pStack, ElemType *pt )
{
/// empty stack
if( pStack->height == 0 )
return -1;
// will To the top of the stack Element to pt
pt->x = pStack->top->x;
pt->y = pStack->top->y;
//The stack is high C minus one
--pStack->height;
//To the top of the stack Point to the To the top of the stack The previous element of the element
pStack->top = &pStack->btm[pStack->height-1];
return pStack->height;
}
int GetTop( ArrStack *pStack, ElemType *pt )
{
pt->x = pStack->top->x;
pt->y = pStack->top->y;
return pStack->height;
}
void ForEachStack( ArrStack *pStack, void (*func)(ElemType *pt) )
{
int i = 0;
for( i = 0; i < pStack->height; ++i )
{
func( &pStack->btm[i] );
}
}
void ReForEachStack( ArrStack *pStack, void (*func)(ElemType *pt) )
{
int i = pStack->height - 1;
for( i; i >= 0; --i )
{
func( &pStack->btm[i] );
}
}
//test
void display( ElemType *pt )
{
printf( "(%d,%d) ", pt->x, pt->y );
}
int main()
{
///test Create the initial size of 5 The stack
ArrStack *psk = CreateStack( 5 );
///test IsEmpty , GetSize , GetHeight
if( IsEmpty(psk) == TRUE )
printf( "Stack Size=%d, Stack Height=%dn", GetSize(psk), GetHeight(psk) );
ElemType pt;
int i = 0;
///testPush, Push into the stack 8 An element
printf( "n Push into the stack 8 After one element :n" );
for( i = 0; i < 8; ++i )
{
pt.x = pt.y = i;
Push( psk, &pt );
}
//Output the stack state after pressing 8 elements
printf( "Is empty = %dn", IsEmpty(psk) );
printf( "Stack size = %dn", GetSize(psk) );
printf( "Stack height = %dn", GetHeight(psk) );
///test ForEachStack , ReForEachStack
printf( "ntest ForEachStack , ReForEachStack:n" );
ForEachStack( psk, display );
putchar('n');
ReForEachStack( psk, display );
putchar('n');
///testgetTop
GetTop( psk, &pt );
printf( "nTo the top of the stack Elements for : (%d,%d)n", pt.x, pt.y );
///test Pop
Pop( psk, &pt );
printf( "nPop The element that pops up is (%d,%d), After the popup The stack is high:%dn", pt.x, pt.y, GetHeight(psk) );
Pop( psk, &pt );
printf( "nPop The element that pops up is (%d,%d), After the popup The stack is high:%dn", pt.x, pt.y, GetHeight(psk) );
///testPush
pt.x = pt.y = 100;
Push( psk, &pt );
printf( "nPop The element pressed in is zero (%d,%d), After the pressure into the The stack is high:%dn", pt.x, pt.y, GetHeight(psk) );
//Perform a full stack operation
printf( "n Perform a full stack push :n" );
int n = GetHeight(psk);
for( i = 0; i < n; ++i )
{
Pop( psk, &pt );
printf( "Pop The element that pops up is (%d,%d), After the popup The stack is high:%dn", pt.x, pt.y, GetHeight(psk) );
}
/// destruction of stack
DestroyStack( psk );
return 0;
}
Test results:
< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201310/20131029151656645.jpg" >