Singly linked list algorithm implemented by Java

Source: Internet
Author: User

Jeff Lee blog:http://www.cnblogs.com/alandre/(Mud brick Carpenter), retain the URL when reproduced! Thanks

Linked list is a normal data structure.here I show how to implements it.

Step 1. Define a structure
public class listnode{public ListNode next;public int value;public listnode (int newvalue) {Value = newvalue;}}
Step 2. Implements the functions
public class Clist{private ListNode head;private listnode tail;private listnode current;private int ListCountValue; Public Clist () {listcountvalue = 0; Head = null; Tail = null;} public void Append (int datavalue) {ListNode NewNode = new ListNode (datavalue); if (Listcountvalue = = 0) {Head = NewNode; Tail = NewNode;} Else{tail.next = NewNode; Tail = NewNode;} current = NewNode; Listcountvalue + = 1;} public void Insert (int datavalue) {ListNode NewNode = new ListNode (datavalue); if (Listcountvalue = = 0) {Append (datavalue); return;} if (current = = Tail) {tail.next = NewNode; Tail = NewNode; current = Tail; Listcountvalue + = 1;} if (current! = Head) && (current! = Tail)) {newnode.next = Current.next; Current.next = NewNode; current = NewNode; Listcountvalue + = 1;}} public void Delete () {if (Listcountvalue! = 0) {if (current = = head) {head = Current.next; current = Head; Listcountvalue-= 1;return;} Else{current = Current.next; Listcountvalue-= 1;}}} public void Printalllistnode () {current = head;for (int i = 0; i < ListCountValue; i++) {System.out.println (current.value); current = Current.next;}}}
Step 3. Test Class for testing
public class Test{public static void Main (string[] args) {Clist Clist = new Clist (); Clist. Append (n); CList. Append (CList); Insert (CList); Insert (CList); Delete (); Clist.printalllistnode ();}}

We'll see:

12226633

Singly linked list algorithm implemented by Java

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.