线性表—概念总结及代码示例

 

概览:

  • 线性表的定义和基本操作
  • 线性表的存储结构
  • 线性表的应用

 

 

  1. 线性表的基本概念和实现
    1. 定义:具有相同特性数据元素的有限序列。
    2. 逻辑特征:前驱后继
    3. 存储结构:
      1. 顺序表:连续的存储空间
      2. 链表:物理上不连续
      3. 两种存储结构的比较

        顺序表有随机访问的特征,占用连续的空间,不利于增删

        链表不支持随机访问,结点的存储空间利用率稍低,利于增删

  2. 线性表的结构体定义个基本操作
    1. 线性表的结构体定义
    2. 顺序表的操作
      1. 插入
      2. 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++;
        }
      3. 删除
      4. 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;   
        }

         

    3. 单链表的操作
      1. 合并
      2. 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;
        }

         

      3. 插入
      4. 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;
        }

         

      5. 删除
      6. 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;
        }

         

    4. 双链表的操作
      1. 尾插
      2. 插入
      3. 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;
            
        }

         

      4. 删除
      5. 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;
        }

         

    5. 循环链表的操作
posted @ 2018-07-22 13:28  StormAX  阅读(251)  评论(0编辑  收藏  举报