数据结构一:线性表

线性表是最常用也是最简单的数据结构,几种常用的线性表的类模板C++描述描述如下:

1.顺序表

顺序表是将所有元素按逻辑顺序存储在一组地址连续的存储空间中的简单数据结构:

 1 const int MAXSIZE = 1000;
 2 template<class T>
 3 class SeqList
 4 {
 5 public:
 6     SeqList(){ lenght = 0; }           // 无参构造函数
 7     SeqList(const T a[], int n);       // 带参构造函数,并用数组初始化
 8
 9     int GetLenght() const{ return lenght; }// 返回顺序表长度
10     void PrintList()const;                 // 依次遍历顺序表各个数据元素
11     void Insert(int i, T x);               // 指定位置插入数据元素
12     T Delete(int i);                       // 删除指定位置的数据元素,并返回这个元素
13     T Get(int i) const;                    // 获取指定位置的数据元素
14     int Locate(T x) const;                 // 查找指定元素并返回其位置
15 private:
16     T data[MAXSIZE];                       // 存储顺序表中各个数据元素
17     int lenght;                            // 顺序表长度
18 };
19
20
21 // 构造函数
22 template<class T>
23 SeqList<T>::SeqList(const T a[], int n)
24 {
25     if (n > MAXSIZE)
26         throw"数组长度超出顺序表最大长度";
27     for (int i = 0; i < n; i++)
28         data[i] = a[i];
29     lenght = n;
30 }
31
32 // 遍历顺序表
33 template<class T>
34 void SeqList<T>::PrintList()const
35 {
36     std::cout << "顺序表中元素依次如下所示:" << endl;
37     for (int i = 0; i < lenght; i++)
38         std::cout << data[i] << " ";
39     std::cout << std::endl;
40 }
41
42 // 插入元素
43 template<class T>
44 void SeqList<T>::Insert(int i, T x)
45 {
46     if (lenght >= MAXSIZE)
47         throw"上溢异常";
48     if (i<1 || i>lenght)
49         throw"插入位置不合法";
50     for (int j = lenght; j >= i; j--)
51         data[j] = data[j - 1];
52     data[i - 1] = x;
53     lenght++;
54 }
55
56 // 删除元素
57 template<class T>
58 T SeqList<T>::Delete(int i)
59 {
60     if (0 == lenght)
61         throw"下溢异常";
62     if (i<1 || i>lenght)
63         throw"删除位置不合法";
64     T X = data[i - 1];
65     for (int j = i; j < lenght; j++)
66         data[j - 1] = data[j];
67     lenght--;
68     return X;
69 }
70
71 // 查找元素
72 template<class T>
73 T SeqList<T>::Get(int i)const
74 {
75     if (i<1 || i>lenght)
76         throw"查找位置不合法";
77
78     return data[i - 1];
79 }
80
81 // 定位元素
82 template<class T>
83 int SeqList<T>::Locate(T x)const
84 {
85     for (int i = 0; i < lenght; i++)
86     {
87         if (x == data[i])
88             return i + 1;
89     }
90
91     throw"查找的元素不存在顺序表中";
92     return 0;
93 }

2.单链表

