示例#1
0
文件: llist.cpp 项目: jorgeer/cpp
bool LinkedList::removeFromBack(string &output)
{
    if (!isEmpty())
    {

        ListNode* currNode = head;
        ListNode* prevNode;


        // move to second last node
        while (currNode->getNext() != NULL)
        {
            prevNode = currNode;
            currNode = currNode->getNext();
        }
            

        output = currNode->value;
        delete currNode;
        prevNode->next = NULL;
        last = prevNode;
    }
    else
    {
        return false;
    }
        
}
示例#2
0
bool List::remove(Object* const object)
{
	bool objectFound = false;

	ListNode* temp = this->first; 
	ListNode* prev = NULL;
	ListNode* next = NULL;

	while( temp != NULL ) {
		if (object == temp->getObject()) {
			next = temp->getNext();
			delete temp;
			if(next == NULL)
				last = NULL;
			if(prev == NULL)
				first = next;
			else
				prev->setNext(next);
			objectFound = true;
			break;
		}
		prev = temp;
		temp = temp->getNext();
	}
	return objectFound;
}
示例#3
0
void LinkedList::printList(ofstream& out){
   out << "ListHead --> (" << listHead->getData() << ", " << listHead->getNext() << ") ";
   ListNode* temp = listHead->getNext();
   while(temp != NULL){
      out << "--> (" << temp->getData() << ", " << temp->getNext() << ") ";
      temp = temp->getNext();
   }
   out << endl;
}
示例#4
0
ListNode* LinkedList::findSpot(int input){
   if(isEmpty()){
      return listHead;
   }
   ListNode* temp = listHead;
   while(temp->getNext() != NULL && temp->getNext()->getData() < input){
      temp = temp->getNext();
   }
   return temp;
}
示例#5
0
ListIterator<Object> List<Object>::findPrevious( const Object& data ) const {
	ListNode<Object>* node = head;
	while( node->getNext() != NULL && node->getNext()->getElement() != data ) {
		node = node->getNext();
	}
	if (node->getNext() == NULL) {
		node = NULL;
	}
	return ListIterator<Object>( node );
}
示例#6
0
// Show the contents of the list
void LinkedList::print(void)
{
  ListNode *node = head.getNext();

  while (node != 0) {
    cout << setw(6) << node->getVal();
    node = node->getNext();
  }
  cout << endl;
}
示例#7
0
void List<Object>::remove( const Object& data ) {
	ListIterator<Object> iter = findPrevious( data );
	if (iter.isValid()) {
		ListNode<Object>* node = findPrevious( data ).current;
		if (node->getNext() != NULL) {
			ListNode<Object> *oldNode = node->getNext();
			node->setNext( node->getNext()->getNext() );  // Skip oldNode
			delete oldNode;
		}
	}
}
示例#8
0
// Insert in sorted order (ascending)
ListNode * LinkedList::insert(const int val)
{
  ListNode *node = head.getNext();
  ListNode *prev = &head;

  while (node != 0 && node->getVal() < val) {
    prev = node;
    node = node->getNext();
  }
  return prev->setNext(new ListNode(val, prev->getNext()));
}
示例#9
0
void LinkedList::listInsert(int input, ofstream& out){
   ListNode* spot = findSpot(input);
   if(spot->getNext() != NULL){
      if(spot->getNext()->getData() == input){
         out << input << " is already in the list." << endl;
         return;
      }
   }
   ListNode* temp = new ListNode(input, spot->next);
   spot->next = temp;
   printList(out);
}
示例#10
0
// Look for a node having the specified value
ListNode * LinkedList::find(const int val)
{
  ListNode *node = head.getNext();

  while (node != 0 && node->getVal() != val) {
    node = node->getNext();
  }
  if (node->getVal() == val) {
    return node;
  }
  return 0;
}
示例#11
0
void List::Print() const {
	if(head==NULL)
		cout << "The list is empty." << endl << endl;
	else{
		cout << "The current list is as follows:" << endl << endl;
		ListNode * index = head;
		cout << index->getMyInt() << endl;
		while(index->getNext()!=NULL){
			index=index->getNext();
			cout << index->getMyInt() << endl;
		}
		cout << endl << endl;
	}
}
示例#12
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
}
示例#13
0
文件: List.cpp 项目: Aillaud/p4l6
bool List::member(ICollectible *c) const{
    for(ListNode *current = head; current != NULL; current = current->getNext())
        if(current->getElem() == c)
            return true;
    return false;
        
}
示例#14
0
// Remove the first node having the specified value (if present)
int LinkedList::remove(const int val)
{
  ListNode *node = head.getNext();
  ListNode *prev = &head;

  while (node != 0 && node->getVal() != val) {
    prev = node;
    node = node->getNext();
  }
  if (node->getVal() == val) {
    prev->setNext(node->getNext());
    delete node;
    return val;
  }
  return -1;
}
示例#15
0
文件: hw2.cpp 项目: APeeples/BS
int List::length(){
	int counter = 0;
	if(empty()) return 0;
	ListNode *n = head->getNext();
	if(n == NULL){
		return 1;
	} 
	else{
		counter++; //list had at least 1 item
		while (n != NULL){ // while list is not empty
			counter++;
			n = n->getNext();
		}
		return counter; //Returns the # of items in the list
	}
}
  vector< Point2d > PointPersistentList::enumerateNE(coord_t x, coord_t y) {
    vector< Point2d > v;
    // determine the time at which to search by searching for the x
    int index = binarySearchX(x);
    // if set of points is empty, bail out
    if(index == -1) return v;
    // while the closest point is too small
    while(points_sorted_by_x[index].x < x) {
      // check the previous point, which should be larger since the
      // array is sorted by x descending
      --index;
      // if we have passed the beginning of the array, then there are no
      // points within the query region
      if(index < 0)
	return v;
    }
    // get the first node in this list at time index
    ListNode<Point2d, Point2d::yxdesc >* pln = points_right.getList(index);
    // while the current point is not null and has a greater or equal
    // y than the query
    while(pln != NULL && pln->data.y >= y) {
      // push the point onto the list to be returned
      v.push_back(pln->data);
      // move on to next point
      pln = pln->getNext(index);
    }
    return v;
  }
示例#17
0
void List::copy(List L){
		Head = NULL;
		ListNode * temp = L.Head;
		while(temp!=NULL){
			this->append(temp->getData());
			temp=temp->getNext();
		}
}
示例#18
0
// Add to the end of the list
ListNode * LinkedList::append(const int val)
{
  ListNode *node = &head;

  while (node->hasNext()) {
    node = node->getNext();
  }
  return node->setNext(new ListNode(val));
}
示例#19
0
文件: List.cpp 项目: Aillaud/p4l6
List::~List() {
    ListNode *current = head;
    ListNode *next;
    while (current != NULL) {
        next = current->getNext();
        delete current;
        current = next;
    }
}
示例#20
0
void* PtrListIterator::next()
{
    if(_lead==NULL)
        throw IndexOutOfBoundsException();

    void* element = _lead->getElement();
    _lead = _lead->getNext();

    return element;
}
示例#21
0
文件: List.cpp 项目: Aillaud/p4l6
void List::remove(ICollectible *c)
{
    ListNode *current = head;
    ListNode *previous = NULL;
    while(current != NULL && current->getElem() != c){
        previous = current;
        current = current->getNext();
    }

    if(current == NULL){ // final de la lista, no estaba
        return;
    } else if(current->getElem() == c){ // ya está, se borra
        --size;
        if(previous == NULL) // se borra el primer elemento
            head = current->getNext();
        else
            previous->setNext(current->getNext());
        delete current;
    }
}
示例#22
0
文件: llist.cpp 项目: jorgeer/cpp
ostream & operator<<(ostream & stream, const LinkedList & list )
{
    ListNode* currNode = list.head;

    while (currNode != NULL)
    {
        cout << currNode->getValue();
        currNode = currNode->getNext();
    }
    
    return stream;
}
示例#23
0
ListIterator::~ListIterator()
{
    if(disposeHeadOnDelete){
        ListNode *n = head;
        while(n != NULL){
            ListNode *deleteMe = n;
            n = n->getNext();
            delete deleteMe;
        }
    }
        
}
/*---------------------------------------------------------------------*//**
	コピー
**//*---------------------------------------------------------------------*/
void ProgMatter::Setting::copy(const Setting* s)
{
	// _listParams のコピー
	{
		_listParams->removeAll();
		ListNode<KeyValue*>* lnode = s->_listParams->getHeadNode();
		while(lnode != 0L)
		{
			_listParams->addTail(new KeyValue(*lnode->getObject()));
			lnode = lnode->getNext();
		}
	}
}
  Point2d* PointPersistentList::leftMostNE(coord_t x, coord_t y) {
    Point2d* leftMost = NULL;
    // determine the time at which to search by searching for the x
    int index = binarySearchX(x);
    // if set of points is empty, bail out
    if(index == -1) return NULL;
    // while the closest point is too small
    while(points_sorted_by_x[index].x < x) {
      // check the previous point, which should be larger since the
      // array is sorted by x descending
      --index;
      // if we have passed the beginning of the array, then there are no
      // points within the query region
      if(index < 0)
	return NULL;
    }
    // get the first node in this list at time index
    ListNode<Point2d, Point2d::yxdesc >* pln = points_right.getList(index);
    // if there are no points NE of the given point, return null
    if(pln == NULL || pln->data.y < y) return NULL;
    // take the first point as left most for now
    leftMost = &(pln->data);
    // set pln to its next pointer
    pln = pln->getNext(index);
    // iterate over nodes in the list until:
    //  - we reach the end
    // OR
    //  - the points are no longer NE of the query point
    while(pln != NULL && pln->data.y >= y) {
      // check if point is more left than current left most
      if(pln->data.x < leftMost->x)
	leftMost = &(pln->data);
      // set pln to its next pointer
      pln = pln->getNext(index);
    }
    // return the left most point
    return leftMost;
  }
