将两个顺序存储的有序表合并成一个有序表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将两个顺序存储的有序表合并成一个有序表相关的知识,希望对你有一定的参考价值。

将两个顺序存储的有序表合并成一个有序表(假设用户输入的是有序表,忽略检测用户有序状况的检查),合并后的有序表由两个有序表中的一个来存储
第1,3,5,7行为用户输入提示,2,4,6,8行为用户输入,“output”为输出提示:第10行为输出的结果

将两个顺序存储的有序表合并成一个有序表的代码如下:

#include <stdio.h>

#include <malloc.h>

#include <stdlib.h>

#define MaxSize 50

typedef struct

    int data[MaxSize];

    int length;

SqList;

void ListInsert(SqList *L,int i,int e)

    int j;

    if(i<1||i>L->length+1)

        exit(-1);

    if(L->length>=MaxSize)

        exit(-1);

    for(j=L->length;j>=i;j--)

        L->data[j]=L->data[j-1];

    L->data[i-1]=e;

    L->length++;    

void DispList(SqList *L)

    int i;

    for(i=0;i<L->length;i++)

        printf("%d ",L->data[i]);

    printf("\\n");

void Exchange(SqList *A,SqList *B,SqList *C)


int i=0,j=0,k=0;

    while(i<A->length&&j<B->length)

   

        if(A->data[i]<B->data[j])

            C->data[k++]=A->data[i++];

        else if(A->data[i]>B->data[j])

            C->data[k++]=B->data[j++];

    while(i<A->length)

        C->data[k++]=A->data[i++];

    while(j<B->length)

        C->data[k++]=B->data[j++];

    C->length=k;


void main()

    SqList *A,*B,*C;

    A=(SqList*)malloc(sizeof(SqList));

    A->length=0;

    B=(SqList*)malloc(sizeof(SqList));

    B->length=0;

    C=(SqList*)malloc(sizeof(SqList));

    C->length=0;

    ListInsert(A,1,1);

    ListInsert(A,2,3);

    ListInsert(A,3,5);

    ListInsert(A,4,7);

    ListInsert(A,5,9);

    ListInsert(B,1,2);

    ListInsert(B,2,4);

    ListInsert(B,3,6);

    ListInsert(B,4,8);

    ListInsert(B,5,10);

    ListInsert(B,6,12);

    Exchange(A,B,C);

    printf("顺序表A:");

    DispList(A);

    printf("顺序表B:");

    DispList(B);

    printf("顺序表C:");

    DispList(C);

扩展资料:

第二种解法的核心代码

bool Merge(SeqList A, SeqList B, SeqList &C)

//C为合并后的顺序表

 if (A.length + B.length > C.MaxSize) return false;//超出最大存储空间

 int i = 0, j = 0, k = 0;

 while( i < A.length && j < B.length)

 

  if (A.data[i] <= B.data[j])

   C.data[k++] = A.data[i++];

  else

   C.data[k++] = B.data[j++];

 

 while (i < A.length) C.data[k++] = A.data[i++];

 while (j < B.length) C.data[k++] = B.data[j++];

 C.length = k;

 return true;


参考技术A 楼主你好
具体代码如下:
#include<stdio.h>
#include<stdlib.h>
#define MAX 40

typedef struct

int data[MAX];
int length;
LinkList;

void Initial_List(LinkList * &l,int n)//初始化顺序表

int i=0;
l=(LinkList *)malloc(sizeof(LinkList));
l->length = 0;

for(;i<n;i++)
scanf("%d",l->data+i);
l->length = n;


void Link(LinkList *l1,LinkList *l2,LinkList * &l3)//连接顺序表

int i,j;
l3=(LinkList *)malloc(sizeof(LinkList));
l3->length = l1->length + l2->length;

for(i=0;i<l3->length;i++)

if(i<l1->length)

l3->data[i] = l1->data[i];

else

l3->data[i] = l2->data[i%(l1->length)];



for(i=0;i<l3->length;i++)

for(j=i+1;j<l3->length;j++)

if(l3->data[i]>l3->data[j])

int temp=l3->data[i];
l3->data[i]=l3->data[j];
l3->data[j]=temp;





void Disp_List(LinkList *l)

int i=0;
printf("output:\n");
for(;i<l->length;i++)
printf("%d ",l->data[i]);
printf("\n");


int main()

LinkList *l1,*l2,*l3;
int n;

printf("请输入第一个序列的元素个数:\n");
scanf("%d",&n);
printf("请输入第一个序列的所有元素:\n");
Initial_List(l1,n);
printf("请输入第二个序列的元素个数:\n");
scanf("%d",&n);
printf("请输入第二个序列的所有元素:\n");
Initial_List(l2,n);

Link(l1,l2,l3);
Disp_List(l3);
return 0;


希望能帮助你哈本回答被提问者采纳

两个有序单链表合并成一个有序单链表的java实现

