public Type NextNode()//设置下一结点为当前结点 { if (current.Next != null) { current = current.Next; } return(current.Data); }
public void CreateRear(Type[] dt, int n)//尾插法生成n个结点 { MakeEmpty(); for (int i = 1; i <= n; i++) { current.Next = new SingleListNodeClass <Type>(dt[i - 1], null); current = current.Next; } }
public void CreateHead(Type[] dt, int n)//头插法生成n个结点 { MakeEmpty(); for (int i = 1; i <= n; i++) { head.Next = new SingleListNodeClass <Type>(dt[i - 1], head.Next); } current = head.Next; }
private void button3_Click(object sender, EventArgs e) { int number = Convert.ToInt16(textBox2.Text); SingleListNodeClass <int> t = new SingleListNodeClass <int>(); t.Data = number; m_SingleList.Update(t); DrawSingleList(); }
private void button4_Click(object sender, EventArgs e) { int number = Convert.ToInt16(textBox2.Text); SingleListNodeClass <int> p = m_SingleList.Head.Next; while (p.Next != null) { if (p.Data == number) { m_SingleList.Currrent = p; break; } p = p.Next; } if (p.Next == null) { MessageBox.Show("没有该数据"); } DrawSingleList(); }
private void sortSingleList() { SingleListNodeClass <int> p = m_SingleList.Head.Next; while (p.Next != null) { SingleListNodeClass <int> q = p.Next; while (q != null) { if (p.Data > q.Data) { int k = p.Data; p.Data = q.Data; q.Data = k; } q = q.Next; } p = p.Next; } }
private void button16_Click(object sender, EventArgs e) { m_SingleList.FirstNode(); int min = Convert.ToInt16(textBox3.Text); int max = Convert.ToInt16(textBox4.Text); SingleListNodeClass <int> p = m_SingleList.Head; SingleListNodeClass <int> q = m_SingleList.Head; while (p.Next != null) { if (p.Next.Data <= Math.Max(max, min) && p.Next.Data >= Math.Min(max, min)) { m_SingleList.DeleteNode(p.Next); continue; } p = p.Next; q = q.Next; } DrawSingleList(); }
public void Delete()//删除当前结点 { if (current == head) { return; } SingleListNodeClass <Type> p = head; while (p.Next != current) { p = p.Next; } p.Next = current.Next; if (p.Next != null) { current = p.Next; } else { current = p; } }
public void Insert(Type value, bool before) { if (current == head) { before = false; } if (before) { SingleListNodeClass <Type> p = head; while (p.Next != current) { p = p.Next; } p.Next = new SingleListNodeClass <Type>(value, p.Next); current = p.Next; } else { current.Next = new SingleListNodeClass <Type>(value, current.Next); current = current.Next; } }
public void DeleteNode(SingleListNodeClass <Type> p1) { if (p1 == head) { return; } SingleListNodeClass <Type> p = head; while (p.Next != p1) { p = p.Next; } p.Next = p1.Next; if (p.Next != null) { current = p.Next; } else { current = p; } }
public SingleListNodeClass(Type data)//构造函数 { this.data = data; next = null; }
public void Update(SingleListNodeClass <Type> t)//修改节点 { current.Data = t.Data; }
public SingleListNodeClass()//构造函数 { next = null; }
public Type FirstNode()//设置头结点为当前结点 { current = head; return(current.Data); }
public void MakeEmpty()//清空单链表 { head.Next = null; current = head; }
private void button15_Click(object sender, EventArgs e) { int min = Convert.ToInt16(textBox3.Text); int max = Convert.ToInt16(textBox4.Text); //通过画刷进行填充 Graphics myg = pictureBox1.CreateGraphics(); Brush bkbrush = new SolidBrush(Color.White); myg.FillRectangle(bkbrush, 0, 0, 741, 628); Color bkColor = Color.FromArgb(255, 255, 215, 0); Brush bkbrush1 = new SolidBrush(bkColor); int interval = 80; int dx = 60, dy = 50; int y = 20; int time = 0; bool direction = true; System.Drawing.Drawing2D.AdjustableArrowCap lineCap = new System.Drawing.Drawing2D.AdjustableArrowCap(4, 4, true); SingleListNodeClass <int> p = m_SingleList.Head; for (int i = 0; i <= m_SingleList.Length; i++) { int x = interval * time + 20; if (x > 740 - interval && direction == true) { x -= interval; y += interval; direction = false; } else if (x < 20 && direction == false) { x += interval; y += interval; direction = true; } if (direction == true) { time += 1; } else { time -= 1; } if (p.Data <= Math.Max(max, min) && p.Data >= Math.Min(max, min)) { Brush t = new SolidBrush(Color.Pink); myg.FillRectangle(t, (float)(x), (float)(y), dx, dy);//画填充矩形 } else { myg.FillRectangle(bkbrush1, (float)(x), (float)(y), dx, dy);//画填充矩形 } //画箭头 Pen penLine = new Pen(Color.Red, 1); penLine.CustomEndCap = lineCap; Point arrowp1 = new Point(0, 0); Point arrowp2 = new Point(0, 0); if (direction == true && i != 0) { arrowp1 = new Point(x + dx - interval, y + dy / 2); arrowp2 = new Point(x, y + dy / 2); } else if (direction == false) { arrowp1 = new Point(x + interval, y + dy / 2); arrowp2 = new Point(x + dx, y + dy / 2); } myg.DrawLine(penLine, arrowp1, arrowp2); Pen pen2 = new Pen(Color.Red, 1); if (direction == false && x + interval > 720) { Point pp1 = new Point(x + interval, y - interval + dy / 2); Point pp2 = new Point(x + interval, y + dy / 2); myg.DrawLine(pen2, pp1, pp2); pp2 = pp1; pp1 = new Point(x + dx, y - interval + dy / 2); myg.DrawLine(pen2, pp1, pp2); time -= 1; } else if (direction == true && x <= 30 && i != 0) { Point pp1 = new Point(x - interval + dx, y - interval + dy / 2); Point pp2 = new Point(x - interval + dx, y + dy / 2); myg.DrawLine(pen2, pp1, pp2); pp2 = pp1; pp1 = new Point(x, y - interval + dy / 2); myg.DrawLine(pen2, pp1, pp2); time += 1; } //画四周四条边 Pen pen1 = new Pen(Color.Red, 1); Point p1 = new Point(x, y); Point p2 = new Point(x, y + dy); myg.DrawLine(pen1, p1, p2); p1 = p2; p2 = new Point(x + dx, y + dy); myg.DrawLine(pen1, p1, p2); p1 = p2; p2 = new Point(x + dx, y); myg.DrawLine(pen1, p1, p2); p1 = p2; p2 = new Point(x, y); myg.DrawLine(pen1, p1, p2); //显示字符串 string str = Convert.ToString(p.Data); if (i == 0) { str = "head"; } p = p.Next; Font font = new Font("Arial", 12); SolidBrush b1 = new SolidBrush(Color.Blue); StringFormat sf1 = new StringFormat(); myg.DrawString(str, font, b1, x + 18, y + 15, sf1); } button11.Text = Convert.ToString(m_SingleList.Currrent.GetHashCode()); button12.Text = Convert.ToString(m_SingleList.Currrent.Data); if (m_SingleList.Currrent.Next == null) { button13.Text = "null"; } else { button13.Text = Convert.ToString(m_SingleList.Currrent.Next.GetHashCode()); } }
public SingleListNodeClass(Type data, SingleListNodeClass <Type> next)//构造函数 { this.data = data; this.next = next; }
public void InsertHead(Type value)//头插法插入结点 { head.Next = new SingleListNodeClass <Type>(value, head.Next); current = head.Next; }
private SingleListNodeClass <Type> current; //当前结点的引用 public SingleListClass() //构造函数,空表,只有头结点 { head = new SingleListNodeClass <Type>(); current = head;//只是一个引用,不是一个对象实体 }
public void AppendRear(Type value)//尾插法插入结点 { current = new SingleListNodeClass <Type>(value, null); this.Rear.Next = current; }