经典的数据结构——栈

来源:互联网 发布:背四级单词软件 编辑:程序博客网 时间:2024/06/06 02:32

很经典的数据结构,在VS2012上,可运行


// test2.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>using namespace std;struct Node{int data;                /*值域*/struct Node *next;       /*链接指针*/};struct stack{struct Node *top;    /*队首指针*/};void initstack(stack *p){p->top = NULL;}void popstack(stack *p){Node * temp = p->top;p->top = p->top->next;temp->next = NULL;delete temp;}void pushstack(stack *p, int x){Node * temp = new Node;temp->data = x;temp->next = p->top;p->top = temp;}int _tmain(int argc, _TCHAR* argv[]){stack sp;initstack(&sp);for (int i=0; i<=9; ++i){pushstack(&sp, i);}popstack(&sp);cout << sp.top->data << endl;system("pause");return 0;}



在VS2012上,可运行


---------------

0 0