分享
 
 
 

.net实现链表类 winform表现

王朝学院·作者佚名  2009-11-10
窄屏简体版  字體: |||超大  

XList.vb

view plaincopy to clipboardprint?

'

' * Created by SharpDevelop.

' * User: fx

' * Date: 2009/11/08

' * Time: 2009年11月8日14:15:43

'

Imports System

Imports System.Collections.Generic

Imports System.Linq

Imports System.Text

'结点类

Public Class XListItem

Public Sub New()

data = ""

[next] = Nothing

End Sub

Public Sub New(ByVal str As String)

data = str

[next] = Nothing

End Sub

Public [next] As XListItem '类似于指针域

Public data As String '数值域

'也可以用如下的属性来表示data,

''Private _data As String

''Public Property Data() As String

'' Get

'' Return _data

'' End Get

'' Set(ByVal value As String)

'' _data = value

'' End Set

''End Property

End Class

'链表类

Public Class XList

Dim firstNode As XListItem '头结点

Dim lastNode As XListItem '尾结点

Dim curNode As XListItem '当前节点,类似游标

Public Sub New()

firstNode = InlineAssignHelper(lastNode, InlineAssignHelper(curNode, Nothing))

End Sub

'**********************************************************************************************

'判断链表是否为空

'**********************************************************************************************

Public Function IsEmpty() As Boolean

'Return firstNode Is Nothing

'如果头结点为空,即空链表

If firstNode Is Nothing Then

Return True

'如果不为空

Else

Return False

End If

End Function

'**********************************************************************************************

'清空链表

'**********************************************************************************************

Public Sub ClearLinklist()

firstNode = Nothing

lastNode = Nothing

curNode = Nothing

End Sub

'**********************************************************************************************

'插入结点

'**********************************************************************************************

Public Sub Insert(ByVal strInsert As String)

'如果链表为空链表,插入第一个结点时

If IsEmpty() Then

lastNode = New XListItem(strInsert)

lastNode.[next] = Nothing

firstNode = lastNode

Return

End If

'不为空链表

curNode = New XListItem(strInsert)

curNode.[next] = Nothing

lastNode.[next] = curNode

lastNode = curNode

End Sub

'**********************************************************************************************

'复杂插入

'引数:strInsert 插入的结点值;iLocator 要插入的结点的位置

'返回值:无

'**********************************************************************************************

Public Sub Insert(ByVal strInsert As String, ByVal iLocator As Integer)

'如果链表为空链表,

If IsEmpty() Then

lastNode = New XListItem(strInsert)

lastNode.[next] = Nothing

firstNode = lastNode

Return

End If

'不为空链表

Dim newNode As New XListItem(strInsert)

newNode.[next] = Nothing

If firstNode.[next] Is Nothing Then

firstNode.[next] = newNode

lastNode = newNode

Return

End If

Dim objNode As XListItem = GetNode(iLocator)

If objNode Is Nothing Then

Insert(strInsert)

Return

End If

Dim tempNode As XListItem = objNode

Dim prevNode As XListItem = GetNode(iLocator - 1)

If prevNode Is lastNode Then

Insert(strInsert)

Return

End If

If objNode Is firstNode Then

newNode.[next] = firstNode

firstNode = newNode

Return

End If

objNode = Nothing

objNode = newNode

objNode.[next] = tempNode

prevNode.[next] = objNode

End Sub

'**********************************************************************************************

'获取结点个数

'引数:iNode

'返回值:XListItem 返回结点

'**********************************************************************************************

Public Function GetNode(ByVal iNode As Integer) As XListItem

If iNode < 0 Then

iNode = 0

End If

If IsEmpty() Then

Return Nothing

End If

If 0 = iNode Then

Return firstNode

End If

Dim currentNode As XListItem = firstNode

For i As Integer = 0 To iNode - 1

If currentNode Is lastNode Then

Return lastNode

End If

currentNode = currentNode.[next]

Next

Return currentNode

End Function

'**********************************************************************************************

'显示链表

'引数:无

'返回值:无

'**********************************************************************************************

Public Sub Print()

If IsEmpty() Then

Console.WriteLine("List is Empty")

Return

End If

Console.Write("The List is: ")

Dim current As XListItem = firstNode

'output current node data while not at end of list

While current IsNot Nothing

Console.Write(current.data & " ")

current = current.[next]

End While

Console.WriteLine(vbLf)

