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
Singly linked list algorithm implemented by Java的更多相关文章
- 单链表反转(Singly Linked Lists in Java)
单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 package dsa.linkedlist; public class Node<E> ...
- [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 ...
- [cc150] check palindrome of a singly linked list
Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...
- LeetCode 206 Reverse a singly linked list.
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- Singly Linked List
Singly Linked List Singly linked list storage structure:typedef struct Node{ ElemType data; struct N ...
- Reverse a singly linked list
Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...
- [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 ...
- [轉]Reverse a singly linked list
Reverse a singly linked list http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...
- Linked List Cycle leetcode II java (寻找链表环的入口)
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
随机推荐
- centos7 防火墙相关命令
启动:systemctl start firewalld禁用:systemctl stop firewalld重新载入规则:firewall-cmd --reload查看所有打开的端口:firewal ...
- appium手机键盘实现方法
首先引入appium的webdriver from appium import webdriver 方法1 AppiumDriver实现了在上述功能,代码如下(java版本) driver.sendK ...
- 申请的阿里云主机ubuntu系统无法显示中文
系统ubuntu 16.04,中文的文件名也无法显示,因为中文包没安装,安装如下: sudo apt-get -y install language-pack-zh-hans sudo apt-get ...
- webapi使用swagger出现“Cannot read property 'parameters' of null”
前端时间在webapi项目使用swagger来提供接口文档及测试工具,按网上方法(http://wmpratt.com/swagger-and-asp-net-web-api-part-1)配置好之后 ...
- JSHFJK师德师风幅度十分时尚大方JSHFJK
sdjfhjksd{104411661166112205880477047710881111099909771088104411111155116605880533055505330500051104 ...
- FPGA计算中定标与位扩展的实现
我不知道名字取对没有,在FPGA计算中有时往往需要在不溢出的情况下将数扩大,从而获得更好的计算精度. 比如.在一个8位宽的系统中,将x=0000_0010,算术左移m=5位之后得到xt=0100_00 ...
- apache与tomcat负载集群集成方法配置
apache与tomcat负载集群集成方法有3种jk.jk_proxy.http_proxy apache:httpd-2.2.17-win32-x86-no_ssl.msi tomcat:apach ...
- 安全运维中基线检查的自动化之ansible工具巧用
i春秋作家:yanzm 原文来自:安全运维中基线检查的自动化之ansible工具巧用 前几周斗哥分享了基线检查获取数据的脚本,但是在面对上百台的服务器,每台服务器上都跑一遍脚本那工作量可想而知,而且都 ...
- 1分钟快速制作漂亮的H5本地记事本
大家好,以前给大家分享过一个五步骤制作精美的HTML5时钟的文章,点击回顾<五步教你制作漂亮精致的HTML时钟>,还有<一分钟教你如何实现唯美的文字描边>:今天给大家分享一个用 ...
- Ubuntu18.04搭建nodejs环境
首先安装sudo apt install curl 然后安装命令(当前最新版本是0.33.2,最新版本可以在https://github.com/creationix/nvm查看): curl -o- ...