我想点击Empty(已经有了清空的函数)就可以清空框里的东西,请问怎么实现?

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp2
{
class Node
{
public T data;//数据
public Node next = null;//下个节点位置
public Node last = null;//上个节点位置
};

class Stack<T>
{
    public Stack()
    {//实例化一个节点作为持续的栈底和当前的栈顶,长度初始为0
        Node<T> node = new Node<T>();
        down = node;
        top = node;
        len = 0;
    }
    public Stack(Stack<T> value)//从另外一个栈复制数据
    {
        Node<T> node = new Node<T>();
        down = node;
        top = node;
        len = 0;
        for (var i = value.GetDownNode(); i != null; i = i.next)//value的所有数据复制过来
        {
            this.Push(i.data);
        }
    }
    public void Push(T _data)
    {
        //实例一个节点作为栈顶
        //该节点的上一个节点位置为上一个栈顶
        //上一个栈顶的下一个位置为该节点
        //栈长度+1
        Node<T> node = new Node<T>();
        var node_top = top;
        top = node;
        node.data = _data;
        node_top.next = node;
        node.last = node_top;
        len++;
    }
    public void Add(T _data)//添加
    {
        this.Push(_data);
    }
    public void Pop()                 //弹出最上面的栈
    {
        Node<T> node = top;
        node.last.next = null;
        top = node.last;
        len--;
    }
    public bool IsEmpty()    //判断集合是否为空
    {
        return this.GetDownNode() == null;
    }
    public void Empty()    //清空集合
    {
        while (len != 0)
        {
            this.Pop();
        }
    }
    public bool IsMemberOf(T value)   //判断对象是否在集合中
    {//遍历所有节点
        for (var i = this.GetDownNode(); i != null; i = i.next)
        {
            if (i.data.Equals(value))
            {
                return true;
            }
        }
        return false;
    }
    public Node<T> IndexIter(int index)//迭代器索引
    {
        if (index < 0 && index != 0)//小于0变成从后面倒数的第几个,例如-1就是最后一个
        {
            index = this.len + index;
        }
        if (index >= len)//越界抛异常
        {
            throw new System.IndexOutOfRangeException();
        }
        if (index > len / 2)//索引靠后
        {
            Node<T> target = top;
            for (int i = 0; i < len - index - 1; i++)//迭代
            {
                target = target.last;
            }
            return target;
        }
        else//索引靠前
        {
            Node<T> target = GetDownNode();
            for (int i = 0; i < index; i++)
            {
                target = target.next;
            }
            return target;
        }

    }
    public T this[int index]
    {
        get
        {
            return this.IndexIter(index).data;
        }
        set
        {
            this.IndexIter(index).data = value;
        }
    }
    public void Insert(int pos, Node<T> node)    
    {
        if (pos == 0)//在头部插入
        {
            Node<T> node_target = this.down;
            if (len == 0)
            {
                this.Push(node.data);
            }
            else
            {
                Node<T> node_target_next = GetDownNode();
                node.last = node_target;
                node_target.next = node;
                node.next = node_target_next;
                node_target_next.last = node;
                this.len++;
            }
        }
        else
        {
            Node<T> node_target = this.IndexIter(pos - 1);
            if (pos == len || pos == -1)//在尾部插入
            {
                this.Push(node.data);
            }
            else//在中间插入
            {
                Node<T> node_target_next = this.IndexIter(pos);
                node_target.next = node;
                node.last = node_target;
                node_target_next.last = node;
                node.next = node_target_next;
                this.len++;
            }
        }
    }
    public void Insert(T data, int pos)     //插入
    {
        Node<T> node = new Node<T>();
        node.data = data;
        this.Insert(pos, node);

    }
    public void Del(int pos)     //删除
    {
        if (pos == -1 || pos == len - 1)
        {
            this.Pop();
            return;
        }
        Node<T> node = this.IndexIter(pos);
        node.last.next = node.next;
        node.next.last = node.last;
        this.len--;
    }

    public override string ToString()     //显示
    {
        string return_str = "";
        for (var i = this.GetDownNode(); i != null; i = i.next)
        {
            return_str += i.data.ToString() + "\n";
        }
        return return_str;
    }
    public bool IsEquals(Stack<T> value)   //判断是否相等
    {
        if (len != value.len)
        {
            return false;
        }
        //遍历对比两个栈的所有节点
        var node_this = this.GetDownNode();
        var node_value = value.GetDownNode();
        while (node_this != null)
        {
            if (!node_this.data.Equals(node_value.data))
            {
                return false;
            }
            node_this = node_this.next;
            node_value = node_value.next;
        }
        return true;
    }
    int len;//链表长度
    private Node<T> down;
    private Node<T> top;
    public Node<T> GetDownNode()
    {//最底部是在类构造时生成的是空的,所以要返回down.next
        return down.next;
    }
}

}

```using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stack stack = new Stack();
private void btn1_Click(object sender, EventArgs e)
{
stack.Add(textBox1.Text);
richTextBox1.Text = stack.ToString();
//stack.Empty();
}

    private void button1_Click(object sender, EventArgs e)
    {
        int pos = 0;
        Int32.TryParse(textBox2.Text,out pos);
        stack.Del(pos);
        richTextBox1.Text = stack.ToString();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        int pos=0;
        Int32.TryParse(richTextBox1.Text, out pos);
        stack.Empty();
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }
}

}图片说明

c#
0

1个回答

stack = new Stack();
richTextBox1.Text = stack.ToString();

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!