DS & A 2. Linked List
Define prototype
Prototype
/// <Summary>
/// Define initialize Node object
/// </Summary>
Public Class Worker Node
{
Private Worker node _ next;
Private Object _ value;
Public worker node ()
{< BR >}
Public jsonnode (object value, jsonnode next)
{< br> This . _ next = next;
This . _ value = value;
}
Public jsonnode (object Value)
: This (value, null )
{< BR >}
Public worker node next
{< br> Get { return _next ;}
set {_ next = value ;}< BR >}
Public ObjectValue
{
Get{Return_ Value ;}
//Set {_ value = value ;}
}
}
Field Definition
Node 'prototype
Private Int _ Length;
Private Worker node _ head;
Private Using node _ tail;
Public int length
{< br> Get { return _length ;}
}
PublicLinkedlistv4 ()
{
This. _ Head= Null;
This. _ Tail= Null;
}
Insert in the header
Addatfirst
Public Void Addatfirst (object value)
{
If ( This . _ Head = Null )
{
This . _ Head = New Worker node (value );
This . _ Tail = This . _ Head;
This . _ Length ++ ;
}
Else
{
Worker node tmpnode = New Worker node (value, This . _ Head );
This . _ Head = Tmpnode;
This . _ Length ++ ;
}
}
Tail insert
/// <Summary>
/// V2, using the tail node.
/// </Summary>
/// <Param name = "value"> </param>
Public Void Addatend (object value)
{
If ( This . _ Head = Null )
{
This . _ Head = New Worker node (value );
This . _ Tail = This . _ Head;
This . _ Length ++ ;
}
Else
{
Worker node newnode = New Worker node (value );
This . _ Tail. Next = Newnode;
This . _ Tail = Newnode;
This . _ Length ++ ;
}
}
Reverse
// Reverse
Public Int Reverse ()
{
Int Count = 0 ;
Worker node pnode = This . _ Head;
While (Pnode. Next ! = Null )
{
Worker node tnode = Pnode. Next;
Pnode. Next = Pnode. Next. Next;
Tnode. Next = This . _ Head;
This . _ Head = Tnode;
Count ++ ;
}
Return Count;
}
Code
Public Void Bubblesort ()
{
Worker node tnode = This . _ Head;
Worker node dnode = This . _ Head;
While(Tnode. Next! = Null)
{
Dnode=Tnode;
Worker node dpnode = Dnode; // Record parent
While (Dnode. Next ! = Null )
{
// If the switch dnode has been moved back, you do not need to assign a value again.
If (Convert. toint32 (dnode. value) > Convert. toint32 (dnode. Next. Value ))
{
// Swap;
If (Dnode = This . _ Head) // Header Node
{
Worker node tmpnode = Dnode;
This . _ Head = Dnode. Next;
Dnode. Next = Dnode;
}
Else
{
Worker node tmpnode = Dnode;
Dpnode. Next = Dnode. Next;
Tmpnode. Next = Dpnode. Next. Next;
Dpnode. Next. Next = Tmpnode;
}
Dpnode = Dpnode. Next;
}
Else
{
Dpnode = Dnode;
Dnode = Dnode. Next;
}
}
Tnode = Tnode. Next;
}
}
Code
// Judge whether it is a circular linked list
// Method 1: traverse and write a mark for each vertex. If the mark appears repeatedly, it indicates a loop.
// Method 2: create two pointers pointing to the current node and the next node of the node respectively.
Public Bool Isloop ()
{
Worker node pnode = This . _ Head;
Worker node nnode = This . _ Head. Next;
While (Nnode ! = Null )
{
If (Nnode. Next = Pnode)
{
Return True ;
}
Pnode = Pnode. Next;
Nnode = Nnode. Next;
}
Return False ;
}