Main.c
#include "Stack.h"
void main()
{
#if 0
Stack* P_Stack = NULL;
for (int i = 0; i < 10; i ++ )
{
Push(&P_Stack, i * 10, i);
}
while (NULL != P_Stack)
{
Stack temp;
Pop(&P_Stack, &temp);
printf("%d\n", temp.Data);
}
#endif
#if 0
Stack* P_Stack = NULL;
for (int i = 0; i < 10; i++)
{
Push(&P_Stack, i * 10, i);
while (NULL != P_Stack)
{
Stack temp;
Pop(&P_Stack, &temp);
printf("%d\n", temp.Data);
}
}
#endif
#if 0
Stack* P_Stack = NULL;
int Num = 10;
while (Num)
{
Push(&P_Stack,Num%2,0);
Num /= 2;
}
while (NULL != P_Stack)
{
Stack temp;
Pop(&P_Stack, &temp);
printf("%d\n", temp.Data);
}
#endif
system("pause");
}
Stack.h
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct Stack
{
DataType Data;
int ID;
struct Stack* P_Next;
}Stack;
void InitNode(Stack* P_Node);
void Push(Stack** PP_Head, DataType Data, int ID);
void Pop(Stack** PP_Head,Stack* PopNode);
void Show(Stack* P_Head);
void Destroy(Stack** PP_Head);
#ifdef __cplusplus
}
#endif
Stack.h
#include "Stack.h"
void InitNode(Stack* P_Node)
{
if (NULL != P_Node)
{
P_Node->P_Next = NULL;
}
}
void Push(Stack** PP_Head, DataType Data, int ID)
{
Stack* P_New = (Stack*)malloc(sizeof(Stack));
P_New->P_Next = NULL;
P_New->Data = Data;
P_New->ID = ID;
if (NULL == *PP_Head)
{
*PP_Head = P_New;
}
else
{
Stack* P_Bak = *PP_Head;
while (NULL != P_Bak->P_Next)
{
P_Bak = P_Bak->P_Next;
}
P_Bak->P_Next = P_New;
}
}
void Pop(Stack** PP_Head, Stack* PopNode)
{
if (NULL == *PP_Head)
{
return;
}
else if (NULL == (*PP_Head)->P_Next)
{
PopNode->ID = (*PP_Head)->ID;
PopNode->Data = (*PP_Head)->Data;
free(*PP_Head);
*PP_Head = NULL;
}
else
{
Stack* P_Bak = *PP_Head;
while (NULL != P_Bak->P_Next->P_Next)
{
P_Bak = P_Bak->P_Next;
}
PopNode->ID = P_Bak->P_Next->ID;
PopNode->Data = P_Bak->P_Next->Data;
free(P_Bak->P_Next);
P_Bak->P_Next = NULL;
}
}
void Show(Stack* P_Head)
{
if (NULL == P_Head)
{
return;
}
else
{
printf("%p\t%p\t%d\t%d\n", P_Head, P_Head->P_Next, P_Head->Data, P_Head->Data);
Show(P_Head->P_Next);
}
}
void Destroy(Stack** PP_Head)
{
if (NULL != *PP_Head)
{
Stack* P1 = *PP_Head;
Stack* P2 = NULL;
while (NULL != P2)
{
P2 = P1->P_Next;
P1 = P2->P_Next;
free(P2);
P2 = NULL;
}
free(*PP_Head);
*PP_Head = NULL;
}
}