//LinkStack.h
#ifndef LINKSTACK_H
#define LINKSTACK_H
#include <stdlib.h>
#include <stdio.h>
typedef struct
{
char strName[32];
int iAge;
struct Node* pNext;
struct Node* pPrev;
}
Node;
typedef struct
{
struct Node* pBase;
struct Node* pTop;
int iSize;
}
LinkStack;
#endif
//LinkStack.c #include "LinkStack.h" LinkStack* InitStack() { LinkStack* pStack = (LinkStack*)malloc( sizeof( LinkStack )); if( !pStack ) return NULL; pStack->iSize = 0; pStack->pBase = NULL; pStack->pTop = NULL; return pStack; } void DestroyStack( LinkStack** pStack ) { ClearStack( *pStack ); free( *pStack ); *pStack = NULL; } Node* Pop( LinkStack* pStack ) { if( !pStack || !pStack->pBase || pStack->iSize == 0 ) return NULL; Node *pNode = pStack->pTop; pStack->pTop = pNode->pPrev; Node* pTemp = pStack->pTop; pTemp->pNext = NULL; pStack->iSize--; return pNode; } void ClearStack( LinkStack* pStack ) { if( !pStack || pStack->iSize == 0 ) return; int iCount = pStack->iSize; while( iCount-- ) { free( Pop( pStack )); } pStack->pBase = NULL; pStack->pTop = NULL; pStack->iSize = 0; } int StackEmpty( LinkStack* pStack ) { if( !pStack || !pStack->iSize ) return 1; return 0;//pStack->iSize; } int StackLength( LinkStack* pStack ) { if( !pStack ) return 0; return pStack->iSize; } Node* GetTop( LinkStack* pStack ) { if( !pStack || !pStack->pBase || pStack->iSize == 0 ) return NULL; return pStack->pTop; } int Push( LinkStack* pStack, Node* pNode ) { if( !pStack || !pNode ) return -1; if( !pStack->pBase ) { pStack->pBase = pNode; pStack->pTop = pNode; Node* pTemp = pStack->pBase; pTemp->pNext = NULL; pTemp->pPrev = NULL; } else { pNode->pNext = NULL; Node* pTemp = pStack->pTop; pTemp->pNext = pNode; pNode->pPrev = pStack->pTop; pStack->pTop = pNode; } pStack->iSize++; return 0; } void StackTraverse( LinkStack* pStack, void (*pPrintNode)( Node* )) { if( !pStack || pStack->iSize == 0 ) return; int iCount = 0; Node* pCur = pStack->pTop; while( pCur ) { pPrintNode( pCur ); pCur = pCur->pPrev; } } void PrintNode( Node* pNode ) { if( !pNode ) return; puts( "" ); printf( "name: %s, age: %d \n", pNode->strName, pNode->iAge ); } Node* CreateNode( char* pName, int iAge ) { Node* pNode = (Node*)malloc( sizeof( Node )); if( !pNode ) return NULL; strcpy( pNode->strName, pName ); pNode->iAge = iAge; return pNode; } int main( int argc, char** argv ) { LinkStack* pStack = InitStack(); if( !pStack ) return -1; Push( pStack, CreateNode( "Jim", 3 )); Push( pStack, CreateNode( "Kate", 4 )); Push( pStack, CreateNode( "Tom", 5 )); StackTraverse( pStack, PrintNode ); //PrintNode( Pop( pStack )); Pop( pStack ); puts( "****************************************" ); printf( "%d\n", StackLength( pStack )); StackTraverse( pStack, PrintNode ); puts( "++++++++++++++++++++++++++++++++++++++++" ); Pop( pStack ); StackTraverse( pStack, PrintNode );
DestroyStack( &pStack ); return 0; }
makefile
default: a.out ./a.out run: a.out ./a.out clean: a.out rm a.out a.out: LinkStack.c LinkStack.h gcc LinkStack.c rebuild: LinkStack.c LinkStack.h gcc LinkStack.c debug: gcc -g LinkStack.c gdb a.out