本帖最后由 鱼塘是我的 于 2019-6-10 14:19 编辑
[md]- 今日作业:
- 栈是一种___的结构?
- 队列是一种___的结构?
- 顺序队列 与 链表队列的区别?
- 顺序队列优点是访问速度快,缺点是插入、删除耗时
- 链表队列是节点的集合,优点是插入、删除快,访问速度不及循序存储,因为访问每一个成员都需要一个节点一个节点······
- 请实现一个链表栈(泛型)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Say {
class Program {
static void Main (string[] args) {
LinkStark<char> stack = new LinkStark<char> ();
stack.Push ('a');
stack.Push ('b');
stack.Push ('c');
Console.WriteLine ("push后的数据个数" + stack.Count);
char temp = stack.Pop ();
Console.WriteLine ("pop 之后数据是:" + temp);
Console.WriteLine ("pop 之后数据个数" + stack.Count);
char temp2 = stack.Peek ();
Console.WriteLine ("Peek 之后数据是:" + temp2);
Console.WriteLine ("Peek 之后数据个数" + stack.Count);
}
}
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> index) {
this.data = data;
this.next = index;
}
public Node (Node<T> index) {
data = default (T);
next = index;
}
public T Data {
get { return data; }
set { data = value; }
}
public Node<T> Next {
get { return next; }
set { next = value; }
}
}
class LinkStark<T> {
private Node<T> top;
private int count = 0;
public int Count {
get {
return count;
}
}
public void Clear () {
count = 0;
top = null;
}
public int GetLength () {
return Count;
}
public bool IsEmpty () {
return count == 0;
}
public T Peek () {
return top.Data;
}
public T Pop () {
T data = top.Data;
top = top.Next;
count--;
return data;
}
public void Push (T item) {
Node<T> newNode = new Node<T> (item);
newNode.Next = top;
top = newNode;
count++;
}
internal interface IStackDS<T> { }
}
}
|