数据结构_顺序表
来源:互联网 发布:淘宝商城加盟代理 编辑:程序博客网 时间:2024/05/01 08:01
运行时效果
/************************************************************ 说明: 1、主函数内的代码是为了测试方便,可以自行修改。 2、宏定义NEWS是人机交互信息提示,若不需要,可修改为0。 3、若是windows系统,请将258行中的 clear 修改为 cls。 4、在输入数据后,请多按一下回车,实现清屏。 环境:ubuntu12.04LTS、codeblocks10.05、2014-04-02************************************************************/#include <iostream>#include <stdlib.h>#include <malloc.h>#include <cstring>#define NEWS 1#define LIST_INIT_SIZE 100#define LIST_ADD_SIZE 10#define NEW_SIZE (L->list_size + LIST_ADD_SIZE)using namespace std;typedef int Elem_Type;typedef struct{ Elem_Type *data;//元素 int len; //长度 int list_size; //空间}Sq_List;typedef enum{ OK = 0, //正常 ERROR = -1, //逻辑异常 OVER = -2 //内存异常}Status;/****************************** 函数名:Init_List 功 能:构造、初始化线性表******************************/Sq_List *Init_List(void){ Sq_List *L = (Sq_List *)malloc(sizeof(Sq_List)); if(!L) exit(OVER); L->data = (Elem_Type *)malloc(LIST_INIT_SIZE*sizeof(Elem_Type)); if(!L->data) exit(OVER); L->len = 0; L->list_size = LIST_INIT_SIZE; #if(NEWS) cout<<"构造成功,已初始化"<<endl; #endif return L;}/****************************** 函数名:Destroy_List 功 能:销毁线性表******************************/Status Destroy_List(Sq_List *L){ free(L); #if(NEWS) cout<<"销毁成功,请不要再使用"<<endl; #endif return OK;}/****************************** 函数名:Clear_List 功 能:清空线性表******************************/Status Clear_List(Sq_List *L){ memset(L->data,-1,sizeof(L->data)); L->len = 0; #if(NEWS) cout<<"已全部清空"<<endl; #endif return OK;}/****************************** 函数名:Empty_List 功 能:判断线性表是否为空******************************/bool Empty_List(Sq_List *L){ return L->len == 0;}/****************************** 函数名:Length_List 功 能:获取线性表长度******************************/int Length_List(Sq_List *L){ return L->len;}/****************************** 函数名:Get_Elem_List 功 能:指定位置获取元素******************************/Status Get_Elem_List(Sq_List *L, int index, Elem_Type *e){ if( !(1 <= index && index <= Length_List(L)) ) #if(NEWS) { cout<<"获取位置不合法"<<endl <<"下面显示的数据是上次输入的num的值,请注意!!!"<<endl; return ERROR; } #else return ERROR; #endif *e = L->data[index-1]; return OK;}/****************************** 函数名:Insert_List 功 能:指定位置插入元素******************************/Status Insert_List(Sq_List *L, int index, Elem_Type e){ if( !(1 <= index && index <= Length_List(L)+1) ) #if(NEWS) { cout<<"插入位置不合法"<<endl; return ERROR; } #else return ERROR; #endif if( Length_List(L) >= L->list_size) #if(NEWS) cout<<"刚才增加了存储空间"<<endl; #endif L->data = (Elem_Type *)realloc(L->data, NEW_SIZE*sizeof(Elem_Type)); if(!L->data) exit(OVER); L->list_size += LIST_ADD_SIZE; for(int i=Length_List(L); i >= index-1; i--) L->data[i+1] = L->data[i]; L->data[index-1] = e; L->len++; #if(NEWS) cout<<"插入成功"<<endl; #endif return OK;}/****************************** 函数名:Delete_List 功 能:指定位置删除元素******************************/Status Delete_List(Sq_List *L, int index, Elem_Type *e){ if( Empty_List(L) || !(1 <= index && index <= Length_List(L)) ) #if(NEWS) { cout<<"删除位置不合法 or 目前是空表"<<endl; return ERROR; } #else return ERROR; #endif *e = L->data[index -1]; for(int i=index; i<Length_List(L); i++) L->data[i-1] = L->data[i]; #if(NEWS) cout<<"删除成功"<<endl; #endif L->len--; return OK;}/****************************** 函数名:Print_List 功 能:输出所有元素******************************/Status Print_List(Sq_List *L){ if( Empty_List(L) ) return ERROR; int temp; for(int i=1; i<=Length_List(L); i++) { Get_Elem_List(L,i,&temp); cout<<temp<<" "; } cout<<endl; return OK;}/****************************** 函数名:print_news 功 能:方便用户选择******************************/void print_news(void){ cout<<"\n\n\n\t\t********************" <<"*****************************"<<endl <<"\t\t*\t\t0 建立、初始化\t\t\t*"<<endl <<"\t\t*\t\t\t\t\t\t*"<<endl <<"\t\t*\t\t1 插入元素\t\t\t*"<<endl <<"\t\t*\t\t\t\t\t\t*"<<endl <<"\t\t*\t\t2 删除元素\t\t\t*"<<endl <<"\t\t*\t\t\t\t\t\t*"<<endl <<"\t\t*\t\t3 销毁 \t\t\t*"<<endl <<"\t\t*\t\t\t\t\t\t*"<<endl <<"\t\t*\t\t4 获取表长\t\t\t*"<<endl <<"\t\t*\t\t\t\t\t\t*"<<endl <<"\t\t*\t\t5 清空 \t\t\t*"<<endl <<"\t\t*\t\t\t\t\t\t*"<<endl <<"\t\t*\t\t6 获取元素\t\t\t*"<<endl <<"\t\t*\t\t\t\t\t\t*"<<endl <<"\t\t*\t\t7 打印 \t\t\t*"<<endl <<"\t\t*\t\t\t\t\t\t*"<<endl <<"\t\t*\t\t8 退出 程序\t\t\t*"<<endl <<"\t\t********************" <<"*****************************"<<endl;}int main(void){ Sq_List *test = NULL; while(true) { print_news(); int choose,index,num; cout<<"要进行什么操作?"<<endl; cin>>choose; switch(choose) { case 0: test = Init_List();break; case 1: cout<<"插入位置 "<<endl; cin>>index; cout<<"插入数据 "<<endl; cin>>num; Insert_List(test,index,num);break; case 2: cout<<"删除的位置"<<endl; cin>>index; Delete_List(test,index,&num); cout<<"被删除的元素是:"<<num<<endl;break; case 3: Destroy_List(test);break; case 4: cout<<Length_List(test)<<endl;break; case 5: Clear_List(test);break; case 6: cout<<"获取哪个位置的元素"<<endl; cin>>index; Get_Elem_List(test,index,&num); cout<<num<<endl;break; case 7: Print_List(test);break; case 8: return 0; default: break; } getchar(); getchar(); system("clear"); } return 0;}
1 0
- 数据结构_顺序表
- 数据结构_顺序表
- 数据结构_顺序表
- 数据结构_顺序表
- 数据结构_动态顺序表
- c语言_数据结构_顺序表
- 数据结构-线性表_顺序表
- 数据结构-线性表_顺序表
- 数据结构的C实现_顺序表
- 数据结构_线性表顺序sequence List
- 数据结构整理_有序顺序表合并
- 数据结构基础1_顺序表
- 数据结构_顺序栈
- 【数据结构_顺序表_List_1102】顺序表上数据划分
- 数据结构_线性表_顺序/链式存储优缺点比较
- C++_012_数据结构_栈_顺序表储存版
- 数据结构_线性表_顺序队列_循环队列_链队列
- 数据结构整理_线性表之顺序表
- JFinal与Qui学习笔记(二)
- java中ExecutorService接口
- Android学习笔记(一)——Android平台简介
- I2C协议
- 可进行查找删除插入的链表
- 数据结构_顺序表
- extern "C"的用法解析
- 短时傅立叶变换、小波变换
- 【算法练习】
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- 【日志】2014_04_01 ssh 的小结
- 浅谈REDIS数据库的键值设计
- GUN automake,autoconf写Makefile(一)
- System.in.read() IOException RuntimeException