Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在泛型列表的顶部构建泛型堆栈?c++;_C++ - Fatal编程技术网

C++ 如何在泛型列表的顶部构建泛型堆栈?c++;

C++ 如何在泛型列表的顶部构建泛型堆栈?c++;,c++,C++,我目前有一个通用堆栈类。但是,我想更改堆栈类,以便它使用我的泛型列表类(提供了两个完整代码)。换句话说,我想使用我的泛型列表类构建一个泛型堆栈。我希望有人能帮我 我的堆栈类: #include <iostream> using namespace std; const int MAX_SIZE = 30; template <class T> class Stack { private: struct Node {

我目前有一个通用堆栈类。但是,我想更改堆栈类,以便它使用我的泛型列表类(提供了两个完整代码)。换句话说,我想使用我的泛型列表类构建一个泛型堆栈。我希望有人能帮我

我的堆栈类:

#include <iostream>
using namespace std;

const int MAX_SIZE = 30;

template <class T>
class Stack
{
    private:
        struct Node
        {
            T data;
            Node *link;
        };

        Node *top;
        int num_items;

    public:
        // constructor
        // remember that an empty list has a "size" of -1 and its "position" is at -1
        Stack()
        {
            top = NULL;
            num_items = 0;
        }

        // copy constructor
        // clones the list l and sets the last element as the current
        Stack(const Stack& s)
        {
            *this = s;
        }

        // copy constructor
        // clones the list l and sets the last element as the current
        void operator=(const Stack& s)
        {
            Node *n = s.top;

            top = NULL;
            num_items = 0;

            // just loop through the list and copy each element
            while (n != NULL)
            {
                Push(n->data);
                n = n->link;
            }
        }

        //insert an item on top of the stack
        void Push(T data)
        {
            Node *n = new Node;
            n -> link =NULL;
            n -> data = data;

            if (top == NULL)
                top = n;

            else
            {
                n -> link = top;
                top = n;
            }
            num_items++;

        }
        //delete an item from the top of the stack (and return it)
        void Pop()
        {
            Node *n;

            if(IsEmpty ())
                return;
            else
            {
                n = top;
                top = top -> link;
                delete n;
            }
            num_items--;

        }

        //return(but do not delete) the item on top of the stack
        int Peek()
        {
            if(IsEmpty ())
                return -1;
            else
                return top -> data;

        }

        // return size of the stack
        int Size()
        {
            return num_items;

        }

        // returns if the list is empty
        bool IsEmpty()
        {
            if(top == NULL)
                return true;
            else
                return false;
        }

        // returns if the list is full
        bool IsFull()
        {
            if(num_items == MAX_SIZE)
                return true;
            else
                return false;
        }

        // returns the concatenation of two lists
        // s should not be modified
        // s should be concatenated to the end of *this
        // the returned list should not exceed MAX_SIZE elements
        // the last element of the new list is the current
        Stack operator+(const Stack& s) const
        {
            // copy the first list
            Stack t = *this;
            Node *n = s.top;

            // iterate through the second list and copy each element to the new list
            while (n != NULL && !t.IsFull())
            {
                t.Push(n->data);
                n = n->link;
            }

            return t;
        }

        // returns if two lists are equal (by value)
        bool operator==(const Stack& s) const
        {
            // the lists are not equal if they're of different sizes
            if (num_items != s.num_items)
                return false;

            Node *p = top;
            Node *q = s.top;

            // iterate through each list
            while (p != NULL)
            {
                // if any pair of elements differ, the lists are not equal
                if (p->data != q->data)
                    return false;
                p = p->link;
                q = q->link;
            }

            return true;
        }

        // returns if two lists are not equal (by value)
        bool operator!=(const Stack& s) const
        {
            return !(*this == s);
        }

