合并单链表
来源:互联网 发布:河北网站seo 编辑:程序博客网 时间:2024/05/29 08:56
线性表接口
public interface ILinarList<E>{
boolean add(E item); //添加元素
boolean add(int index, E item);//插入元素
E remove(int index); //删除元素
int indexOf(E item); //定位元素
E get(int index); //取表元素
int size(); //求线性表长度
void clear();//清空线性表
boolean isEmpty(); //判断线性表是否为空
}
单链表实现线性表
public class SLinkList<E> implements ILinarList<E> {
private Node<E> start;// 单链表的头引用
int size;// 单链表的长度
//链表数据类以及构造方法
private static class Node<E> {
E item;
Node<E> next;
Node(E element, Node<E> next) {
this.item = element;
this.next = next;
}
}
// 初始化线性表
public SLinkList() {
start = null;
}
// 添加元素,将元素添加在单链表的末尾
public boolean add(E item) {
if (start == null) {
start = new Node<E>(item, null);
} else {
Node<E> current = start;
while (current.next != null) {
current = current.next;
}
current.next = new Node<E>(item, null);
}
size++;
return true;
}
// 在单链表的第index索引位置前插入一个数据元素
public boolean add(int index, E item) {
Node<E> current;
Node<E> previous;
if (index < 0 || index > size) {
return false;
}
Node<E> newnode = new Node<E>(item, null);
// 在空链表或第一个元素前插入第一个元素
if (index == 0) {
newnode.next = start;
start = newnode;
size++;
} else {
// 单链表的两个元素间插入一个元素
current = start;
previous = null;
int j = 0;
while (current != null && j < index) {
previous = current;
current = current.next;
j++;
}
if (j == index) {
previous.next = newnode;
newnode.next = current;
size++;
}
}
return true;
}
// 删除单链表中的索引位置为index的数据元素
public E remove(int index) {
E oldValue = null;
if (isEmpty() || index < 0 || index > size - 1) {
oldValue = null;
}
Node<E> current = start;
if (index == 0) {
oldValue = current.item;
start = current.next;
size--;
} else {
Node<E> previous = null;
int j = 1;
while (current.next != null && j <= index) {
previous = current;
current = current.next;
j++;
}
previous.next = current.next;
oldValue = current.item;
current = null;
size--;
}
return oldValue;
}
// 在单链表中查找数据元素item数据位置
public int indexOf(E item) {
int index = 0;
if (item == null) {
for (Node<E> x = start; x != null; x = x.next) {
if (x.item == null)
return index;
index++;
}
} else {
for (Node<E> x = start; x != null; x = x.next) {
if (item.equals(x.item))
return index;
index++;
}
}
return -1;
}
// 获得单链表的第index索引位置的数据元素
public E get(int index) {
E item = null;
if (isEmpty() || index < 0 || index > size - 1) {
item = null;
}
Node<E> current = start;
int j = 0;
while (current.next != null && j < index) {
current = current.next;
j++;
}
if (j == index) {
item = current.item;
}
return item;
}
//求单链表长度
public int size() {
return size;
}
//清空单链表
public void clear() {
for (Node<E> x = start; x != null;) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x = next;
}
start = null;
size = 0;
}
//判断单链表是否为空
public boolean isEmpty() {
return size == 0;
}
//输出单链表
public void P(){
Node<E> current = start;
while(current != null){
System.out.print(current.item + " ");
current=current.next;
}
System.out.println();
}
}
测试类
public class Test1 {
public static void main(String[] args) {
Scanner read=new Scanner(System.in);
while(read.hasNext()){
int n=read.nextInt();int m=read.nextInt();
int[] a=getRandomArrayByIndex(n,100);
int[] b=getRandomArrayByIndex(m,100);
SLinkList<Integer> A = new SLinkList<Integer>();
SLinkList<Integer> B = new SLinkList<Integer>();
System.out.println("排序前:");
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
System.out.println("排序后:");
Arrays.sort(a);Arrays.sort(b);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
for(int i=0;i<n;i++){
A.add(a[i]);
}
for(int i=0;i<m;i++){
B.add(b[i]);
}
System.out.println("合并后:");
merge(A,B);
A.P();
}
read.close();
}
//参数说明:num--无重复随机数的个数 scope--随机数所在范围(不包含scope)
public static int[] getRandomArrayByIndex(int num,int scope){
//1.获取scope范围内的所有数值,并存到数组中
int[] randomArray=new int[scope];
for(int i=0;i<randomArray.length;i++){
randomArray[i]=i;
}
//2.从数组random中取数据,取过后的数改为-1
int[] numArray=new int[num];//存储num个随机数
int i=0;
while(i<numArray.length){
int index=(int)(Math.random()*scope+1);
if(randomArray[index]!=-1){
numArray[i]=randomArray[index];
randomArray[index]=-1;
i++;
}
}
return numArray;
}
//合并单链表
public static void merge(SLinkList<Integer> A,SLinkList<Integer> B) {
int o=0,p=0,s=0;
while(s<B.size()){
p=s;
while(p<B.size()){
if(A.get(o)>B.get(p)){
A.add(o,B.get(p));s++;
}
p++;
}
o++;
}
}
}
- 单链表合并
- 单链表合并
- 单链表合并
- 单链表合并
- 合并单链表
- 合并单链表
- 合并两个单链表练习,多项式合并
- 单链表(合并单链表)
- 单链表合并操作
- 合并两个有序单链表
- /*循环单链表合并操作
- 单链表合并操作
- 有序单链表的合并
- 有序单链表的合并
- 合并两个有序单链表
- 单链表创建、排序、合并
- 两个单链表的合并
- 循环单链表的合并
- CodeForces 893B Beautiful Divisors (打表)
- Hadoop完全分布式集群安装Hbase
- springboot依赖下载很慢,使用maven镜像
- UV旋转动画Shader
- 抽象类和接口的区别
- 合并单链表
- centos7安装shipyard没有本地容器及镜像附安装步骤
- win下mysql启动配置
- 网狐荣耀版短信验证开发流程说明
- 一个简单的整数计数器程序
- sql server 2008R2数据库导出表里所有数据成insert 语句
- 双11越来越“高大上”,你的工资还配得上它吗?
- 十三周 项目1(3)
- 深度学习