本文为数据结构-链栈的代码实现。
作者水平比较差,有错误的地方请见谅。
1、C#实现
栈接口
IStack.cs
/// <summary>
/// 栈接口
/// </summary>
public interface IStack<T>
{
int GetLength(); //求栈的长度
bool IsEmpty(); //判断栈是否为空
void Clear(); //清空
void Push(T data); //入栈
T Pop(); //出栈
T Peek(); //取栈顶元素
void ShowAllElem(); //显示栈中所有元素
}
链栈
LinkStack.cs
class LinkStack<T> : IStack<T>
{
//链栈不需要附加一个头结点
/// <summary>
/// 栈顶元素
/// </summary>
private StackNode<T> mTopNode;
/// <summary>
/// 栈元素个数
/// </summary>
private int mCount;
public LinkStack()
{
mTopNode = null;
mCount = 0;
}
public bool IsEmpty()
{
return GetLength() == 0;
}
public int GetLength()
{
return mCount;
}
public void Push(T data)
{
StackNode<T> newNode = new StackNode<T>(data);
//栈中无元素
if (GetLength() == 0)
{
mTopNode = newNode;
}
else
{
newNode.Next = mTopNode;
mTopNode = newNode;
}
mCount++;
}
public T Pop()
{
//栈中无元素
if (GetLength() == 0)
{
Console.WriteLine("栈中无元素,无法出栈。");
return default(T);
}
else
{
T data = mTopNode.Data;
mTopNode = mTopNode.Next;
mCount--;
return data;
}
}
public T Peek()
{
//栈中无元素
if (GetLength() == 0)
{
Console.WriteLine("栈中无元素,无法获取栈顶元素。");
return default(T);
}
else
{
return mTopNode.Data;
}
}
public void Clear()
{
mTopNode = null;
mCount = 0;
}
public void ShowAllElem()
{
StackNode<T> temp = mTopNode;
while (temp != null)
{
Console.WriteLine(temp.Data);
temp = temp.Next;
}
}
}
Program.cs
class Program
{
static void Main(string[] args)
{
IStack<string> linkStack = new LinkStack<string>();
//测试
linkStack.Push("111");
linkStack.Push("222");
linkStack.Push("333");
linkStack.Push("444");
linkStack.Clear();
linkStack.Push("111");
linkStack.Push("222");
linkStack.Push("333");
linkStack.Push("444");
//Console.WriteLine(linkStack.IsEmpty());
//Console.WriteLine(linkStack.GetLength());
//linkStack.Pop();
//Console.WriteLine(linkStack.Peek());
linkStack.ShowAllElem();
Console.ReadKey();
}
}
2、C++实现
栈接口
IStack.cpp
#include <iostream>
using namespace std;
typedef int ElemType;
class IStack
{
public :
///求栈的长度
virtual int GetLength() = 0;
///判断栈是否为空
virtual bool IsEmpty() = 0;
///清空操作
virtual void Clear() = 0;
///入栈操作
virtual void Push(ElemType item) = 0;
///出栈操作
virtual ElemType Pop() = 0;
///取栈顶元素
virtual ElemType Peek() = 0;
///显示所有元素
virtual void ShowAllElem() = 0;
};
链栈
LinkStack.cpp
#include <iostream>
#include "IStack.cpp"
using namespace std;
typedef int ElemType;
typedef struct SNode{
ElemType data;
SNode* next;
}SNode,*StackNode;
class LinkStack : public IStack
{
private :
StackNode topNode;
public :
LinkStack();
~LinkStack();
int GetLength();
bool IsEmpty();
void Clear();
void Push(ElemType item);
ElemType Pop();
ElemType Peek();
void ShowAllElem();
};
LinkStack::LinkStack()
{
topNode = NULL;
}
LinkStack::~LinkStack()
{
//释放所有结点指向的空间
while(topNode != NULL){
StackNode temp = topNode;
topNode = topNode->next;
delete temp;
}
}
int LinkStack::GetLength()
{
StackNode temp = topNode;
int length = 0;
while(temp != NULL){
length++;
temp = temp->next;
}
return length;
}
bool LinkStack::IsEmpty()
{
return GetLength() == 0;
}
void LinkStack::Push(ElemType item)
{
StackNode newNode = new SNode();
if(newNode == NULL){
cout<<"分配失败
";
return ;
}
newNode->data = item;
newNode->next = topNode;
topNode = newNode;
}
ElemType LinkStack::Pop()
{
//栈中无元素
if (GetLength() == 0)
{
cout<<"栈中无元素,无法出栈。";
return 0;
}
ElemType data = topNode->data;
StackNode temp = topNode;
topNode = topNode->next;
delete temp;
return data;
}
ElemType LinkStack::Peek()
{
//栈中无元素
if (GetLength() == 0)
{
cout<<"栈中无元素,无法获取栈顶元素。";
return 0;
}
return topNode->data;
}
void LinkStack::Clear()
{
while(topNode != NULL){
StackNode temp = topNode;
topNode = topNode->next;
delete temp;
}
topNode = NULL;
}
void LinkStack::ShowAllElem()
{
StackNode temp = topNode;
while(topNode != NULL){
cout<<topNode->data<<endl;
topNode = topNode->next;
}
}
main.cpp
#include <iostream>
#include "LinkStack.cpp"
using namespace std;
int main()
{
///链栈
LinkStack linkStack;
//测试
linkStack.Push(111);
linkStack.Push(222);
linkStack.Push(333);
linkStack.Push(444);
linkStack.Clear();
linkStack.Push(111);
linkStack.Push(222);
linkStack.Push(333);
linkStack.Push(444);
//cout<<linkStack.GetLength()<<endl;
//cout<<linkStack.IsEmpty()<<endl;
//cout<<linkStack.Peek()<<endl;
//cout<<linkStack.Peek()<<endl;
linkStack.ShowAllElem();
return 0;
}
3、lua实现
--链栈栈顶结点
topNode = {}
count = 0
--初始化
function Init()
count = 0
end
--销毁
function Destory()
topNode = nil
count = nil
end
--清空
function Clear()
count = 0
end
--是否为空
function IsEmpty()
return count == 0
end
--长度
function GetLength()
return count
end
--入栈
function Push(value)
local node = {}
node.data = value
if(GetLength() == 0) then
node.next = nil
topNode = node
else
node.next = topNode
topNode = node
end
count = count + 1
end
--出栈
function Pop()
local data = topNode.data
topNode = topNode.next
return data
end
--获取栈顶元素
function Peek()
return topNode.data
end
--显示所有元素
function ShowAllElem()
local temp = topNode
while(temp ~= nil) do
print(temp.data)
temp = temp.next
end
end
--主运行函数
function main()
Init()
Push(111)
Push(222)
Push(333)
Push(444)
Clear()
Push(111)
Push(222)
Push(333)
Push(444)
print(Pop())
print(Peek())
print(IsEmpty())
print(GetLength())
ShowAllElem()
end
main()
4、新知识和疑问
4.1 C++中的delete和NULL
delete是把对象new后,系统分配给其的空间给释放掉。释放了之后,这个内存空间就可以被其他对象申请。
但是此对象的值本身是不变的,只是回收了指向的空间。
NULL则是把指针置为空指针,其不再指向任何实际的对象或者函数。
此时其就是一个值为0的常量。
测试
#include <iostream>
using namespace std;
class classA
{
};
int main()
{
classA* test = new classA();
cout<<test<<endl;
//输出test指向的地址
delete test;
cout<<test<<endl;
//输出test指向的地址
test = NULL;
cout<<test<<endl;
//输出0
return 0;
}
4.2 lua中的冒号: 问题
因为我们定义的头结点是每当入栈一个元素时,都会改变指向的。
那么定义函数的时候再使用冒号:加函数名的形式,会报错。
所以把方式改为了直接调用函数形式。