Detailed Explanation of C Using Object Class to Implement Stack

  • 2021-10-24 23:36:06
  • OfStack

This article illustrates how C # uses Object classes to implement the stack. Share it for your reference, as follows:

Code for the Stack class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace  Use Object Class implements a last-in first-out queue 
{
 class Stack
 {
  private Object[] _items;
  public Object[] Items
  {
   get { return this._items; }
   set { this._items = value; }
  }
  // Press the object into the 
  public void Push(Object obj)
  {
   // No. 1 1 During secondary press-in, initialization is carried out, and the length is 1
   if (this._items == null)
   {
    this._items = new Object[1];
    this._items[0] = obj;
   }
   else
   {
    int count = this._items.Length;
    Object[] objTemp = this._items;
    this._items = new Object[count + 1];
    int i = 0;
    foreach (Object o in objTemp)
    {
     this._items[i++] = o;
    }
    this._items[i] = obj;
   }
  }
  // Press last in first out to take out 
  public Object Pop()
  {
   // Is initialized or has a length of 0 Cannot fetch any elements when 
   if (this._items == null||this._items.Length == 0)
    return null;
   else
   {
    Object obj = this._items[this._items.Length - 1];
    // Delete the last 1 Elements 
    this.DeleteLastObj();
    return obj;
   }
  }
  private void DeleteLastObj()
  {
   Object[] objTemp = new Object[this._items.Length - 1];
   for (int i = 0; i < this._items.Length - 1; i++)
   {
    objTemp[i] = this._items[i];
   }
   this._items = objTemp;
  }
 }
}

Form detection code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace  Use Object Class implements a last-in first-out queue 
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
  private Stack stack = new Stack();
  private Stack<string> stackGeneric= new Stack<string>();
  private void button1_Click(object sender, EventArgs e)
  {
   stack.Push(this.textBox1.Text);
  }
  private void button2_Click(object sender, EventArgs e)
  {
   Object[] objs = stack.Items;
   foreach(Object o in objs)
   {
    Console.WriteLine(o.ToString());
   }
  }
  private void button1_Click_1(object sender, EventArgs e)
  {
   try
   {
    Console.WriteLine(this.stack.Pop().ToString());
   }
   catch
   {
    Console.WriteLine("null");
   }
  }
  private void button3_Click(object sender, EventArgs e)
  {
   this.stackGeneric.Push(this.textBox2.Text);
  }
  private void button4_Click(object sender, EventArgs e)
  {
   try
   {
    Console.WriteLine(this.stackGeneric.Pop());
   }
   catch (InvalidOperationException)
   {
    Console.WriteLine("null");
   }
  }
 }
}

1. When using Stack class, many uncontrollable resource occupations are formed, waiting for GC recovery;

2. Type is not safe, any type of data can be loaded into object

3. You can set an initial length of an Object array, Instead of temporarily changing the length of the array every time you push it in or take it out, The specific method is to generate an array with a specified length through the constructor of Stack. When pressing in and taking out, the initialized length is not adjusted, but only the position of the current value is recorded with an int value intPoint. For the object that has been taken out, it is actually not deleted, but it is ignored. The advantage of this is that it is preferable to set the length of the array once and use something like a pointer to locate the "valid" element.

In fact, Stack is available above. net 2.0 < > Generic classes can directly complete the stack, which is very convenient to use, and avoids the loss caused by forced type conversion, thus realizing type safety. The way to use it has been given in the code in paragraph 2, which is very simple.

More readers interested in C # can check the topic of this site: C # data structure and algorithm tutorial, C # traversal algorithm and skills summary, C # programming thread use skills summary, C # operating Excel skills summary, XML file operation skills summary in C #, C # common control usage tutorial, WinForm control usage summary, C # array operation skills summary and C # object-oriented programming introduction tutorial

I hope this article is helpful to everyone's C # programming.


Related articles: