C语言实现简单顺序栈

来源:互联网 发布:三个月考研知乎 编辑:程序博客网 时间:2024/05/17 05:09
//SeqStack.h#ifndef SEQSTACK_H#define SEQSTACK_H#include <stdlib.h>#include <stdio.h>#define STACK_INIT_SIZE 100#define STACK_INCREMENT 10typedef struct{char  strName[32];int iAge;}Node;typedef struct{Node* pBase;int iSize;int iStackSize;}SeqStack;#endif

//SeqStack.c#include "SeqStack.h"int InitStack( SeqStack* pStack ){pStack->pBase = (Node*)malloc( sizeof( Node ) * STACK_INIT_SIZE );if( !pStack->pBase )return -1;pStack->iSize = 0;pStack->iStackSize = STACK_INIT_SIZE;return 0;}void DestroyStack( SeqStack** pStack ){ClearStack( pStack );free( pStack );pStack = NULL;}Node* Pop( SeqStack* pStack ){if( !pStack || !pStack->pBase || pStack->iSize == 0 )return NULL;pStack->iSize--;return (Node*)( pStack->pBase + pStack->iSize );}void ClearStack( SeqStack* pStack ){if( !pStack || pStack->iSize == 0 )return;int iCount = pStack->iSize;free( pStack->pBase );pStack->pBase = NULL;pStack->iSize = 0;pStack->iStackSize = 0;}int StackEmpty( SeqStack* pStack ){if( !pStack || !pStack->iSize )return 1;return 0;//pStack->iSize;}int StackLength( SeqStack* pStack ){if( !pStack || !pStack->iSize )return 0;return pStack->iSize;}Node* GetTop( SeqStack* pStack ){if( !pStack || !pStack->pBase || pStack->iSize == 0 )return NULL;return pStack->pBase + pStack->iSize - 1;}int Push( SeqStack* pStack, Node* pNode ){if( !pStack || !pStack->pBase || !pNode )return -1;if( pStack->iSize >= pStack->iStackSize ){pStack->pBase = (Node*)realloc( pStack->pBase, pStack->iSize + STACK_INCREMENT );if( !pStack->pBase )return -1;pStack->iStackSize += STACK_INCREMENT;}strcpy( (pStack->pBase + pStack->iSize)->strName, pNode->strName );(pStack->pBase + pStack->iSize)->iAge = pNode->iAge;pStack->iSize++;return 0;}void StackTraverse( SeqStack* pStack, void (*pPrintNode)( Node* )){if( !pStack || pStack->iSize == 0 )return;int iCount = 0;while( iCount < pStack->iSize ){pPrintNode( pStack->pBase + iCount );iCount++;}}void PrintNode( Node* pNode ){if( !pNode )return;puts( "" );printf( "name: %s, age: %d \n", pNode->strName, pNode->iAge ); }int main( int argc, char** argv ){SeqStack* pStack = (SeqStack*)malloc( sizeof( SeqStack ) );InitStack( pStack );Node* pNode = (Node*)malloc( sizeof( Node ) );strcpy( pNode->strName, "Jim" );pNode->iAge = 28;Push( pStack, pNode );strcpy( pNode->strName, "Tom" );pNode->iAge = 20;Push(  pStack, pNode );int i;char pStr[32];for( i = 0; i < 190; i++ ){memset( pNode, 0, sizeof( Node ));memset( pStr, 0, 32 );sprintf( pStr, "name%d", i );strcpy( pNode->strName, pStr );pNode->iAge = i;Push( pStack, pNode );}StackTraverse( pStack, PrintNode );for( i = 0; i < 100; i++ ){Pop( pStack );}StackTraverse( pStack, PrintNode );puts( "Top" );printf( "%d\n", StackLength( pStack ));PrintNode( GetTop( pStack ));if( StackEmpty( pStack )){puts( "Empty" );}else{puts( "Not empty!" );}DestroyStack( &pStack );if( StackEmpty( pStack )){puts( "Empty" );}else{puts( "Not empty!" );}return 0;}


makefile

default: a.out./a.outrun: a.out./a.outclean: a.outrm a.outa.out: SeqStack.c SeqStack.hgcc SeqStack.crebuild: SeqStack.c SeqStack.hgcc SeqStack.cdebug: gcc -g SeqStack.cgdb a.out