DS & A 2. Linked List

Source: Internet
Author: User
Tags define prototype

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 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.