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. ...
随机推荐
- tensorflow学习之(十一)RNN+LSTM神经网络的构造
#RNN 循环神经网络 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data tf.se ...
- Greenplum 日常维护手册 (汇总、点评、备查)
1. 数据库启动:gpstart常用可参数: -a : 直接启动,不提示终端用户输入确认-m:只启动master 实例,主要在故障处理时使用2. 数据库停止:gpstop:常用可参数:-a:直接停止, ...
- HDU 6377 度度熊看球赛 (计数DP)
度度熊看球赛 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- Astrology PHP 框架
1.Web 服务器配置 PHP 支持 URL Rewrite.PATH_INFO 2.环境需求 PHP 7.2+ 扩展:gettext 3.目录结构 + src | - autoload.php | ...
- react-eslintrc
{ "extends": ["eslint:recommended"], "plugins": [ "react" ], ...
- Python核心团队计划2020年停止支持Python2,NumPy宣布停止支持计划表
Python核心团队计划在2020年停止支持Python 2.NumPy项目自2010年以来一直支持Python 2和Python 3,并且发现支持Python 2对我们有限的资源增加了负担:因此,我 ...
- STM32 HAL库的使用心得
1.I2C函数中HAL_I2C_Mem_Write和HAL_I2C_Master_Transmit有啥区别?{ 使用HAL_I2C_Mem_Write等于 先使用HAL_I2C_Master_Tran ...
- 在ASP.NET Core MVC中构建简单 Web Api
Getting Started 在 ASP.NET Core MVC 框架中,ASP.NET 团队为我们提供了一整套的用于构建一个 Web 中的各种部分所需的套件,那么有些时候我们只需要做一个简单的 ...
- linux下如何把php升级到5.6
1: 进入终端后查看php版本 php -v 输出可能如下: PHP (cli) (built: Nov ::) Copyright (c) - The PHP Group Zend Engine v ...
- Javascript高级编程学习笔记(21)—— 对象原型
JS中对象相关的最重要的恐怕就是原型链了 原型链也是JS中对象继承的实现的基础 接昨天的文章,我们使用构造函数创建对象的时候仍然存在一些问题 那就是所有的实例没法共用一个函数 这样无疑会造成极大的内存 ...