顺序表的相关操作
来源:互联网 发布:js获取当前时间格式化 编辑:程序博客网 时间:2024/05/22 17:42
//头文件的引入#include#include#include//函数结果状态代码#define TRUE 1#define FLASE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2typedef int Status;typedef int ElemType;//定义顺序表#define LIST_INIT_SIZE 100#define LISTINCREMENT 10typedef struct{ ElemType *elem; int length; int listsize;}SqList;//////////顺序表的初始化Status InitList_Sq(SqList &L){ L.elem = (ElemType*)malloc(sizeof(ElemType)*LIST_INIT_SIZE); if(!L.elem) exit(OVERFLOW); L.length = 0; L.listsize = LIST_INIT_SIZE; return OK;}//Status ListCreate_Sq(SqList &L, int n){ InitList_Sq(L); for(int i = 1; i <= n; ++i){ scanf("%d",&L.elem[i-1]); L.length++; } return OK;}//顺序表的插入Status ListInsert_Sq(SqList &L, int i, ElemType e){ ElemType *newbase; ElemType *q, *p; //先进行插入范围的合理判断,如不合理错误退出 if(i < 1||i > L.length+1) return ERROR; //如果空间不够,则重新创建新空间 if(L.length >= L.listsize){ newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT)*sizeof(ElemType)); if(!newbase) exit(OVERFLOW); L.elem = newbase; L.listsize += LISTINCREMENT; } //将i位置找到,并把地址赋值给q q = &(L.elem[i-1]); //让之后的元素后移,并给i位置赋值e for(p = &(L.elem[L.length -1 ]); p >= q; --p) *(p+1) = *p; *q = e; ++L.length; return OK;}//顺序表的删除Status ListDelete_Sq(SqList &L, int i, ElemType &e){ ElemType *q, *p; //首先,判断删除位置是否合法 if(i < 1||i > L.length) return ERROR; p = &(L.elem[i-1]); e = *p; q = L.elem + L.length - 1; //将i后面的元素前移,表长减一 for(++p; p <= q; ++p) *(p-1) = *p; --L.length; return OK;}//顺序表查找元素位置int ListLocate_Sq(SqList L, ElemType e){ int i,n = 0; for(i = 0; i < L.length; i++) if(L.elem[i] == e){ n = i+1; break; } return n;}//顺序表的输出void ListPrint_Sq(SqList L){ for(int i = 1; i <= L.length; ++i){ if(i == 1) printf("%d",L.elem[i-1]); else printf(" %d",L.elem[i-1]); } printf("\n");}int main(){ SqList L; int n, x; scanf("%d",&n); if(!ListCreate_Sq(L, n)) printf("错误");// scanf("%d",&x);// ListInsert_Sq(L, 3, x);// ListPrint_Sq(L, n); int operationNumber; //操作次数 scanf("%d", &operationNumber); while(operationNumber != 0) { int operationType; //操作种类 scanf("%d", & operationType); if(operationType == 1) { //增加操作 int pos, elem; scanf("%d%d", &pos, &elem); ListInsert_Sq(L, pos, elem); } else if(operationType == 2) { //删除操作 int pos; ElemType elem; scanf("%d", &pos); ListDelete_Sq(L, pos, elem); printf("%d\n", elem); } else if(operationType == 3) { //查找定位操作 ElemType elem; scanf("%d", &elem); int pos = ListLocate_Sq(L, elem); if(pos >= 1 && pos <= L.length) printf("%d\n", pos); else printf("NOT FIND!\n"); } else if(operationType == 4) { //输出操作 ListPrint_Sq(L); } operationNumber--; } return 0;}
阅读全文
0 0
- 顺序表的相关操作
- 顺序表的相关操作
- 顺序表的相关操作
- 顺序表相关操作
- 顺序表相关操作
- 顺序栈的相关操作
- 顺序栈的相关操作
- 数据结构-顺序表相关操作
- 顺序表及相关操作
- 线性表(顺序表)相关操作的c++代码
- (四)线性表的顺序实现及相关操作
- 顺序表的相关操作+删除+添加+遍历+逆序
- C语言:静态顺序表的实现和相关操作
- stack 顺序栈的相关操作
- 顺序容器的相关操作整理
- 顺序栈的定义及相关操作
- 队列的相关操作_顺序存储
- 数据结构顺序表相关基本操作练笔
- CF486D Valid Sets(枚举+树形dp)
- 滚轮事件(wheel)与Transform实现图片上下翻滚
- c语言第一周作业
- 第一篇博客之Android编译环境配置---离线Gradle
- 学习水晶头制作
- 顺序表的相关操作
- Dubbo学习总结
- Qt5学习-模板库、工具类、控件-1
- POJ 1469COURSES
- VHDL中信号与变量的区别及赋值的讨论
- psql -W
- 第K小数(二分)
- LeetCode85 Maximal Rectangle
- Gourmet and Banquet CodeForces