RT,纯练手,记录,不多解释,高手飘过。
- //Code by Pnig0s1992
- //Date:2012,3,20
- #include <stdio.h>
- #include <Windows.h>
- typedef struct Node * ptrNode;
- typedef ptrNode Stack;
- typedef int Element_type;
- struct Node{
- Element_type Element;
- ptrNode pNext;
- };
- Stack CreateStack(void);
- BOOL isEmpty(Stack S);
- void MakeEmpty(Stack S);
- void Push(Element_type x,Stack S);
- Element_type Pop(Stack S);
- void PrintStack(Stack S);
- int main(int argc,char ** argv)
- {
- Stack pHead = CreateStack();
- Push(20,pHead);
- Push(10,pHead);
- Push(40,pHead);
- Push(70,pHead);
- Push(50,pHead);
- Push(90,pHead);
- Push(30,pHead);
- Push(80,pHead);
- printf("\nThe stack:");
- PrintStack(pHead);
- printf("\nThe stack after pop %d.",Pop(pHead));
- printf("\n");
- PrintStack(pHead);
- printf("\nThe stack is %s",isEmpty(pHead) ==TRUE ? "Empty":"Not Empty");
- MakeEmpty(pHead);
- printf("\nAfter empty.");
- printf("\nThe stack is %s",isEmpty(pHead) ==TRUE ? "Empty":"Not Empty");
- system("pause");
- return 0;
- }
- Stack CreateStack(void)
- {
- ptrNode pNewNode = (ptrNode)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Node));
- pNewNode->pNext = NULL;
- return pNewNode;
- }
- void Push(Element_type x,Stack S)
- {
- ptrNode pNewNode = (ptrNode)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Node));
- pNewNode->Element = x;
- pNewNode->pNext = NULL;
- pNewNode->pNext = S->pNext;
- S->pNext = pNewNode;
- }
- Element_type Pop(Stack S)
- {
- ptrNode pTemp = S->pNext;
- Element_type x = pTemp->Element;
- S->pNext = pTemp->pNext;
- HeapFree(GetProcessHeap(),0,pTemp);
- return x;
- }
- void PrintStack(Stack S)
- {
- ptrNode pTemp = S->pNext;
- while(pTemp!=NULL)
- {
- printf("%d ",pTemp->Element);
- pTemp = pTemp->pNext;
- }
- }
- BOOL isEmpty(Stack S)
- {
- return S->pNext == NULL;
- }
- void MakeEmpty(Stack S)
- {
- if(isEmpty(S))
- {
- printf("\nThe stack is empty.");
- }else
- {
- while(!isEmpty(S))
- Pop(S);
- }
- }
本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/811861,如需转载请自行联系原作者