首页 技术 正文
技术 2022年11月16日
0 收藏 743 点赞 4,859 浏览 1824 个字

C&C++是那么难学,以至于我连指针是什么都不知道。所以只能学习java了。

如今想用java实现N年前学过“数据结构(c语言版)”却又是那么吃力!

慢慢练吧!

写此博客,仅标记自己学过数据结构。以《数据结构(C语言版)》-严蔚敏 书本为参考。

顺序链表的java类文件:SequenceList.java文件

package list;public class SequenceList {
private int LIST_INIT_SIZE = 5;//链表的原始大小
private int INCREMENT =1;//链表的增量大小
private Object []SqList = null;//链表
private int curIndex=0;//当前位置
/**
* 初始化链表
* */
public void initList()
{
SqList = new Object[LIST_INIT_SIZE];
}
/**
* 向链表中插入元素
* */
public void insertList(Object o)
{
if(curIndex>LIST_INIT_SIZE-1)//判断当前链表是否已经满
{
//从新为链表分配空间
System.out.println("从新分配空间");
LIST_INIT_SIZE+=INCREMENT;
Object []temp = new Object[LIST_INIT_SIZE];
for(int i=0;i<curIndex;i++)
{
temp[i]=SqList[i];
}
SqList=null;
SqList=temp;
}
//链表中如果不让其包含重复元素,则加入这段代码
/*
if(isContain(o))
{
System.out.println("链表中已包含此元素"+o);
}else
{}
*/
SqList[curIndex++]= o;
}
/**
* 判断链表中是否包含某元素
* */
Boolean isContain(Object o)
{
for(int i=0;i<curIndex;i++)
{
if(SqList[i].equals(o))
{
return true;
}
}
return false;
}
/**
* 删除链表中的某元素
*
* 如果包含重复元素都删除
* */
public void delete(Object o)
{
for(int i=0;i<curIndex;i++)
{
if(SqList[i].equals(o))
{
for(int j=i;j<curIndex-1;j++)
{
SqList[j]=SqList[j+1];
}
curIndex--;
continue;
}
if(i==curIndex-1)
{
System.out.println("不存在此元素"+o);
}
}
}/**
* 获取链表中的某个元素
* */
public Object getElement(int i)
{
if (i <= 0 || i > curIndex)
{
System.out.println("获取位置超出了链表中元素个数"+curIndex);
}
return SqList[i-1];
}
/**
* 打印链表
* */
public void print()
{
for(int i=0;i<curIndex;i++)
{
System.out.print(SqList[i]+"\t");
}
System.out.println();
}}

  Main函数测试类

package list;public class SequenceListMain {public static void main(String[] args) {
SequenceList sqList = new SequenceList();
sqList.initList();
sqList.insertList(1);
sqList.insertList(2);
sqList.insertList(3);
sqList.insertList(4);
sqList.insertList(5);
sqList.insertList(6);
sqList.delete(5);
sqList.delete(9);
sqList.insertList(1);
sqList.print();sqList.delete(1);
sqList.print();System.out.println("第2个元素是:"+sqList.getElement(1));
System.out.println("第4个元素是:"+sqList.getElement(4));}}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,993
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845