示例#26
0
文件: llist.cpp 项目: jorgeer/cpp
void LinkedList::remove(const string &value)
{
    ListNode* currNode = head;
    ListNode* prevNode;

    if (head == NULL) return;

    while (currNode != NULL)
    {
        if (currNode->getValue() == value)
        {
            if (prevNode != NULL)
                prevNode->next = currNode->getNext();
            else
                head = currNode->getNext();

            delete currNode;
        }
        prevNode = currNode;
        currNode = currNode->getNext();
    }
    last = currNode;
}
示例#27
0
文件: llist.cpp 项目: jorgeer/cpp
ListNode *LinkedList::search(const string &value)
{
    ListNode* currNode = head;

    if (head == NULL) return NULL;

    while (currNode != NULL)
    {
        if (currNode->getValue() == value)
            return currNode;

        currNode = currNode->getNext();
    }
}
示例#28
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
}
示例#29
0
ostream& operator<<(ostream &stream, const LinkedList &l) {
    
    stream << "[";
    
    ListNode *i = l.head;
    while(i != NULL) {
        stream << i->getValue();
        i = i->getNext();
        if(i != NULL)
            stream << ", ";
    }
    
    stream << "]";
    
}
示例#30
0
void List::Insert(int newInt){
	if(head==NULL){
		ListNode * h = new ListNode(newInt,NULL);
		head=h;
	}
	else{
		ListNode * index = head;
		ListNode * prev = NULL;
		ListNode * newListNode = new ListNode(newInt);
		while(index->getNext()!=NULL && index->getMyInt()<=newInt){
			prev=index;
			index=index->getNext();
		}
		if(index->getMyInt()>newInt){
			newListNode->setNext(index);
			if(prev!=NULL)
				prev->setNext(newListNode);
			else
				head=newListNode;
		}
		else
			index->setNext(newListNode);
	}
}