分享

顺序表(Seqlist)&链表(List)的基础操作

 青叶i生活 2018-02-07

描述:

顺序表和链表是数据结构的基础结构之一,同样也是面试的基础。初学者对于Seqlist和List的增删改查的基础练习,为其后的Tree,HashTable,Binary Linked List,Trigeminal linked list等数据结构打下坚实的基础。


C语言下的Seqlist:

  1. <span style="font-size:18px;">  
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<cassert>  
  4. #include<string.h>  
  5. #define Max_Size 10  
  6.   
  7. typedef int Datatype;  
  8. typedef struct Seqlist  
  9. {  
  10.     Datatype arr[Max_Size];//顺序表的存储空间  
  11.     size_t size;//用以表示顺序表的有效数据  
  12.     Datatype x;  
  13. }Seqlist;  
  14.   
  15. void Init(Seqlist* ptr)  
  16. {  
  17.     assert(ptr);  
  18.     memset(ptr->arr, 0, sizeof(Datatype)*Max_Size);  
  19.     ptr->size = 0;  
  20. }  
  21.   
  22. void PushBack(Seqlist* ptr,Datatype x)  
  23. //尾插,直接将size值+1,然后将x值填入  
  24. {  
  25.     assert(ptr);  
  26.     if (ptr->size >= Max_Size)  
  27.     {  
  28.         printf("Seqlist is Full!\n");  
  29.         return;  
  30.     }  
  31.     ptr->arr[ptr->size++] = x;  
  32. }  
  33.   
  34. void PopBack(Seqlist* ptr)  
  35. //尾删,直接将size值-1,注意末位置的数据并未真的删去  
  36. {  
  37.     assert(ptr);  
  38.     if (ptr->size <= 0)  
  39.     {  
  40.         printf("Seqlist is Empty!\n");  
  41.         return;  
  42.     }  
  43.     --ptr->size;  
  44. }  
  45.   
  46. void PushFront(Seqlist* ptr,Datatype x)  
  47. //头插,将size值+1,然后从倒数第二个数据开始,不断后移,将首元素位置空出,然后填充x值。注意:若从前往后移动,会覆盖后续的所有数据。  
  48. {  
  49.     assert(ptr);  
  50.     int i = ptr->size - 1;  
  51.     if (ptr->size >= Max_Size)  
  52.     {  
  53.         printf("Seqlist is Full!\n");  
  54.         return;  
  55.     }  
  56.     ptr->size++;  
  57.     for (; i >= 0; --i)  
  58.     {  
  59.         ptr->arr[i + 1] = ptr->arr[i];  
  60.     }  
  61.     ptr->arr[0] = x;  
  62. }  
  63.   
  64. void PopFront(Seqlist* ptr)  
  65. //头删,直接从第二个数据开始不断覆盖前一数据,然后size值-1  
  66. {  
  67.     assert(ptr);  
  68.     size_t i = 0;  
  69.     if (ptr->size <= 0)  
  70.     {  
  71.         printf("Seqlist is Empty!\n");  
  72.         return;  
  73.     }  
  74.     for (; i < ptr->size;++i)  
  75.     {  
  76.         ptr->arr[i] = ptr->arr[i+1];  
  77.     }  
  78.     --ptr->size;  
  79. }  
  80.   
  81. void Erase(Seqlist* ptr, size_t pos)  
  82. //定向删除,首先从后往前执行类似头删的移动过程,直至到pos位置时停止。注意:数组的下标从0开始,所以应为pos-1的位置处  
  83. {  
  84.     assert(ptr);  
  85.     size_t i = pos - 1;  
  86.     if (ptr->size <= 0)  
  87.     {  
  88.         printf("Seqlist is Empty!\n");  
  89.         return;  
  90.     }  
  91.     for (; i < ptr->size; ++i)  
  92.     {  
  93.         ptr->arr[i] = ptr->arr[i + 1];  
  94.     }  
  95.     --ptr->size;  
  96. }  
  97.   
  98. void Insert(Seqlist* ptr, Datatype x, size_t pos)  
  99. //定向插入,思想与Erase类似,在此不再赘述  
  100. {  
  101.     assert(ptr);  
  102.     assert(pos);  
  103.     if (ptr->size >= Max_Size)  
  104.     {  
  105.         printf("Seqlist is Full!\n");  
  106.         return;  
  107.     }  
  108.     if (pos > ptr->size)  
  109.     {  
  110.         printf("Illegal Insert!\n");  
  111.         return;  
  112.     }  
  113.     size_t i = ptr->size++;  
  114.     for (; i >= pos - 1; i--)  
  115.     {  
  116.         ptr->arr[i] = ptr->arr[i - 1];  
  117.     }  
  118.     ptr->arr[pos - 1] = x;  
  119. }  
  120.   
  121. int Find_One(Seqlist* ptr,Datatype x)  
  122. //查找某元素值  
  123. {  
  124.     assert(ptr);  
  125.     size_t i = 0;  
  126.     for (; i < ptr->size; i++)  
  127.     {  
  128.         if (ptr->arr[i] == x)  
  129.         {  
  130.             return i;  
  131.         }  
  132.     }  
  133.     return -1;  
  134. }  
  135.   
  136. void Display(Seqlist* ptr)  
  137. //输出顺序表  
  138. {  
  139.     assert(ptr);  
  140.     size_t i = 0;  
  141.     if (ptr->size == 0)  
  142.     {  
  143.         printf("Seqlist is Empty!\n");  
  144.         return;  
  145.     }  
  146.     for (; i < ptr->size; i++)  
  147.     {  
  148.         printf("%d ", ptr->arr[i]);  
  149.     }  
  150.     printf("\n");  
  151. }</span>  




