C++ 实现栈(使用单链表)

继承 单链表 以实现 栈,并对其进行基本操作;

创建文件夹 Stack,放入 Stack.h、Stack.cpp、main.cpp;

在 Stack 中创建子文件夹 LinkList,放入 LinkList.h、LinkList.cpp;

对 main.cpp 执行编译即可;

本人萌新,评论区欢迎各位大佬提问、指正;

Stack.h

// implement Stack using single linked lists
// class Stack : public LinkList
#ifndef STACK_H_
#define STACK_H_
#include "LinkList\LinkList.h"
#include "LinkList\LinkList.cpp"

class Stack : public LinkList
{
    public:
        Stack();                                                    // create a empty Stack
        Stack(DataType ar[],int n);                                 // using ar[] to create a Stack
        ~Stack();                                                   // delete Stack
        bool push(const DataType &d);                               // enter a Node
        bool pop(DataType &d);                                      // delete a Node
        // you can also use the following method defined in LinkList
        // bool isempty() const;                                    // judge it's empty or not
        // bool display() const;                                    // show all the Node
};

#endif

Stack.cpp

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

Stack::Stack() : LinkList()
{
    // call LinkList::LinkList()
}
Stack::Stack(DataType ar[], int n)
{
    // create a temp point to first Node
    Node *ptemp = new Node; 
    ptemp->data = ar[n-1];
    ptemp->next = NULL;
    // create the head point to temp
    head = new Node;
    head->data = n;
    head->next = ptemp;
    // add all Node
    for (int i = n - 2; i >= 0; i--)
    {
		Node *pnew = new Node;
        pnew->data = ar[i];
        pnew->next = NULL;
        ptemp->next = pnew;
        ptemp = pnew;
    }
    ptemp->next = NULL;
    // create the tail point to last Node
    tail = new Node;
    tail->data = 0;
    tail->next = ptemp;
}
Stack::~Stack()
{
    // do nothing
}
bool Stack::push(const DataType &d)
{
    // call LinkList::insert(DataType d1, int location)
    LinkList::insert(d, 0);
    return true;
}
bool Stack::pop(DataType &d)
{
    // call bool LinkList::remove(int location, DataType &d1)
    return LinkList::remove(1, d);
}

 main.cpp

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

int main()
{
    double ar[5] = {1.5, 2.5, 3.5, 4.5, 5.5};
    Stack S1(ar, 5);

    S1.display();

    while(true)
        cin.get();
    return 0;
}

 LinkList.h

// LinkList with head and tail
#ifndef LINKLIST_H_
#define LINKLIST_H_
#include <iostream>
typedef double DataType;                                              // DataType can't be int

struct Node
{
	DataType data;
	Node *next;
};
class LinkList
{
    protected:
	    Node *head;                                                   // head->next == first Node, head->data == size
        Node *tail;                                                   // tail->next == last Node, tail->next == 0
        inline Node *search(int location) const;                      // search by location
        inline Node *search(DataType value) const;                    // search by value
    public:
        LinkList();                                                   // create a empty LinkList
        LinkList(DataType ar[],int n);                                // using ar[] to create a LinkList
        ~LinkList();                                                  // destructor
        bool isempty() const;                                         // judge it's empty or not
        bool insert(DataType d1, DataType d2);                        // insert d1 after d2
        bool insert(DataType d1, int location);                       // insert d1 to a location
        bool remove(DataType d1);                                     // remove d1 from LinkList
        bool remove(int location, DataType &d1);                      // remove location's Node and return to d1
        bool display() const;                                         // show all the Node
};

#endif

 LinkList.cpp

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

LinkList::LinkList()
{
    // create the head
    head = new Node;
    head->data = 0;
    head->next = NULL;
    // create the tail
    tail = new Node;
    tail->data = 0;
    tail->next = NULL;
}
LinkList::LinkList(DataType ar[], int n)
{
    // create a temp point to first Node
    Node *ptemp = new Node; 
    ptemp->data = ar[0];
    ptemp->next = NULL;
    // create the head point to temp
    head = new Node;
    head->data = n;
    head->next = ptemp;
    // add all Node
    for (int i = 1; i < n; i++)
    {
		Node *pnew = new Node;
        pnew->data = ar[i];
        pnew->next = NULL;
        ptemp->next = pnew;
        ptemp = pnew;
    }
    ptemp->next = NULL;
    // create the tail point to last Node
    tail = new Node;
    tail->data = 0;
    tail->next = ptemp;
}
LinkList::~LinkList()
{
    delete head;
    delete tail;
}
bool LinkList::isempty() const
{
    if(head->data == 0)
        return true;
    else
        return false;
}
bool LinkList::display() const
{
    if(head->data == 0)
    {
        cout << "NULL";
        return false;
    }
    Node *ptemp = head->next;
    while(ptemp)
    {
        cout << ptemp->data << " -> ";
        ptemp = ptemp->next;
    }
    cout << "NULL " << "(the length is "<<head->data<<")";
    return true;
}
inline Node *LinkList::search(int location) const
{
    if (location == 0)
        return head;
    Node *ptemp = head->next;
    int n = 1;
    while(n != location)
    {
        ptemp = ptemp->next;
        n++;
    }
    return ptemp;
}
inline Node *LinkList::search(DataType value) const
{
    Node *ptemp = head->next;
    while (ptemp && ptemp->data != value)
        ptemp = ptemp->next;
    return ptemp;             // will return a *Node or NULL
}
bool LinkList::insert(DataType d1, DataType d2)
{
    if(search(d2) == NULL)
        return false;
    Node *pd2 = search(d2);
    Node *pnew = new Node;
    pnew->data = d1;
    pnew->next = pd2->next;
    pd2->next = pnew;
    // if pnew is the new tail, move tail
    if(pnew->next == NULL)
        tail->next = pnew;
    head->data++;
    return true;
}
bool LinkList::insert(DataType d1, int location)
{
    if(location < 0)
        return false;
    // first insertion
    if(location == 0)
    {
        Node *pnew = new Node;
        pnew->data = d1;
        pnew->next = head->next;
        head->next = pnew;
    }
    // middle insertion
    else if (location <= head->data)
    {
        Node *ploc = search(location);
        Node *pnew = new Node;
        pnew->data = d1;
        pnew->next = ploc->next;
        ploc->next = pnew;
    }
    // tail insertion
    else
    {
        Node *plast = search(int(head->data));
        Node *pnew = new Node;
        pnew->data = d1;
        pnew->next = NULL;
        plast->next = pnew;
        // pnew is the new tail, move tail
        tail->next = pnew;
    }
    head->data++;
    return true;
}
bool LinkList::remove(DataType d1)
{
    if(search(d1) == NULL)
    {
        cout << d1 << " is not in the list...";
        return false;
    }
    else
    {
        Node *pd1 = search(d1);
        int i;
        for (i = 1; i <= head->data; i++)
            if (search(i) == pd1)
                break;
        Node *pr = search(--i);
        pr->next = pd1->next;
        // if pr is new tail, move tail
        if (pr->next == NULL)
            tail->next = pr;
        head->data--;
        return true;
    }
}
bool LinkList::remove(int location, DataType &d1)
{
    if (location < 0 || location > head->data)
    {
        cout << "The list isn't so long...";
        return false;
    }
    else
    {
        Node *ploc = search(location);
        d1 = ploc->data;
        Node *pr = search(--location);
        pr->next = ploc->next;
        // if pr is new tail, move tail
        if (pr->next == NULL)
            tail->next = pr;
        head->data--;
        return true;
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gaoqizhong7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值