单链表与顺序表不同的地方是存放的元素地址是零散的,可能连续也可能不连续,保证彼此之间的联系,因此,在存储元素的时候需要存储下一个元素的地址:

  1 template<class T>
  2 struct Node
  3 {
  4     T data;
  5     Node*next;
  6 };
  7
  8
  9 template<class T>
 10 class LinkList
 11 {
 12 public:
 13     LinkList(){ front = new Node<T>; front->next = NULL; }// 无参构造函数
 14     LinkList(T a[], int n);                               // 带参构造函数,用数组初始化链表
 15     ~LinkList();                                          // 析构函数
 16     void PrintList()const;                                // 依次遍历链表元素
 17     int GetLenght()const;                                 // 返回链表长度
 18     Node<T>*Get(int i)const;                              // 返回指定位置的数据元素
 19     int Locate(T x)const;                                 // 查找指定元素,并返回其位置
 20     void Insert(int i, T x);                              // 指定位置插入指定元素
 21     T Delete(int i);                                      // 删除指定位置的元素
 22 private:
 23     Node<T>*front;                                        // 头指针
 24 };
 25
 26
 27  // 头插法
 28 template<class T>
 29 LinkList<T>::LinkList(T a[], int n)
 30 {
 31     front = new Node<T>;
 32     front->next = NULL;                                   // 构造空链表
 33     for (int i = n - 1; i => 0;i--)
 34     {
 35         Node<T>*s = new Node<T>;                          // 建立新结点
 36         s->data = a[i];                                   // 将值写入结点数据域
 37         s->next = front->next;                            // 修改新节点指针域
 38         front->next = s;                                  // 修改头结点指针域
 39     }
 40 }
 41
 42 // 尾插法
 43 template<class T>
 44 LinkList<T>::LinkList(T a[], int n)
 45 {
 46     front = new Node<T>;
 47     Node<T>*r = front;
 48     for (int i = 0; i < n;i++)
 49     {
 50         Node<T>*s = new Node<T>;           // 建立新结点
 51         s->data = a[i];                    // 将值写入新结点数据域
 52         r->next = s;                       // 将新结点加入链表
 53         r = s;                             // 修改尾指针
 54     }
 55     r->next = NULL;
 56 }
 57
 58 template<class T>
 59 LinkList<T>::~LinkList()
 60 {
 61     Node<T>*p = front;                    // 初始化工作指针
 62     while (p)
 63     {
 64         front = p;                        // 暂存工作指针
 65         p = p->next;                      // 移动工作指针到下一个结点
 66         delete front;                     // 释放结点
 67     }
 68 }
 69
 70 template<class T>
 71 Node<T>*LinkList<T>::Get(int i)const
 72 {
 73     Node<T>*p = front->next;              // 初始化工作指针
 74     int j = 1;                            // 初始化计数器
 75     while (p&&j!=i)
 76     {
 77         p = p->next;                      // 移动工作指针
 78         j++;                              // 计数器递增
 79     }
 80     return p;                             // 返回查找结果
 81 }
 82
 83 template<class T>
 84 int LinkList<T>::Locate(T x)const
 85 {
 86     Node<T>*p = front->next;              // 初始化工作指针
 87     int j = 1;                            // 初始化计数器
 88     while (p)
 89     {
 90         if (x == p->data)                 // 找到指定元素就返回位置
 91             return j;
 92         p = p->next;                      // 移动工作指针
 93         j++;                              // 递增计时器
 94     }
 95     return -1;                            // 若找不到,返回-1
 96 }
 97
 98 template<class T>
 99 void LinkList<T>::Insert(int i, T x)
100 {
101     Node<T>*p = front;                    // 初始化工作指针
102
103     if (i != 1)
104         p = Get(i - 1);                   // 返回插入位置前一个元素的位置
105     if (p)
106     {
107         Node<T>*s = new Node<T>;
108         s->data = x;                      // 插入元素数据区域赋值
109         s->next = p->next;                // 插入元素指针区域赋值
110         p->next = s;                      // 插入元素接入链表
111     }
112     else
113         throw"插入位置错误!";
114 }
115
116 template<class T>
117 T LinkList<T>::Delete(int i)
118 {
119     Node<T>*p = front;                 // 初始化工作指针
120     if (i != 1)
121         p = Get(i - 1);                // 获取第i-1个元素的地址
122     Node<T>*q = p->next;               // 获取第i个元素的地址
123     p->next = q->next;                 // 将第i+1个元素与第i个元素链接
124     T x = q->data;                     // 保存被删除元素
125     delete q;                          // 删除指定位置元素
126     return x;                          // 返回被删除的元素
127 }
128
129 template<class T>
130 void LinkList<T>::PrintList()const
131 {
132     Node<T>*p = front->next;
133     std::cout << "链表元素依次为:";
134     while (p)
135     {
136         std::cout << (p->data) << " ";
137         p = p->next;
138     }
139     std::cout << std::endl;
140 }
141
142 template<class T>
143 int LinkList<T>::GetLenght()const
144 {
145     Node<T>*p = front->next;
146     int j = 1;
147     while (p)
148     {
149         p = p->next;
150         j++;
151     }
152
153     return j;
154 }

3.单循环链表

单循环链表与单链表没有太大区别,为了访问链表的首尾段,将之前的头指针换成了尾指针,尾指针指向链表尾部,尾指针指针域指向链表第一个元素:

  1 template<class T>
  2 struct Node
  3 {
  4     T data;
  5     Node*next;
  6 };
  7
  8 template<class T>
  9 class CLinkList
 10 {
 11 public:
 12     CLinkList(){ rear = new Node<T>; rear->next = rear; } // 无参构造函数
 13     CLinkList(T a[], int n);                              // 用数组初始化单循环链表
 14
 15     ~CLinkList();                                         // 析构函数
 16     int GetLenght()const;                                 // 返回链表长度
 17     Node<T>*Get(int i)const;                              // 返回指定位置元素数据
 18     int Locate(T x)const;                                 // 查找指定元素,并返回其位置
 19     void Insert(int i, T x);                              // 指定位置插入元素
 20     T Delete(int i);                                      // 删除指定位置的元素,并返回其值
 21     void PrintList()const;                                // 依次遍历链表元素
 22
 23 private:
 24     Node<T>*rear;                                         // 尾指针(指向最后一个元素)
 25 };
 26
 27
 28 template<class T>
 29 CLinkList<T>::CLinkList(T a[], int n)   // 尾插法
 30 {
 31     rear = new Node<T>;
 32     rear->next = rear;       // 初始化尾指针
 33     for (int i = 0; i < n; i++)
 34     {
 35         Node <T>*s = new Node <T>;
 36         s->data = a[i];      // 数据域赋值
 37         s->next = rear->next;// 新增结点指针指向头结点
 38         rear->next = s;      // 新增结点添加到链表中
 39         rear = s;            // 尾指针指向尾结点
 40     }
 41 }
 42
 43
 44 template<class T>
 45 void CLinkList<T>::PrintList()const
 46 {
 47     std::cout << "单循环链表各元素如下:";
 48     Node<T>*p = rear->next;              // 初始化工作指针指向头结点
 49     do
 50     {
 51         p = p->next;                     // 修改工作指针
 52         std::cout << p->data << " ";     // 打印
 53     } while (p!=rear);
 54
 55     std::cout << std::endl;
 56 }
 57
 58 template<class T>
 59 CLinkList<T>::~CLinkList()
 60 {
 61     Node<T>*p = rear->next;         // 初始化工作指针
 62     do
 63     {
 64         rear->next = p;             // 暂存待删除结点
 65         p = p->next;                // 移动工作指针
 66         delete rear->next;          // 删除结点
 67
 68     } while (p!=rear);
 69 }
 70
 71
 72 template<class T>
 73 int CLinkList<T>::GetLenght()const
 74 {
 75     Node<T>*p = rear->next;         // 初始化工作指针
 76     int j = 0;                      // 元素计数器
 77     while (p!=rear)
 78     {
 79         p = p->next;                // 移动工作指针
 80         j++;
 81     }
 82     return j;
 83 }
 84
 85 template<class T>
 86 Node<T>*CLinkList<T>::Get(int i)const
 87 {
 88     Node<T>*p = rear->next;                   // 初始化工作指针
 89     int j = 0;                                // 计数器
 90     while((p!=rear)&&(j!=i))
 91     {
 92         p = p->next;                          // 移动工作指针
 93         j++;
 94     }
 95     return p;
 96 }
 97
 98 template<class T>
 99 int CLinkList<T>::Locate(T x)const
100 {
101     Node<T>*p = rear->next;             // 初始化工作指针
102     int j = 0;                          // 计数器
103     while (p!=rear)
104     {
105         if (p->data == x)
106             return j;
107         p = p->next;                   // 移动工作指针
108         j++;
109     }
110     return -1;
111 }
112
113 template<class T>
114 T CLinkList<T>::Delete(int i)
115 {
116     Node<T>*P = rear;                  // 初始化工作指针
117     if (i != 1)
118         P = Get(i - 1);                // 获取第i-1个元素的地址
119     Node<T>*q = P->next;               // 获取第i个元素的地址
120     P->next = q->next;                 // 将第i+1个元素与第i个元素链接
121     T x = q->data;                     // 保存被删除元素
122     delete q;                          // 删除指定位置元素
123     return x;                          // 返回被删除的元素
124 }
125
126 template<class T>
127 void CLinkList<T>::Insert(int i, T x)
128 {
129     Node<T>*p = rear;
130
131     if (i != 1)
132         p = Get(i - 1);                // 获取第i-1个元素的地址
133     if (p)
134     {
135         Node<T>*s = new Node<T>;       // 新建结点数据
136         s->data = x;                   // 新增结点数据域赋值
137         s->next = p->next;             // 新增结点指针域赋值
138         p->next = s;                   // 连接链表
139     }
140     else
141         throw"插入位置错误!";
142 }

4.双向链表

相比单向链表,就是多了一个前驱结点指针,方便访问相连的前后结点:

  1 template<class T>
  2 struct DNode
  3 {
  4     T data;      // 数据域
  5     DNode*prior; // 指针域,指向前一个结点
  6     DNode*next;  // 指针域,指向后一个结点
  7 };
  8
  9 template<class T>
 10 class DLinkList
 11 {
 12 public:
 13     DLinkList(){ front = new DNode<T>; front->next = front; front->prior = front; } // 无参构造函数
 14     DLinkList(T a[], int n);                         // 带参构造函数,数组初始化链表
 15     ~DLinkList();                                    // 析构函数
 16
 17     void Insert(int i, T X);                         // 指定位置插入指定元素
 18     T Delete(int i);                                 // 删除指定位置的元素,并返回这个元素
 19     int GetLenght()const;                            // 返回链表长度
 20     DNode<T>*Get(int i)const;                        // 查找指定位置元素
 21     int Locate(T X)const;                            // 查找指定元素,并返回其位置
 22     void PrintList()const;                           // 遍历链表
 23 private:
 24     DNode<T>*front;
 25 };
 26
 27 template<class T>
 28 DLinkList<T>::DLinkList(T a[], int n)
 29 {
 30     front = new DNode<T>;                 // 构造头结点
 31     DNode<T>*p = front;                   // 存储头指针
 32     front->next = front;                  // 初始化头结点
 33     front->prior = front;                 // 初始化头结点
 34
 35     for (int i = 0; i < n;i++)
 36     {
 37         DNode<T>*s = new DNode<T>;        // 构造新结点数据结构
 38         s->data = a[i];                   // 给新结点数据域赋值
 39         s->next = front->next;            // 新结点后驱指针指向头结点
 40         front->next = s;                  // 新结点加入后驱链中
 41         s->prior = front;                 // 新结点前驱指针指向头结点
 42         p->prior = s;                     // 修改头结点前驱指针指向新结点
 43
 44         front = s;                        // 传递尾结点地址到下一个结点
 45     }
 46     front = p;                            // 修改头指针指向头结点
 47 }
 48
 49 template<class T>
 50 DLinkList<T>::~DLinkList()
 51 {
 52     DNode<T>*p = front;                   // 初始化工作结点
 53     DNode<T>*q = front;                   // 初始化工作结点
 54     do
 55     {
 56         p = p->next;                      // 移动工作结点
 57         q = p;                            // 暂存待释放结点
 58         delete q;
 59     } while (p!=front->prior);
 60
 61     delete front;                          // 释放头结点
 62 }
 63
 64 template<class T>
 65 void DLinkList<T>::PrintList()const
 66 {
 67     DNode<T>*p = front;                    // 初始化工作链表
 68     std::cout << "按后驱指针遍历链表:";
 69     while(p!=front->prior)
 70     {
 71         p = p->next;                       // 移动工作指针
 72         std::cout << p->data << " ";
 73     }
 74     std::cout << std::endl;
 75
 76     DNode<T>*q = front->prior;             // 初始化工作链表
 77     std::cout << "按前驱指针遍历链表:";
 78     while (p != front)
 79     {
 80         std::cout << p->data << " ";
 81         p = p->prior;                       // 移动工作指针
 82     }
 83     std::cout << std::endl;
 84 }
 85
 86 template<class T>
 87 T DLinkList<T>::Delete(int i)
 88 {
 89     DNode<T>*p = Get(i);           // 获取待删除结点地址
 90     DNode<T>*p1 = p->prior;        // 获取待删除结点的前驱结点位置
 91     DNode<T>*p2 = p->next;         // 获取待删除结点的后驱结点位置
 92     T x = p->data;                 // 暂存待删除结点数据域
 93     p1->next = p2;                 // 修改后驱链
 94     p2->prior = p1;                // 修改前驱链
 95
 96     delete p;                      // 删除结点
 97
 98     return x;
 99 }
100
101 template<class T>
102 void DLinkList<T>::Insert(int i, T X)
103 {
104     DNode<T>*p = Get(i);                // 获取待插入位置地址
105     if (p)
106     {
107         DNode<T>*s = new DNode<T>;      // 构造新结点数据存储结构
108         DNode<T>*q = p->prior;          // 获取待插入位置的前驱结点位置
109
110         s->data = X;                    // 插入的新结点数据域赋值
111         s->next = p;                    // 插入结点的后驱指针赋值
112         q->next = s;                    // 插入结点的前一结点的后驱指针赋值
113         s->prior = q;                   // 插入结点的前驱指针赋值
114         p->prior = s;                   // 插入结点的后一节点的前驱指针赋值
115     }
116     else throw"插入位置错误";
117 }
118
119 template<class T>
120 int DLinkList<T>::GetLenght()const
121 {
122     DNode<T>*p = front;                  // 初始化工作指针
123     int j = 0;                           // 定义一个计数器
124     while (p!=front->prior)
125     {
126         p = p->next;                     // 移动工作指针
127         j++;
128     }
129     return j;
130 }
131
132 template<class T>
133 DNode<T>*DLinkList<T>::Get(int i)const
134 {
135     DNode<T>*p = front;              // 初始化工作指针
136     int j = 0;                       // 定义一个计数器
137     while ((p!=front->prior)&&j!=i)
138     {
139         p = p->next;                 // 移动工作指针
140         j++;
141     }
142     return p;                        // 找到返回地址
143 }
144
145 template<class T>
146 int DLinkList<T>::Locate(T X)const
147 {
148     DNode<T>*p = front;              // 初始化工作结点
149     int j = 0;
150     while (p!=front->prior)
151     {
152         p = p->next;                 // 移动工作指针
153         j++;
154         if (p->data == X)
155             return j;                // 找到元素,返回位置
156     }
157     return -1;                       // 找不到返回-1
158 }

5.静态链表

