1.单链表
package unLinkedList;
package unLinkedList;
public interface IList {
//判断是否为空
public boolean isEmpty();
//插入元素
public void insert(int index,Object o)throws Exception;
//删除元素
public void delete(int index)throws Exception;
//获取链表长度
public int getLength();
//获取元素
public Object get(int index)throws Exception;
}
//单向链表
public class UNode {
//数据域
Object obj;
//指针
UNode next;
public UNode(UNode nextval){
this.next=nextval;
}
public UNode(Object obj,UNode nextval){
this.next=nextval;
this.obj=obj;
}
public UNode getNext(){
return this.next;
}
public void setNext(UNode nextval){
this.next=nextval;
}
public Object getObj(){
return this.obj;
}
public void setObj(Object obj){
this.obj=obj;
}
public String toString(){
return this.obj.toString();
}
}
package unLinkedList;
//单向链表
public class UnLinkedList implements IList{
//头指针
UNode head;
//当前节点对象
UNode cur;
int size;
//初始化
public UnLinkedList(){
this.head=cur=new UNode(null);
this.size=0;
}
//指针定位,定位到插入节点之前的一个节点
public void index(int index)throws Exception{
if(index<-1 || index>size-1){
System.out.println("错误!");
}
//头结点
if(index==-1)
return;
cur=head.next;
int j=0;
while (cur!=null && j<index){
cur=cur.next;
j++;
}
}
@Override
public boolean isEmpty() {
return size==0;
}
@Override
public void insert(int index, Object o) throws Exception {
if(index<0 || index>size){
throw new Exception("参数错误!");
}
//插入节点的前一个节点
index(index-1);
cur.setNext(new UNode(o,cur.next));
size++;
}
@Override
public void delete(int index) throws Exception {
if(isEmpty()){
throw new Exception("链表为空,删除错误!");
}
if(index<0 || index>size){
throw new Exception("传入参数错误!");
}
//定位到删除节点之前的一个节点
index(index-1);
cur.setNext(cur.next.next);
size--;
}
@Override
public int getLength() {
return this.size;
}
@Override
public Object get(int index) throws Exception {
if (index<-1 || index>size-1){
throw new Exception("传入参数错误!");
}
index(index);
return cur.getObj();
}
public static void main(String[] args)throws Exception{
UnLinkedList list=new UnLinkedList();
for (int i = 0; i < 5; i++) {
int temp=((int)(Math.random() * 1000)) * 1000;
list.insert(i,temp);
System.out.println(temp+"--");
}
list.delete(4);
System.out.println("---------------------");
for (int i = 0; i <list.size ; i++) {
System.out.println(list.get(i)+"--");
}
}
}
2.双向循环链表
package unLinkedList;
//双向链表
public class DNode {
//数据域
Object obj;
//后继针域
DNode next;
//前驱指针域
DNode prior;
public DNode(DNode nextval){
this.next=nextval;
}
public DNode(Object obj, DNode nextval){
this.next=nextval;
this.obj=obj;
}
public DNode getNext(){
return this.next;
}
public void setNext(DNode nextval){
this.next=nextval;
}
public Object getObj(){
return this.obj;
}
public void setObj(Object obj){
this.obj=obj;
}
public DNode getPrior(){
return this.prior;
}
public void setPrior(DNode priorval){
this.prior=priorval;
}
public String toString(){
return this.obj.toString();
}
}
package unLinkedList;
//双向链表
public class DLinkedList implements IList{
//头指针
DNode head;
//当前节点对象
DNode cur;
int size;
//初始化
public DLinkedList(){
this.head=cur=new DNode(null);
this.size=0;
this.head.next=head;
this.head.prior=head;
}
//指针定位,定位到插入节点之前的一个节点
public void index(int index)throws Exception{
if(index<-1 || index>size-1){
System.out.println("错误!");
}
//头结点
if(index==-1)
return;
cur=head.next;
int j=0;
while (cur!=head && j<index){
cur=cur.next;
j++;
}
}
@Override
public boolean isEmpty() {
return size==0;
}
@Override
public void insert(int index, Object o) throws Exception {
if(index<0 || index>size){
throw new Exception("参数错误!");
}
//插入节点的前一个节点
index(index-1);
cur.setNext(new DNode(o,cur.next));
cur.next.setPrior(cur.next);
cur.next.next.setPrior(cur.next);
size++;
}
@Override
public void delete(int index) throws Exception {
if(isEmpty()){
throw new Exception("链表为空,删除错误!");
}
if(index<0 || index>size){
throw new Exception("传入参数错误!");
}
//定位到删除节点之前的一个节点
index(index-1);
cur.setNext(cur.next.next);
cur.next.setPrior(cur);
size--;
}
@Override
public int getLength() {
return this.size;
}
@Override
public Object get(int index) throws Exception {
if (index<-1 || index>size-1){
throw new Exception("传入参数错误!");
}
index(index);
return cur.getObj();
}
public static void main(String[] args)throws Exception{
DLinkedList list=new DLinkedList();
for (int i = 0; i < 5; i++) {
int temp=((int)(Math.random() * 1000)) * 1000;
list.insert(i,temp);
System.out.println(temp+"--");
}
list.delete(4);
System.out.println("---------------------");
for (int i = 0; i <list.size ; i++) {
System.out.println(list.get(i)+"--");
}
}
}
3.链表反转
package unLinkedList;
public class reverseList {
public static UNode reverseList(UNode head){
//前一个节点的指针
UNode preNode=null;
//当前节点指针
UNode curNode=head;
//下一个节点指针
UNode nextNode=null;
//如果首节点为空
if(head==null || head.getNext()==null){
return head;
}
//首节点不为空
while (curNode!=null){
//前后节点交换
nextNode=curNode.getNext();
curNode.setNext(preNode);
preNode=curNode;
curNode=nextNode;
}
return preNode;
}
}
4.合并链表、求链表中间节点
package unLinkedList;
package unLinkedList;
public class MNode {
//数据域
int obj;
//指针
MNode next;
public MNode(int obj){
this.obj=obj;
}
public MNode(int obj,MNode nextval){
this.next=nextval;
this.obj=obj;
}
public MNode getNext(){
return this.next;
}
public void setNext(MNode nextval){
this.next=nextval;
}
public Object getObj(){
return this.obj;
}
public void setObj(int obj){
this.obj=obj;
}
}
//将两个有序链表合并
/*
*@param node1有序链表1
*@param node2有序链表2
*@return 合并后的链表
* */
public class mergeList {
public static MNode mergeList(MNode node1, MNode node2) {
//使用递归的方式进行合并,终止条件
if (node1 == null && node2 == null) {
return null;
}
if (node1 == null) {
return node2;
}
if (node2 == null) {
return node1;
}
//合并后的链表
MNode node = null;
//为了方便,这里将数据类型转换为Int
if ((int) node1.obj > (int) node2.obj) {
//小的放在前面
node = node2;
node.next = mergeList(node1, node2.next);
} else {
node = node1;
node.next = mergeList(node2, node1.next);
}
return node;
}
//求链表中间节点
public static MNode middleNode(MNode head) {
if (head == null || head.next == null) {
return head;
}
MNode slow = head;
MNode fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return fast == null ? slow : slow.next;
}
public static void main(String[] args) throws Exception {
MNode node1 = new MNode(1);
MNode node2 = new MNode(2);
MNode node3 = new MNode(3);
MNode node4 = new MNode(4);
MNode node5 = new MNode(5);
node1.next = node3;
node3.next = node5;
node2.next = node4;
MNode node = mergeList(node2, node1);
while (node != null) {
System.out.print(node.obj + " ");
node = node.next;
}
}
}