深圳摇号申请网站,DW怎么做电商网站,杭州网站定制,最好的设计师平台网站什么是栈
栈和队列是非常重要的两种数据结构,在软件设计中应用很多。栈和队列也是线性结构,线性表、栈和队列这三种数据结构的数据元素以及数据元素间的逻辑关系完全相同,差别是线性表的操作不受限制,而栈和队列的操作受到眼制。栈的操作只能在表的一端进行,队列的插入操作在表…
什么是栈
栈和队列是非常重要的两种数据结构,在软件设计中应用很多。栈和队列也是线性结构,线性表、栈和队列这三种数据结构的数据元素以及数据元素间的逻辑关系完全相同,差别是线性表的操作不受限制,而栈和队列的操作受到眼制。栈的操作只能在表的一端进行,队列的插入操作在表的一端进行而其它操作在表的另一端进行,所以,把栈和队列称为操作受限的线性表。 BCL中的栈 使用原本有的栈
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 栈
{internal class Program{static void Main(string[] args){Stackchar stack new Stackchar();stack.Push(a);stack.Push(b);stack.Push(c);//栈顶数据Console.WriteLine(push a b c之后的数据个数为 stack.Count);char temp stack.Pop();//去的栈顶的数据并把栈顶的数据删除Console.WriteLine(pop 之后的数据是temp);Console.WriteLine(pop 之后栈中数据的个数stack.Count);char temp2 stack.Peek();//取出栈顶数据不删除Console.WriteLine(peek 之后栈中数据的个数 stack.Count);stack.Clear();Console.WriteLine(clear之后栈中数据的个数 stack.Count);Console.WriteLine(空栈的时候去的栈顶的值为stack.Peek());//出现异常//空栈的时候不要进行peek和pop操作否则会出现异常Console.ReadKey();}}
}
实现顺序栈 自定义顺序栈
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 栈
{internal interface IStackDST{int Count { get; }//get data numberint Getlenth();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 栈
{internal class SeqStackT : IStackDST{private T[] data;
private int top;
public SeqStack(int size){data new T[size];top -1;}public SeqStack():this(10){
}public int Count{get { return top 1; }}
public void Clear(){top -1;}
public int Getlenth(){return Count;}
public bool IsEmpty(){return Count 0;}
public T Peek(){return data[top];}
public T Pop(){T temp data[top];top--;return temp;}
public void Push(T item){data[top1] item;top;}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 栈
{internal class Program{static void Main(string[] args){SeqStackchar stack new SeqStackchar();stack.Push(a);stack.Push(b);stack.Push(c);//栈顶数据Console.WriteLine(push a b c之后的数据个数为 stack.Count);char temp stack.Pop();//去的栈顶的数据并把栈顶的数据删除Console.WriteLine(pop 之后的数据是temp);Console.WriteLine(pop 之后栈中数据的个数stack.Count);char temp2 stack.Peek();//取出栈顶数据不删除Console.WriteLine(peek 之后栈中数据的个数 stack.Count);stack.Clear();Console.WriteLine(clear之后栈中数据的个数 stack.Count);Console.WriteLine(空栈的时候去的栈顶的值为stack.Peek());//出现异常//空栈的时候不要进行peek和pop操作否则会出现异常Console.ReadKey();}}
}
链栈 public class NodeT
{public T Data { get; set; }public NodeT Next { get; set; }
public Node(T data){Data data;Next null;}
}
public class LinkedStackT
{private NodeT top;
public LinkedStack(){top null;}
// 判断栈是否为空public bool IsEmpty(){return top null;}
// 入栈操作public void Push(T data){NodeT newNode new NodeT(data);newNode.Next top;top newNode;}
// 出栈操作public T Pop(){if (IsEmpty()){throw new InvalidOperationException(栈为空无法执行出栈操作。);}T data top.Data;top top.Next;return data;}
// 查看栈顶元素public T Peek(){if (IsEmpty()){throw new InvalidOperationException(栈为空无法查看栈顶元素。);}return top.Data;}
// 清空栈public void Clear(){top null;}
}
// 使用示例
public class Program
{public static void Main(string[] args){LinkedStackint stack new LinkedStackint();
stack.Push(1);stack.Push(2);stack.Push(3);
Console.WriteLine(stack.Pop()); // 输出 3Console.WriteLine(stack.Peek()); // 输出 2
stack.Push(4);Console.WriteLine(stack.Pop()); // 输出 4
while (!stack.IsEmpty()){Console.WriteLine(stack.Pop());}}
} 什么是队列
队列Queue是一种遵循先进先出FIFOFirst In First Out原则的抽象数据类型。在队列中元素的添加入队总是在一端进行称为队尾Rear而元素的移除出队总是在另一端进行称为队首Front。
顺序队列 using System;
public class SequentialQueueT
{private T[] queue;private int head;private int tail;private int count;private int capacity;
public SequentialQueue(int capacity){this.capacity capacity;queue new T[capacity];head 0;tail 0;count 0;}
// 判断队列是否为空public bool IsEmpty(){return count 0;}
// 判断队列是否已满public bool IsFull(){return count capacity;}
// 入队操作public void Enqueue(T item){if (IsFull()){throw new InvalidOperationException(队列已满无法入队。);}queue[tail] item;tail (tail 1) % capacity;count;}
// 出队操作public T Dequeue(){if (IsEmpty()){throw new InvalidOperationException(队列为空无法出队。);}T item queue[head];queue[head] default(T); // 清除队首元素head (head 1) % capacity;count--;return item;}
// 查看队首元素public T Peek(){if (IsEmpty()){throw new InvalidOperationException(队列为空无法查看队首元素。);}return queue[head];}
// 获取队列的当前元素数量public int Count(){return count;}
}
// 使用示例
public class Program
{public static void Main(string[] args){SequentialQueueint queue new SequentialQueueint(5);
queue.Enqueue(1);queue.Enqueue(2);queue.Enqueue(3);
Console.WriteLine(queue.Dequeue()); // 输出 1Console.WriteLine(queue.Peek()); // 输出 2
queue.Enqueue(4);queue.Enqueue(5);queue.Enqueue(6);
while (!queue.IsEmpty()){Console.WriteLine(queue.Dequeue());}}
}
链队列