c03p150.txt

来自「Data Abstraction & Problem Solving with 」· 文本 代码 · 共 64 行

TXT
64
字号
// *********************************************************// Implementation file ListA.cpp for the ADT list// Array-based implementation// *********************************************************#include "ListA.h"  //header fileList::List() : size(0){}   // end default constructorbool List::isEmpty() const{   return size == 0;}  // end isEmptyint List::getLength() const{   return size;} // end getLengthvoid List::insert(int index, ListItemType newItem,                  bool& success){   success = (index >= 1) &&             (index <= size + 1) &&             (size < MAX_LIST);   if (success)   {  // make room for new item by shifting all items at      // positions >= index toward the end of the      // list (no shift if index == size+1)      for (int pos = size; pos >= index; --pos)         items[translate(pos+1)] = items[translate(pos)];      // insert new item      items[translate(index)] = newItem;      ++size;  // increase the size of the list by one   }  // end if} // end insertvoid List::remove(int index, bool& success){   success = (index >= 1) && (index <= size) ;   if (success)   {  // delete item by shifting all items at positions >      // index toward the beginning of the list      // (no shift if index == size)      for (int fromPosition = index+1;               fromPosition <= size; ++fromPosition)         items[translate(fromPosition-1)] = items[translate(fromPosition)];      --size;  // decrease the size of the list by one   }  // end if}  // end removevoid List::retrieve(int index, ListItemType& dataItem,                    bool& success) const{   success = bool( (index >= 1) && (index <= size) );   if (success)      dataItem = items[translate(index)];} // end retrieveint List::translate(int index) const{   return index-1;} // end translate//  End of implementation file.

⌨️ 快捷键说明

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