        // returns a string representation of the entire stack (e.g., 1 2 3 4 5)
        // the string "NULL" should be returned for an empty stack
        friend ostream& operator<<(ostream& out, const Stack &s)
        {
            // "NULL" if the stack is empty
            if (s.top == NULL)
                out << "NULL";
            else
            {
                Node *n = s.top;

                // otherwise iterate through the list and display each element separated by a space
                while (n != NULL)
                {
                    out << n->data << " ";
                    n = n->link;
                }
            }

            return out;
        }
};
#包括
使用名称空间std;
const int MAX_SIZE=30;
模板
类堆栈
{
私人:
结构体类型
{
T数据;
节点*链接;
};
节点*顶部;
int num_项目;
公众:
//建造师
//请记住,空列表的“大小”为-1,其“位置”为-1
堆栈()
{
top=NULL;
num_items=0;
}
//复制构造函数
//克隆列表l并将最后一个元素设置为当前元素
堆栈(常量堆栈&s)
{
*这=s;
}
//复制构造函数
//克隆列表l并将最后一个元素设置为当前元素
void运算符=(常量堆栈&s)
{
节点*n=s.top;
top=NULL;
num_items=0;
//只需在列表中循环并复制每个元素
while(n!=NULL)
{
推送(n->数据);
n=n->link;
}
}
//在堆栈顶部插入一项
无效推送(T数据)
{
Node*n=新节点;
n->link=NULL;
n->data=数据;
if(top==NULL)
top=n;
其他的
{
n->link=top;
top=n;
}
num_items++;
}
//从堆栈顶部删除项目(并返回)
void Pop()
{
节点*n;
if(IsEmpty())
返回;
其他的
{
n=顶部;
顶部=顶部->链接;
删除n;
}
num_项--;
}
//返回(但不要删除)堆栈顶部的项
int Peek()
{
if(IsEmpty())
返回-1;
其他的
返回top->data;
}
//返回堆栈的大小
int Size()
{
返回num_项;
}
//如果列表为空,则返回
布尔是空的
{
if(top==NULL)
返回true;
其他的
返回false;
}
//如果列表已满,则返回
bool IsFull()
{
if(num\u items==最大大小)
返回true;
其他的
返回false;
}
//返回两个列表的串联
//不应修改的
//s应连接到此*的结尾
//返回的列表不应超过最大大小的元素
//新列表的最后一个元素是当前列表
堆栈运算符+(常量堆栈和s)常量
{
//复制第一个列表
堆栈t=*这个;
节点*n=s.top;
//迭代第二个列表,并将每个元素复制到新列表中
而(n!=NULL&&!t.IsFull())
{
t、 推送(n->数据);
n=n->link;
}
返回t;
}
//如果两个列表相等(按值),则返回
布尔运算符==(常量堆栈&s)常量
{
//如果列表的大小不同,则列表不相等
if(num\u项目!=s.num\u项目)
返回false;
节点*p=顶部;
节点*q=s.top;
//遍历每个列表
while(p!=NULL)
{
//如果任何一对元素不同,则列表不相等
如果(p->data!=q->data)
返回false;
p=p->link;
q=q->link;
}
返回true;
}
//如果两个列表(按值)不相等,则返回
布尔运算符!=(常量堆栈&s)常量
{
返回!(*this==s);
}
//返回整个堆栈的字符串表示形式(例如,1 2 3 4 5)
//空堆栈应返回字符串“NULL”
friend ostream&operatordata=数据;
head->link=curr;
curr=头;
num_items++;
}
//否则,请导航到上一个节点并在之后插入
其他的
{
Prev();
插入者(数据);
}
}
}
}
//在当前元素后插入项
//新元素成为当前元素
//对于完整的列表,这应该是不可能的
void InsertAfter(T数据)
{
如果(!IsFull())
{
Node*n=新节点;
n->data=数据;
n->link=NULL;
//如果列表为空,则所有内容都指向单个节点
if(IsEmpty())
头=尾=电流=n;
其他的
{
//如果我们到了终点,就把新节点钉在上面
如果(当前==尾部)
{
当前->链接=n;
curr=tail=n;
}
//否则,请更改链接以插入节点
其他的
{
n->link=curr->link;
curr=curr->link=n;
}
}
num_items++;
}
}
//删除当前元素(折叠列表)
//这个
#include <iostream>
using namespace std;

const int MAX_SIZE = 30;

template <class T>
class List
{
    private:
        struct Node
        {
            T data;
            Node *link;
        };

        Node *head;
        Node *tail;
        Node *curr;
        int num_items;

    public:
        // constructor
        // remember that an empty list has a "size" of -1 and its "position" is at -1
        List()
        {
            head = tail = curr = NULL;
            num_items = 0;
        }

        // copy constructor
        // clones the list l and sets the last element as the current
        List(const List& l)
        {
            *this = l;
        }

        // copy constructor
        // clones the list l and sets the last element as the current
        void operator=(const List& l)
        {
            Node *n = l.head;

            head = tail = curr = NULL;
            num_items = 0;

            // just loop through the list and copy each element
            while (n != NULL)
            {
                InsertAfter(n->data);
                n = n->link;
            }
        }

        // navigates to the beginning of the list
        // this should not be possible for an empty list
        void First()
        {
            curr = head;
        }

        // navigates to the end of the list
        // the end of the list does not necessarily correspond to its maximum size; it's just at the last existing element
        void Last()
        {
            curr = tail;
        }

        // navigates to the specified element (0-index)
        // this should not be possible for an empty list
        // this should not be possible for invalid positions
        void SetPos(T pos)
        {
            if (!IsEmpty() && pos >=0 && pos < num_items)
            {
                curr = head;

                // move curr to the specified position
                for (int i=0; i<pos; i++)
                    curr = curr->link;
            }
        }

