public void PushAndPop() { var students = new Student[] { new Student { No = 1, Name = "李1", Score = 22.1 }, new Student { No = 2, Name = "李2", Score = 32.3 }, new Student { No = 3, Name = "李3", Score = 42.5 } }; var linkStack = new LinkStack <Student>(); foreach (var student in students) { linkStack.Push(new Node <Student>(student)); } var popStudent = new Node <Student>(); while (linkStack.Pop(out popStudent)) { } }
public static void Creat(LinkStack <int>[] pillar, int n) { for (int a = 0; a < 3; a++) { pillar[a] = new LinkStack <int>(); } pillar[0].Name = 'A'; //把所有的圆盘按从大到小的顺序放在柱子A上 for (int i = 0; i < n; i++) { pillar[0].Push(n - i); } //柱子B,C上开始没有没有圆盘 for (int i = 0; i < n; i++) { pillar[1].Push(0); pillar[2].Push(0); } //若n为偶数,按顺时针方向依次摆放 A B C if (n % 2 == 0) { pillar[1].Name = 'B'; pillar[2].Name = 'C'; } else //若n为奇数,按顺时针方向依次摆放 A C B { pillar[1].Name = 'C'; pillar[2].Name = 'B'; } }
static void Main(string[] args) { //1.使用BCL中的Stack<T> //Stack<char> stack = new Stack<char>(); //2.使用自己的顺序栈 //IStackDS<char> stack = new SeqStack<char>(30); //3.使用自己的链栈 IStackDS<char> stack = new LinkStack<char>(); stack.Push('a'); stack.Push('b'); stack.Push('c'); Console.WriteLine(stack.Count); char temp = stack.Pop(); //取得栈顶数据 并把栈顶的数据删除 Console.WriteLine(temp); Console.WriteLine(stack.Count); char temp2 = stack.Peek(); //取得栈顶的数据 不删除 Console.WriteLine(temp2); Console.WriteLine(stack.Count); stack.Clear(); Console.WriteLine(stack.Count); Console.ReadKey(); }
bool Railroad(int[] p, int n, int k) { LinkStack <int>[] H; H = new LinkStack <int> [k + 1]; for (int i = 1; i <= k; i++) { H[i] = new LinkStack <int>(); } int NowOut = 1; int minH = n + 1; int minS = 0; for (int i = 0; i < n; i++) { if (p[i] == NowOut) { Console.WriteLine("Move Car {0} from input to output", p[i]); NowOut++; while (minH == NowOut) { Output(ref minH, ref minS, ref H, k, n); NowOut++; } } else { if (!Hold(p[i], ref minH, ref minS, ref H, k, n)) { return(false); } } } return(true); }
public void Move() { var x = 1; var y = 1; var linkStack = new LinkStack <Step>(); maze[1, 1] = 2; while (x <= exitX && y <= exitY) { if (x == exitX && y == exitY) { return; } if (maze[x - 1, y] == 0) { x -= 1; linkStack.Push(new Node <Step>(new Step { X = x, Y = y })); } else if (maze[x + 1, y] == 0) { x += 1; linkStack.Push(new Node <Step>(new Step { X = x, Y = y })); } else if (maze[x, y - 1] == 0) { y -= 1; linkStack.Push(new Node <Step>(new Step { X = x, Y = y })); } else if (maze[x, y + 1] == 0) { y += 1; linkStack.Push(new Node <Step>(new Step { X = x, Y = y })); } else { var stepNode = new Node <Step>(); linkStack.Pop(out stepNode); if (x == stepNode.Data.X && y == stepNode.Data.Y) { linkStack.Pop(out stepNode); } x = stepNode.Data.X; y = stepNode.Data.Y; } maze[x, y] = 2; } }
public void Push(string e) { LinkStack p = new LinkStack(); p.data = e; p.next = head.next; head.next = p; }
public static void StackPrint(LinkStack <string> lis) { foreach (var item in lis) { Console.Write(item + "-"); } Console.WriteLine(); }
/// <summary> /// Clears all of the renderer's layout parameter stacks. /// </summary> public void ClearLayoutStacks() { StyleStack.Clear(); FontStack.Clear(); ColorStack.Clear(); GlyphShaderStack.Clear(); LinkStack.Clear(); }
public void PopLink() { if (LinkStack.Count == 0) { return; } LinkStack.Pop(); }
/// <summary> ///LinkStack`1 构造函数 的测试 ///</summary> public void LinkStackConstructorTestHelper <T>() { LinkStack <T> target = new LinkStack <T>(); Assert.IsNotNull(target); Assert.IsNull(target.Top); Assert.AreEqual(0, target.Count); }
/// <summary> ///Clear 的测试 ///</summary> public void ClearTestHelper <T>() { LinkStack <T> target = new LinkStack <T>(); // TODO: 初始化为适当的值 target.Clear(); Assert.AreEqual(0, target.Count); Assert.IsNull(target.Top); }
// //编写测试时,还可使用以下属性: // //使用 ClassInitialize 在运行类中的第一个测试前先运行代码 //[ClassInitialize()] //public static void MyClassInitialize(TestContext testContext) //{ //} // //使用 ClassCleanup 在运行完类中的所有测试后再运行代码 //[ClassCleanup()] //public static void MyClassCleanup() //{ //} // //使用 TestInitialize 在运行每个测试前先运行代码 //[TestInitialize()] //public void MyTestInitialize() //{ //} // //使用 TestCleanup 在运行完每个测试后运行代码 //[TestCleanup()] //public void MyTestCleanup() //{ //} // #endregion /// <summary> ///Push 的测试 ///</summary> public void PushTestHelper <T>() { LinkStack <T> target = new LinkStack <T>(); // TODO: 初始化为适当的值 T p = default(T); // TODO: 初始化为适当的值 target.Push(p); Assert.AreEqual(1, target.Count); Assert.AreEqual(p, target.Pop()); }
/// <summary> ///GetPop 的测试 ///</summary> public void GetPopTestHelper <T>() { LinkStack <T> target = new LinkStack <T>(); // TODO: 初始化为适当的值 T expected = default(T); // TODO: 初始化为适当的值 T actual; target.Push(expected); actual = target.GetPop(); Assert.AreEqual(expected, actual); Assert.AreEqual <int>(1, target.Count); }
/// <summary> ///IsEmpty 的测试 ///</summary> public void IsEmptyTestHelper <T>() { LinkStack <T> target = new LinkStack <T>(); // TODO: 初始化为适当的值 bool expected = true; // TODO: 初始化为适当的值 bool actual; actual = target.IsEmpty(); Assert.AreEqual(expected, actual); target.Push(default(T)); Assert.IsFalse(target.IsEmpty()); }
static void Main(string[] args) { IStack<string> stack = new LinkStack<string>(); stack.Push("a1"); stack.Push("a2"); stack.Push("a3"); while (stack.IsEmpty() == false) { Console.WriteLine(stack.StackTop); stack.Pop(); } }
/// <summary> ///GetLength 的测试 ///</summary> public void GetLengthTestHelper <T>() { LinkStack <T> target = new LinkStack <T>(); // TODO: 初始化为适当的值 int expected = 0; // TODO: 初始化为适当的值 int actual; actual = target.GetLength(); Assert.AreEqual(expected, actual); target.Push(default(T)); target.Push(default(T)); Assert.AreEqual <int>(2, target.GetLength()); }
public void ConversionOct(int n) //【例3-1】数制转换问题 { LinkStack <int> s = new LinkStack <int>(); while (n > 0) { s.Push(n % 8); n = n / 8; } while (!s.IsEmpty()) { n = s.Pop(); Console.WriteLine("{0}", n); } } //【例3-1】数制转换问题 //8进制转换//public void Conversion(int n)
static void Main(string[] args) { //非递归解法 int n; //这里的n表示圆盘的个数, Console.WriteLine("请输入盘子数:"); n = Convert.ToInt32(Console.ReadLine()); LinkStack <int>[] H; H = new LinkStack <int> [3]; HannoiNotUseRecursion.Creat(H, n); //给结构数组设置初值 long max = HannoiNotUseRecursion.Pow(2, n) - 1; //动的次数应等于2^n - 1 HannoiNotUseRecursion.Hannoi(H, max); //移动汉诺塔的主要函数 Console.Read(); }
void Start() { LinkStack <int> seqStack = new LinkStack <int>(); for (int i = 0; i < 3; i++) { seqStack.Push(i); } print("展示栈顶元素:" + seqStack.Peek()); print("栈数量:" + seqStack.GetLength()); for (int i = 0; i < 3; i++) { print(seqStack.Pop()); } }
public void PopStringTest() { LinkStack <string> target = new LinkStack <string>(); // TODO: 初始化为适当的值 target.Push("A"); target.Push("B"); target.Push("C"); Assert.AreEqual <string>("C", target.Pop()); Assert.AreEqual <string>("B", target.Pop()); Assert.AreEqual <string>("A", target.Pop()); Assert.AreEqual(0, target.Count); }
static void Main(string[] args) { //1.使用BCL中的Stack<T> IStackDS<Char> STACK = new LinkStack<char>(); STACK.Push('a'); STACK.Push('b'); STACK.Push('c'); Console.WriteLine(STACK.Count); char temp = STACK.pop();//取得栈顶数据,并把该数据删除 Console.WriteLine(temp); Console.WriteLine(STACK.Count); char temp2 = STACK.peek(); Console.WriteLine(temp2); Console.ReadKey(); }
/// <summary> /// 二进制转换 /// </summary> /// <param name="number">被转换的10进制数</param> static void ConvertTo(long number) { LinkStack <long> myLinkStack = new LinkStack <long>(); Console.WriteLine("input number:{0}:", number); while (number > 0) { myLinkStack.Push(number % 2); number = number / 2; } Console.Write("Result:"); while (!myLinkStack.IsEmpty()) { Console.Write(myLinkStack.Pop()); } }
public void Test() { LinkStack <int> stack = new LinkStack <int>(); stack.Push(1); stack.Push(2); stack.Push(3); Assert.Equal(3, stack.Pop()); Assert.Equal(2, stack.Pop()); Assert.Equal(1, stack.Pop()); stack.Push(1); stack.Push(2); Assert.Equal(2, stack.Pop()); Assert.Equal(1, stack.Pop()); stack.Push(3); Assert.Equal(3, stack.Pop()); }
public string DispLinkStack() { int i; string str = ""; if (StackEmpty()) { str = ""; } else { LinkStack p = head.next; while (p.next != null) { str += p.data + ","; p = p.next; } str += p.data; } return(str); }
public void TestAsserLinkStack() { LinkStack <int> A = new LinkStack <int>(); A.Push(1); A.Push(2); A.Push(3); A.Push(4); A.Push(5); //Pop //Normal int testnum1 = A.Pop(); //1 int testnum2 = A.Pop(); //2 Assert.AreEqual(5, testnum1); Assert.AreEqual(4, testnum2); //Edge A.Pop(); //3 A.Pop(); //4 A.Pop(); //5 }
public void PushLink(Int16 linkIndex) { LinkStack.Push(linkIndex); }
public void TestAsserLinkStack() { LinkStack<int> A = new LinkStack<int>(); A.Push(1); A.Push(2); A.Push(3); A.Push(4); A.Push(5); //Pop //Normal int testnum1 = A.Pop(); //1 int testnum2 = A.Pop(); //2 Assert.AreEqual(5, testnum1); Assert.AreEqual(4, testnum2); //Edge A.Pop(); //3 A.Pop(); //4 A.Pop(); //5 }