数据结构——线性表——顺序存储结构——C++实现线性表
2017-05-12 10:02
459 查看
顺序存储结构C++实现篇:主要实现了线性表的定义、初始化、显示、增、删结点操作。
切记亲力亲为,动手实践写代码
main.cpp
Linear_list.h
Linear_list.cpp
切记亲力亲为,动手实践写代码
main.cpp
/*************************************************************************** * @file main.cpp * @author MISAYAONE * @date 11 May 2017 * @remark 11 May 2017 * @theme Sequence List ***************************************************************************/ #include "Linear_list.h" #include <iostream> using namespace std; int main(int argc, char** argv) { Seque_list L; Init_list(L); List_dispaly(L); Insertion(L,56,3); cout<<endl; cout<<"插入结点后:"<<endl; List_dispaly(L); Delete(L,2); cout<<endl; cout<<"删除结点后:"<<endl; List_dispaly(L); system("pause"); return 0; }
Linear_list.h
//线性表数据结构的定义 #define MAXSIZE 1024 typedef struct { int data[MAXSIZE]; //存放线性表结点的数据空间 int length; //指示线性表当前长度 } Seque_list; //线性表的初始化 bool Init_list(Seque_list &L); //显示线性表的内容 bool List_dispaly(Seque_list L); //线性表的插入操作 bool Insertion(Seque_list &L,int x,int i); //线性表的删除操作 bool Delete(Seque_list &L,int i);
Linear_list.cpp
#include "Linear_list.h" #include <iostream> using std::cin; using std::cout; using std::endl; bool Init_list(Seque_list &L) //传入线性表的引用或者指针,否则不能改变 { L.length = 0; //初始化当前线性表长度 int List_data,i = 0; while (cin>>List_data) { L.data[i] = List_data; ++(L.length); ++i; } return true; } bool List_dispaly(Seque_list L) { cout<<"线性表的大小为:"<<L.length<<endl; cout<<"线性表的数据元素为:"<<" "; for (int i = 0; i < L.length; ++i) { cout<<L.data[i]<<" "; } return true; } //三个参数,讲结点为x的数据插入到线性表L的位置i上 bool Insertion(Seque_list &L,int x,int i) { if (i >= MAXSIZE || L.length>=MAXSIZE-1) //加上溢出判断 { return false; } else { if (i<0 || i>=L.length) //加上非法位置判断 { cout<<"插入位置非法"<<endl; return false; } else { for (int j = L.length;j > i;j--) { L.data[j] = L.data[j-1]; } L.data[i] = x; L.length +=1; //修改插入元素后的线性表大小 } } } bool Delete(Seque_list &L,int i) { if (i<0 || i>=L.length) //非法位置判断 { return false; } else { for (int j = i; j<L.length; j++) { L.data[j] = L.data[j+1]; } L.length -= 1; //修改插入元素后的线性表大小 } }
相关文章推荐
- C++ 数据结构线性表-数组实现
- C++实现数据结构作业——表达式求值
- 程序员面试宝典之数据结构基础----C++两个栈实现一个队列功能
- C++数据结构——二叉搜索树迭代器的实现
- 数据结构之二叉查找树的C++实现
- 数据结构,图的邻接矩阵创建,邻接矩阵与邻接表的交换,两种表的输出,过程用C++实现
- 软件设计师教程 数据结构之线性链表的实现 (C/C++语言)
- 数据结构(5) 链栈 c++ 模板实现
- C++数据结构——二叉搜索树(实现自定义迭代器)
- C/C++用匿名数据结构实现时间和空间名利双收
- C++中实现通用数据结构(转)
- 数据结构之单链表(C++实现)
- 数据结构单项链表C++实现改变C版本
- c++实现List数据结构
- 数据结构之单链表 C++ 实现
- 数据结构之双向循环链表(C++实现)
- 数据结构实验:一元多项式计算器(C++ 实现)
- 数据结构(2)单链表 c++ 模板实现
- 数据结构之链表的实现-------C++课程设计-----学生选课管理系统
- 顺序表.cpp [数据结构实现 之 线性表]