线性表之顺序表

顺序表

顺序表的基本概念

  • 是用一组地址连续的存储单元依次存储线性表中的数据元素,从而使得逻辑上相邻的两个元素在位置上也相邻
  • 存在唯一的一个被称作第一个的数据元素
  • 存在唯一的一个被称作最后一个的数据元素
  • 除第一个外,集合中的每个数据元素均只有一个前继元素
  • 除最后一个外,集合中的每个数据元素均只有一个后继元素。

代码实现

 1 #include<iostream>
 2 using namespace std;
 3 #define MaxSize 50
 4 class Node
 5 {
 6 public:
 7     int Date;
 8 };
 9 class SqList  
10 {
11 public:
12     SqList();//初始化顺序表
13     virtual ~SqList();//在析构函数中销毁
14     bool ListInsert(int i,int Date);//插入第i个元素
15     bool ListDelete(int i);//删除第i个元素
16     bool Delete(int Date);//按值删除元素
17     int LocateElem(int a);//按值查找
18     int GetLength();//当前线性表长度
19     bool Isempty();//是否为空
20     bool IsFull();//是否满
21     void DeleteAll();//删除所有元素    
22 private:
23     Node *date;
24     int Length;
25 
26 };
  1 #include "stdafx.h"
  2 #include "SqList.h"
  3 #include<iostream>
  4 #include"cstdlib"
  5 using namespace std;
  6 //////////////////////////////////////////////////////////////////////
  7 // Construction/Destruction
  8 //////////////////////////////////////////////////////////////////////
  9 //构造函数
 10 SqList::SqList()
 11 {
 12     date=new Node[MaxSize];
 13     if(!date)
 14         exit(0);//溢出报错
 15     Length=0;
 16 
 17     
 18 }
 19 //析构函数
 20 SqList::~SqList()
 21 {
 22     delete []date;
 23 
 24 }
 25 //判断是否为满
 26 bool SqList::IsFull()
 27 {
 28     if(Length==MaxSize)
 29         return true;
 30     else
 31         return false;
 32 }
 33 //判断是否为空
 34 bool SqList::Isempty()
 35 {
 36     if(Length==0)
 37         return true;
 38     else 
 39         return false;
 40 }
 41 //计算长度
 42 int SqList::GetLength()
 43 {
 44     return Length;
 45 }
 46 //插入
 47 bool SqList::ListInsert(int i,int Date)//在第i个位置插入元素Date
 48 {
 49     if(i<1||i>Length+1)
 50         return false;
 51     if(Length>=MaxSize)
 52         return false;
 53     for(int j=Length;j>=i;j++)
 54     {
 55         date[j]=date[j-1];//元素进行后移
 56     }
 57     date[i-1].Date=Date;
 58     Length++;
 59     return true;
 60 
 61 
 62 }
 63 //删除第一个数值为Date的元素
 64 bool SqList::Delete(int  Date)
 65 {
 66     int location=-1 ;
 67     for(int i=1;i<=Length;i--)
 68     {
 69         if(date[i-1].Date==Date)
 70         location=i;
 71     }
 72     if(location ==-1)
 73         return false;
 74     for(int j=location ;j<=Length;j++)
 75     {
 76         date[j-1].Date=date[j].Date;
 77     }
 78     Length--;
 79     return true;
 80 }
 81 //删除所有元素
 82 void SqList::DeleteAll()
 83 {
 84     for(int i=1;i<=Length;i++)
 85         ListDelete(i);
 86     
 87 }
 88 //删除第i个元素
 89 bool SqList::ListDelete(int i)
 90 {
 91     if(i<1||i>Length)
 92         return false;
 93     int e=date[i].Date;
 94     for(int j=i;j<Length;j++)//将第i个元素后移
 95     {
 96         date[j-1].Date=date[j].Date;
 97     }
 98     Length--;
 99     return true;
100         
101 }
102 //按值查找顺序表中第一个元素
103 int SqList::LocateElem(int a)
104 {
105     for(int j=1;j<=Length;j++)
106     {
107         if(date[j-1].Date==a)
108             return j;
109     }
110     return -1;
111 }

 

posted @ 2019-07-19 17:05  Brad1  阅读(95)  评论(0编辑  收藏  举报