静态链表主要是为了满足那些没有“指针”这一类数据类型的语言(JAVA,BASIC等),采用数组来模拟链表,数组的下标来模拟指针:

  1 template<class T>
  2 struct StaticNode
  3 {
  4     T data;            // 数据域
  5     int next;          // 游标(模拟指针)
  6 };
  7
  8 const int SLISTMAXSIZE = 100;
  9
 10 template<class T>
 11 class StaticLinkList
 12 {
 13 public:
 14     StaticLinkList();                     // 无参构造函数
 15     StaticLinkList(T a[], int n);         // 带参构造函数,数组初始化链表
 16     void Insert(int i, T X);              // 指定位置插入指定元素
 17     T Delete(int i);                      // 删除指定位置元素,并返回该元素
 18     int Get(int i)const;                  // 按位查找,返回下标
 19     int Locate(T X)const;                 // 按值查找,返回下标
 20     int GetLenght()const;                 // 返回链表长度
 21     int NewNode();                        // 申请结点空间
 22     void DeleteNode(int i);               // 释放指定结点
 23     void PrintList()const;                // 遍历链表
 24 private:
 25     int front;                            // 数据域起始下标
 26     int tail;                             // 空闲域起始下标
 27     StaticNode<T> Sarray[SLISTMAXSIZE];        // 数据表
 28 };
 29
 30
 31 template<class T>
 32 StaticLinkList<T>::StaticLinkList()
 33 {
 34     for (int i = 0; i < SLISTMAXSIZE - 1; i++)
 35         Sarray[i].next = i + 1;            // 链表每个游标都是指向下一个数组元素
 36     Sarray[SLISTMAXSIZE - 1].next = -1;         // 将最后一个元素的游标指向-1
 37     tail = 0;                             // 显然空链表的头结点下标就是0
 38     front = -1;                           // 空链表的头结点游标定义为-1
 39 }
 40
 41 template<class T>
 42 StaticLinkList<T>::StaticLinkList(T a[], int n)
 43 {
 44     // 初始化数组
 45     if (n > SLISTMAXSIZE)
 46         throw"数据溢出";
 47     for (int i = 0; i < SLISTMAXSIZE - 1; i++)
 48         Sarray[i].next = i + 1;             // 链表中每个游标指向下一个数组元素
 49     Sarray[SLISTMAXSIZE - 1].next = -1;          // 将最后一个元素的游标指向-1
 50     for (int i = 0; i < n; i++)
 51         Sarray[i].data = a[i];              // 给链表数据域赋值
 52     front = 0;                             // front指向可用数据域的第一个元素的下标
 53     tail = Sarray[n - 1].next;              // tail指向未分配空间的第一个元素
 54     Sarray[n - 1].next = -1;                // 将可用数据域最有一个元素的游标指向-1
 55 }
 56
 57 template<class T>
 58 int StaticLinkList<T>::NewNode()
 59 {
 60     if (-1 == tail)                         // 判断是否还有未分配空间
 61         throw"内存不足";
 62     int pos=tail;                           // 未分配空间第一个结点分配给新结点
 63     tail = Sarray[tail].next;               // 未分配空间起点游标后移
 64     Sarray[tail].next = -1;                 // 修改数据域最后一个元素指向-1
 65     return pos;                             // 返回新结点的下标
 66 }
 67
 68 template<class T>
 69 void StaticLinkList<T>::DeleteNode(int i)
 70 {
 71     if (i<0 || i>SLISTMAXSIZE - 1 || -1 == front)
 72         throw"释放空间错误";
 73     int p = Get(i);                             // 记录第i个元素下标
 74     int q = Get(i - 1);;                        // 记录第i-1个元素的下标
 75     if (p==front)                               // 单独处理删除点为头结点的情况
 76     {
 77         Sarray[front].next = tail;
 78         tail = front;                           // 删除的结点插入到未使用空间的头结点位置
 79         front = Sarray[front].next;             // 更改数据域的起始下标为原来的下一个位置
 80     }
 81     else if (-1 == Sarray[p].next)              // 单独处理待删除点为尾结点的情况
 82     {
 83         Sarray[p].next = tail;
 84         tail = p;
 85         Sarray[q].next = -1;
 86     }
 87     else                                        // 一般情况处理
 88     {
 89         Sarray[q].next = Sarray[p].next;
 90         Sarray[p].next = tail;
 91         tail = p;
 92     }
 93 }
 94
 95 template<class T>
 96 T StaticLinkList<T>::Delete(int i)
 97 {
 98     T x = Sarray[Get(i)].data;   // 将待删除元素暂存
 99     DeleteNode(i);               // 删除元素
100
101     return x;
102 }
103
104 template<class T>
105 void StaticLinkList<T>::Insert(int i, T X)
106 {
107     int p = Get(i);                       // 记录第i个元素的下标
108     if (p < 0)
109         throw"插入位置错误";
110
111     if (p==front)                         // 单独处理在头结点插入的情况
112     {
113         Sarray[tail].data = X;            // 从未分配空间第一个元素开始使用
114         Sarray[tail].next = front;        // 新结点与后一结点链接
115         front = tail;                     // 更新头结点
116         tail = Sarray[tail].next;         // 未分配空间的头结点顺移
117     }
118     else
119     {
120         int q = Get(i - 1);               // 记录第i-1个元素下标
121
122         Sarray[tail].data = X;            // 从未分配空间第一个元素开始使用
123         Sarray[q].next = tail;            // 新结点与前一结点链接
124         Sarray[tail].next = p;            // 新结点与后一结点链接
125         tail = Sarray[tail].next;         // 未分配空间的头结点顺移
126     }
127 }
128
129 template<class T>
130 int StaticLinkList<T>::Get(int i)const
131 {
132     if (i<0 || i>GetLenght())
133         return -1;               // 找不到就返回错误-1
134     int p = front;               // 初始化工作指针
135     int j = 1;                   // 计数器
136     while (p!= -1)
137     {
138         if (j == i)
139             return p;
140         p = Sarray[p].next;      // 移动工作指针
141         j++;
142     }
143     return p;
144 }
145
146 template<class T>
147 int StaticLinkList<T>::GetLenght()const
148 {
149     if (-1 == front)
150         throw"长度为0";
151
152     int j = 0;                  // 计数器
153     int k = front;              // 工作指针
154     while (k!=-1)
155     {
156         k = Sarray[k].next;     // 移动工作指针
157         j++;
158     }
159     return j;
160 }
161
162 template<class T>
163 int StaticLinkList<T>::Locate(T X)const
164 {
165     int p = front;                      // 工作指针
166     while (p!=-1)
167     {
168         if (X == Sarray[p].data)        // 找到就返回下标
169             return p;
170         p = Sarray[p].next;
171     }
172     return -1;
173 }
174
175 template<class T>
176 void StaticLinkList<T>::PrintList()const
177 {
178     int p = front;                   // 工作游标
179     std::cout << "链表元素依次如下:";
180     while (p!=-1)
181     {
182         std::cout << Sarray[p].data << " ";
183         p = Sarray[p].next;          // 移动工作游标
184     }
185     std::cout << std::endl;
186 }

