Data Structure Experimentation Report - Merging Algorithm of Two Ordered Linear Tables

Keywords: Algorithm data structure linked list

1. Contents and requirements of the experiment:

1. Create two ordered linear tables from keyboard input (input data of each linear table is entered in order from smallest to largest, regardless of sorting algorithm); Output the two ordered linear tables; Merge the two ordered linear tables into an ordered linear table. The output is a merged ordered linear table.

2. Implement data input and output formats from the keyboard; Two programs with the same function are required, one with sequential storage structure and the other with chain table for linear table storage. The implementation of chain table requires merging by using the nodes of two ascending chain tables, that is, new nodes cannot be created during merging, and the storage space of the original two ascending chain tables does not exist after merging.

2. Main Instructions

1. Brief description of data structure design:

Sequential storage structures use structures with arrays and the maximum storage space for arrays.

Chained storage structures also use structs to define nodes, and use one-way chain lists with additional head nodes, each of which includes data fields of integer or floating type and a pointer field.

2. Brief description of algorithm design:

     Both sequential and chain storage compare the values in the bilinear table one after another. Sequential storage is a new application storage space which stores data in a static array in turn. Chain storage uses the original space to reconnect nodes according to the size of the data.

The first is to initialize two linear tables by allocating memory dynamically, and then reassign them to the newly created third table by comparing the values of the same indexes between the two tables so as to merge the two tables.

The second program establishes two chained tables by setting up a header, setting a loop, and then comparing the data values in the two tables, making the third table equal to the first table, inserting all the first table after sorting, and releasing the second table, so as to merge the tables.

3. Brief description of input/output design:

The first program enters several integers that are not equal to 0, separated by spaces (or CR or TAB) from the keyboard in small to large order, until 0 is entered, then stops the input, establishes the nodes in the order of integer input and connects them sequentially.

The second program enters several integers that are not equal to 0 separated by spaces (or CR or TAB) from the keyboard in random order until 0 is entered, then stops the input, establishes the nodes in the sorted integer input order, and connects the nodes sequentially.

4. Programming language description:

   Programming Platform: Visual Stdio 2019. Programming language: c language.    

5. Description of main functions:

The first program: first dynamically allocate space for linear tables through InitList function, then assign values to linear tables through Input, then merge bilinear tables through MergeList function after initialization, and finally output linear tables through Output function.

    The second program: first, the CreateList function is used to dynamically create the table header while pointing to the next node and assigning values, then the SortList function is used to sort the number of inputs, then the MergeList is used to merge the bilinear tables, and finally the OutputList is used to output the linear tables.

3. Source code:

Procedure 1:

Run example:

Program source code:

#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
typedef struct {
	int *elem;
	int length;
	int listsize;
}SqList;
void InitList(SqList &L) {
	L.elem = (int *)malloc(LIST_INIT_SIZE * sizeof(int));
	if (!L.elem) exit(1);
	L.length = 0;
	L.listsize = LIST_INIT_SIZE;
}
void Input(SqList& L) {
	int x;
	while (1) {
		scanf_s("%d", &x);
		if (x == 0 || L.length >= LIST_INIT_SIZE) break;
		L.elem[L.length++] = x;
	}
}
void Output(SqList &L) {
	for (int i = 0; i < L.length; i++) {
		printf("%3d", L.elem[i]);
	}
}
void MergeList(SqList &La, SqList &Lb, SqList &Lc) {
	int i = 0, j = 0, k = 0;
	Lc.length = La.length + Lb.length;
	while (i < La.length && j < Lb.length) {
		if (La.elem[i] < Lb.elem[j]) {
			Lc.elem[k] = La.elem[i];
			i++;
		}
		else {
			Lc.elem[k] = Lb.elem[j];
			j++;
		}
		k++;
	}
	while (k < Lc.length) {
		if (i == La.length) {
			Lc.elem[k] = Lb.elem[j];
			j++;
		}
		else {
			Lc.elem[k] = La.elem[i];
			i++;
		}
		k++;
	}
}

int main()
{
	SqList a, b, c;
	InitList(a);
	InitList(b);
	InitList(c);
	printf("Input First Linear Table Data:");
	Input(a);
	printf("Input Second Linear Table Data:");
	Input(b);
	printf("First Ordered Linear Table:");
	Output(a);
	printf("\n");
	printf("Second Ordered Linear Table:");
	Output(b);
	printf("\n");
	MergeList(a, b, c);
	printf("Merged Ordered Linear Table:");
	Output(c);
}

Procedure 2:

Run example:

  Program source code:

#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
typedef struct LNode {
	int data;
	struct LNode* next;
}LNode,*LinkList;
LinkList CreatList() {
	LinkList head, tail,p;
	int e;
	head = (LinkList )malloc(sizeof(LNode)*LIST_INIT_SIZE);
	tail = head;
	scanf_s("%d", &e);
	while (e) {
		p = (LinkList )malloc(sizeof(LNode) * LIST_INIT_SIZE);
		p->data = e;
		tail->next = p;
		tail = p;
		scanf_s("%d", &e);
	}
	tail->next = NULL;
	return head;
}
void OutputList(LinkList L) {
	LinkList p = L->next;
	while (p) {
		printf("%3d", p->data);
		p = p->next;
	}
}
void SortList(LinkList L) {
	LinkList p, q;
	int temp;
	for(p=L;p!=NULL;p=p->next)
		for (q = p->next; q != NULL; q = q->next)
		{
			if (p->data >q->data) {
				temp = p->data;
				p->data = q->data;
				q->data = temp;
			}
		}
}
void MergeList(LinkList La, LinkList Lb) {
	LinkList pa, pb, pc;
	pa = La->next;
	pb = Lb->next;
	pc = La;
	free(Lb);
	while(pa && pb)
		if (pa->data <= pb->data) {
			pc->next = pa;
			pc = pa;
			pa = pa->next;
		}
		else {
			pc->next = pb;
			pc = pb;
			pb = pb->next;
		}
	if (pa) pc->next = pa;
	else pc->next = pb;
}
int main()
{
	LinkList La,Lb;
	printf("Enter the first list:");
	La = CreatList();
	SortList(La);
	printf("Enter the second linked list:");
	Lb = CreatList();
	SortList(Lb);
	printf("First sorted list:");
	OutputList(La);
	printf("\n");
	printf("Second sorted list:");
	OutputList(Lb);
	MergeList(La, Lb);
	printf("\n");
	printf("Merged Chain List:");
	OutputList(La);
}

 

Posted by Tryweryn on Tue, 19 Oct 2021 09:26:54 -0700