| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 405 人关注过本帖
标题:大家帮忙看看我的通用栈错在哪里
取消只看楼主 加入收藏
PENGGB023
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-10-30
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
大家帮忙看看我的通用栈错在哪里
//stack.h
#ifndef STACK_H_
#define STACK_H_

template <class T>
class LinkStack
{
public:
    LinkStack(){top = NULL;}
    ~LinkStack() {};
    struct node {
        T data;
        node *next;
    };
    void SetEmpty();
    bool IsEmpty();
    bool Push(T element);
    bool Pop(T & element);
     void show();
private:
    node * top;
};

#endif

// stack.cpp
#include "stack.h"

template <class T> void LinkStack <T>::SetEmpty()
{
    node * temp;
    while (top != NULL)
    {
        temp = top;
        top = top->next;
        delete temp;
    }
}

template <class T> bool LinkStack <T>::IsEmpty()
{
    return top == NULL;
}

template <class T> bool LinkStack <T>::Push(T element)
{
    node *temp = new node();
    if (temp == NULL)
        return false;
    temp->data = element;
    temp->next = top;
    top = temp;
    return true;
}

template <class T> bool LinkStack <T>::Pop(T & element)
{
    if (IsEmpty())
        return false;
    node *q = top;
    element = top->data;
    top = top->next;
    delete q;
    return true;
}

template <class T> void LinkStack <T>::show(T & element)
{
    node *temp = top;
    std::cout << std::endl << "----top----" << std::endl;
    while (temp->next != NULL)
    {
        std::cout << "    " << temp->data <<std::endl;
        temp = temp->next;
    }
    std::cout << "----top----" << std::endl << std:endl;
}

//main.cpp
#include <iostream>
#include "stack.h"

int main(int argc, char **argv)
{
    LinkStack<int> st;
    int a = 7;
    st.Push(a);
    st.Push(5);
    st.Push(8);
    st.show();
    return 0;
}
搜索更多相关主题的帖子: private element public 通用 
2015-10-30 12:13
PENGGB023
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-10-30
收藏
得分:0 
他说我调用的那些函数都没有定义
2015-10-30 14:33
快速回复:大家帮忙看看我的通用栈错在哪里
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015495 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved