Home > Back-end >  Singly linked lists stack unable to read from memory
Singly linked lists stack unable to read from memory

Time:09-22

 # # ifndef _MY_LIST_H_ 
# define _MY_LIST_H_

The class OutOfBound {};
The class IllegalSize {};

The template & lt; The class elemType>
The class MyList
{
Public:
Virtual void the clear ()=0;
Virtual int length (const)=0;
Virtual void insert (int, const elemType & amp;)=0;
Virtual void remove (int)=0;
Virtual int search (const elemType & amp;) Const=0;

Virtual const elemType& Visit (int) const=0;
Virtual void traverse by const ()=0;
Virtual ~ MyList () {}
};

# endif
# # ifndef _MYDIRLIST_H_
# define _MYDIRLIST_H_
# include "mylist. H"
#include
using namespace std;

The template & lt; The class elemType>
The class MyDirList: public MyList & lt; ElemType & gt;
{
Private:
Struct node {
ElemType data;
Node * pNext;
The node () : pNext (NULL) {}
The node (const elemType & amp; _data while forming, node * p=NULL) :
Data (_data while forming), pNext (p) {}
};
Node * m_pHead;
Int m_dLength;
Node * moveTo (int) const;
Public:
MyDirList () {
M_pHead=new node;
M_dLength=0;
}
~ MyDirList () {
The clear ();
The delete m_pHead;
}
void clear();
Const int length () {return m_dLength; }
Void insert (int, const elemType & amp;);
Void the remove (int);
Int search (const elemType & amp;) Const;
Const elemType & amp; Visit (int) const;
Void traverse by () the const;
};

Template
Typename MyDirList : : node * MyDirList : : moveTo (int pos) const
{
If (pos & lt; 1 | | pos & gt;=m_dLength) throw OutOfBound ();
Node * p=m_pHead;
While (pos - & gt; P=p=0) - & gt; PNext;
return p;
}

Template
Void MyDirList : : clear ()
{
The node * p=m_pHead - & gt; PNext, * q;
while (p !=NULL)
{
Q=p - & gt; PNext;
The delete p;
P=q;
}
M_dLength=0;
}

Template
Void MyDirList : : insert (int pos, const elemType & amp; _data while forming)
{
If (pos & lt; 0 | | pos & gt; M_dLength) throw OutOfBound ();
Node * p=moveTo (pos - 1);
Node * TMP=new node (_data while forming, p - & gt; PNext);
p-> PNext=TMP;
M_dLength + +;
}

Template
Void MyDirList : : remove (int pos)
{
If (pos & lt; 0 | | pos & gt;=m_dLength) throw OutOfBound ();
Node * p=moveTo (pos - 1);
Node * q=p - & gt; PNext;
p-> PNext=q - & gt; PNext;
The delete q;
M_dLength -;
}

Template
Int MyDirList : : search (const elemType & amp; Const _data while forming)
{
The node * p=m_pHead - & gt; PNext;
Int pos=0;
while (p !=NULL)
{
If (_data while forming p=https://bbs.csdn.net/topics/=-> data) return pos.
P=p - & gt; PNext;
pos++;
}
return -1;
}

Template
Const elemType & amp; MyDirList : : visit (int pos) const
{
If (pos & lt; 0 | | pos & gt;=m_dLength) throw OutOfBound ();
Node * p=moveTo (pos);
The return p - & gt; The data;
}

Template
Void MyDirList : : traverse by const ()
{
The node * p=m_pHead - & gt; PNext;
while (p !=NULL)
{
int i=0;
cout <"The Data #" & lt; The data & lt; P=p - & gt; PNext;
}
cout <"The End of the data" & lt; }

# endif
# # ifndef _MYSTACK_H_
# define _MYSTACK_H_
# include "mydirlist. H"
//in this file to complete the definition of the class interface
The template & lt; The class elemType>
The class MyStack
{
Public:
MyStack ()
{
M_pStack=new MyDirList;
};
~ MyStack ();
Void push_back (const elemType & amp; Value);
Void pop_back ();
Const Boolean isEmpty () {return m_pStack==NULL; };
Const elemType & amp; Const back () {return m_pStack - & gt; Visit (m_pStack - & gt; The length ()); };
Private:
MyDirList * m_pStack;

};

The template & lt; The class elemType>
MyStack : : ~ MyStack ()
{
The delete m_pStack;
}

Template
Void MyStack : : push_back (const elemType & amp; Value)
{
M_pStack - & gt; Insert (m_pStack - & gt; Length (), value);
}
Template
Void MyStack : : pop_back ()
{
M_pStack - & gt; Remove (m_pStack - & gt; The length ());
}

# endif
#include
# include "mystack. H"
using namespace std;
Int main ()
{
MyStack a;
Amy polumbo ush_back (1);
Amy polumbo op_back ();
cout}

Best to change in the stack, only a small white please comment...
  • Related