6.4. Basic stack operations
Pushing an element on the stack
You can push an element on the stack (append it to the end of the list) :
template <class T> void TCODList::push(const T elt)
void TCOD_list_push(TCOD_list_t l, const void * elt)
Parameter | Description |
---|---|
elt | Element to append to the list. |
l | In the C version, the list handler, returned by a constructor. |
Example:
TCODList<int> intList; // the list is empty (contains 0 elements)
intList.push(5); // the list contains 1 element at position 0, value = 5
intList.push(2);
TCOD_list_t intList = TCOD_list_new();
TCOD_list_push(intList,(const void *)5);
TCOD_list_push(intList,(const void *)2);
Poping an element from the stack
You can pop an element from the stack (remove the last element of the list).
template <class T> T TCODList::pop()
void * TCOD_list_pop(TCOD_list_t l)
Parameter | Description |
---|---|
l | In the C version, the list handler, returned by a constructor. |
Example:
TCODList<int> intList; // the list is empty (contains 0 elements)
intList.push(5); // the list contains 1 element at position 0, value = 5
intList.push(2); // the list contains 2 elements : 5,2
int val = intList.pop(); // val == 2, the list contains 1 element : 5
val = intList.pop();
TCOD_list_t intList = TCOD_list_new();
TCOD_list_push(intList,(const void *)5);
TCOD_list_push(intList,(const void *)2);
int val = (int)TCOD_list_pop(intList);
val = (int)TCOD_list_pop(intList);
Peeking the last element of the stack
You can read the last element of the stack without removing it :
template <class T> T TCODList::peek() const
void * TCOD_list_peek(TCOD_list_t l)
Parameter | Description |
---|---|
l | In the C version, the list handler, returned by a constructor. |
Example:
TCODList<int> intList;
intList.push(3); // intList contains 1 elements : 3
int val = intList.peek(); // val == 3, inList contains 1 elements : 3
intList.push(2); // intList contains 2 elements : 3, 2
val = intList.peek();
TCOD_list_t intList = TCOD_list_new();
TCOD_list_push(intList,(const void *)3);
int val = (int)TCOD_list_peek(intList);
TCOD_list_push(intList,(const void *)2);
val = (int)TCOD_list_peek(intList);