⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 用图像表示链表的创建、插入和删除
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Example
{
    public partial class Form1 : Form
    {
        SList a;
        Graphics g;
        int maxWidth, maxHeight, width, height;
        int rows, rest;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            maxWidth = pictureBox1.Width;
            maxHeight = pictureBox1.Height;

            width = (maxWidth - 150) / 10;
            height = (maxHeight - 150) / 10;

            a = new SList();
            a.head = new SListnode();


            
        }

        void Draw()
        {
            g = pictureBox1.CreateGraphics();
            g.Clear(Color.White);

            Pen p1 = new Pen(Color.Purple, 2);
            Pen p2 = new Pen(Color.White, 2);
            Brush b1 = new SolidBrush(Color.Yellow);
            Brush b2 = new SolidBrush(Color.LightBlue);
            Brush b3 = new SolidBrush(Color.Blue);

            if (a.Length()>= 0)
            {
                SListnode b = a.head;
                int  x = 30, y = 30;

                rows = (a.Length() + 1) / 10;                             //节点的整行数
                for (int i = 1; i <= rows; i++)
                {
                    for (int j = 1; j < 11; j++)
                    {
                        g.DrawRectangle(p1, x, y, width, height);                                  //节点外框

                        if ((i == 1) && (j == 1))
                            g.FillRectangle(b3, x, y, width, height);               //填充头节点
                        else
                            g.FillRectangle(b1, x, y, width, height);               //填充其他节点

                        g.DrawLine(p1, (x + width), (y + height / 2), (x + width + 10), (y + height / 2));    //画水平箭头

                        if (i % 2==1)
                        {
                            g.DrawLine(p1, (x + width + 10), (y + height / 2), (x + width + 5), (y + height / 2 - 5));
                            g.DrawLine(p1, (x + width + 10), (y + height / 2), (x + width + 5), (y + height / 2 + 5));
                        }
                        else
                        {
                            g.DrawLine(p1, (x + width), (y + height / 2), (x + width + 5), (y + height / 2 - 5));
                            g.DrawLine(p1, (x + width), (y + height / 2), (x + width + 5), (y + height / 2 + 5));
                        }
                        x += width + 10;
                    }

                    if (i > 1)                                            //从第二行开始画第一个节点前的箭头以及连接上一行的线
                    {
                        g.DrawLine(p1, 20, (y + height / 2), 30, (y + height / 2));

                        if (i % 2==1)
                        {
                            g.DrawLine(p1, 30, (y + height / 2), 25, (y + height / 2 - 5));
                            g.DrawLine(p1, 30, (y + height / 2), 25, (y + height / 2 + 5));
                            g.DrawLine(p1, 20, (y + height / 2), 20, (y - height / 2 - 10));
                        }
                        else
                        {
                            g.DrawLine(p1, 20, (y + height / 2), 25, (y + height / 2 - 5));
                            g.DrawLine(p1, 20, (y + height / 2), 25, (y + height / 2 + 5));
                            g.DrawLine(p1, x, (y + height / 2), x, (y - height / 2 - 10));
                        }
                    }

                    x = 30;
                    y += height + 10;
                }

                rest = (a.Length() + 1) % 10;                                                          //不满节点行

                if (rest == 0)
                {
                    if (rows % 2==1)
                    {
                        g.DrawLine(p2, (maxWidth - 38), (y - height / 2 - 10), (maxWidth - 28), (y - height / 2 - 10));    //消除最后一个水平箭头

                        g.DrawLine(p2, (maxWidth - 28), (y - height / 2 - 10), (maxWidth - 33), (y - height / 2 - 15));
                        g.DrawLine(p2, (maxWidth - 28), (y - height / 2 - 10), (maxWidth - 33), (y - height / 2 - 5));
                    }
                    else
                    {
                        g.DrawLine(p2, 20, (y - height / 2 - 10), 30, (y - height / 2 - 10));    //消除第一个水平箭头

                        g.DrawLine(p2, 20, (y - height / 2 - 10), 25, (y - height / 2 - 15));
                        g.DrawLine(p2, 20, (y - height / 2 - 10), 25, (y - height / 2 - 5));
                    }
                }
                else
                {
                    if (rows % 2==1)                                //有奇数行满行节点
                    {
                        x += (10 - rest) * (width + 10);
                        for (int j = 1; j <= rest; j++)
                        {
                            g.DrawRectangle(p1, x, y, width, height);                                  //节点外框
                            g.FillRectangle(b1, x, y, width, height);                   //填充节点

                            g.DrawLine(p1, (x + width), (y + height / 2), (x + width + 10), (y + height / 2));    //画水平箭头
                            g.DrawLine(p1, (x + width), (y + height / 2), (x + width + 5), (y + height / 2 - 5));
                            g.DrawLine(p1, (x + width), (y + height / 2), (x + width + 5), (y + height / 2 + 5));

                            x += width + 10;
                        }
                        g.DrawLine(p1, x, (y + height / 2), x, (y - height / 2 - 10));                           //画连接上一行的线
                    }
                    else                                   //有奇数行满行节点
                    {
                        for (int j = 1; j <= rest; j++)
                        {
                            g.DrawRectangle(p1, x, y, width, height);                                  //节点外框
                            if ((j == 1) && (rows == 0))
                                g.FillRectangle(b3, x, y, width, height);               //填充头节点
                            else
                                g.FillRectangle(b1, x, y, width, height);               //填充其他节点

                            g.DrawLine(p1, (x + width), (y + height / 2), (x + width + 10), (y + height / 2));    //画水平箭头

                            g.DrawLine(p1, (x + width + 10), (y + height / 2), (x + width + 5), (y + height / 2 - 5));
                            g.DrawLine(p1, (x + width + 10), (y + height / 2), (x + width + 5), (y + height / 2 + 5));

                            x += width + 10;
                        }

                        g.DrawLine(p2, (x - 10), (y + height / 2), x, (y + height / 2));    //消除最后一个水平箭头

                        g.DrawLine(p2, x, (y + height / 2), (x - 5), (y + height / 2 - 5));
                        g.DrawLine(p2, x, (y + height / 2), (x - 5), (y + height / 2 + 5));

                        if (rows != 0)
                        {
                            g.DrawLine(p1, 20, (y + height / 2), 20, (y - height / 2 - 10));                      //竖线

                            g.DrawLine(p1, 20, (y + height / 2), 30, (y + height / 2));             //第一个节点前的箭头
                            g.DrawLine(p1, 30, (y + height / 2), 25, (y + height / 2 - 5));
                            g.DrawLine(p1, 30, (y + height / 2), 25, (y + height / 2 + 5));
                        }
                    }
                }
                
                y=35;

                for (int i = 1; i <= rows+1; i++)                                          //显示data
                {
                    if ((rest == 0) & (i == rows + 1))
                        break;
                    if (i % 2==1)
                    {
                        x = 40;
                        for (int j = 1; j < 11; j++)
                        {
                            if(b==a.current)                                              //判断是否为当前节点
                                g.FillRectangle(b2, (x - 10), (y-5), width, height);
                            
                            Font font = new Font("Arial", 16);
                            g.DrawString(b.data.ToString(), font, b3, x, y);

                            x += width + 10;
                            if (b.next != null)
                                b = b.next;
                            else
                                break;
                        }

                    }
                    else
                    {
                        x = maxWidth - 70;
                        for (int j = 1; j < 11; j++)
                        {
                            if (b == a.current)                      //判断是否为当前节点
                                g.FillRectangle(b2, (x - 12), (y-5), width, height);
                            
                            Font font = new Font("Arial", 16);
                            g.DrawString(b.data.ToString(), font, b3, x, y);

                            x =x- width - 10;
                            if (b.next != null)
                                b = b.next;
                            else
                                break;
                        }
                    }
                    y += height + 10;
                }

            }

           
        }

        
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            int x,y=30;
            SListnode b = a.head;

            for (int i = 1; i <= rows+1; i++)                                          //改变当前节点
            {
                if ((rest == 0) & (i == rows + 1))
                    break;
                if (i % 2 == 1)
                {
                    x = 30;
                    for (int j = 1; j < 11; j++)
                    {
                        if ((e.X >= x) && (e.X <= x + width) && (e.Y >= y) && (e.Y <= y + height))
                            a.current = b;

                        x += width + 10;
                        if (b.next != null)
                            b = b.next;
                        else
                            break;
                    }
                }
                else
                {
                    x = maxWidth - 40-width;
                    for (int j = 1; j < 11; j++)
                    {
                        if ((e.X >= x) && (e.X <= x + width) && (e.Y >= y) && (e.Y <= y + height))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -