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. ...
随机推荐
- 离线eclipse添加web工程
下载了eclipse,先写的后台程序,后来写前台程序的时候发现没有 新建->web dynamic project,如下方式添加: 帮助->安装新软件,在work with中选择版本对应的 ...
- 关于git的一些命令
git命令 1.git init 初始化仓库 2.git status 查看当前状态 3.git add -A(提交所有的) 提交本地文件到缓存区 4.git commit -m"提交信息& ...
- linux shell数组赋值方法(常用)
http://blog.csdn.net/shaobingj126/article/details/7395161 Bash中,数组变量的赋值有两种方法: (1) name = (value1 ... ...
- JAVA Bean和XML之间的相互转换 - XStream简单入门
JAVA Bean和XML之间的相互转换 - XStream简单入门 背景介绍 XStream的简介 注解简介 应用实例 背景介绍 我们在工作中经常 遇到文件解析为数据或者数据转化为xml文件的情况, ...
- 学以致用三十六-----弄懂python装饰器
看了海峰老师讲解的装饰器视频,讲解的非常棒.根据视频,记录笔记如下: 装饰器: 1.本质是函数,用def来定义.功能就是用来(装饰)其他函数,为其他函数添加附加功能 现有两个函数如下, def tes ...
- 学以致用三十一-----IPAddressField has been removed
python 和 django版本 在进行makemigrations的时候报错 设置的字段 class Servers(models.Model): '''服务器信息''' hostname = m ...
- DAO和service的解释
转自:http://blog.sina.com.cn/s/blog_4b1452dd0102wvox.html 我们都知道有了Hibernate后,单独对数据的POJO封装以及XML文件要耗损掉一个类 ...
- 《Linux就该这么学》
参加了第19期课程的培训,感谢刘老师的辛苦付出,课程讲的很好,真心推荐老刘的这本书真是<Linux就该这么学>!!! 本书是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的 ...
- post和get请求的参数乱码
对于做Java WEB项目同学来说,中文乱码问题是一个经常遇到而又非常头痛的问题,而最容易出现乱码的环节就是在浏览器向服务器发送请求的过程,至于出现乱码的原因不是本文的关注的重点,想了解的朋友可以参考 ...
- TFIDF<细读>
概念 TF-IDF(term frequency–inverse document frequency)是一种用于资讯检索与资讯探勘的常用加权技术.TF-IDF是一种统计方法,用以评估一字词对于一个文 ...