C#简单实现顺序栈与链式栈
2018-02-26 21:01
253 查看
其实就是顺序表与链表的简化版本
接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn_03
{
interface IStackDS<T>
{
int Count { get; }
int GetLength();
bool IsEmpty();
void Clear();
void Push(T item);
T Pop();
T Peek();
}
}
顺序栈:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn_03
{
class SeqStack<T>:IStackDS<T>
{
private T[] data;
private int top;
public SeqStack(int size)
{
data = new T[size];
top = -1;
}
public SeqStack()
: this(10) { } //默认长度10
public int Count
{
get { return top + 1; }
}
public int GetLength()
{
return Count;
}
public bool IsEmpty()
{
return Count == 0;
}
public void Clear()
{
top = -1;
}
public void Push(T item)
{
data[top + 1] = item;
top++;
}
public T Pop()
{
T temp = data[top];
top--;
return temp;
}
public T Peek()
{
return data[top];
}
}
}
链栈:
节点类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn_03
{
class Node<T>
{
private T data;
private Node<T> next;
public Node()
{
data = default(T);
next = null;
}
public Node(T data)
{
this.data = data;
next = null;
}
public Node(T data, Node<T> next)
{
this.data = data;
this.next = next;
}
public Node(Node<T> next)
{
data = default(T);
this.next = next;
}
public T Data
{
get { return data; }
set { data = value; }
}
public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
}
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn_03
{
class LinkStack<T>:IStackDS<T>
{
private Node<T> top;
private int count = 0;
public int Count
{
get { return count; }
}
public int GetLength()
{
return count;
}
public bool IsEmpty()
{
return count == 0;
}
public void Clear()
{
count = 0;
top = null;
}
public void Push(T item)
{
Node<T> newNode = new Node<T>(item);
newNode.Next = top;
top = newNode;
count++;
}
public T Pop()
{
T data = top.Data;
top = top.Next;
return data;
count--;
}
public T Peek()
{
return top.Data;
}
}
}
接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn_03
{
interface IStackDS<T>
{
int Count { get; }
int GetLength();
bool IsEmpty();
void Clear();
void Push(T item);
T Pop();
T Peek();
}
}
顺序栈:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn_03
{
class SeqStack<T>:IStackDS<T>
{
private T[] data;
private int top;
public SeqStack(int size)
{
data = new T[size];
top = -1;
}
public SeqStack()
: this(10) { } //默认长度10
public int Count
{
get { return top + 1; }
}
public int GetLength()
{
return Count;
}
public bool IsEmpty()
{
return Count == 0;
}
public void Clear()
{
top = -1;
}
public void Push(T item)
{
data[top + 1] = item;
top++;
}
public T Pop()
{
T temp = data[top];
top--;
return temp;
}
public T Peek()
{
return data[top];
}
}
}
链栈:
节点类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn_03
{
class Node<T>
{
private T data;
private Node<T> next;
public Node()
{
data = default(T);
next = null;
}
public Node(T data)
{
this.data = data;
next = null;
}
public Node(T data, Node<T> next)
{
this.data = data;
this.next = next;
}
public Node(Node<T> next)
{
data = default(T);
this.next = next;
}
public T Data
{
get { return data; }
set { data = value; }
}
public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
}
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn_03
{
class LinkStack<T>:IStackDS<T>
{
private Node<T> top;
private int count = 0;
public int Count
{
get { return count; }
}
public int GetLength()
{
return count;
}
public bool IsEmpty()
{
return count == 0;
}
public void Clear()
{
count = 0;
top = null;
}
public void Push(T item)
{
Node<T> newNode = new Node<T>(item);
newNode.Next = top;
top = newNode;
count++;
}
public T Pop()
{
T data = top.Data;
top = top.Next;
return data;
count--;
}
public T Peek()
{
return top.Data;
}
}
}
相关文章推荐
- C#简单实现顺序队列与链队列
- 数据结构--链式栈、顺序栈的基本实现与简单应用:进制转换
- 简单的自定义实现Stack模板(顺序栈以及链式栈没有迭代器和销毁)
- 【顺序表】用c语言简单实现顺序表
- 设计模式C#实现(九)——工厂方法模式和简单工厂
- C#实现Socket传输简单数据
- 用C#实现实现简单的 Ping 的功能,用于测试网络是否已经联通
- C# 简单反射实现winform左侧树形导航,右侧切换内容
- C#之使用AutoResetEvent实现线程的顺序执行
- C#实现简单屏幕监控的方法
- 用C#实现实现简单的 Ping 的功能,用于测试网络是否已经联通
- C#内容分页简单实现代码及祥解
- C#实现的简单链表类实例
- 动态顺序表的简单实现
- Unity3D 利用C#实现简单的代理模式Delegate(委托)
- [数据结构]C#基于数组实现泛型顺序表
- xmlHTTP xmlDOC 与 C#中DataSet的结合 实现AJAX简单示例
- C#简单实现动态数据生成Word文档并保存
- C#中TextBox水印提示的简单实现
- 用C#实现实现简单的 Ping 的功能,用于测试网络是否已经联通