End Sub

''**********************************************************************************************

''显示链表

''引数:无

''返回值:无

''**********************************************************************************************

Public Function PrintLinkList() As String

Dim strResult As String = ""

If IsEmpty() Then

strResult = "链表为空"

Return strResult

End If

Dim current As XListItem = firstNode

'不是尾结点 继续输出

While Not current Is Nothing

'Console.Write(current.data & " ")

strResult &= current.data & ","

current = current.[next]

End While

'去掉末尾的,

strResult = strResult.Substring(0, strResult.Length - 1)

Return strResult

End Function

'**********************************************************************************************

'移除的链表的结点

'引数:strInsert 要移除的链表的结点值

'返回值:true or false 是否移除成功 成功true 失败 false

'**********************************************************************************************

Public Function Remove(ByVal strInsert As String) As Boolean

If IsEmpty() Then

Return False

End If

If firstNode.data = strInsert Then

firstNode = firstNode.[next]

Return True

End If

Dim curNodeF As XListItem = firstNode

curNode = curNodeF

If curNode Is Nothing Then

Return False

End If

While True

curNode = curNode.[next]

If curNode Is lastNode Then

If curNode.data = strInsert Then

curNode = Nothing

lastNode = curNodeF

lastNode.[next] = Nothing

Return True

End If

Return False

End If

If curNode.data = strInsert Then

curNodeF.[next] = curNode.[next]

curNode = Nothing

Return True

End If

curNodeF = curNodeF.[next]

End While

End Function

'**********************************************************************************************

'逆序显示链表

'引数:t 链表

'返回值:newList 逆序链表

'**********************************************************************************************

Public Function Revert(ByVal t As XListItem) As XListItem

Dim newList As XListItem = Nothing

While t IsNot Nothing

Dim temp As New XListItem()

temp.data = t.data

temp.[next] = newList

newList = temp

t = t.[next]

End While

Return newList

End Function

'**********************************************************************************************

'逆序显示链表

'引数:t 链表

'返回值:newList 逆序链表

'**********************************************************************************************

Public Function Revert() As XListItem

Dim newList As XListItem = Nothing

While firstNode IsNot Nothing

Dim temp As New XListItem()

temp.data = firstNode.data

temp.[next] = newList

newList = temp

firstNode = firstNode.[next]

End While

firstNode = newList

Return newList

End Function

Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T

target = value

Return value

End Function

End Class

'class EmptyListException definition

Public Class EmptyListException

Inherits ApplicationException

Public Sub New(ByVal name As String)

MyBase.New("The " & name & "is empty")

End Sub

End Class

'end class EmptyListException

'

' * Created by SharpDevelop.

' * User: fx

' * Date: 2009/11/08

' * Time: 2009年11月8日14:15:43

'

Imports System

Imports System.Collections.Generic

Imports System.Linq

Imports System.Text

'结点类

Public Class XListItem

Public Sub New()

data = ""

[next] = Nothing

End Sub

Public Sub New(ByVal str As String)

data = str

[next] = Nothing

End Sub

Public [next] As XListItem '类似于指针域

Public data As String '数值域

'也可以用如下的属性来表示data,

''Private _data As String

''Public Property Data() As String

'' Get

'' Return _data

'' End Get

'' Set(ByVal value As String)

'' _data = value

'' End Set

''End Property

End Class

'链表类

Public Class XList

Dim firstNode As XListItem '头结点

Dim lastNode As XListItem '尾结点

Dim curNode As XListItem '当前节点,类似游标

Public Sub New()

firstNode = InlineAssignHelper(lastNode, InlineAssignHelper(curNode, Nothing))

End Sub

'**********************************************************************************************

'判断链表是否为空

'**********************************************************************************************

Public Function IsEmpty() As Boolean

'Return firstNode Is Nothing

'如果头结点为空,即空链表

If firstNode Is Nothing Then

Return True

'如果不为空

Else

Return False

End If

End Function

'**********************************************************************************************

'清空链表

'**********************************************************************************************

Public Sub ClearLinklist()

firstNode = Nothing

lastNode = Nothing

curNode = Nothing

End Sub

'**********************************************************************************************

'插入结点

'**********************************************************************************************

Public Sub Insert(ByVal strInsert As String)

'如果链表为空链表,插入第一个结点时

If IsEmpty() Then

lastNode = New XListItem(strInsert)

lastNode.[next] = Nothing

