合并单链表

线性表接口

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++;   
         }
  }
 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设有两个单链表A和B,我们需要将它们合并成一个单链表C,那么可以使用以下代码实现: ```c #include <stdio.h> #include <stdlib.h> // 定义单链表节点结构体 typedef struct node { int data; // 数据域 struct node *next; // 指针域 } Node, *LinkedList; // 创建单链表 LinkedList createList(int n) { LinkedList head, p, q; head = (LinkedList)malloc(sizeof(Node)); // 创建头节点 head->next = NULL; // 头节点指针域置为空 q = head; // 令q指向头节点 for (int i = 1; i <= n; i++) { p = (LinkedList)malloc(sizeof(Node)); // 创建新节点 printf("请输入第%d个节点的值:", i); scanf("%d", &p->data); p->next = NULL; // 新节点指针域置为空 q->next = p; // 将新节点插入链表中 q = p; // q指向新节点 } return head; // 返回头节点 } // 合并单链表 LinkedList mergeList(LinkedList A, LinkedList B) { LinkedList C, p, q, r; p = A->next; // p指向链表A的第一个节点 q = B->next; // q指向链表B的第一个节点 C = (LinkedList)malloc(sizeof(Node)); // 创建头节点 C->next = NULL; // 头节点指针域置为空 r = C; // 令r指向头节点 while (p && q) { // 当p和q都不为空时,执行以下操作 if (p->data < q->data) { // 如果A链表当前节点的值小于B链表当前节点的值 r->next = p; // 将A链表当前节点插入到C链表中 p = p->next; // A链表指针后移 } else { // 否则 r->next = q; // 将B链表当前节点插入到C链表中 q = q->next; // B链表指针后移 } r = r->next; // C链表指针后移 } if (p) { // 如果A链表还有剩余节点 r->next = p; // 将剩余节点插入到C链表中 } if (q) { // 如果B链表还有剩余节点 r->next = q; // 将剩余节点插入到C链表中 } free(A); // 释放A链表头节点的空间 free(B); // 释放B链表头节点的空间 return C; // 返回C链表的头节点 } // 输出单链表 void printList(LinkedList head) { LinkedList p = head->next; // p指向链表的第一个节点 while (p) { // 当p不为空时,执行以下操作 printf("%d ", p->data); // 输出节点的值 p = p->next; // 指针后移 } printf("\n"); } int main() { LinkedList A, B, C; int n, m; printf("请输入链表A的长度:"); scanf("%d", &n); A = createList(n); printf("请输入链表B的长度:"); scanf("%d", &m); B = createList(m); C = mergeList(A, B); printf("合并后的链表为:"); printList(C); return 0; } ``` 注:以上代码仅供参考,实际使用中需根据具体情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值