/**
* The stack of sequential structure types implemented @brief C language
* @author WID
* @date 2013-10-29
*
* @note If there are bugs or bug in the code, please message feedback, thank you!
*/
#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; Bottom of Stack
Elemtype *top; Top of Stack
int height; Stack height
int size; Total stack size
}arrstack; 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 within the stack Pstack
int getheight (Arrstack *pstack); Get the height of the stack pstack
int GetSize (Arrstack *pstack); Gets the total capacity of the stack Pstack
int IsEmpty (Arrstack *pstack); Detect whether stack pstack is an empty stack
int Push (Arrstack *pstack, Elemtype *pt); Press the element pt into the stack pstack
int Pop (Arrstack *pstack, Elemtype *pt); Stack top elements out to PT
int GetTop (Arrstack *pstack, Elemtype *pt); Get stack top element to PT
void Foreachstack (Arrstack *pstack, Void (*func) (Elemtype *pt)); Every element from the bottom of the stack to the top of the stack executes the Func function sequentially
void Reforeachstack (Arrstack *pstack, Void (*func) (Elemtype *pt)); Every element from the top of the stack to the bottom of the stack executes the Func function in turn
Stack method implementation
/**
* @brief Create a stack of size nsize
*
* @param the initial size of the nsize stack
*
* @return return a pointer to the created stack
*
* @note nsize Initial size should be greater than 0
*/
Arrstack *createstack (int nsize)
{
Create a stack based on the stack structure
Arrstack *pstack = (Arrstack *) malloc (sizeof (arrstack));
Application Stack Initial space
PSTACK->BTM = (Elemtype *) calloc (nsize, sizeof (elemtype));
Make the top of the stack point to the bottom element
Pstack->top = &pStack->btm[0];
Initialize stack height to 0
pstack->height = 0;
Initialize stack size to initial size
Pstack->size = nsize;
return pstack;
}
/**
* @brief Destroy Stack Pstack
*
* @param pstack Pointer to the stack to be destroyed
*
* @return void
*/
void Destroystack (Arrstack *pstack)
{
Releasing elements in the stack
Free (PSTACK->BTM);
Release stack
Free (pstack);
}
/**
* @brief Empty the elements inside the stack
*
* @param pstack Pointer to the stack of elements to be emptied
*
* @return void
*/
void Clearstack (Arrstack *pstack)
{
Make the top of the stack point to the bottom of the stack
Pstack->top = &pStack->btm[0];
Set stack height to 0
pstack->height = 0;
}
/**
* @brief Get the height of the stack pstack
*
* @param pstack Pointer to the stack to get height
*
* @param returns the height of the current stack
*/
int getheight (Arrstack *pstack)
{
Return pstack->height;
}
/**
* @brief get the total capacity of stack Pstack
*
* @param pstack Pointer to the stack for the total capacity to be fetched
*
* @return Returns the current total capacity of the stack
*/
int GetSize (Arrstack *pstack)
{
Return pstack->size;
}
/**
* @brief detect whether stack pstack is empty stack
*
* @param pstack Pointer to the stack to be detected
*
* @return returns TRUE if the stack is empty, otherwise returns FALSE
*/
int IsEmpty (Arrstack *pstack)
{
return pstack->height = = 0? True:false;
}
/**
* @brief to press the element pt into the stack pstack
*
* @param pstack Pointer to the stack to be pressed into the element
* @param pt points to a pointer to be pressed into the element
*
* @return return to the height of the stack when successfully pressed
*/
int Push (Arrstack *pstack, Elemtype *pt)
{
Check for expansion
if (pstack->height = = pstack->size)
{//Need expansion
Re-apply for stack space twice times the size of the original stack
Elemtype *pe = (Elemtype *) calloc (pstack->size * 2, sizeof (Elemtype));
Copy old stack content to new stack content
memcpy (PE, PSTACK->BTM, pstack->size * sizeof (elemtype));
Reset stack Total capacity size
Pstack->size = pstack->size * 2;
Free up old stack space
Free (PSTACK->BTM);
Point the bottom of the stack to the newly opened stack space
PSTACK->BTM = PE;
The top of the stack points to the last element of the new stack
Pstack->top = &pe[pStack->height-1];
}
Push new elements into stack
pstack->btm[pstack->height].x = pt->x;
PSTACK->BTM[PSTACK->HEIGHT].Y = pt->y;
Stack height self-increasing one
++pstack->height;
Top of Stack point to latest stack element
Pstack->top = &pStack->btm[pStack->height-1];
Return pstack->height;
}
/**
* @brief Stack top elements out of the stack to PT
*
* @param a pointer to the stack that pstack the element to be ejected
* @param pt Pointer to receive ejected element
*
* @return The stack success is returned to the stack after the height of the stack, otherwise return-1
*/
int Pop (Arrstack *pstack, Elemtype *pt)
{
is an empty stack
if (pstack->height = 0)
return-1;
Assign the top element of the stack to PT
Pt->x = pstack->top->x;
Pt->y = pstack->top->y;
Stack height minus One
--pstack->height;
The top of the stack points to the last element of the top element
Pstack->top = &pStack->btm[pStack->height-1];
Return pstack->height;
}
/**
* @brief get stack top element to PT
*
* @param a pointer to the stack that pstack the element to be ejected
* @param pt Pointer to receive ejected element
*
* @return Get success Returns the position of the top element of the stack, otherwise return-1
*
* @note element position from 0 meters
*/
int GetTop (Arrstack *pstack, Elemtype *pt)
{
Pt->x = pstack->top->x;
Pt->y = pstack->top->y;
Return pstack->height;
}
/**
* @brief Perform the Func function sequentially from the bottom of the stack to each element at the top of the stack
*
* @param pstack Pointer to the stack to be processed
* @param a pointer to the function to be executed Func
*
* @return void
*/
void Foreachstack (Arrstack *pstack, Void (*func) (Elemtype *pt))
{
int i = 0;
for (i = 0; i < pstack->height; ++i)
{
Func (&pstack->btm[i]);
}
}
/**
* @brief Perform the Func function sequentially from the top of the stack to the bottom of the stack
*
* @param pstack Pointer to the stack to be processed
* @param a pointer to the function to be executed Func
*
* @return void
*/
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 creates a stack with an initial size of 5
Arrstack *PSK = Createstack (5);
Test IsEmpty, GetSize, getheight
if (IsEmpty (PSK) = = TRUE)
printf ("Stack size=%d, Stack height=%d\n", GetSize (PSK), GetHeight (PSK));
Elemtype pt;
int i = 0;
Test push, press 8 elements into the stack
printf ("\ n) after pressing 8 elements into the stack: \ n");
for (i = 0; i < 8; ++i)
{
Pt.x = Pt.y = i;
Push (PSK, &pt);
}
The stack state after the output is pressed into 8 elements
printf ("is empty =%d\n", IsEmpty (PSK));
printf ("Stack size =%d\n", GetSize (PSK));
printf ("Stack height =%d\n", GetHeight (PSK));
Test Foreachstack, Reforeachstack
printf ("\ n test foreachstack, reforeachstack:\n");
Foreachstack (PSK, display);
Putchar (' \ n ');
Reforeachstack (PSK, display);
Putchar (' \ n ');
Test GetTop
GetTop (PSK, &pt);
printf ("\ n stack top element: (%d,%d) \ n", Pt.x, Pt.y);
Test Pop
POPs (PSK, &pt);
printf ("\npop pops up the element for (%d,%d), pops up after stack height:%d\n", Pt.x, Pt.y, GetHeight (PSK));
POPs (PSK, &pt);
printf ("\npop pops up the element for (%d,%d), pops up after stack height:%d\n", Pt.x, Pt.y, GetHeight (PSK));
Test push
Pt.x = PT.Y = 100;
Push (PSK, &pt);
printf ("\npop pressed elements for (%d,%d), pressed into the stack High:%d\n", Pt.x, Pt.y, GetHeight (PSK));
Perform a full stack operation
printf ("\ n execute full stack: \ n");
int n = getheight (PSK);
for (i = 0; i < n; ++i)
{
POPs (PSK, &pt);
printf ("Pop pops the element for (%d,%d), pop-up stack High:%d\n", Pt.x, Pt.y, GetHeight (PSK));
}
Destroying stacks
Destroystack (PSK);
return 0;
}