仅作为备注, 便于自己回顾.

import java.util.Arrays;

public class MergeSort {
    public static class LinkedNode<V extends Comparable<V>> {
        public V value;
        public LinkedNode<V> next;

        public LinkedNode(V value) {
            this.value = value;
        }
        public LinkedNode(V value, LinkedNode<V> next) {
            this.value = value;
            this.next = next;
        }
    }

    public static <T extends Comparable<T>> LinkedNode<T> prepareSortedLinkedList(T...list) {
        Arrays.sort(list);
        LinkedNode<T> head = new LinkedNode<>(list[0]);
        LinkedNode<T> begin = head;
        for (int i = 1; i < list.length; i++) {
            begin.next = new LinkedNode<>(list[i]);
            begin = begin.next;
        }
        return head;
    }

    public static <T extends Comparable<T>> LinkedNode<T> cloneLinkedList(LinkedNode<T> head) {
        if (head == null) {
            return null;
        }
        LinkedNode<T> result = new LinkedNode<>(head.value);
        LinkedNode<T> clonedList = result;
        while (true) {
            head = head.next;
            if (head != null) {
                clonedList.next = new LinkedNode<>(head.value);
                clonedList = clonedList.next;
            } else {
                break;
            }
        }
        return result;
    }

    public static <T extends Comparable<T>> void dumpLinkedList(LinkedNode<T> head) {
        while (true) {
            if (head != null) {
                System.out.print(String.valueOf(head.value) + ‘ ‘);
            } else {
                break;
            }
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        LinkedNode<String> head11 = prepareSortedLinkedList("dump", "just", "found", "get");
        LinkedNode<String> head21 = cloneLinkedList(head11);
        dumpLinkedList(head11);
        System.out.println("-------------------------------------------");
        dumpLinkedList(head21);
        System.out.println("-------------------------------------------");
        LinkedNode<String> head12 = prepareSortedLinkedList("get", "zara", "yes", "but", "row", "ok", "another");
        LinkedNode<String> head22 = cloneLinkedList(head12);
        dumpLinkedList(head12);
        System.out.println("-------------------------------------------");
        dumpLinkedList(head22);
        // end prepare
        System.out.println("\n++++++++++++++++++++++++++++++++++++++++++\n");
        // start test
        LinkedNode<String> result1 = mergeSortedLinkedList1(head11, head12);
        dumpLinkedList(result1);
        System.out.println("-------------------------------------------");
        LinkedNode<String> result2 = mergeSortedLinkedList2(head21, head22);
        dumpLinkedList(result2);
    }

    /** recusive. return head node as ascending. will change parameters, non reentrant. */
    public static <T extends Comparable<T>> LinkedNode<T> mergeSortedLinkedList1(LinkedNode<T> a, LinkedNode<T> b) {
        LinkedNode<T> head;
        if (a == null) {
            return b;
        }
        if (b == null) {
            return a;
        }
        if (isFirstLessThanSecond(a.value, b.value)) {
            head = a;
            head.next = mergeSortedLinkedList1(a.next, b);
        } else {
            head = b;
            head.next = mergeSortedLinkedList1(a, b.next);
        }
        return head;
    }

    /** virtual node + loop. return head node as ascending. will change parameters, non reentrant. */
    public static <T extends Comparable<T>> LinkedNode<T> mergeSortedLinkedList2(LinkedNode<T> a, LinkedNode<T> b) {
        LinkedNode<T> header = null, head = null;
        if (a == null) {
            return b;
        }
        if (b == null) {
            return a;
        }
        while (true) {
            if (isFirstLessThanSecond(a.value, b.value)) {
                if (head == null) {
                    header = head = a;
                } else {
                    head.next = a;
                    head = head.next;
                }
                a = a.next;
            } else {
                if (head == null) {
                    header = head = b;
                } else {
                    head.next = b;
                    head = head.next;
                }
                b = b.next;
            }
            if (a == null) {
                head.next = b;
                break;
            } else if (b == null) {
                head.next = a;
                break;
            }
        }
        return header;
    }

    /**
     * 1. null is smallest;
     * 2. if first == null && second == null then return true;
     * 3. if first equals second then return false;
     */
    private static <T extends Comparable<T>> boolean isFirstLessThanSecond(T first, T second) {
        if (first == null) {
            return true;
        }
        if (second == null) {
            return false;
        }
        return first.compareTo(second) < 0;
    }
}

 

 
 

以上是关于将两个顺序存储的有序表合并成一个有序表的主要内容,如果未能解决你的问题,请参考以下文章

两个递增有序表合并成一个递增有序表

两个递增有序表合并成一个递增有序表

两个有序单链表合并成一个有序单链表的java实现

c语言实现两个顺序表的合并

Java实现两个递增有序链表合并成一个递增有序链表和两个非递减有序链表合成一个非递增有序链表

考研数据结构-顺序表(综合应用3)-合并顺序表