示例#1
0
文件: hw2.cpp 项目: APeeples/BS
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
}
示例#2
0
文件: hw2.cpp 项目: APeeples/BS
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
}
示例#3
0
文件: hw2.cpp 项目: APeeples/BS
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~");
    }
}