Review of data structure algorithm: stack operation related

Source: Internet
Author: User
Tags empty printf

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.