華農oj數據結構——8577

8577 合併順序表
時間限制:1000MS 代碼長度限制:10KB
提交次數:5339 通過次數:2251

題型: 編程題 語言: G++;GCC
Description
順序表的基本操作代碼如下:
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef int Status;
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;

Status InitList_Sq(SqList &L)
{ // 算法2.3
// 構造一個空的線性表L。
L.elem = (ElemType )malloc(LIST_INIT_SIZEsizeof(ElemType));
if (!L.elem) return OK; // 存儲分配失敗
L.length = 0; // 空表長度爲0
L.listsize = LIST_INIT_SIZE; // 初始存儲容量
return OK;
} // InitList_Sq

Status ListInsert_Sq(SqList &L, int i, ElemType e)
{ // 算法2.4
// 在順序線性表L的第i個元素之前插入新的元素e,
// i的合法值爲1≤i≤ListLength_Sq(L)+1
ElemType *p;
if (i < 1 || i > L.length+1) return ERROR; // i值不合法
if (L.length >= L.listsize) { // 當前存儲空間已滿,增加容量
ElemType *newbase = (ElemType *)realloc(L.elem,
(L.listsize+LISTINCREMENT)*sizeof (ElemType));
if (!newbase) return ERROR; // 存儲分配失敗
L.elem = newbase; // 新基址
L.listsize += LISTINCREMENT; // 增加存儲容量
}
ElemType *q = &(L.elem[i-1]); // q爲插入位置
for (p = &(L.elem[L.length-1]); p>=q; --p) *(p+1) = *p;
// 插入位置及之後的元素右移
*q = e; // 插入e
++L.length; // 表長增1
return OK;
} // ListInsert_Sq

Status ListDelete_Sq(SqList &L, int i, ElemType &e)
{ // 算法2.5
// 在順序線性表L中刪除第i個元素,並用e返回其值。
// i的合法值爲1≤i≤ListLength_Sq(L)。
ElemType *p, *q;
if (i<1 || i>L.length) return ERROR; // i值不合法
p = &(L.elem[i-1]); // p爲被刪除元素的位置
e = *p; // 被刪除元素的值賦給e
q = L.elem+L.length-1; // 表尾元素的位置
for (++p; p<=q; ++p) *(p-1) = *p; // 被刪除元素之後的元素左移
–L.length; // 表長減1
return OK;
} // ListDelete_Sq

編寫算法,將兩個非遞減有序順序表A和B合併成一個新的非遞減有序順序表C。

輸入格式
第一行:順序表A的元素個數
第二行:順序表A的各元素(非遞減),用空格分開
第三行:順序表B的元素個數
第四行:順序表B的各元素(非遞減),用空格分開

輸出格式
第一行:順序表A的元素列表
第二行:順序表B的元素列表
第三行:合併後順序表C的元素列表

輸入樣例
5
1 3 5 7 9
5
2 4 6 8 10

輸出樣例
List A:1 3 5 7 9
List B:2 4 6 8 10
List C:1 2 3 4 5 6 7 8 9 10

#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef int Status;
typedef struct
{
	int* elem;
	int length;
	int listsize;
}SqList;

Status InitList_Sq(SqList& L)
{  // 算法2.3
  // 構造一個空的線性表L。
	L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
	if (!L.elem) return OK;        // 存儲分配失敗
	L.length = 0;                  // 空表長度爲0
	L.listsize = LIST_INIT_SIZE;   // 初始存儲容量
	return OK;
} // InitList_Sq

Status ListInsert_Sq(SqList& L, int i, ElemType e)
{  // 算法2.4
  // 在順序線性表L的第i個元素之前插入新的元素e,
  // i的合法值爲1≤i≤ListLength_Sq(L)+1
	ElemType* p;
	if (i < 1 || i > L.length + 1) return ERROR;  // i值不合法
	if (L.length >= L.listsize) {   // 當前存儲空間已滿,增加容量
		ElemType* newbase = (ElemType*)realloc(L.elem,
			(L.listsize + LISTINCREMENT) * sizeof(ElemType));
		if (!newbase) return ERROR;   // 存儲分配失敗
		L.elem = newbase;             // 新基址
		L.listsize += LISTINCREMENT;  // 增加存儲容量
	}
	ElemType* q = &(L.elem[i - 1]);   // q爲插入位置
	for (p = &(L.elem[L.length - 1]); p >= q; --p) *(p + 1) = *p;
	// 插入位置及之後的元素右移
	*q = e;       // 插入e
	++L.length;   // 表長增1
	return OK;
} // ListInsert_Sq

Status ListDelete_Sq(SqList& L, int i, ElemType& e)
{  // 算法2.5
  // 在順序線性表L中刪除第i個元素,並用e返回其值。
  // i的合法值爲1≤i≤ListLength_Sq(L)。
	ElemType* p, * q;
	if (i<1 || i>L.length) return ERROR;  // i值不合法
	p = &(L.elem[i - 1]);                   // p爲被刪除元素的位置
	e = *p;                               // 被刪除元素的值賦給e
	q = L.elem + L.length - 1;                // 表尾元素的位置
	for (++p; p <= q; ++p) *(p - 1) = *p;     // 被刪除元素之後的元素左移
	--L.length;                           // 表長減1
	return OK;
} // ListDelete_Sq
int main()
{
	SqList A, B, C;
	InitList_Sq(A);
	InitList_Sq(B);
	InitList_Sq(C);
	int a, b, c, each1, each2;
	scanf("%d", &a);
	for (int i = 0; i < a; i++) {
		scanf("%d", &each1);
		ListInsert_Sq(A, A.length + 1, each1);
	}
	printf("List A:");
	for (int i = 0; i < A.length; i++) {
		printf("%d ", A.elem[i]);
	}
	printf("\n");
	scanf("%d", &b);
	for (int j = 0; j < b; j++) {
		scanf("%d", &each2);
		ListInsert_Sq(B, B.length + 1, each2);
	}
	printf("List B:");
	for (int i = 0; i < B.length; i++) {
		printf("%d ", B.elem[i]);
	}
	printf("\n");
	int i = 0, j = 0;
	while (i < a && j < b) {
		if (A.elem[i] <= B.elem[j]) {
			ListInsert_Sq(C, C.length + 1, A.elem[i]);
			i++;
		}
		else {
			ListInsert_Sq(C, C.length + 1, B.elem[j]);
			j++;
		}
	}
	while (i < a) {
		ListInsert_Sq(C, C.length + 1, A.elem[i]);
		i++;
	}
	while (j < b) {
		ListInsert_Sq(C, C.length + 1, B.elem[j]);
		j++;
	}
	printf("List C:");
	for (int i = 0; i < C.length; i++) {
		printf("%d ", C.elem[i]);
	}
	printf("\n");
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章