时间: 2024-08-09 21:50:05

数据结构一:线性表的相关文章

自学数据结构——顺序线性表

胡乱写了一些代码 /* ============================================================================ Name : sqlist.c Author :codecup Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ==========================================

软考之路--数据结构之线性表

数据就是数值,也就是我们通过观察.实验或计算得出的结果.数据有很多种,最简单的就是数字.数据也可以是文字.图像.声音等.数据可以用于科学研究.设计.查证等.结构,组成整体的各部分的搭配和安排,两者完美结合在一起,我们这样需要重新认识她,对她重新审视与定义:数据结构是程序设计的重要理论和技术基础,她所讨论的内容和技术,对从事软件项目的开发有重要作用,通过学习数据结构,我们学会从问题出发,分析和研究计算机加工的数据的特性,以便为应用所设计的数据悬着适当的逻辑结构.存储结构及其相应的操作方法,为提高应

[笔记]python数据结构之线性表:linkedlist链表,stack栈,queue队列

python数据结构之线性表 python内置了很多高级数据结构,list,dict,tuple,string,set等,在使用的时候十分舒心.但是,如果从一个初学者的角度利用python学习数据结构时,这些高级的数据结构可能给我们以迷惑. 比如,使用list实现queue的时候,入队操作append()时间复杂度可以认为是O(1),但是,出队操作pop(0)的时间复杂度就是O(n). 如果是想利用python学学数据结构的话,我觉得还是自己实现一遍基本的数据结构为好. 1.链表 在这里,我想使

自学数据结构——顺序线性表2

1 /* 2 ============================================================================ 3 Name : sqlist.c 4 Author : codecup 5 Version : 6 Copyright : Your copyright notice 7 Description : Hello World in C, Ansi-style 8 ==================================

数据结构:线性表插入一次删除一次的代码

#include <iostream> #include <cmath> #include <cstring> #include <algorithm> #include <stack> #include <queue> #include <cstdio> using namespace std; int insertsqlist(int weizhi,double charu,int *t,double b[]){   

数据结构之线性表(顺序存储结构)

小学生放学都是要按顺序排队的,一个接一个,每个小学生的前后位置是固定的,这样便于迅速清点. 其实这就是一个线性表,从这件事里我们就可以找到很多关于线性表的特性,如 1.线性表是一个序列,它是有顺序的(排队) 2.第一个元素无前驱,最后一个无后继,其他每个元素都有一个前驱和后继(一个接一个) 3.元素是有限的(小学生的个数是有限的) 4.数据类型都相同(都是小学生在排队) 说明白线性表示什么,下面我们直接看线性表的实现 线性表的实现分顺序存储结构和链式存储结构 顺序存储结构: #define LI

数据结构:线性表之单链表

线性表(亦作顺序表)是最基本.最简单.也是最常用的一种数据结构.线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的.线性表有两种存储结构: ①顺序存储结构,即存储单元在一段连续的地址上存储,常见的数组就是顺序存储结构的线性表: ②链式存储结构,即存储单元在不连续的地址上存储.因为其不连续性,除了要存数据元素信息(数据域)外,还要存储它后继元素(结点)的地址(指针域,链).学习链式结构最好将结点结构牢记于心,如下图: 链表的每个结点只含有一个指

数据结构之线性表

线性表是最简单最常用的一种数据结构,在生活中各个方面都有应用. 线性表的定义:线性表大多数情况下是除了第一个位置的数据元素只存在后继元素,最后一个位置的数据元素只存在前驱元素外,所有数据元素都存在前驱和后继的一个有限序列.举个简单的例子就是:字母表中除了 a 只存在后继 b,z 只存在前驱 y之外,剩余的所有字母全部都有前驱和后继.为什么是大多数情况下,是因为线性表的链式存储结构中除了单向链表,还有循环链表和双向链表. 线性表的存储结构:顺序存储(数组实现,需要预先分配连续的内存空间)和链式存储

浅谈数据结构之线性表顺序存储(一)

 首先,数据结构是由某一数据元素集合及该集合中所有数据元素之间的关系组成.具体来说,数据结构应当包含三方面的内容:(1).数据的逻辑结构:(2).数据的存储结构:(3).对数据所施加的操作.而数据的存储结构形式有两种:顺序存储与链式存储.在这里,先谈一谈线性表的顺序存储. 线性表:零个或多个数据元素的有限序列.第一,它是一个序列,也就是说,元素之间是有顺序的:第二,它是有限的,即元素个数是有限的.而线性表的顺序存储结构,说白了,就是在内存中找块地,通过占位的形式把一定的内存空间给占了,然后把相同

数据结构_线性表的顺序表示和链式表示

/********************************************************************************************************************/ 声明: (1)*.h文件是代码声明, *.cpp文件是代码实现; (2)一般头文件的内容有: ①类型声明; ②函数声明; ③枚举; ④常量; ⑤宏 (3)以下说明是为了方便代码文件的管理而设定的一些规则, 以后代码都会按照此规则编写: 1)Pubuse.h 是几