数据结构算法复习:栈操作相关2013-11-29RT,纯练手,记录,不多解释,高手飘过。
//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("
The stack:"); PrintStack(pHead); printf("
The stack after pop %d.",Pop(pHead)); printf("
"); PrintStack(pHead); printf("
The stack is %s",isEmpty(pHead) ==TRUE ? "Empty":"Not Empty"); MakeEmpty(pHead); printf("
After empty."); printf("
The 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("
The stack is empty."); }else{ while(!isEmpty(S)) Pop(S); } }
本文出自 “About:Blank H4cking” 博客,请务必保留此出处http://pnig0s1992.blog.51cto.com/393390/811861