数据结构题解:线性表的创建,插入数据,排序,合并。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define ERROR 0
#define INFEASIBLR -1

using namespace std;
typedef int Status;
typedef int ElemType;
int k;
typedef struct
{
	ElemType *elem; 
	int length; 
	int listsize; 
}SQList;

Status InitList_Sq(SQList &L, int n)
{
	L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
	if(! L.elem)
		exit(1);  存储分配失败
	L.length = 0;
	L.listsize = LIST_INIT_SIZE;
	cout << "请输入" << n << "个表中的数据" << endl;
	for(int i = 0;i < n;i++)
	{
		cin >> L.elem[i];
		++L.length;
		if(L.length>= L.listsize){
			ElemType *newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT)* sizeof(ElemType));
			if(!newbase)  exit(0);
			L.elem = newbase;
			L.listsize += LISTINCREMENT;
		}
	}
	return true;
}
Status ListInsert_Sq(SQList &L,int i,ElemType e)    
{    
    //在顺序线性表L中第i个位置之前插入新的元素e      
    //i的合法值为1<=i<=ListLength_Sq(L)+1      
    if(i <1 || i> L.length + 1)    
        return false;   //i值不合法      
    if(L.length >= L.listsize)   //当前存储空间已满,增加分配      
    {    
        ElemType *newbase = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT )* sizeof(ElemType));    
        if(!newbase)    
            exit(1);    //存储分配失败      
        L.elem = newbase;//新基址      
        L.listsize += LISTINCREMENT;    //增加存储容量      
    }    
    
    ElemType *q = &(L.elem[i-1]);//q为插入位置      
    
    for(ElemType *p = &(L.elem[L.length-1]);p>=q;--p)    
        *(p+1) = *p;    //插入位置及之后的元素右移      
    
    *q = e;     //插入e      
    ++L.length;     //表长增1   
	
	ElemType l = *q; //用一个自定义的变量 返回指针q中所指代的值
    return l;    
}
void union(List &La, List Lb
)
{
	int La_len = ListLeng(La);
	int Lb_len = ListLeng(Lb);
	for(int i = 1;i <= Lb_len; i++){
		GetElem(Lb, i, e);
		if(!LocateElem(La, e, equal))
			ListInsert(La, ++La_len, e);
	}
}

viod MergeList(Lisit La, List Lb, List &Ld){
	initList(Ld);
	int i, j;
	i = j = 1;	
	k = 0;
	int La_len = ListLength(La);
	int Lb_len = ListLength(Lb);
	while((i <= La_len) && (j <= Lb_len)){
		GetElem(La, i, ai);
		GetElem(Lb, j, bj);
		if(ai <= bj){
			ListInsert(Ld, ++k, ai);
			++i;
		}
		else {
			ListInsert(Ld, ++k, bj); 
			++j;
		}
	}
	while(i <= La_len){
		GetElem(La, i++ ,ai);
		ListInsert(Lc, ++k, ai);
	}
	while(j <= Lb_len){
		GetElem(Lb, j++, bj);
		ListInsert(Lc, ++k, bj);
	}
}
Status display(SQList &L,int m){
	for(int i =0;i <= m;i++){
		cout << L.elem[i] << endl;
	}
	return true;
}
int main(){
	cout << "请输入你要插入La线性表的数量na" << endl;
	int na; cin >> na;
	InitList(La, na);
	cout << "请输入你要插入Lb线性表的数量nb" << endl;
	int nb; cin >> nb;
	InitList(Lb, nb);
	initList(Lc);
	union(La, Lb);
	cout << "Lc为La,Lb表中的数据为:" << endl;
	display_sq(Lc, k);
	MerList(La, Lb, Ld);
	cout << "Lb非递减表中的数据为:" << endl;
	display_sq(Ld, k);
	return 0
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值