Singly linked list algorithm implemented by Java

Jeff Lee blog:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),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(12);
		clist.Append(22);
		clist.Insert(66);
		clist.Insert(33);
		clist.Delete();
		clist.printAllListNode();
	}

}

we will see:

12
22
66
33
时间: 2024-10-08 10:21:05

Singly linked list algorithm implemented by Java的相关文章

单链表反转(Singly Linked Lists in Java)

单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 Java代码   package dsa.linkedlist; public class Node<E>{ E data; Node<E> next; } Java代码   package dsa.linkedlist; public class ReverseLinkedListRecursively { public static void main(String args[])

[cc150] check palindrome of a singly linked list

Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse and compare. 另外一种非常经典的办法是用 Recursive 的思路,把一个list看成这种形式: 0 ( 1 ( 2 ( 3 ) 2 ) 1 ) 0 0 ( 1 ( 2 ( 3 3 ) 2 ) 1 ) 0 CC150里面给出的Code,非常简洁,贴在下面: length == 1 对应

Singly Linked List

Singly linked list storage structure:typedef struct Node{ ElemType data; struct Node *next;}Node; typedef struct Node *LinkList; LinkedList without head node: LinkedList with head node: Operations: /*check the size of link list.*/int ListLength(LinkL

To find the kth to Last Element of a Singly Linked List

To find the kth to Last Element of a Singly Linked List To find the kth to Last Element of a Singly Linked List Web Link Description Code - C Tips Web Link None Description Write a pro-gram to find the kth to Last Ele-ment of a Singly Linked List For

Linked List Cycle leetcode II java (寻找链表环的入口)

题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 题解: 这个连同I都是很经典的题啦,刷CC150时候就折磨了半天. 其实就推几个递推公式就好..首先看图(图引用自CC150): 从链表起始处到环入口长度为:a,从环入口到Faster和Sl

待字闺中之快排(QuickSort)单向链表(Singly Linked List)

题目来源,待字闺中,原创@陈利人 ,欢迎大家继续关注微信公众账号"待字闺中" 分析:思路和数据的快速排序一样,都需要找到一个pivot元素.或者节点.然后将数组或者单向链表划分为两个部分,然后递归分别快排. 针对数组进行快排的时候,交换交换不同位置的数值,在分而治之完成之后,数据就是排序好的.那么单向链表是什么样的情况呢?除了交换节点值之外,是否有其他更好的方法呢?可以修改指针,不进行数值交换.这可以获取更高的效率. 在修改指针的过程中,会产生新的头指针以及尾指针,要记录下来.在par

LeetCode 206 Reverse a singly linked list.

Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 递归的办法: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { v

Convert Sorted List to Binary Search Tree -- leetcodeGiven a singly linked list where elements are s

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 基本思路: 中序遍历的过程,与有序链表的顺序是一一对应的. 采用中序遍历构造进树的构造. 并利用取值范围,确定,根的位置,以及子树的范围. 故需要遍历整个链表,求得总的长度. 链表长度的一半,即为根结点所在位置. 左边则为左子树的取值范围,右边即为右子树的取值范围. 同上,递归应

Linked List {singly linked list -&gt; doubly linked list -&gt; circular linked list}

Linked List 单向链表 Singly linked list /********************************************************* Code writer : EOF Code file : single_linked_list.c Code date : 2015.01.15 e-mail : [email protected] Code description: Here is a implementation for singly