C++__链式栈(练习)

链式栈

stack.h

#ifndef STACK_H_
#define STACK_H_

typedef int data_type;

enum STACK_OP {
    STACK_ERR = -1, STACK_OK, STACK_EMPTY
};

class STACK {
private:
    data_type data;
    STACK *next;
public:
    STACK();
    STACK(data_type data);
    ~STACK();

    data_type getData() const;
    STACK *getNext() const;
    void setData(data_type data);
    void setNext(STACK *next);

    int Push(STACK *pStack, data_type tData);
    int Pop(STACK *pStack, data_type *pData);
    int IsEmpty(STACK *pStack);
    void operator delete(void *pStack);
};

#endif /* STACK_H_ */

stack.cpp

#include "STACK.h"
#include <iostream>
using namespace std;

STACK::STACK(data_type data) {
    // TODO Auto-generated constructor stub
    this->setData(data);
    this->setNext(NULL);
    cout<<"STACK(data)"<<endl;
}

STACK::STACK() {
    // TODO Auto-generated constructor stub
    this->setData(0);
    this->setNext(NULL);
    cout<<"STACK"<<endl;
}

STACK::~STACK() {
    // TODO Auto-generated destructor stub
    cout<<"~STACK"<<endl;
}

data_type STACK::getData() const {
    return data;
}

STACK *STACK::getNext() const {
    return next;
}

void STACK::setData(data_type data) {
    this->data = data;
}

void STACK::setNext(STACK *next) {
    this->next = next;
}

int STACK::Push(STACK *pStack, data_type tData) {
    if (!pStack)
        return STACK_ERR;

    STACK *New = new STACK(tData);
    if (!New)
        return STACK_ERR;
    New->setNext(pStack->getNext());
    pStack->setNext(New);

    return STACK_OK;
}

int STACK::Pop(STACK *pStack, data_type *pData) {
    if ((!pStack) || (!pData))
        return STACK_ERR;

    STACK *Tmp = pStack->getNext();
    if (!Tmp) {
        cout << "pop over" << endl;
        return STACK_ERR;
    }
    *pData = Tmp->getData();
    pStack->setNext(Tmp->getNext());
    Tmp->setNext(NULL);
    delete Tmp;

    return STACK_OK;
}

int STACK::IsEmpty(STACK *pStack) {
    if (!pStack)
        return STACK_ERR;

    if (!pStack->getNext())
        return STACK_EMPTY;

    return STACK_OK;
}

void STACK::operator delete(void *pStack) {
    if (!pStack)
        return;

    STACK *Tmp = ((STACK *)pStack)->getNext();
    while(Tmp){
        ((STACK *)pStack)->setNext(Tmp->getNext());
        free(Tmp);
        Tmp = ((STACK *)pStack)->getNext();
    }
    free(pStack);

    return;
}

main.cpp

#include "STACK/STACK.h"
#include <iostream>
using namespace std;

void function() {
    STACK *pStack = new STACK;
    int *data = new int[5];
    int pdata;
    int i;
    for (i = 4; i >= 0; i--) {
        data[i] = i;
    }
    for (i = 5; i >= 0; i--) {
        pStack->Push(pStack, i);
        cout << i << "  ";
    }
    cout << endl;
    for (i = 5; i >= 0; i--) {
        pStack->Pop(pStack, &pdata);
        cout << pdata << " ";
    }
    cout << endl;
    pStack->Push(pStack, 9);
    pStack->Pop(pStack, &pdata);
    cout << pdata << " ";
    pStack->Push(pStack, 5);
    pStack->Push(pStack, 3);
    pStack->Push(pStack, 7);
    pStack->Pop(pStack, &pdata);
    cout << pdata << " ";
    pStack->Push(pStack, 0);
    pStack->Pop(pStack, &pdata);
    cout << pdata << " ";
    pStack->Pop(pStack, &pdata);
    cout << pdata << " ";
    pStack->Pop(pStack, &pdata);
    cout << pdata << " " << endl;
    delete pStack;
}

int main() {
    function();
    return 0;
}
时间: 2024-12-17 12:35:00

C++__链式栈(练习)的相关文章

显示数据库中的存储过程__转

It's no easy trick to see stored procedures in a database programmatically with a scripting language like ASP. If you're using MS Access, you're out of luck. Access provides no way to see the actual meat of a stored procedure although you can get the

Android 5.0 怎样正确启用isLoggable(二)__原理分析

前置文章 <Android 5.0 怎样正确启用isLoggable(一)__使用具体解释> 概要 在上文<Android 5.0 怎样正确启用isLoggable(一)__使用具体解释>中分析了isLoggable的用法,本文主要分析isLoggable实现原理以及user版系统root后永久enable isLoggable的原理,并使用脚本自己主动设置isLoggable相关属性. 本文来自http://blog.csdn.net/yihongyuelan 转载请务必注明出处

python中_、__和__xx__的区别

python中_.__和__xx__的区别 本文为译文,版权属于原作者,在此翻译为中文分享给大家. 英文原文地址:Difference between _, __ and __xx__ in Python 在学习Python时,很多人都弄不清楚各种下划线的意思,而且在这之前已经给其他人解释过很多遍了,是时候把它记录下来. "_"单下划线 Python中不存在真正的私有方法.为了实现类似于c++中私有方法,可以在类的方法或属性前加一个“_”单下划线,意味着该方法或属性不应该去调用,它并不

链式栈

 1.编写头文件 #define datatype int struct statcknode { int num;                 //编号 datatype data;           //数据 struct statcknode *pNext;//指针域 }; typedef struct statcknode StackNode; //初始化 StackNode * init(StackNode *pHead); //进栈 StackNode * push(Sta

算法竞赛_入门经典_刘汝佳__(2)

1,有几位数字 #include<stdio.h> int main_2_1_digit(){ int n; while(scanf("%d",&n)){ int count = 0; if(n==0) count = 1; while(n){ count++; n/=10; } printf("%d\n",count); } return 0; } 2,三位数的三个数字 #include<stdio.h> int main_2_2_

WCF开山篇__图片传输

WCF开山篇__图片传输 一.  简介 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序接口,可以翻译为Windows通讯接口,它是.NET框架的一部分,由 .NET Framework 3.0 开始引入,与 Windows Presentation Foundation及 Windows Workflow Foundation并行为新一代 Windows 操作系统以及 WinFX 的三个重大应用程序开发类库.WCF由于集合了几乎由.

C++链式栈

LinkStack: //链式栈 #include<iostream> using namespace std; typedef int elemType; typedef struct StackNode { elemType data; StackNode *next; }*LinkList; struct LinkStack { LinkList top; int count; }; //初始化 void InitStack(LinkStack *S) { S->top = NUL

算法导论第八章__实现计数排序

计数排序:不须要比較就能得出排序的顺序__比如.本章的计数排序.基数排序.桶排序 比較排序:须要进行比較才干得出排序的顺序__比如,本章的堆排序.高速排序(本质是插入排序).插入排序 代码清单:计数排序__完美演绎下标的作用 public class Count_Sort { //接收须要排序的数组 private int[] A; //排序后的数组 private int[] B; //用于计数的数组 private int[] C; // 初始化 public Count_Sort(int[

Difference between _, __ and __xx__ in Python

When learning Python many people don't really understand why so much underlines in the beginning of the methods, sometimes even in the end like __this__! I've already had to explain it so many times, it's time to document it. One underline in the beg