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. 虹软人脸检测和识别C# - API

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D ...

  2. HTTP Status 404 – Not Found

    一般都是配置中的问题,这次发现扫描controller时,自己的包是com.aaa.conlller,而springmvc.xml中扫描的是com.aaa.controller,,多写了一个l

  3. selenium之复选框操作

    HTML源码: <!DOCTYPE html> <div lang="en"></div></div> <head> & ...

  4. redis学习-string常用命令

    keys * :查询所有的key值 set:为指定键设置对应的值 get:获取指定键的值 mset:一次传入多个键值对 mget:一次获取多个键的值 del:删除指定键 strlen:获取指定键值的长 ...

  5. 在Linux下部署mysql时,使用group by碰到的问题

    mysql使用group by 的时候报错,错误信息如下: 1055:ER_WRONG_FIELD_WITH_GROUP: Expression #2 of SELECT list is not in ...

  6. 2019浙江省赛K zoj4110 Strings in the Pocket(manachar)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 题意 给你两个串,可以翻转a串的一个区间,问有多少对l,r使得翻转后的a ...

  7. springboot整合redis-sentinel支持Cache注解

    一.前提 已经存在一个redis-sentinel集群,两个哨兵分别如下: /home/redis-sentinel-cluster/sentinel-1.conf port 26379 dir &q ...

  8. vue中集成pdfjs自定义分页

    <template> <div id="div_read_area_scrool" class="no-scrollbar--x" :styl ...

  9. 20155205 郝博雅 Exp9 Web安全基础

    20155205 郝博雅 Exp9 Web安全基础 一.实验内容 一共做了13个题目. 1.WebGoat 输入java -jar webgoat-container-7.1-exec.jar 在浏览 ...

  10. 20155205 郝博雅 Exp7 网络欺诈防范

    20155205 郝博雅 Exp7 网络欺诈防范 一.实践内容 (1)简单应用SET工具建立冒名网站 (1分) (2)ettercap DNS spoof (1分) (3)结合应用两种技术,用DNS ...