▲顺序表有其限制性,对于静态的顺序表来说,其存储空间由一开始宏定义的Max_Size设置,而后不能再作变化,除非更改宏值。对于动态的顺序表,其增加了capacity容量的概念,可每次动态增长其size的值,可以参考点击进入此网站,不过cplusplus中的顺序表不再是Seqlist,而更名为Vector。



C++下的Seqlist:

  1. <span style="font-size:18px;">  
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. SeqList::SeqList()  
  5. :_array(NULL)  
  6. , _size(0)  
  7. , _capacity(0)  
  8. {}  
  9. //顺序表只考虑深拷贝  
  10.   
  11.   
  12. //传统写法  
  13. SeqList(const SeqList& s)  
  14.       :_array(new DataType[s._size])  
  15.       ,_size(s._size)  
  16.       ,_capacity(s._capacity)  
  17.       memcpy(_array, s._array, sizeof(DataType)*(_size));  
  18. }  
  19. SeqList& operator=(const SeqList& s)  
  20. {  
  21.       if (this != &s)  
  22.       {  
  23.       DataType* tmp = new DataType[s._size];  
  24.       delete[] _array;  
  25.       _array = tmp;  
  26.        _size = s._size;  
  27.        _capacity = s._capacity;  
  28.       memcpy(_array, s._array, sizeof(DataType)*(_size));  
  29.      }  
  30.      return *this;  
  31. }  
  32.   
  33.   
  34. //现代写法  
  35. SeqList::SeqList(DataType* array, const size_t size)  
  36. :_array(new DataType[size])  
  37. , _size(size)  
  38. , _capacity(size)  
  39. {  
  40.     memcpy(_array, array, sizeof(DataType)*size);  
  41. }  
  42. SeqList::SeqList(const SeqList& s)  
  43. :_array(NULL)  
  44. {  
  45.     SeqList tmp(s._array, s._size);  
  46.     swap(_array, tmp._array);  
  47.     _size = s._size;  
  48.     _capacity = s._size;                                   //容量用_size,防止越界  
  49. }  
  50.   
  51.   
  52. SeqList& SeqList::operator=(SeqList& s)  
  53. {  
  54.     if (this != &s)  
  55.     {  
  56.         swap(_array, s._array);  
  57.         _size = s._size;  
  58.         _capacity = s._capacity;  
  59.     }  
  60.     return *this;  
  61. }  
  62.   
  63.   
  64. SeqList::~SeqList()  
  65. {  
  66.     if (_array)  
  67.     {  
  68.         delete[] _array;  
  69.     }  
  70. }  
  71.   
  72.   
  73.   
  74. void SeqList::_CheckCapacity()//检查容量  
  75. {  
  76.     if (_size >= _capacity)  
  77.     {  
  78.         _capacity = 2 * _capacity + 3;//_capacity是否为0  
  79.         _array = (DataType*)realloc(_array, _capacity*sizeof(DataType));//realloc扩容,后面用字节数表示  
  80.     }  
  81. }  
  82.   
  83. void SeqList::PrintSepList()//打印顺序表  
  84. {  
  85.     for (int i = 0; i < (int)_size; i++)  
  86.     {  
  87.         cout << _array[i] << "-";  
  88.     }  
  89.     cout << "NULL" << endl;  
  90. }  
  91.   
  92.   
  93.   
  94. void SeqList::PushBack(DataType x)//尾插  
  95. {  
  96.     _CheckCapacity();  
  97.     _array[_size++] = x;  
  98. }  
  99.   
  100.   
  101.   
  102. void SeqList::PopBack()//尾删  
  103. {  
  104.     if (_size > 0)  
  105.     {  
  106.         _array[_size--] = NULL;  
  107.     }  
  108.     else  
  109.         cout << "SeqList is empty" << endl;  
  110. }  
  111.   
  112.   
  113.   
  114. void SeqList::PushFront(DataType x)////头插  
  115. {  
  116.     _CheckCapacity();  
  117.     for (int i = (int)_size; i >0 ; i--)  
  118.     {  
  119.         _array[i] = _array[i - 1];  
  120.     }  
  121.     _array[0] = x;  
  122.     _size++;  
  123. }  
  124.   
  125.   
  126.   
  127. void SeqList::PopFront()//头删  
  128. {  
  129.     if (_size > 0)  
  130.     {  
  131.         _array[0] = NULL;  
  132.         for (int i = 0; i < (int)_size; i++)  
  133.         {  
  134.             _array[i] = _array[i + 1];  
  135.         }  
  136.         _size--;  
  137.     }  
  138.     else  
  139.         cout << "SeqList is empty" << endl;  
  140. }  
  141.   
  142.   
  143.   
  144. void SeqList::Insert(size_t pos, DataType x)//指定位置处插入一个数  
  145. {  
  146.     if (pos <= 0 || pos > _size + 1)  
  147.     {  
  148.         cout << "position is error!" << endl;  
  149.     }  
  150.     else if (pos == _size)  
  151.         SeqList::PushBack(x);  
  152.     else  
  153.     {  
  154.         for (int i = (int)(++_size); i > ((int)pos - 1); i--)  
  155.         {  
  156.             _array[i] = _array[i - 1];  
  157.         }  
  158.         _array[pos - 1] = x;  
  159.     }  
  160. }  
  161.   
  162.   
  163.   
  164. void SeqList::Erase(size_t pos)//删除指定位置  
  165. {  
  166.     if (pos <= 0 || pos > _size + 1)  
  167.     {  
  168.         cout << "position is error!" << endl;  
  169.     }  
  170.     else if (pos == _size)  
  171.         SeqList::PopBack();  
  172.     else  
  173.     {  
  174.         _array[pos - 1] = NULL;  
  175.         for (int i = pos - 1; i < (int)_size; i++)  
  176.         {  
  177.             _array[i] = _array[i + 1];  
  178.         }  
  179.         _size--;  
  180.     }  
  181. }  
  182.   
  183.   
  184. size_t SeqList::Find(DataType x)//查找某数  
  185. {  
  186.     int i;  
  187.     for (i = 0; i < (int)_size; i++)  
  188.     {  
  189.         if (x == _array[i])  
  190.             return i + 1;  
  191.     }  
  192.     if (i == (int)_size)  
  193.         return 0;  
  194.     return -1;  
  195. }</span>  



