using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using System.IO;
namespace 栈和队列
{
class Program
{
static void Main(string[] args)
{
}
#region 栈
// 栈和队列是两种面向表的数据结构
// 栈中的数据只能在表的某一端进行增加和删除操作,
// 被广泛用于从表达式计算到处理函数调用的任何编程语言的实现中,
// 队列中的数据则在表的一端进行增加操作,在表的令一端进行删除操作,
// 用于区分优先次序的操作系统处理以及模拟现实世界的事件方面
//c#语言使用这些数据结构的2个类:stack类和queue类
//栈定义为数据项的列表,而且这些数据项只能从表的末端进行存取访问,可存取访问的这端被称为 栈顶
//栈的标准模型是自助餐厅的盘子堆。人们始终从顶部拿走盘子,而且当洗完工或者杂工把盘子放回盘子堆的时候也是把它放在盘堆的顶部。
//栈是著名的 后进先出(LIFO) 的数据结构
#region 1,栈的操作
//最基本的两种操作:进栈Push 和 出栈Pop
//栈的另外一种基本操作就是察看栈顶的数据项,Pop会返回栈顶的数据项,但是此操作会把数据项从栈中移除。
//如果只是希望察看栈顶的操作而不是真的移除它,那么在c#语言中有一种名为取数(Peek)的操作可以实现。当然此操作在其他语言实现中可以采用其他名称(比如Top)
#endregion
#region 2,Stack类的实现
class CStack
{
private int p_index;
private ArrayList list;
public CStack()
{
list = new ArrayList();
p_index = -1;
}
public int count
{
get { return list.Count; }
}
public void push(object item)
{
list.Add(item);
p_index++;
}
public object pop()
{
object obj = list[p_index];
list.RemoveAt(p_index);
p_index--;
return obj;
}
public void clear()
{
list.Clear();
p_index = -1;
}
public object peek()
{
return list[p_index];
}
}
#endregion
#region 3,stack类
//a,实例化:
Stack myStack = new Stack();
Stack<string> myStack = new Stack<string>();
//创建来自另一个群集的栈对象:
string[] name = new string[] { "James", "Tom", "Jason" };
Stack myStack = new Stack(name); //执行pop方法首先会把Jason从栈中移除;
//也可以初始化时给栈规定 指定的容量
Stack myStack = new Stack(50);
//b,stack类实例
//实现进制转换的方法n为10进制数,b为要转换的进制基数
static void MulBase(int n, int b)
{
Stack Digits = new Stack();
do
{
Digits.Push(n % b);
n = n / b;
} while (n != 0);
}
#endregion
#endregion
#region 队列
#region 1,队列操作
/*队列包含两种主要操作:增加和删除;
*增加新数据项的操作成为Enqueue,而从队列中移除数据项的操作则称为Dequeue
*Enqueue会在队列的末尾增加一个数据项,而Dequeue会从队列的前端移动一个数据项。
*队列的另一个操作就是察看初始数据项,。和stack类中的对应操作一样。
*peek方法用来察看初始的数据项。这种方法仅仅返回数据项。而不会从数据项中移除
*/
#endregion
#region 2,Queue类的实现
public class CQueue
{
private ArrayList pqueue;
public CQueue()
{
pqueue=new ArrayList();
}
public void EnQueue(object item)
{
pqueue.Add(item);
}
public void DeQueue(object item)
{
pqueue.RemoveAt(item);
}
public void Peek()
{
return pqueue[0];
}
public void ClearQueue()
{
pqueue.Clear();
}
public int Count()
{
return pqueue.Count;
}
}
#endregion
#region 3,Queue类
//a,实例化
Queue myQueue = new Queue();
Queue myQueue = new Queue(23,3);
//泛型Queue的初始化如下所示
Queue<int> numbers = new Queue<int>();
//队列经常用模拟人们排队的情况
//b,Queue类实例
public struct Dancer
{
public string name;
public string sex;
public void Getname(string n)
{
name = n;
}
public override string ToString()
{
return name;
}
}
class class1
{
static void newDancers(Queue male,Queue female)
{
Dancer m, w;
m = new Dancer();
w = new Dancer();
if (male.Count > 0 && female.Count > 0)
{
m.Getname(male.Dequeue().ToString());
w.Getname(female.Dequeue().ToString());
}
else if (male.Count > 0 && female.Count == 0)
Console.WriteLine("Waiting on a female Dancer");
else if (male.Count == 0 && female.Count > 0)
Console.WriteLine("Waiting on a male Dancer");
}
static void headOfLine(Queue male, Queue female)
{
Dancer w, m;
m = new Dancer();
w = new Dancer();
if (male.Count > 0)
m.Getname(male.Peek().ToString());
if (female.Count > 0)
w.Getname(female.Peek().ToString());
if (m.name != "" && w.name != "")
Console.WriteLine("Next in line are :" + m.name + "\t" + w.name);
else if (m.name != "")
Console.WriteLine("Next in line is:" + m.name);
else
Console.WriteLine("Next in line is:"+w.name);
}
static void startDancing(Queue male, Queue female)
{
Dancer m, w;
m = new Dancer();
w = new Dancer();
Console.WriteLine("Dancer partners are:");
Console.WriteLine();
for (int count = 0; count <= 3; count++)
{
m.Getname(male.Dequeue().ToString());
w.Getname(female.Dequeue().ToString());
Console.WriteLine(w.name+"\t"+m.name);
}
}
static void formLines(Queue male, Queue female)
{
Dancer d=new Dancer();
StreamReader inFile;
inFile = File.OpenText(@"c:\dancers.dat");
string line;
while (inFile.Peek() != -1)
{
line = inFile.ReadLine();
d.sex = line.Substring(0, 1);
d.name = line.Substring(2,line.Length-2);
if (d.sex == "M")
male.Enqueue(d);
else
female.Enqueue(d);
}
}
}
#endregion
#region 4,用队列进行排序
/*基数排序:通过对一组数据进行两遍排序来操作的,在这种情况下,证书的取值范围是0-99
* 第一遍时基于各位上的数字来进行排序,而第二遍则是基于十位上的数字进行排序。
* 然后,根据这些位置上的数字来把每一个数放置在一个箱子内。
* 假设有下列这些数
* 91,46,85,15,92,35,31,22
* 在箱子结构上第一遍排序的结果是
* 0号:
* 1号:91,31
* 2号:92,22
* 3号:
* 4号:
* 5号:85,15,35
* 6号:46
* 7号:
* 8号:
* 9号:
* 现在,把这些数按照他们在箱子中的位置排列起来
* 91,31,92,22,85,15,35,46
* 接下来,取上述列表,按照十位上的数组对这些书排序后放入适当的位置
* 0号:
* 1号:15
* 2号:22
* 3号:31.35
* 4号:46
* 5号:
* 6号:
* 7号:
* 8号:85
* 9号:91.92
* 把这些数从箱子中取出来,并且把他们放回到列表内
* 15.22.31.35.46.85.91.92
* 用队列来表示这些箱子就可以实现这个算法。
*
*/
class class1
{
enum DigitType { ones=1,tens=10}
static void DisplayArray(int[] n)
{
for (int x = 0; x <= n.GetUpperBound(0); x++)
{
Console.Write(n[x] + " ");
}
}
static void RSort(Queue que, int[] n, DigitType digit)
{
int snum;
for (int x = 0; x < n.GetUpperBound(0); x++)
{
if (digit == DigitType.ones)
{
snum = n[x] % 10;
}
else
snum = n[x] / 10;
que.Enqueue(n[x]);
}
}
static void BuildArray(Queue que, int[] n)
{
int y = 0;
for(int x=0;x>=9;x++)
while (que.Count > 0)
{
n[y] = Int32.Parse(que.Dequeue().ToString());
y++;
}
}
static void Main()
{
Queue[] numQueue = new Queue[10];
int[] nums = new int[] { 91, 46, 85, 15, 92, 35, 31, 22 };
for (int i = 0; i < 10; i++)
{
numQueue = new Queue();
}
RSort(numQueue,nums,DigitType.ones);
BuildArray(numQueue,nums);
Console.WriteLine();
Console.WriteLine("First pass results:");
DisplayArray(nums);
Console.WriteLine();
Console.WriteLine("Second pass results:");
RSort(numQueue,nums,DigitType.tens);
BuildArray(numQueue,nums);
}
}
/*
* 源自queue类的优先队列
* 队列是一种先进先出的数据结构;
*/
#endregion
#endregion
}
}