        // navigates to the previous element
        // this should not be possible for an empty list
        // there should be no wrap-around
        void Prev()
        {
            if (!IsEmpty() && curr != head)
            {
                Node *n = head;

                // move n to the previous element
                while (n->link != curr)
                    n = n->link;

                curr = n;
            }
        }

        // navigates to the next element
        // this should not be possible for an empty list
        // there should be no wrap-around
        void Next()
        {
            if (!IsEmpty() && curr != tail)
                curr = curr->link;
        }

        // returns the location of the current element (or -1)
        int GetPos()
        {
            if (IsEmpty())
                return -1;

            Node *n = head;
            int i = 0;

            // traverse the list to get the current position
            while (n != curr)
            {
                n = n->link;
                i++;
            }

            return i;
        }

        // returns the value of the current element (or -1)
        int GetValue()
        {
            return ((!IsEmpty()) ? curr->data : -1);
        }

        // returns the size of the list
        // size does not imply capacity
        int GetSize()
        {
            return num_items;
        }

        // inserts an item before the current element
        // the new element becomes the current
        // this should not be possible for a full list
        void InsertBefore(T data)
        {
            if (!IsFull())
            {
                // if the list is empty, just insert after
                if (IsEmpty())
                    InsertAfter(data);
                else
                {
                    // if we're at the beginning, just create a new head that points to the current one
                    if (curr == head)
                    {
                        head = new Node;
                        head->data = data;
                        head->link = curr;
                        curr = head;
                        num_items++;
                    }
                    // otherwise, navigate to the previous node and insert after
                    else
                    {
                        Prev();
                        InsertAfter(data);
                    }
                }
            }
        }

        // inserts an item after the current element
        // the new element becomes the current
        // this should not be possible for a full list
        void InsertAfter(T data)
        {
            if (!IsFull())
            {
                Node *n = new Node;

                n->data = data;
                n->link = NULL;

                // if the list is empty, everything points to the single node
                if (IsEmpty())
                    head = tail = curr = n;
                else
                {
                    // if we're at the end, just tack the new node on
                    if (curr == tail)
                    {
                        curr->link = n;
                        curr = tail = n;
                    }
                    // otherwise, change the links to insert the node
                    else
                    {
                        n->link = curr->link;
                        curr = curr->link = n;
                    }
                }

                num_items++;
            }
        }

        // removes the current element (collapsing the list)
        // this should not be possible for an empty list
        void Remove()
        {
            if (!IsEmpty())
            {
                // if we're at the beginning, reset the head
                if (curr == head)
                {
                    head = curr = curr->link;

                    if (head == NULL)
                        tail = NULL;
                }
                else
                {
                    Prev();
                    // and rearrange the pointer to remove this node
                    curr->link = curr->link->link;
                    // we handle removing the tail vs. other internal nodes a bit differently
                    if (curr->link == NULL)
                        tail = curr;
                    Next();
                }
                num_items--;
            }
        }

        // replaces the value of the current element with the specified value
        // this should not be possible for an empty list
        void Replace(T data)
        {
            if (!IsEmpty())
                curr->data = data;
        }

        // returns if the list is empty
        bool IsEmpty()
        {
            return (head == NULL);
        }

        // returns if the list is full
        bool IsFull()
        {
            return (num_items == MAX_SIZE);
        }

        // returns the concatenation of two lists
        // l should not be modified
        // l should be concatenated to the end of *this
        // the returned list should not exceed MAX_SIZE elements
        // the last element of the new list is the current
        List operator+(const List& l) const
        {
            // copy the first list
            List t = *this;
            Node *n = l.head;

            // iterate through the second list and copy each element to the new list
            while (n != NULL && !t.IsFull())
            {
                t.InsertAfter(n->data);
                n = n->link;
            }

            return t;
        }

        // returns if two lists are equal (by value)
        bool operator==(const List& l) const
        {
            // the lists are not equal if they're of different sizes
            if (num_items != l.num_items)
                return false;

            Node *p = head;
            Node *q = l.head;

            // iterate through each list
            while (p != NULL)
            {
                // if any pair of elements differ, the lists are not equal
                if (p->data != q->data)
                    return false;
                p = p->link;
                q = q->link;
            }

            return true;
        }

        // returns if two lists are not equal (by value)
        bool operator!=(const List& l) const
        {
            return !(*this == l);
        }

        // returns a string representation of the entire list (e.g., 1 2 3 4 5)
        // the string "NULL" should be returned for an empty list
        friend ostream& operator<<(ostream& out, const List &l)
        {
            // "NULL" if the list is empty
            if (l.head == NULL)
                out << "NULL";
            else
            {
                Node *n = l.head;

                // otherwise iterate through the list and display each element separated by a space
                while (n != NULL)
                {
                    out << n->data << " ";
                    n = n->link;
                }
            }

            return out;
        }
};