▲注意:代码中并未给出struct/class结构体的定义,只是实现了结构体中每一个函数的定义,且是在类体外定义的(前有Seqlist::作用域符)。



C下的List:

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<assert.h>  
  4.   
  5. typedef int Datatype;  
  6.   
  7. typedef struct ListNode  
  8. {  
  9.     Datatype data;//值域  
  10.     struct ListNode *next;//指针域,用于链向下一个节点  
  11. }ListNode;  
  12.   
  13. ListNode* createNode(Datatype x)//节点的创建  
  14. {  
  15.     ListNode *tmp = (ListNode*)malloc(sizeof(ListNode));  
  16.     if (tmp != NULL)  
  17.     {  
  18.         tmp->data = x;  
  19.         tmp->next = NULL;  
  20.     }  
  21.     return tmp;  
  22. }  
  23.   
  24.   
  25.   
  26. void pushFront(ListNode*& pHead, Datatype x)//头插  
  27. {  
  28.     if (pHead == NULL)  
  29.     {  
  30.         pHead = createNode(x);  
  31.     }  
  32.     else  
  33.     {  
  34.         ListNode*tmp = createNode(x);  
  35.         tmp->next = pHead;  
  36.         pHead = tmp;  
  37.     }  
  38. }  
  39.   
  40.   
  41.   
  42. void PopFront(ListNode*& pHead)//头删  
  43. {  
  44.     if (pHead == NULL)  
  45.     {  
  46.         printf("empty LinkedList\n");  
  47.         return;  
  48.     }  
  49.     else if (pHead->next == NULL)  
  50.     {  
  51.         free(pHead);  
  52.         pHead = NULL;  
  53.     }  
  54.     else  
  55.     {  
  56.         ListNode* tmp = pHead;  
  57.         pHead = pHead->next;  
  58.         free(tmp);  
  59.     }  
  60. }  
  61.   
  62.   
  63. void Insert(ListNode* pos, Datatype x)//定向插入  
  64. {  
  65.     assert(pos);  
  66.     ListNode*tmp = createNode(x);  
  67.     ListNode*cur = pos->next;  
  68.     pos->next = tmp;  
  69.     tmp->next = cur;  
  70. }  
  71.   
  72.   
  73. void Erase(ListNode*pHead, ListNode* pos)//定向删除  
  74. {  
  75.     assert(pos);  
  76.     if (pos->next == NULL)  
  77.     {  
  78.         free(pos);  
  79.     }  
  80.     else  
  81.     {  
  82.         ListNode*cur = NULL;  
  83.         ListNode*tail = pos;  
  84.   
  85.         while (pHead->data != tail->data)  
  86.         {  
  87.             cur = pHead;  
  88.             pHead = pHead->next;  
  89.         }  
  90.         ListNode*tmp = pos->next;  
  91.   
  92.         cur->next = tmp;  
  93.         free(pos);  
  94.     }  
  95.   
  96. }  
  97.   
  98.   
  99.   
  100. ListNode* Find(ListNode* pHead, Datatype x)//查找某元素值  
  101. {  
  102.     assert(pHead);  
  103.     ListNode* tail = pHead;  
  104.     while (tail->next)  
  105.     {  
  106.         if (tail->data == x)  
  107.         {  
  108.             return tail;  
  109.         }  
  110.         tail = tail->next;  
  111.     }  
  112.     if (tail->next == NULL)  
  113.     {  
  114.         return tail;  
  115.     }  
  116.     return NULL;  
  117. }  
  118.   
  119.   
  120. void print(ListNode *pHead)//输出该链表  
  121. {  
  122.     ListNode* cur = pHead;  
  123.     while (cur)  
  124.     {  
  125.         printf("%d->", cur->data);  
  126.         cur = cur->next;  
  127.     }  
  128.     printf("NULL");  
  129.     printf("\n");  
  130. }<span style="color:#ff0000;font-weight: bold;">  
  131. </span>  


