概览:
-
线性表的定义和基本操作
-
线性表的存储结构
-
线性表的应用
-
线性表的基本概念和实现
-
定义:具有相同特性数据元素的有限序列。
-
逻辑特征:前驱后继
-
存储结构:
-
顺序表:连续的存储空间
-
链表:物理上不连续
-
两种存储结构的比较
顺序表有随机访问的特征,占用连续的空间,不利于增删
链表不支持随机访问,结点的存储空间利用率稍低,利于增删
-
-
-
线性表的结构体定义个基本操作
-
线性表的结构体定义
-
顺序表的操作
-
插入
-
template <class T> void SeqList<T>::Insert(int i, T x) { if (length >= MaxSize) throw "上溢"; if (i < 1 || i > length) throw "位置"; //0 1 2 3 4 i 5 6 7 length = 8 i = 6 for (int j = length; j >= i; j--) data[j] = data[j - 1]; data[i] = x; length++; }
-
删除
-
template <class T> T SeqList<T>::Delete(int i) { if (length == 0) throw "下溢"; if (i < 0 || i > length) throw "位置"; T s = data[i - 1]; for (int j = i - 1; j < length - 1; j++) data[j] = data[j + 1]; length--; return s; }
-
-
单链表的操作
-
合并
-
template <class T> void merge(Node<T> *A, Node<T> *B, Node<T> *&C) { Node<T> *p = A->next; Node<T> *q = B->next; Node<T> *r; C = A; C->next = NULL; free(B); r = C; while (p != NULL && q != NULL) { if (p->data <= q->data) { r->next = p; p = p->next; r = r->next; } else { r->next = q; q = q->next; r = r->next; } } r->next = NULL; if (p != NULL) r->next = p; if (q != NULL) r->next = q; }
-
插入
-
template <class T> void LinkList<T>::Insert(int i, T a) { int count = 0; T *p = first; while (++count < i && p != NULL) p = p->next; if (p == NULL) throw "位置"; T *s = new Node<T>; s->data = a; s->next = p->next; p->next = s; }
-
删除
-
template <class T> T LinkList<T>::Delete(int i) { int count = 0; T *p = first; while (++count < i && p != NULL) p = p->next; if (p == NULL || p->next == NULL) throw "位置"; T *q = p->next; T x = q->data; p->next = q->next; delete q; return x; }
-
-
双链表的操作
-
尾插
-
插入
-
template<class T> void CycleList<T>::Insert(int i,T t) { DLNode<T> *p; p = first; int count = 0; while(++ count < i) p = p->next; DLNode<T> *s = new DLNode<T>; s->data = t; s->next = p->next; s->prior = p; p->next->prior = s; p->next = s; }
-
删除
-
template<class T> T CycleList<T>::Delete(int i) { DLNode<T> *p; p = first; int count = 0; while(++ count < i) p = p->next; DLNode<T> *q; q = p->next; T x = q->data; p->next = q->next; q->next->prior = p; return x; }
-
-
循环链表的操作
-