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