线性表的基本操作
来源:互联网 发布:淘宝订单价格什么字体 编辑:程序博客网 时间:2024/05/17 05:24
//各种头文件 #include<iostream.h> #include<string.h> #include<ctype.h> #include<malloc.h> /* malloc()等 */ #include<limits.h> /* INT_MAX等 */ #include<stdio.h> /* EOF(=^Z或F6),NULL */ #include<stdlib.h> /* atoi() */ #include<io.h> /* eof() */ #include<math.h> /* floor(),ceil(),abs() */ #include<process.h> /* exit() */ /* 函数结果状态代码 */ #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define LIST_INIT_SIZE 10 /* 线性表存储空间的初始分配量 */ #define LISTINCREMENT 2 /* 线性表存储空间的分配增量 */ typedef int Status; /* Status是函数的类型,其值是函数结果状 态代码,如OK等 */ typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */ typedef int ElemType; typedef struct //定义结构体SqList { ElemType *elem; int length; int listsize; }SqList; Status InitList(SqList &L) /* 算法2.3 */ { // 构造一个空的线性表L。 L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if (!L.elem) exit(OVERFLOW); // 存储分配失败 L.length = 0; // 空表长度为0 L.listsize = LIST_INIT_SIZE; // 初始存储容量 return OK; } Status ListInsert(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; } void GetElem(SqList L,int i,int &ai) { ai=L.elem[i]; } void MergeList(SqList La,SqList Lb,SqList &Lc) /* 算法2.2 */ { // 已知线性表La和Lb中的元素按值非递减排列。 // 归并La和Lb得到新的线性表Lc,Lc的元素也按值非递减排列。 int La_len, Lb_len; ElemType ai, bj; int i=0, j=0, k=0; InitList(Lc); La_len = La.length-1; Lb_len = Lb.length-1; while ((i <= La_len) && (j <= Lb_len)) { // La和Lb均非空 GetElem(La, i, ai); GetElem(Lb, j, bj); if (ai <= bj) { ListInsert(Lc, ++k, ai); ++i; } else { ListInsert(Lc, ++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); } } void print(SqList L){for(int i=0;i<L.length;i++){cout<<L.elem[i]<<"\t";}cout<<endl; } void main() { SqList La,Lb,Lc; int j,a[4]={3,5,8,11},b[7]={2,6,8,9,11,15,20}; InitList(La); /* 创建空表La */ for(j=1;j<=4;j++) /* 在表La中插入4个元素 */ ListInsert(La,j,a[j-1]); cout<<"La= "; /* 输出表La的内容 */ print(La); InitList(Lb); /* 创建空表Lb */ for(j=1;j<=7;j++) /* 在表Lb中插入7个元素 */ ListInsert(Lb,j,b[j-1]); cout<<"Lb= "; /* 输出表Lb的内容 */ print(Lb); MergeList(La,Lb,Lc); cout<<"Lc= "; /* 输出表Lc的内容 */ print(Lc); }
0 0
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表基本操作
- 线性表基本操作
- 严重: Error configuring application listener of class com.sun.xml.ws.transport.http.servlet.WSS
- 【算法】轰炸(BOMB)解题报告(模拟提高组)
- 【算法】棋盘游戏(game)解题报告
- 【算法】NOIP2010提高组解题报告
- idea64.exe启动错误:Cannot start under Java 1.7.0xxx
- 线性表的基本操作
- Android DeviceUtils-设备相关工具类
- 相似度计算
- eclipse代码自动提示配置详解
- 获取properties自定义资源文件
- JAVA基础:GUI事件处理及布局要点
- 利用征迈科技物联网检测节能灯亮度产生的时间
- Python中*args 和**kwargs的用法
- 两种方法让scanf遇到空格继续输入字符串