博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Singly linked list algorithm implemented by Java
阅读量:4343 次
发布时间:2019-06-07

本文共 1868 字,大约阅读时间需要 6 分钟。

Jeff Lee blog:     (泥沙砖瓦浆木匠),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:

12226633

转载于:https://www.cnblogs.com/Alandre/p/4102848.html

你可能感兴趣的文章
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>