firstNode = lastNode

Return

End If

'不为空链表

curNode = New XListItem(strInsert)

curNode.[next] = Nothing

lastNode.[next] = curNode

lastNode = curNode

End Sub

'**********************************************************************************************

'复杂插入

'引数:strInsert 插入的结点值;iLocator 要插入的结点的位置

'返回值:无

'**********************************************************************************************

Public Sub Insert(ByVal strInsert As String, ByVal iLocator As Integer)

'如果链表为空链表,

If IsEmpty() Then

lastNode = New XListItem(strInsert)

lastNode.[next] = Nothing

firstNode = lastNode

Return

End If

'不为空链表

Dim newNode As New XListItem(strInsert)

newNode.[next] = Nothing

If firstNode.[next] Is Nothing Then

firstNode.[next] = newNode

lastNode = newNode

Return

End If

Dim objNode As XListItem = GetNode(iLocator)

If objNode Is Nothing Then

Insert(strInsert)

Return

End If

Dim tempNode As XListItem = objNode

Dim prevNode As XListItem = GetNode(iLocator - 1)

If prevNode Is lastNode Then

Insert(strInsert)

Return

End If

If objNode Is firstNode Then

newNode.[next] = firstNode

firstNode = newNode

Return

End If

objNode = Nothing

objNode = newNode

objNode.[next] = tempNode

prevNode.[next] = objNode

End Sub

'**********************************************************************************************

'获取结点个数

'引数:iNode

'返回值:XListItem 返回结点

'**********************************************************************************************

Public Function GetNode(ByVal iNode As Integer) As XListItem

If iNode < 0 Then

iNode = 0

End If

If IsEmpty() Then

Return Nothing

End If

If 0 = iNode Then

Return firstNode

End If

Dim currentNode As XListItem = firstNode

For i As Integer = 0 To iNode - 1

If currentNode Is lastNode Then

Return lastNode

End If

currentNode = currentNode.[next]

Next

Return currentNode

End Function

'**********************************************************************************************

'显示链表

'引数:无

'返回值:无

'**********************************************************************************************

Public Sub Print()

If IsEmpty() Then

Console.WriteLine("List is Empty")

Return

End If

Console.Write("The List is: ")

Dim current As XListItem = firstNode

'output current node data while not at end of list

While current IsNot Nothing

Console.Write(current.data & " ")

current = current.[next]

End While

Console.WriteLine(vbLf)

End Sub

''**********************************************************************************************

''显示链表

''引数:无

''返回值:无

''**********************************************************************************************

Public Function PrintLinkList() As String

Dim strResult As String = ""

If IsEmpty() Then

strResult = "链表为空"

Return strResult

End If

Dim current As XListItem = firstNode

'不是尾结点 继续输出

While Not current Is Nothing

'Console.Write(current.data & " ")

strResult &= current.data & ","

current = current.[next]

End While

'去掉末尾的,

strResult = strResult.Substring(0, strResult.Length - 1)

Return strResult

End Function

'**********************************************************************************************

'移除的链表的结点

'引数:strInsert 要移除的链表的结点值

'返回值:true or false 是否移除成功 成功true 失败 false

'**********************************************************************************************

Public Function Remove(ByVal strInsert As String) As Boolean

If IsEmpty() Then

Return False

End If

If firstNode.data = strInsert Then

firstNode = firstNode.[next]

Return True

End If

Dim curNodeF As XListItem = firstNode

curNode = curNodeF

If curNode Is Nothing Then

Return False

End If

While True

curNode = curNode.[next]

If curNode Is lastNode Then

If curNode.data = strInsert Then

curNode = Nothing

lastNode = curNodeF

lastNode.[next] = Nothing

Return True

End If

Return False

End If

If curNode.data = strInsert Then

curNodeF.[next] = curNode.[next]

curNode = Nothing

Return True

End If

curNodeF = curNodeF.[next]

End While

End Function

'**********************************************************************************************

'逆序显示链表

'引数:t 链表

'返回值:newList 逆序链表

'**********************************************************************************************

Public Function Revert(ByVal t As XListItem) As XListItem

Dim newList As XListItem = Nothing

While t IsNot Nothing

Dim temp As New XListItem()

temp.data = t.data

temp.[next] = newList

newList = temp

t = t.[next]

End While

Return newList

End Function

'**********************************************************************************************

'逆序显示链表

'引数:t 链表

