📄 ladttex.cpp
字号:
const int LIST_SIZE = 10;
template <class 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)
};
template <class ELEM>
list<ELEM>::list(const int sz) { // Constructor: initialize
msize = sz; numinlist = curr = 0;
listarray = new ELEM[sz];
}
template <class ELEM>
list<ELEM>::~list() // Destructor: return array space
{ delete [] listarray; }
template <class ELEM>
void list<ELEM>::clear() // Remove all ELEMs from list
{ numinlist = curr = 0; } // Simply reinitialize values
// Insert ELEM at current position
template <class ELEM>
void list<ELEM>::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
}
template <class ELEM>
void list<ELEM>::append(const ELEM& item) { // Insert at tail
assert(numinlist < msize); // List must not be full
listarray[numinlist++] = item; // Increment list size
}
template <class ELEM>
ELEM list<ELEM>::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;
}
template <class ELEM>
void list<ELEM>::setFirst() // Set curr to first position
{ curr = 0; }
template <class ELEM>
void list<ELEM>::prev() { curr--; } // Move to previous pos
template <class ELEM>
void list<ELEM>::next() { curr++; } // Move to next position
template <class ELEM>
int list<ELEM>::length() const // Return length of list
{ return numinlist; }
template <class ELEM>
void list<ELEM>::setPos(const int pos) // Set to specified pos
{curr = pos; }
int main() { // Create and use a list variable
list<int> L1(15);
list<double> L2;
L1.append(12);
L2.append(12.0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -