qq_38211455 2017-04-07 12:12 采纳率: 50%
浏览 1206

单链表栈无法读取内存

#ifndef MY_LIST_H
#define MY_LIST_H

class OutOfBound {};
class IllegalSize{};

template
class MyList
{
public:
virtual void clear() = 0;
virtual int length() const = 0;
virtual void insert(int, const elemType &) = 0;
virtual void remove(int) = 0;
virtual int search(const elemType &) const = 0;

virtual const elemType& visit(int) const = 0;
virtual void traverse() const = 0;
virtual ~MyList(){}

};

#endif
#ifndef MYDIRLIST_H
#define MYDIRLIST_H
#include "mylist.h"
#include
using namespace std;

template
class MyDirList : public MyList < elemType >
{
private:
struct node{
elemType data;
node *pNext;
node() : pNext(NULL){}
node(const elemType &_data, node *p = NULL) :
data(_data), pNext(p){}
};
node *m_pHead;
int m_dLength;
node *moveTo(int) const;
public:
MyDirList(){
m_pHead = new node;
m_dLength = 0;
}
~MyDirList(){
clear();
delete m_pHead;
}
void clear();
int length() const { return m_dLength; }
void insert(int, const elemType &);
void remove(int);
int search(const elemType &) const;
const elemType &visit(int) const;
void traverse() const;
};

template
typename MyDirList::node* MyDirList::moveTo(int pos) const
{
if (pos < -1 || pos >= m_dLength) throw OutOfBound();
node *p = m_pHead;
while (pos-->=0) p = p->pNext;
return p;
}

template
void MyDirList::clear()
{
node *p = m_pHead->pNext, *q;
while (p != NULL)
{
q = p->pNext;
delete p;
p = q;
}
m_dLength = 0;
}

template
void MyDirList::insert(int pos, const elemType &_data)
{
if (pos < 0 || pos > m_dLength) throw OutOfBound();
node *p = moveTo(pos - 1);
node *tmp = new node(_data, p->pNext);
p->pNext = tmp;
m_dLength++;
}

template
void MyDirList::remove(int pos)
{
if (pos < 0 || pos >= m_dLength) throw OutOfBound();
node *p = moveTo(pos - 1);
node *q = p->pNext;
p->pNext = q->pNext;
delete q;
m_dLength--;
}

template
int MyDirList::search(const elemType &_data) const
{
node *p = m_pHead->pNext;
int pos = 0;
while (p != NULL)
{
if (_data == p->data) return pos;
p = p->pNext;
pos++;
}
return -1;
}

template
const elemType &MyDirList::visit(int pos) const
{
if (pos < 0 || pos >= m_dLength) throw OutOfBound();
node *p = moveTo(pos);
return p->data;
}

template
void MyDirList::traverse() const
{
node *p = m_pHead->pNext;
while (p != NULL)
{
int i = 0;
cout << "Data #" << i++ << ": " << p->data << endl;
p = p->pNext;
}
cout << "End of data" << endl;
}

#endif
#ifndef MYSTACK_H
#define MYSTACK_H
#include "mydirlist.h"
//在本文件中完成类接口的定义
template
class MyStack
{
public:
MyStack()
{
m_pStack = new MyDirList;
};
~MyStack();
void push_back(const elemType &value);
void pop_back();
bool isEmpty() const { return m_pStack == NULL; };
const elemType &back() const { return m_pStack->visit(m_pStack->length()); };
private:
MyDirList *m_pStack;

};

template
MyStack::~MyStack()
{
delete m_pStack;
}

template
void MyStack::push_back(const elemType & value)
{
m_pStack->insert(m_pStack->length(), value);
}
template
void MyStack::pop_back()
{
m_pStack->remove(m_pStack->length());
}

#endif
#include

#include "mystack.h"

using namespace std;
int main( )

{
MyStacka;
}

图片说明
最好只改stack里的。小白求赐教...

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-04-07 14:55
    关注

    p这个指针你没有初始化,还是cccccccc

    评论

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题