注册 登录
编程论坛 C++教室

一个自己实现的栈,如有缺陷请指出

MC189 发布于 2018-11-26 20:12, 2261 次点击
#ifndef _STACK_H_
#define _STACK_H_
#include<iostream>
//#include<stack>
using namespace std;
#include<string>
template<typename T>
class Stack{
    private:
        T data;
    public:
        Stack(){}
        ~Stack(){}
        public:
        void push(T a){
            data=a;
        }
        T pop(){
            return data;
        }
};
#endif
4 回复
#2
Jonny02012018-11-26 22:17
只有一个位置的栈?
#3
复旦2018-11-27 07:17
栈是干什么的?

#4
rohalloway2018-11-27 12:09
程序代码:

#include <stdlib.h>
#include <stdio.h>

int push(char* stack, int top, char elem) {
    stack[++top] = elem;

    printf("入栈元素:%c\n", elem);

    return top;
}

int pop(char* stack, int top) {
    if(top == -1) {
        return -1;
    }

    printf("出栈元素:%c\n", stack[top]);
    top--;

    return top;
}

int main()
{
    char c[10];
    int top = -1;

    top = push(c, top, 'A');
    top = push(c, top, 'B');
    top = push(c, top, 'C');
    top = push(c, top, 'D');
    top = push(c, top, 'E');

    top = pop(c, top);
    top = pop(c, top);
    top = pop(c, top);
    top = pop(c, top);
    top = pop(c, top);
    return 0;
}


[此贴子已经被作者于2018-12-1 19:47编辑过]

#5
cstdio2018-12-01 10:37
回复 2楼 Jonny0201
是啊
1