用栈实现队列
最近补习了一些简单的算法,从头开始学习了数据结构。
有被人问到一个有意思的题目。就是提供一个有Count属性,Pop 和 Push 方法的Stack类,如何用它完成一个Queue的结构。
第一次写的时候并不够完美,后来修改成下面的代码并记录下来,希望有大侠能帮忙指出其中不足之处。
欢迎大家前来讨论。
代码
using System;
namespace TestQueueWithStack
{
public class TestStack<T>
{
private int _count; //栈的总容量
private T[] _data; //数组,用于存储栈中的数据元素
private int point; //栈的指针
//栈的总容量
public int Count
{
get { return _count; }
}
//已经装入的个数
public int Length
{
get
{
return point + 1;
}
}
//构造函数
public TestStack(int count)
{
_data = new T[count];
_count = count;
point = -1;
}
//入栈 Push
public void Push(T item)
{
if (point.Equals(_count - 1))
{
Console.WriteLine("Stack is full");
return;
}
_data[++point] = item;
}
//出栈
public T Pop()
{
T temp = default(T);
if (point.Equals(-1))
{
Console.WriteLine("Stack is empty");
return temp; // 超出报错
}
temp = _data[point];
--point;
return temp;
}
}
/// <summary>
/// Queue
/// </summary>
/// <typeparam name="T"></typeparam>
public class TestQueue<T>
{
private TestStack<T> _data; //主数组,用于存储栈中的数据元素
private TestStack<T> _aideData; //辅助数组,用于暂时存储栈中的数据
public int Length
{
get
{
return _data.Length+_aideData.Length;
}
}
//构造函数
public TestQueue(int count)
{
_data = new TestStack<T>(count);
_aideData = new TestStack<T>(count);
}
//入队列 Push
public void Push(T item)
{
if (Length < _data.Count)
{
_data.Push(item);
}
else
{
Console.WriteLine("Queue is full");
}
}
//出队列 Pop
public T Pop()
{
if (_aideData.Length == 0)//如果辅助存贮数据的Data为空
{
if(_data.Length==0)
{
T temp = default(T);
Console.WriteLine("Queue is empty");
return temp;
}
else
{
while (_data.Length > 0)
{
_aideData.Push(_data.Pop());
}
return _aideData.Pop();
}
}
else
{
return _aideData.Pop();
}
}
}
class Program
{
static void Main(string[] args)
{
/*------------------ 为了说明 TestStack 的Push 和 Pop 方法 -----------------------*/
//TestStack<int> stack = new TestStack<int>(10);
//int i = 0;
//while (i++ < 10)
//{
// stack.Push(i);
//}
//while (stack.Length.CompareTo(0) > 0)
//{
// Console.WriteLine(stack.Pop());
//}
//Console.Read();
/*------------------------------------- End ------------------------------------*/
TestQueue<int> queue = new TestQueue<int>(10);
queue.Pop();//空
int i = 0;
while (i++ < 11)//故意多放入一个
{
queue.Push(i);
}
//只取出其中一个 理论为 1 2
Console.WriteLine(queue.Pop());
Console.WriteLine(queue.Pop());
//压入数字 11 12
queue.Push(11);
queue.Push(12);
while (queue.Length.CompareTo(0) > 0)
{
Console.WriteLine(queue.Pop());
}
Console.Read();
}
}
}
namespace TestQueueWithStack
{
public class TestStack<T>
{
private int _count; //栈的总容量
private T[] _data; //数组,用于存储栈中的数据元素
private int point; //栈的指针
//栈的总容量
public int Count
{
get { return _count; }
}
//已经装入的个数
public int Length
{
get
{
return point + 1;
}
}
//构造函数
public TestStack(int count)
{
_data = new T[count];
_count = count;
point = -1;
}
//入栈 Push
public void Push(T item)
{
if (point.Equals(_count - 1))
{
Console.WriteLine("Stack is full");
return;
}
_data[++point] = item;
}
//出栈
public T Pop()
{
T temp = default(T);
if (point.Equals(-1))
{
Console.WriteLine("Stack is empty");
return temp; // 超出报错
}
temp = _data[point];
--point;
return temp;
}
}
/// <summary>
/// Queue
/// </summary>
/// <typeparam name="T"></typeparam>
public class TestQueue<T>
{
private TestStack<T> _data; //主数组,用于存储栈中的数据元素
private TestStack<T> _aideData; //辅助数组,用于暂时存储栈中的数据
public int Length
{
get
{
return _data.Length+_aideData.Length;
}
}
//构造函数
public TestQueue(int count)
{
_data = new TestStack<T>(count);
_aideData = new TestStack<T>(count);
}
//入队列 Push
public void Push(T item)
{
if (Length < _data.Count)
{
_data.Push(item);
}
else
{
Console.WriteLine("Queue is full");
}
}
//出队列 Pop
public T Pop()
{
if (_aideData.Length == 0)//如果辅助存贮数据的Data为空
{
if(_data.Length==0)
{
T temp = default(T);
Console.WriteLine("Queue is empty");
return temp;
}
else
{
while (_data.Length > 0)
{
_aideData.Push(_data.Pop());
}
return _aideData.Pop();
}
}
else
{
return _aideData.Pop();
}
}
}
class Program
{
static void Main(string[] args)
{
/*------------------ 为了说明 TestStack 的Push 和 Pop 方法 -----------------------*/
//TestStack<int> stack = new TestStack<int>(10);
//int i = 0;
//while (i++ < 10)
//{
// stack.Push(i);
//}
//while (stack.Length.CompareTo(0) > 0)
//{
// Console.WriteLine(stack.Pop());
//}
//Console.Read();
/*------------------------------------- End ------------------------------------*/
TestQueue<int> queue = new TestQueue<int>(10);
queue.Pop();//空
int i = 0;
while (i++ < 11)//故意多放入一个
{
queue.Push(i);
}
//只取出其中一个 理论为 1 2
Console.WriteLine(queue.Pop());
Console.WriteLine(queue.Pop());
//压入数字 11 12
queue.Push(11);
queue.Push(12);
while (queue.Length.CompareTo(0) > 0)
{
Console.WriteLine(queue.Pop());
}
Console.Read();
}
}
}
另外还有一个问题,想遗留在这,看看大家是否有完美的解决方案
从一个有N个数字的数组中取出 i个最大或最小的数字,唯一条件 N 远大于 i


浙公网安备 33010602011771号