📄 laimpl.cpp
字号:
const int LIST_SIZE = 10;
typedef int ELEM;
class list { // Array-based list class
private:
int msize; // Maximum size of list
int numinlist; // Actual number of ELEMs in list
int curr; // Position of "current" ELEM
ELEM* listarray; // Array holding list ELEMs
public:
list(const int =LIST_SIZE); // Constructor
~list(); // Destructor
void clear(); // Remove all ELEMs from list
void insert(const ELEM&); // Insert ELEM at current position
void append(const ELEM&); // Insert ELEM at tail of list
ELEM remove(); // Remove and return current ELEM
void setFirst(); // Set curr to first position
void prev(); // Move curr to previous position
void next(); // Move curr to next position
int length() const; // Return current length of list
void setPos(const int); // Set curr to specified position
void setValue(const ELEM&); // Set current ELEM's value
ELEM currValue() const; // Return current ELEM's value
bool isEmpty() const; // Return TRUE if list is empty
bool isInList() const; // TRUE if curr is within list
bool find(const ELEM&); // Find value (from current pos)
};
list::list(const int sz) { // Constructor: initialize
msize = sz; numinlist = curr = 0;
listarray = new ELEM[sz];
}
list::~list() // Destructor: return array space
{ delete [] listarray; }
void list::clear() // Remove all ELEMs from list
{ numinlist = curr = 0; } // Simply reinitialize values
// Insert ELEM at current position
void list::insert(const ELEM& item) {
// Array must not be full and curr must be a legal position
assert((numinlist < msize) && (curr >=0)
&& (curr <= numinlist));
for(int i=numinlist; i>curr; i--) // Shift up to make room
listarray[i] = listarray[i-1];
listarray[curr] = item;
numinlist++; // Increment current list size
}
void list::append(const ELEM& item) { // Insert at tail
assert(numinlist < msize); // List must not be full
listarray[numinlist++] = item; // Increment list size
}
ELEM list::remove() { // Remove and return current ELEM
assert(!isEmpty() && isInList()); // Must be ELEM to remove
ELEM temp = listarray[curr]; // Store removed ELEM
for(int i=curr; i<numinlist-1; i++) // Shift elements down
listarray[i] = listarray[i+1];
numinlist--; // Decrement current list size
return temp;
}
void list::setFirst() // Set curr to first position
{ curr = 0; }
void list::prev() { curr--; } // Move curr to previous pos
void list::next() { curr++; } // Move curr to next position
int list::length() const // Return current length of list
{ return numinlist; }
void list::setPos(const int pos) // Set curr to specified pos
{curr = pos; }
void list::setValue(const ELEM& val) { // Set current value
assert(isInList()); // Curr must be at valid pos
listarray[curr] = val;
}ELEM list::currValue() const { // Return current ELEM's value
assert(isInList()); // Must be at a valid position
return listarray[curr];
}
bool list::isEmpty() const // Return TRUE if list is empty
{ return numinlist == 0; }
bool list::isInList() const // TRUE if curr is within list
{ return (curr >= 0) && (curr < numinlist); }
bool list::find(const ELEM& val) { // Find value
while (isInList()) // Stop if reach end
if (currValue() == val) return TRUE; // Found it
else next();
return FALSE; // Not found
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -