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); } }
本文出自 “About:Blank H4cking” 博客,请务必保留此出处http://pnig0s1992.blog.51cto.com/393390/811861
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索printf
, stack
, push
isempty
,以便于您获取更多的相关知识。
时间: 2024-09-27 13:38:12