标签:
今天实现了链表栈的简单实现,贴出来以后可以看一看。链表栈就是用链表来实现栈的一些操作。
LinkStack.h
#ifndef LINKSTACK_H_
#define LINKSTACK_H_
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
template <typename T>
struct Node
{
T data;
Node<T> *next;
Node();
Node(T item, Node<T> *link=NULL);
};
template <typename T>
Node<T>::Node()
{
next=NULL;
}
template <typename T>
Node<T>::Node(T item, Node<T> *link)
{
data=item;
next=link;
}
template<typename T>
class LinkStack
{
protected:
Node<T> *top;
void Init();
Node<T> *GetElemPtr(int position) const;
public:
LinkStack();
virtual ~LinkStack();
bool IsEmpty();
int Length() const;
void Clear();
void Push(T e);
T Top();
T Pop();
LinkStack(const LinkStack<T> ©);
LinkStack<T> &operator=(const LinkStack<T> ©);
};
template <typename T>
void LinkStack<T>::Init()
{
top=new Node<T>;
}
template <typename T>
Node<T> *LinkStack<T>::GetElemPtr(int position) const
{
int Len=Length();
Node<T> *tempPtr=top;
int curPosition=0;
while(tempPtr->next!=NULL && curPosition != position)
{
tempPtr=tempPtr->next;
++curPosition;
}
if(/*tempPtr->next!=NULL && */curPosition == position)
{
return tempPtr;
}
else
return NULL;
}
template <typename T>
LinkStack<T>::LinkStack()
{
Init();
}
template<typename T>
LinkStack<T>:: ~LinkStack()
{
Clear();
delete top;
}
template <typename T>
bool LinkStack<T>::IsEmpty()
{
return top->next==NULL;
}
template <typename T>
int LinkStack<T>::Length() const
{
Node<T> *tempPtr=top;
int count=0;
while(tempPtr->next!=NULL)
{
++count;
tempPtr=tempPtr->next;
}
return count;
}
template <typename T>
void LinkStack<T>::Clear()
{
Node<T> *tempPtr=top;
int Len=Length();
while(tempPtr->next!=NULL)
{
Node<T> *newPtr=GetElemPtr(Len-1);
delete newPtr->next;
newPtr->next=NULL;
}
}
template <typename T>
void LinkStack<T>::Push(T e)
{
int Len=Length();
Node<T> *tempPtr;
tempPtr=GetElemPtr(Len);
Node<T> *newPtr=new Node<T>;
tempPtr->next=newPtr;
newPtr->data=e;
}
template <typename T>
T LinkStack<T>::Top()
{
int Len=Length();
Node<T> *tempPtr=GetElemPtr(Len);
return tempPtr->data;
}
template<typename T>
T LinkStack<T>::Pop()
{
int Len=Length();
Node<T> *tempPtr=GetElemPtr(Len-1);
Node<T> *newPtr=tempPtr->next;
tempPtr->next=NULL;
return newPtr->data;
}
template <typename T>
LinkStack<T>::LinkStack(const LinkStack<T> ©)
{
Init();
/*Node<T> *tempPtr=copy.top;*/
Node<T> *newPtr=top;
Node<T> *nowPtr;
int Len=copy.Length();
int count=0;
while(Len--)
{
++count;
nowPtr=copy.GetElemPtr(count);
Node<T>* tempPtr=new Node<T>;
newPtr->next=tempPtr;
tempPtr->data=nowPtr->data;
newPtr=tempPtr;
}
}
template <typename T>
LinkStack<T> &LinkStack<T>::operator=(const LinkStack<T> ©)
{
if(copy!=this)
{
Init();
/*Node<T> *tempPtr=copy.top;*/
Node<T> *newPtr=top;
Node<T> *nowPtr;
int Len=copy.Length();
int count=0;
while(Len--)
{
++count;
nowPtr=copy.GetElemPtr(count);
Node<T>* tempPtr=new Node<T>;
newPtr->next=tempPtr;
tempPtr->data=nowPtr->data;
newPtr=tempPtr;
}
}
return *this;
}
template <typename T>
ostream &operator <<(ostream &os, LinkStack<T> &Ls)
{
cout<<"Last in first out: ";
while(!Ls.IsEmpty())
{
cout<<Ls.Pop()<<" ";
}
cout<<endl;
return os;
}
#endifLinkStack.cpp
// LinkStack.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "LinkStack.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
LinkStack<int> Stack;
for(int i=0; i<100; i++)
{
Stack.Push(i);
}
cout<<"链表的头结点为:"<<Stack.Top()<<endl;
LinkStack<int> copy=Stack;
LinkStack<int> copy1(Stack);
cout<<"链表栈为:"<<Stack;
cout<<"复制链表栈为:"<<copy;
cout<<"复制链表栈为:"<<copy1;
system("pause");
return 0;
}
结果就不贴了。标签:
原文地址:http://blog.csdn.net/jiang111_111shan/article/details/46004813