1、基于链表实现的堆栈代码,内存动态分配;相对于数组的实现方式,内存使用方式更加灵活,同时,程序不用为堆栈开辟连续的内存空间,所以可能省去一些内存拷贝的时间。
2、在VS2010的C++编译器中编译通过。由于VS配置问题,不能使用bool类型,用int代替;也没有分.c和.h文件书写;如果要分开书写,最好给Create_Node和Stack_Node_Destory加上static关键字,该函数只在.c文件中使用。
3、满篇的英文注释......有点难看,但是比较详细
// Cpp_Study.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
/*
********************************************
@name : StackNode
@func : To recode the info of the node of the stack,including
Data:The number in the stack
Next:The pointer of the next node in the direction of the root of the stack
********************************************
*/
typedef struct StackNode
{
int Data;
struct StackNode* Next;
} STACK_NODE;
/*
********************************************
@name : Stack
@func : To recode the info of the stack,including
top : The pointer of the top node of the stack
StackSize : The size of the stack, so we don't try to calculate it each time
********************************************
*/
typedef struct Stack
{
STACK_NODE* top;
int StackSize;
} STACK;
/*
********************************************
@name : Stack_Init
@func : To init the stack,where we init the top of the stack as null,because
there is temprorily no node in the stack and the top is NULL, and
the StackSize is zero
@argv : stack: The pointer of stack to init
@return value: null
********************************************
*/
void Stack_Init(STACK* stack)
{
stack->top = NULL;
stack->StackSize = 0;
}
/*
********************************************
@name : Create_Node
@func : To creat a node on a stack.
@argv : stack : The pointer of stack to create a node on
Data : The data to record in the node
@return value: The pointer of the stack node it has created
********************************************
*/
StackNode* Create_Node(STACK* stack,int Data)
{
StackNode* Stack_Node_Pointer=(StackNode*)malloc(sizeof(STACK));
Stack_Node_Pointer->Next = stack->top;
Stack_Node_Pointer->Data = Data;
return Stack_Node_Pointer;
}
/*
********************************************
@name : Stack_Is_Empty
@func : To check whether the stack is empty
@argv : stack: The pointer of stack to check
@return value: 1:The stack is empty
0:The stack is not empty
********************************************
*/
int Stack_Is_Empty(STACK* stack)
{
return stack->StackSize == 0 ? 1 : 0;
}
/*
********************************************
@name : Stack_Push
@func : Create a new node on the stack and record a new data,where we create a
new node, update the info in the STACK* stack
@argv : stack: The pointer of stack to create a node on
Data : The data to record
@return value: NULL
********************************************
*/
void Stack_Push(STACK* stack,int Data)
{
StackNode* Stack_Node_Pointer = Create_Node(stack,Data);
stack->StackSize ++;
stack->top = Stack_Node_Pointer;
}
/*
********************************************
@name : Stack_Node_Destory
@func : Delete a certain node and free the memory. The node to
destory must be on the top of the stack, otherwise there will
be a memory leak
@argv : stack_node: The node to destory
@return value: The next node of the stack, which will be the top of the
stack after the function.
********************************************
*/
STACK_NODE* Stack_Node_Destory(STACK_NODE* stack_node)
{
STACK_NODE* next = stack_node->Next;
free(stack_node);
return next;
}
/*
********************************************
@name : Stack_Pop
@func : To delete a top node from the stack,where we delete the node
and update the data in the STACK* stack
@argv : stack: The stack to operate on
@return value: NULL
********************************************
*/
void Stack_Pop(STACK* stack)
{
if (Stack_Is_Empty(stack))
{
printf("Stack is already empty\n");
return ;
}
stack->top = Stack_Node_Destory(stack->top);
stack->StackSize --;
}
/*
********************************************
@name : Stack_Deinit
@func : Destory the stack and free the memory,where we delete all the nodes
@argv : stack: The stack to destory
@return value: NULL
********************************************
*/
void Stack_Deinit(STACK* stack)
{
while (stack->top != NULL)
{
stack->top = Stack_Node_Destory(stack->top);
}
stack->StackSize = 0;
}
/*
********************************************
@name : Stack_Output_Node_Inof
@func : Print the info recorded in the stack node
@argv : stack_node: The stack_node the information of which we need to output
@return value: NULL
********************************************
*/
STACK_NODE* Stack_Output_Node_Inof(STACK_NODE* stack_node)
{
printf("%d ",stack_node->Data);
return stack_node->Next;
}
/*
********************************************
@name : Stack_Output_All_Info
@func : Print all the info in the stack from the top to the root
@argv : stack : The stack the information of which we need to output
@return value: NULL
********************************************
*/
void Stack_Output_All_Info(STACK* stack)
{
STACK_NODE* stack_node = stack->top;
printf("The data in the stack is ");
while(stack_node != NULL)
{
stack_node = Stack_Output_Node_Inof(stack_node);
}
printf("\n");
}
int main()
{
//Test Code
STACK stack;
Stack_Init(&stack);
while(1)
{
int a,b;
printf("please input the operation num: 1.Push 2.Pop 3.Check the size 4.Show all 5.Deinit\n");
scanf("%d%d",&a,&b);
if (a == 1)
{
Stack_Push(&stack,b);
}
else if (a == 2)
{
Stack_Pop(&stack);
}
else if (a == 3)
{
printf("The size is %d\n",stack.StackSize);
}
else if (a == 4)
{
Stack_Output_All_Info(&stack);
}
else if (a == 5)
{
Stack_Deinit(&stack);
}
Stack_Output_All_Info(&stack);
}
}