数据结构(清华大学c版//可运行代码)1--顺序表

关于数据结构这一类的书,所提供的代码都是算法码,全码没有给出为此提供关于可运行的全码提供细节参考

#include<bits/stdc++.h>
#include<stdlib.h>
using namespace std;

#define LIST_SIZE 100
#define LIST_C 10
#define OK 1
#define OVERFLOW -1
//#define Status int//宏定义 不推荐代码维护性变差 
typedef int Status;//类型定义 
typedef struct{
	int data;
    
    




    
    
    
}Elem;          
typedef struct {
	
	Elem *elem;//结构体的嵌套 
	int length;
	int listsize;
}sqlist,*lk; 







Status inlist(sqlist &L)//创建 
{
	L.elem=(Elem*)malloc(sizeof(Elem)*LIST_SIZE);    //类c是支持的 
	//L.elem=new Elem[LIST_SIZE];
	if(!L.elem)exit(OVERFLOW);
	L.length=0;
	L.listsize=LIST_SIZE;
	return OK;
 } 
 
 
 
 
 
 
 
 Status Listin(sqlist &L,int i,Elem e)//插入 
 {
 	if(i<1||i>L.length+1) return -2;
	 if(L.length>=L.listsize)
	 {
	 	 
	 	Elem*newbase=(Elem*)realloc(L.elem,(L.listsize+LIST_C)*sizeof(Elem));//newbase 需要声明 
	 	        /*  c++分配空间    std::vector<Elem> newbase(L.listsize + LIST_C);
                std::copy(L.elem, L.elem + L.listsize, newbase.begin());
                L.elem = newbase.data();*/ 
         if(!newbase)exit(OVERFLOW);
         L.elem=newbase;
         L.listsize+=LIST_C;
	  } 
	   Elem*q;
	   Elem*p;
	  
	  q=&(L.elem[i-1]);
	  for(p=&(L.elem[L.length-1]);p>=q;--p)
	  {
	  	*(p+1)=*p;
	  }
	  *q=e;
	  ++L.length;
	  return OK;
 }





 int List_D(sqlist&L,int i,Elem&e)//删除 
 {
 	if(i<L.length||i>L.length+1)return -1;
 	Elem*q;
 	Elem*p;
 	q=&(L.elem[i-1]);       /*
 	                           for(int j=i;j<L.length-1;j++)
								{
                                       L.elem[i-1]=L.elem[i];							
 							
							}   */
 	e=*q;
 	p=L.elem+L.length-1;
 	for(q++;q<=p;p++)
 	*(q-1)=*q;
 	L.length--;
 	
 }
 void Mergelist (sqlist La,sqlist Lb,sqlist &Lc)
 {
 	Elem*pa=La.elem;
	Elem*pb=Lb.elem;
 	Lc.listsize=Lc.length=La.length+Lb.length;
 	Elem*pc=Lc.elem=(Elem*)malloc(sizeof(Elem)*Lc.listsize);
 	if(!Lc.elem)exit(OVERFLOW);
 	Elem*a_last=La.elem+La.length-1;
 	Elem*b_last=Lb.elem+Lb.length-1;
 	while(pa<=a_last&&pb<=b_last)
 	{
 		if(pa->data <= pb->data)
 		{
 			*pc++=*pa++;
		 }
		 else
		 {
		 	*pc++=*pb++;
		 }
	 }
	 while(pa<=a_last)
	 {
	 	*pc++=*pa++;
	 }
	 while(pb<=b_last)
	 {
	 	*pc++=*pb++;
	 }
  } 
  
 
int main()
{
	sqlist La, Lb, Lc;
La.elem = (Elem*)malloc(sizeof(Elem) * LIST_SIZE);
La.length = 5;
La.listsize = LIST_SIZE;
La.elem[0].data = 1;
La.elem[1].data = 3;
La.elem[2].data = 5;
La.elem[3].data = 7;
La.elem[4].data = 10;

Lb.elem = (Elem*)malloc(sizeof(Elem) * LIST_SIZE);
Lb.length = 4;
Lb.listsize = LIST_SIZE;
Lb.elem[0].data = 2;
Lb.elem[1].data = 4;
Lb.elem[2].data = 6;
Lb.elem[3].data = 8;

Lc.elem = (Elem*)malloc(sizeof(Elem) * (La.length + Lb.length));
Lc.length = 0;
Lc.listsize = La.length + Lb.length;

Mergelist(La, Lb, Lc);
for (int i = 0; i <Lc.length; i++) {
    cout << Lc.elem[i].data << " ";
}
cout << endl;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值