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