//用头插法建立单链表
public void GreatLoneList(Object[] a){
LoneListOne s;
for (int i = 0; i < a.length; i++) {
s = new LoneListOne<>(a[i]);
s.next = head.next;
head.next = s;
}
}
3.尾插法的代码实现:
//用尾插法建表
public void GreatLoneListEnd(Object[] a){
LoneListOne s,t;
t = head;
for (int i = 0; i < a.length; i++) {
s = new LoneListOne<>(a[i]);
t.next = s;
t = s;
}
t.next = null;
}
4.求单链表的长度:
//求线性表的长度
public int size(){
LoneListOne p = head;
int count = 0;
while(p.next!=null){
count++;
p = p.next;
}
return count;
}
5.代码实现向单链表中增加元素:
//增:向单链表中末尾增加一个元素
public void Add(Object e){
LoneListOne p = head;
LoneListOne s = new LoneListOne<>(e);
while(p.next!=null){
p = p.next;
p.next = s;
}
}
6.代码实现在单链表中删除元素: 删除元素时,首先需找到其结点所在位置,再改变其指向 1).找结点
//找到序号为i的结点,为删做准备
public LoneListOne geti(int i){
LoneListOne p = head;
int j = -1;
while(j
j++;
p = p.next;
}
return p;
}
2).删元素
//删:在单链表中删除第i个元素
public void Delete(int i,Object e){
if (i<0 ||i>size()-1) {
throw new IllegalArgumentException("元素i不在有效范围之内!");
}
LoneListOne p = geti(i-1);
p.next = p.next.next;
}
7.代码实现单链表中元素修改: 找结点,改元素
//改:设置线性表中序号为i的元素为e
public void SetElem(int i,Object e){
if(i<0||i>size()-1){
throw new IllegalArgumentException("元素i不在有效范围之内!");
}
LoneListOne p = geti(i);
p.data = e;
}
8.代码实现单链表中元素的查找:
//查:寻找序号为i的结点,返回线性表中序号为i的元素
public Object getElem(int i){
LoneListOne p = head;
int j = -1;
while(j
j++;
p = p.next;
}
return p.data;
}
9.代码实现返回单链表中所有元素: 原理为将各个结点遍历,将各结点中的data数据赋给字符串
//将单链表中全部元素转换为字符串并返回
public String ToString(){
String ans = "";
LoneListOne p = head.next;
while(p!=null){
ans+=p.data+" ";
p=p.next;
}
return ans;
}
以上代码均在 class LoneListTwo{} 泛型类中实现
在主类中创建LoneListTwo对象调用方法实现其基本功能:
public class LoneList {
public static void main(String[] args) {
Integer[] a={1,2,3,4,5};
LoneListTwo loneListTwo = new LoneListTwo<>();
loneListTwo.GreatLoneList(a);
String s = loneListTwo.ToString();
System.out.println("输出单链表:"+s);
loneListTwo.GreatLoneListEnd(a);
String s1 = loneListTwo.ToString();
System.out.println("输出单链表:"+s1);
}
}
完整代码如下:
package com.other;
class LoneListOne{
Object data;
LoneListOne next;
//建立单链表头结点模型
public LoneListOne(){
next = null;
}
//建立单链表数据结点模型
public LoneListOne(Object d){
data = d;
next = null;
}
}
class LoneListTwo{
//建立头结点
LoneListOne head = new LoneListOne<>();
//用头插法建立单链表
public void GreatLoneList(Object[] a){
LoneListOne s;
for (int i = 0; i < a.length; i++) {
s = new LoneListOne<>(a[i]);
s.next = head.next;
head.next = s;
}
}
//用尾插法建表
public void GreatLoneListEnd(Object[] a){
LoneListOne s,t;
t = head;
for (int i = 0; i < a.length; i++) {
s = new LoneListOne<>(a[i]);
t.next = s;
t = s;
}
t.next = null;
}
//求线性表的长度
public int size(){
LoneListOne p = head;
int count = 0;
while(p.next!=null){
count++;
p = p.next;
}
return count;
}
//增:向单链表中末尾增加一个元素
public void Add(Object e){
LoneListOne p = head;
LoneListOne s = new LoneListOne<>(e);
while(p.next!=null){
p = p.next;
p.next = s;
}
}
//找到序号为i的结点,为删做准备
public LoneListOne geti(int i){
LoneListOne p = head;
int j = -1;
while(j
j++;
p = p.next;
}
return p;
}
//删:在单链表中删除第i个元素
public void Delete(int i,Object e){
if (i<0 ||i>size()-1) {
throw new IllegalArgumentException("元素i不在有效范围之内!");
}
LoneListOne p = geti(i-1);
p.next = p.next.next;
}
//改:设置线性表中序号为i的元素为e
public void SetElem(int i,Object e){
if(i<0||i>size()-1){
throw new IllegalArgumentException("元素i不在有效范围之内!");
}
LoneListOne p = geti(i);
p.data = e;
}
//查:寻找序号为i的结点,返回线性表中序号为i的元素
public Object getElem(int i){
LoneListOne p = head;
int j = -1;
while(j
j++;
p = p.next;
}
return p.data;
}
//将单链表中全部元素转换为字符串并返回
public String ToString(){
String ans = "";
LoneListOne p = head.next;
while(p!=null){
ans+=p.data+" ";
p=p.next;
}
return ans;
}
}
public class LoneList {
public static void main(String[] args) {
Integer[] a={1,2,3,4,5};
LoneListTwo loneListTwo = new LoneListTwo<>();
loneListTwo.GreatLoneList(a);
String s = loneListTwo.ToString();
System.out.println("输出单链表:"+s);
loneListTwo.GreatLoneListEnd(a);
String s1 = loneListTwo.ToString();
System.out.println("输出单链表:"+s1);
}
}