C语言实现简单顺序栈

//SeqStack.h
#ifndef SEQSTACK_H
#define SEQSTACK_H

#include <stdlib.h>
#include <stdio.h>

#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10

typedef 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.out

run: a.out
	./a.out

clean: a.out
	rm a.out

a.out: SeqStack.c SeqStack.h
	gcc SeqStack.c

rebuild: SeqStack.c SeqStack.h
	gcc SeqStack.c

debug: 
	gcc -g SeqStack.c
	gdb a.out



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值