▲对于单链表来说,节点的增删其头删/头插比尾删/尾插来的简单些,所以只实现了头插和头删操作。



C++下的List:

  1. <span style="font-size:18px;">  
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. template<class T>  
  5. struct ListNode  
  6. {  
  7.     ListNode(const T& x)  
  8.         :_next(NULL)  
  9.         , _prev(NULL)  
  10.         , _data(x)  
  11.     {}  
  12.   
  13.     ListNode<T>* _next;  
  14.     ListNode<T>* _prev;  
  15.     T _data;  
  16. };  
  17.   
  18. template<class T>  
  19. class List  
  20. {  
  21. public:  
  22.     List()  
  23.         :_head(NULL)  
  24.         , _tail(NULL)  
  25.     {}  
  26.   
  27.     List(const List<T>& l)  
  28.     {  
  29.         ListNode<T>* cur = l._head;  
  30.         while (cur)  
  31.         {  
  32.             this->PushBack(cur->_data);  
  33.             cur = cur->_next;  
  34.         }  
  35.     }  
  36.   
  37.     List<T>& operator=(const List<T>& l)  
  38.     {  
  39.         //先删除节点,再插入节点  
  40.         if (&s != this)  
  41.         {  
  42.             ListNode<T>* pcur = _head;  
  43.             while (pcur)  
  44.             {  
  45.                 ListNode<T>* del = pcur;  
  46.                 pcur = pcur->_next;  
  47.                 delete del;  
  48.                 del = NULL;  
  49.             }  
  50.   
  51.             ListNode<T>* cur = _head;  
  52.             while (cur)  
  53.             {  
  54.                 this->PushBack(cur->_data);  
  55.                 cur = cur->_next;  
  56.             }  
  57.         }  
  58.         return *this;  
  59.     }  
  60.   
  61.     ~List()//一个节点一个节点的删除  
  62.     {  
  63.         ListNode<T>* cur = _head;  
  64.         while (cur)  
  65.         {  
  66.             ListNode<T>* del = cur;  
  67.             cur = cur->_next;  
  68.             delete del;  
  69.             del = NULL;  
  70.         }  
  71.     }  
  72.   
  73.     void PushBack(const T& x);//尾插  
  74.     void PopBack();//尾删  
  75.     void Unique();//去重  
  76.     void PrintList();//打印  
  77.     void Reverse();//逆置  
  78.     int Length();//链表长  
  79.     void Sort();//元素排序  
  80.   
  81. protected:  
  82.     ListNode<T>* _head;  
  83.     ListNode<T>* _tail;  
  84. };  
  85.   
  86.   
  87. //尾插  
  88. template<class T>  
  89. void List<T>::PushBack(const T& x)  
  90. {  
  91.     //分析:分为两种情况:无节点、有节点  
  92.     if (_head == NULL)  
  93.     {  
  94.         _head = _tail = new ListNode<T>(x);  
  95.     }  
  96.     else  
  97.     {  
  98.         ListNode<T>* cur = new ListNode<T>(x);  
  99.         _tail->_next = cur;  
  100.         cur->_prev = _tail;  
  101.         _tail = cur;  
  102.         _tail->_next = NULL;  
  103.     }  
  104. }  
  105.   
  106. //尾删  
  107. template<class T>  
  108. void List<T>::PopBack()  
  109. {  
  110.     //分析:分为三种情况:无节点、一个节点、多个节点  
  111.     if (_head == _tail)  
  112.     {  
  113.         if (_head == NULL)  
  114.         {  
  115.             return;  
  116.         }  
  117.         else  
  118.         {  
  119.             delete _head;  
  120.             _head = _tail = NULL;  
  121.         }  
  122.     }  
  123.     else  
  124.     {  
  125.         ListNode<T>* prev = _tail->_prev;  
  126.         delete _tail;  
  127.         _tail = NULL;  
  128.         _tail = prev;  
  129.         _tail->_next = NULL;  
  130.     }  
  131. }  
  132.   
  133. //去重:前提是针对已排序的有重复数据的链表  
  134. template<class T>  
  135. void List<T>::Unique()  
  136. {  
  137.    // 分析:分为三种情况:无节点一个节点(无需删除节点)、两个节点、两个以上节点  
  138.     if (_head == _tail)  
  139.     {  
  140.         return;     
  141.     }  
  142.     else  
  143.     {  
  144.         ListNode<T>* pcur = _head;  
  145.         ListNode<T>* pnext = _head->_next;  
  146.         if (pnext->_next == NULL)    //两个节点  
  147.         {  
  148.             if (pcur->_data == pnext->_data)  
  149.             {  
  150.                 delete pnext;  
  151.                 pnext = NULL;  
  152.                 _tail = _head = pcur;  
  153.                 return;  
  154.             }  
  155.             else  
  156.             {  
  157.                 return;  
  158.             }             
  159.         }  
  160.         else  
  161.         {  
  162.             //两个以上节点  
  163.             ListNode<T>* cur = _head;  
  164.             while (cur->_next)  
  165.             {  
  166.                 ListNode<T>* next = cur->_next;  
  167.                 ListNode<T>* nextnext = next->_next;  
  168.                 if (cur->_data == next->_data)  
  169.                 {  
  170.                     cur->_next = nextnext;  
  171.                      nextnext->_prev = cur;  
  172.                     delete next;  
  173.                     next = NULL;  
  174.                 }  
  175.                 cur = cur->_next;  
  176.             }  
  177.         }                                       
  178.     }     
  179. }  
  180.   
  181. //逆置  
  182. template<class T>  
  183. void List<T>::Reverse()  
  184. {  
  185.    // 分析:从两头开始走,交换数据(分奇数个数据和偶数个数据)  
  186.     ListNode<T>* begin = _head;  
  187.     ListNode<T>* end = _tail;  
  188.     while (!((begin == end) || end->_next == begin))  
  189.     {  
  190.         swap(begin->_data, end->_data);  
  191.         begin = begin->_next;  
  192.         end = end->_prev;  
  193.     }  
  194. }  
  195.   
  196. //长度  
  197. template<class T>  
  198. int List<T>::Length()  
  199. {  
  200.     ListNode<T>* cur = _head;  
  201.     int count = 0;  
  202.     while (cur)  
  203.     {  
  204.         count++;  
  205.         cur = cur->_next;  
  206.     }  
  207.     return count;  
  208. }  
  209.   
  210. //元素排序  
  211. template<class T>  
  212. void List<T>::Sort()  
  213. {  
  214.     //使用冒泡排序,实现升序或者降序  
  215.     ListNode<T>* i = _head;  
  216.     while (i != _tail)  
  217.     {  
  218.         ListNode<T>* j = _head;  
  219.         ListNode<T>* end = _tail;  
  220.         while (j != end)  
  221.         {  
  222.             if (j->_data >(j->_next)->_data)  
  223.             {  
  224.                 swap(j->_data, (j->_next)->_data);  
  225.             }  
  226.             j = j->_next;  
  227.   
  228.         }  
  229.         end = end->_prev;  
  230.         i = i->_next;  
  231.     }  
  232. }  
  233.   
  234. //打印  
  235. template<class T>  
  236. void List<T>::PrintList()  
  237. {  
  238.     ListNode<T>* cur = _head;  
  239.     while (cur)  
  240.     {  
  241.         cout << cur->_data << "->";  
  242.         cur = cur->_next;  
  243.     }  
  244.     cout << "NULL" << endl;  
  245. }</span>  



