RT, pure practicing, record, not much explanation, the master floated over.
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 (No
de));
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); }
}
This article is from the "About:blank h4cking" blog, please be sure to keep this source http://pnig0s1992.blog.51cto.com/393390/811861