将写代码过程中重要的代码片段备份一次,如下的代码段是关于C++基于链式存储结构的栈的代码。
#pragma once
#include
using namespace std;
template
class LinkNode
{
public:
T data;
public:
LinkNode()
{
}
LinkNode(T &da)
{
}
{}
};
链式栈结构中的top真的成为了一个指针,是一个结点类型指针,用来指向栈顶元素的位置。
#pragma once
#include"LinkNode.h"
#include
using namespace std;
template
class Stack
{
public:
Stack();
Stack(Stack &S);
~Stack();
};
template
Stack::Stack()
{
}
template
{
elem = srcptr->data;
newNode = new LinkNode<T>(elem);
destptr = top;
while(srcptr != NULL)
{
destptr = destptr->link;
}
}
template
Stack::~Stack()
{
makeEmpty();
}
template
void Stack::makeEmpty()
{
while(top != NULL)
{
top = del->link;
delete del;
}
}
template
{
return top;
}
template
void Stack::push(T elem)
{
newNode = new LinkNode(elem);
newNode->link = top;
top = newNode;
}
template
T Stack::pop()
{
T elem;
top = del->link;
elem = del->data;
delete del;
}
template
int Stack::Length()
{
int count = 0;
while(current != NULL)
{
current = current->link;
count++;
}
return count;
}
template
bool Stack::isEmpty()
{
return ((top == NULL) ? true : false);
}
template
void Stack::output()
{
int count = 0;
while(current != NULL)
{
cout<<"#"<<count+1<<":"<data<<endl;
current = current->link;
count++;
}
}
template
void Stack::operator= (Stack &S)
{
elem = srcptr->data;
newNode = new LinkNode<T>(elem);
destptr = top;
srcptr = srcptr->link;
while(srcptr != NULL)
{
cout<<"hello1"<<endl;
destptr = destptr->link;
}
}