数据结构(栈)
来源:互联网 发布:华为交换机 允许mac表 编辑:程序博客网 时间:2024/06/05 22:58
栈和队列也是线性结构,线性表,栈和队列这三种数据结构的数据元素,以及数据元素之间的关系完全相同,差别是线性表的操作不受限制,而栈和队列的操作受到限制。栈的操作只能在表的一端进行,队列的插入操作在表的一端而其他操作在表的另一端记性,所以把栈和队列称为操作受限制的线性表。
栈的定义:栈是操作限定在尾端的线性表。表尾由于要进行插入,删除等操作,所以它具有特殊的含义,把表尾称为栈顶,另一端是固定的,称为栈底。栈中没有数据时称为空栈。
栈的接口定义:
interface IStackDS<T> { int GetLength();//求栈的长度 bool IsEmpty();//判空 void Clear();//清空栈 void Push(T item);//入栈 T Pop();//出栈 T GetTop();//取占顶元素 // T this[int index] { get; set; }所以其就相当于一个属性 }
顺序栈:
class SeqStack<T> : IStackDS<T> { private T[] data; private int top = -1;//栈顶元素索引 public SeqStack(int size) { data = new T[size]; } public SeqStack() : this(10) { } public int GetLength() { return top + 1; } public bool IsEmpty() { return top == -1; } public void Clear() { top = -1; } public void Push(T item) { if (top >= data.Length) { throw new Exception("栈已满,无法继续输入!"); } else { top++; data[top] = item; } } public T Pop() { if (top == -1) { throw new Exception("没有元素了!"); } else { T temp = data[top]; top--; return temp; } } public T GetTop() { if (top == -1) { throw new Exception("没有元素了!"); } else { return data[top]; } } }
链栈:
class LinkStack<T> : IStackDS<T> { private Node<T> top; public int GetLength() { int count = 0; Node<T> temp = top; while (temp != null) { count++; temp = temp.Next; } return count; } public bool IsEmpty() { return top == null; } public void Clear() { top = null; } public void Push(T item) { Node<T> temp = new Node<T>(item); temp.Next = top; top = temp; } public T Pop() { if (top == null) { throw new Exception("栈没有元素了!"); } else { T data = top.Data; top = top.Next; return data; } } public T GetTop() { if (top == null) { throw new Exception("栈没有元素了!"); } else { return top.Data; } } }
**链栈需要定义结点类 class Node
{
private T data;
private Node next;//用来指向下一个元素
//多种构造方法 public Node(T data, Node<T> next) { this.data = data; this.next = next; } public Node(T data) { this.data = data; } public Node(Node<T> next) { this.next = next; } public Node() { data = default(T);//获取该类型的默认值 next = null; } public T Data { get { return data; } set { data = value; } } public Node<T> Next { get { return next; } set { next = value; } } }试代码**:
// SeqStack<string> myStr = new SeqStack<string>(); LinkStack<string> myStr = new LinkStack<string>(); myStr.Push("I"); myStr.Push("am"); myStr.Push("a"); myStr.Push("good"); myStr.Push("boy"); Console.WriteLine(myStr.GetTop()); Console.WriteLine(myStr.Pop()); Console.WriteLine(myStr.GetTop()); Console.WriteLine(myStr.GetLength()); Console.WriteLine(myStr.IsEmpty()); myStr.Clear(); Console.WriteLine(myStr.IsEmpty()); Console.ReadKey();
测试结果:
阅读全文
0 0
- 数据结构(七) --- 栈
- 数据结构--栈(基础知识)
- 数据结构(四):栈
- (转载)数据结构-栈
- 数据结构-栈(1)
- 数据结构-栈(2)
- 数据结构-栈(3)
- 数据结构 (3.3) 栈
- 数据结构--栈(java)
- 数据结构---栈(C++)
- 栈(stack )数据结构
- 数据结构:(三)栈
- Java数据结构(栈)
- 数据结构(Java)---栈
- 数据结构-栈(1)
- 数据结构(栈)
- 数据结构-栈(线性栈)
- 数据结构 栈实现(练手题)传说中的数据结构
- 第12周项目2- Kruskal算法的验证
- ScrollView嵌套webview之后滑动冲突的解决方案
- 打印输入中单词长度的水平直方图
- 用java实现把sql语句查询的结果集写到dbf文件中
- coursera视频无法播放的解决方法
- 数据结构(栈)
- Android中TabLayout修改字体大小
- 第九周项目三
- 数据结构上机实践第11周项目1
- 第10周项目3- 中序线索化二叉树的算法验证
- Linux command
- 自己看别人看了没用-多组调图
- 第十周项目二 哈夫曼树
- Java Arraylist实现原理.