▲对于Sort排序算法,在此选择了最易实现和理解的冒泡排序,对于其它的排序(选排,插排,堆排,计数排,基数排,快排等),可自由选择合适的排序算法。



最后总结下顺序表&链表的异同:

★顺序表特点:逻辑上数据元素相邻,物理存储位置也相邻,并且存储空间需要预先分配。

      优点: 
      (1)方法简单,各种高级语言中都有数组,容易实现。   

   (2)不用为表示节点间的逻辑关系而增加额外的存储开销。   

   (3)顺序表具有按元素序号随机访问的特点。 

    缺点: 
  (1)在顺序表中做插入、删除操作时,平均移动表中的一半元素,因此对n较大的顺序表效率低。 

(2)需要预先分配足够大的存储空间,估计过大,可能会导致顺序表后部大量闲置;预先分配过小,又会造成溢出。 

★单链表特点:在链表中逻辑上相邻的数据元素,物理存储位置不一定相邻,它使用指针实现元素之间的逻辑关系。并且存储空间是动态分配的。 

    优点:

链表的最大特点是:   插入、删除运算方便。 

    缺点: 

(1)要占用额外的存储空间存储元素之间的关系,存储密度降低。存储密度是指一个节点中数据元素所占的存储单元和整个节点所占的存储单   元之比。   