'返回值:newList 逆序链表

'**********************************************************************************************

Public Function Revert() As XListItem

Dim newList As XListItem = Nothing

While firstNode IsNot Nothing

Dim temp As New XListItem()

temp.data = firstNode.data

temp.[next] = newList

newList = temp

firstNode = firstNode.[next]

End While

firstNode = newList

Return newList

End Function

Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T

target = value

Return value

End Function

End Class

'class EmptyListException definition

Public Class EmptyListException

Inherits ApplicationException

Public Sub New(ByVal name As String)

MyBase.New("The " & name & "is empty")

End Sub

End Class

'end class EmptyListException

Form1.vb

删除结点自己添加下吧

view plaincopy to clipboardprint?

Public Class Form1

Dim fxXlist As New XList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'默认值第一个,显示链表

Me.combFxOptions.SelectedIndex = 0

End Sub

Private Sub btnDo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDo.Click

Select Case Me.combFxOptions.SelectedIndex

'显示链表

'在链表头部插入结点

'在链表尾部插入结点

'在链表中插入结点(包括头尾)

'链表逆序显示s

'显示链表

Case 0

If Not fxXlist Is Nothing Then

Me.txtFxCurLinklist.Text = fxXlist.PrintLinkList()

Else

Me.txtFxCurLinklist.Text = "空链表"

End If

'在链表头部插入结点

Case 1

If Me.txtFxInsertValue.Text.Trim = String.Empty Then

MessageBox.Show("要插入的结点不能为空!", "错误", _

MessageBoxButtons.OK, MessageBoxIcon.Warning)

Me.txtFxInsertValue.Focus()

Else

fxXlist.Insert(Me.txtFxInsertValue.Text.Trim, 0)

MsgBox("插入成功!", MsgBoxStyle.Information, "提示")

Me.txtFxCurLinklist.Text = fxXlist.PrintLinkList()

Me.txtFxInsertValue.SelectAll()

Me.txtFxInsertValue.Focus()

End If

'在链表尾部插入结点

Case 2

If Me.txtFxInsertValue.Text.Trim = String.Empty Then

MessageBox.Show("要插入的结点不能为空!", "错误", _

MessageBoxButtons.OK, MessageBoxIcon.Warning)

Me.txtFxInsertValue.Focus()

Else

fxXlist.Insert(Me.txtFxInsertValue.Text.Trim)

MsgBox("插入成功!", MsgBoxStyle.Information, "提示")

Me.txtFxCurLinklist.Text = fxXlist.PrintLinkList()

Me.txtFxInsertValue.SelectAll()

Me.txtFxInsertValue.Focus()

End If

'在链表中插入结点(包括头尾)

Case 3

If Me.txtFxInsertValue.Text.Trim = String.Empty Then

MessageBox.Show("要插入的结点不能为空!", "错误", _

MessageBoxButtons.OK, MessageBoxIcon.Warning)

Me.txtFxInsertValue.Focus()

ElseIf Me.txtFxLocation.Text.Trim = String.Empty Then

MessageBox.Show("要插入的位置不能为空!", "错误", _

MessageBoxButtons.OK, MessageBoxIcon.Warning)

Me.txtFxLocation.Focus()

ElseIf IsNumeric(Me.txtFxLocation.Text) = False Then

MessageBox.Show("要插入的位置必须为数字!", "错误", _

MessageBoxButtons.OK, MessageBoxIcon.Warning)

Me.txtFxLocation.Focus()

Else

fxXlist.Insert(Me.txtFxInsertValue.Text.Trim, Me.txtFxLocation.Text.Trim)

MsgBox("插入成功!", MsgBoxStyle.Information, "提示")

Me.txtFxCurLinklist.Text = fxXlist.PrintLinkList()

Me.txtFxInsertValue.SelectAll()

Me.txtFxInsertValue.Focus()

End If

'链表逆序显示

Case 4

fxXlist.Revert()

Me.txtFxCurLinklist.Text = fxXlist.PrintLinkList()

Case 5

Case 6

Case Else

End Select

End Sub

Private Sub btnClearLinklist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearLinklist.Click

Me.fxXlist.ClearLinklist()

Me.txtFxCurLinklist.Text = "空链表"

Me.txtFxInsertValue.Text = ""

Me.txtFxLocation.Text = ""

Me.combFxOptions.SelectedIndex = 0

End Sub

End Class

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有