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

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

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

    单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法   package dsa.linkedlist; public class Node<E> ...

  2. [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点

    Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...

  3. [cc150] check palindrome of a singly linked list

    Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...

  4. LeetCode 206 Reverse a singly linked list.

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  5. Singly Linked List

    Singly Linked List Singly linked list storage structure:typedef struct Node{ ElemType data; struct N ...

  6. Reverse a singly linked list

    Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...

  7. [TS] Implement a singly linked list in TypeScript

    In a singly linked list each node in the list stores the contents of the node and a reference (or po ...

  8. [轉]Reverse a singly linked list

    Reverse a singly linked list  http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...

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

    题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

随机推荐

  1. vue框架中的日期组件转换为yyy-mm-dd格式

    最近在用vue框架写一个app,这个是用到的日期格式转换,把下面的标准格式转换为字符串连接格式

  2. "车羊门问题"作业

    作业完成人: 学号:20171301008 潘松泉: 学号:20171301022 陈霖彬: 1.按照你的第一感觉回答,你觉得不换选择能有更高的几率获得汽车,还是换选择能有更高的几率获得汽车?或几率没 ...

  3. Sorl 4.10 入门合集

    Sorl4.10 + Tomcat 7.0  win7环境下的安装 1.首先是到apache官网下载sorl 4.10 ,解压 2.进入路径\solr-4.10.4\example\webapps,拷 ...

  4. The First Day Of Cnblogs

    The fear of the LORD is the beginning of wisdom,and knowledge of the Holy One is understanding. ——Pr ...

  5. 《Linux就该这么学》第十三天课程

    使用Apache服务部署静态网站 原创地址:https://www.linuxprobe.com/chapter-10.html 今天学了Apache,这只是RHCE课程的开始,估计后面越来越难 今天 ...

  6. Jmeter+ant集成接口测试报告

    一.jdk1.8下载及环境配置 1.1 下载地址 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-download ...

  7. Oracle授权

    给连接权限 grant connect to 用户; 给资源权限 grant resource to 用户; 给DBA权限 grant dba to 用户; 授权语句 --select * from ...

  8. 《python语言程序设计》_第6章_函数

    # 6.1_引言 程序1: 结果: Sum from 1 to 10 is 55Sum from 20 to 38 is 513Sum from 35 to 50 is 630 程序2: #程序1和2 ...

  9. 英语演讲稿——Get Along with Fear

    Hi. My name is Zhang Meng. I’m an engineer at Keysight. Today I’m not going to introduce my birthpla ...

  10. 彻底搞懂CSS文本、空白换行问题

    首先,我们来整理一下与换行有关的3个CSS属性: word-break 该属性决定文本内容超出容器时,浏览器是否自动插入换行符. 属性值: normal:默认换行规则——英文以词为单位换行,连续字符不 ...