(2)链表不是一种随机存储结构,不能随机存取元素。 


★两种结构的适用场景: 
      (1)顺序表的存储空间是静态分配的,在程序执行之前必须明确规定它的存储规模,也就是说事先对“Max_Size”要有合适的设定,设定过大会造成存储空间的浪费,过小造成溢出。因此,当对线性表的长度或存储规模难以估计时,不宜采用顺序表。

      (2)链表的动态分配则可以克服这个缺点。链表不需要预留存储空间,也不需要知道表长如何变化,只要内存空间尚有空闲,就可以再程序运行时随时地动态分配空间,不需要时还可以动态回收。因此,当线性表的长度变化较大或者难以估计其存储规模时,宜采用动态链表作为存储结构。   

      (3)链表中除数据域外还需要在每个节点上附加指针。如果节点的数据占据的空间小,则链表的结构性开销就占去了整个存储空间的大部分。当顺序表被填满时,则没有结构开销。在这种情况下,顺序表的空间效率更高。由于设置指针域额外地开销了一定的存储空间,从存储密度的角度来讲,链表的存储密度小于1。因此,当线性表的长度变化不大而且事先容易确定其大小时,为节省存储空间,则采用顺序表作为存储结构比较适宜。   

      (4)基于运算的考虑(时间) :顺序存储是一种随机存取的结构,而链表则是一种顺序存取结构,因此它们对各种操作有完全不同的算法和时间复杂度。例如,要查找线性表中的第i个元素,对于顺序表可以直接计算出a(i)的的地址,不用去查找,其时间复杂度为0(1).而链表必须从链表头开始,依次向后查找,平均需要0(n)的时间。所以,如果经常做的运算是按序号访问数据元素,显然顺表优于链表。 反之,在顺序表中做插入,删除时平均移动表中一半的元素,当数据元素的信息量较大而且表比较长时,这一点是不应忽视的;在链表中作插入、删除,虽然要找插入位置,但操作是比较操作,从这个角度考虑显然后者优于前者。   

     (5)基于环境的考虑(语言) 
  顺序表容易实现,任何高级语言中都有数组类型;链表的操作是基于指针的。所以相较而言顺序表的适应范围更广一些,相对来讲前者简单些,也是用户考虑的一个因素。 

 


▲ 总之,两种存储结构各有优劣,选择哪一种由实际问题中的主要因素决定。通常“较稳定”的线性表,即主要操作是查找操作的线性表,适于选择顺序存储;而频繁做插入删除运算的(即动态性比较强)的线性表适宜选择链式存储。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多