数据结构-顺序表
来源:互联网 发布:还珠格格天上人间知画 编辑:程序博客网 时间:2024/06/07 11:43
顺序表的基本算法的实现,实现如下功能:
BOOL InitList(List *list) //创建线性表
void DestroyList(List *list) //销毁线性表
void ClearList(List *list) //清空线性表
BOOL ListEmpty(List *list) //判断线性表是否为空
int ListLength(List *list) //获取线性表的长度
BOOL GetElem(List *list,int i,Elem *e) //获取指定元素
int LocateElem(List *list,Elem *e) //寻找元素e的位置
BOOL PriorElem(List *list,Elem *currentElem,Elem *preElem) //查找指定元素的直接前驱
BOOL NextElem(List *list,Elem *currentElem,Elem *nextElem) //查找指定元素的直接后继
BOOL ListInsert(List *list,int i,Elem *e) //在指定位置插入指定元素
BOOL ListDelete(List *list,int i,Elem *e) //在指定位置删除元素
void ListTraverse(List *list) //遍历线性表
具体代码如下:
首先是List.h
#ifndef LIST_H#define LIST_Hclass List{public:List(int size);~List();void ClearList();bool ListEmpty();int ListLength();bool GetElem(int i,int *e);int LocateElem(int *e);bool PriorElem(int *currentElem,int *preElem);bool NextElem(int *currentElem,int *nextElem);bool ListInsert(int i,int *e) ; bool ListDelete(int i,int *e) ; void ListTraverse();private:int *m_pList; //一个指针,指向一块内存,存储线性表int m_iSize; //线性表的容量int m_iLength; //已经放了多少个成员};#endif
List.cpp
#include"List.h"#include<iostream>using namespace std;List::List(int size) //构造函数,创建线性表{m_iSize = size;m_pList = new int[m_iSize];//分配内存m_iLength = 0;}List::~List() //析构函数,销毁线性表{delete []m_pList; //释放内存m_pList=NULL;}void List::ClearList() //清空线性表{m_iLength=0;}bool List::ListEmpty() //判断线性表是否为空{if(m_iLength==0)return true;elsereturn false;}int List::ListLength() //获取线性表的长度{return m_iLength;}bool List::GetElem(int i,int *e) //获取指定元素{if(i>m_iLength)return false ;else {*e=m_pList[i];return true;}}int List::LocateElem(int *e) //寻找指定元素的位置{ for(int i=0;i<=m_iLength;i++) { if(m_pList[i] ==*e) return i; } return -1;}bool List::PriorElem(int *currentElem,int *preElem) //查找指定元素的直接前驱{int temp=LocateElem(currentElem);if(temp==-1||temp==0)return false;else {*preElem=m_pList[temp-1];return true;}}bool List::NextElem(int *currentElem,int *nextElem) //查找指定元素的直接后继{int temp=LocateElem(currentElem);if(temp==-1||temp==m_iLength)return false;else{*nextElem=m_pList[temp+1];return true ;}}bool List::ListInsert(int i,int *e) //在指定位置插入元素{if(i<0 || i>m_iLength) //区间 return false;if(m_iSize==m_iLength) return false;else{for(int j=m_iLength+1;j>=i;j--)//插入元素后,从m_iLength个元素开始往后赋值m_pList[j]=m_pList[j-1]; m_iLength++; m_pList[i]=*e; return true ;}}bool List::ListDelete(int i,int *e) //删除指定位置的元素{ if(i<0 || i>m_iLength) //注意区间 return false; else { *e=m_pList[i];for(int j=i+1;j<=m_iLength;j++) //删除元素后,从第i+1个数往前赋值 m_pList[j-1]=m_pList[j] ; m_iLength--; return true ; } } void List::ListTraverse(){cout<<"线性表元素为:"<<endl;for(int i=0;i<m_iLength;i++) //将线性表所有元素依次打印cout<<m_pList[i]<<" ";cout<<endl;}main.cpp
#include<stdio.h>#include<stdlib.h>#include<iostream>#include"List.h"using namespace std;void main(){int e1=3;int e2=5;int e3=4;int e4=6;int e5=9;int e6=1;int e7=2;int temp;List *list1=new List(10);list1->ListInsert(0,&e1);list1->ListInsert(1,&e2);list1->ListInsert(2,&e3); list1->ListInsert(3,&e4); list1->ListInsert(4,&e5); list1->ListInsert(5,&e6);list1->ListInsert(6,&e7);temp=list1->ListLength();list1->GetElem(1,&temp);cout<<"第2个元素为:"<<temp<<endl;cout<<"线性表中是否有3:"<<list1->LocateElem(&e1)<<endl;cout<<"线性表的长度:"<<temp<<endl;list1->PriorElem(&e2,&temp);cout<<"5的直接前驱是:"<<temp<<endl;list1->NextElem(&e2,&temp);cout<<"5的直接后继是:"<<temp<<endl;list1->ListDelete(0,&temp);cout<<"删除的元素为:"<<temp<<endl;list1->ListTraverse();//delete list1;system("pause"); }
编译后的结果如下:
- 数据结构之 顺序表
- 数据结构--顺序线性表
- 数据结构---顺序表
- 数据结构之顺序表
- 数据结构顺序表
- 数据结构顺序表
- 顺序表(数据结构)
- 数据结构之顺序表
- 数据结构之顺序表
- 数据结构笔记--顺序表
- 【数据结构】顺序表
- 数据结构《顺序链表》
- 数据结构之顺序表
- 数据结构试验 顺序表
- 数据结构--顺序表
- 数据结构--顺序表
- 数据结构之顺序表
- 基本数据结构:顺序表
- javamail中文乱码解决
- 第二天学习数据结构
- unity shader (7)--实现高光反射光照模型--逐像素光照
- Project facet Java version 1.7 is not supported.解决方法
- 对板子的检查
- 数据结构-顺序表
- win10系统任务栏卡死怎么办(三种方式)
- Moldflow 2017 CADDoctor 2017 破解方法和下载网址
- libevent学习笔记【使用篇】——7. evbuffer:缓冲IO的实用功能
- PAT 1065. A+B and C
- 算法学习之排序算法(一)(冒泡法)
- mysql内存优化
- 计算机图形学 - 多边形裁剪算法
- 【PAT甲级】1029. Median (25)