ordlist.cpp

来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 82 行

CPP
82
字号
 
Ordered_list::Ordered_list()
{
}
 
Error_code Ordered_list::insert(const Record &data)
/* 
 
Post: If the Ordered_list is not full,
the function succeeds: The Record data is
inserted into the list, following the
last entry of the list with a strictly lesser key
(or in the first list position if no list
element has a lesser key).

 
Else:
the function fails with the diagnostic Error_code overflow.
 
*/
{
   int s = size();
   int position;
   for (position = 0; position < s; position++) {
      Record list_data;
      retrieve(position, list_data);
      if (data >= list_data) break;
   }
   return List<Record>::insert(position, data);
}
 
Error_code Ordered_list::insert(int position, const Record &data)
/* 
 
Post: If the Ordered_list is not full,
0 <= position <= n,
where n is the number of entries in the list,
and the Record ata can be inserted at
position in the list, without disturbing
the list order, then
the function succeeds:
Any entry formerly in
position and all later entries have their
position numbers increased by 1 and
data is inserted at position of the List.

 
Else:
the function fails with a diagnostic Error_code.
 
*/
{
   Record list_data;
   if (position > 0) {
      retrieve(position - 1, list_data);
      if (data < list_data)
         return fail;
   }
   if (position < size()) {
      retrieve(position, list_data);
      if (data > list_data)
         return fail;
   }
   return List<Record>::insert(position, data);
}
 
Error_code Ordered_list::replace(int position, const Record &data)
{
   Record list_data;
   if (position > 0) {
      retrieve(position - 1, list_data);
      if (data < list_data)
         return fail;
   }
   if (position < size()) {
      retrieve(position, list_data);
      if (data > list_data)
         return fail;
   }
   return List<Record>::replace(position, data);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?