Beispiel #1
0
Item List::get(int n){
	
	if(n <= 0) return Item("Integer invalid");
	
	ListNode *k = head->getNext();
	
	if(k == NULL) return Item("Not enough items"); //No items
	if(n == 1) return Item(head->getItem()); //First item on the list
	
	for(int i = 2; i < n ; i++){
		k = k->getNext();
		if(k == NULL) return Item("Not enough items");
	}
	return Item(k->getItem()); //Returns the item of n on the list
}
Beispiel #2
0
bool List::remove(Item &copy) 
{
  if (!empty()) // if list is not empty
    {
      copy = head->getItem(); // return copy
      ListNode *tmp = head->getNext();
      delete head; // delete the node
      head = tmp;  // update the head
      if (tmp==NULL) // removed last element 
	tail = NULL;
      return true;
    }
  return false; // nothing in list
}
Beispiel #3
0
Item List::remove_front(){
	Item copy;
	if (!empty()) // if list is not empty
    {
      copy = head->getItem(); // return copy
      ListNode *tmp = head->getNext();
      delete head; // delete the node
      head = tmp;  // update the head
      if (tmp==NULL) // removed last element 
	tail = NULL;
      return copy;
    }
    else{
	return Item("~ERROR~");
    }
}