예제 #1
0
 /**
  *Devuelve y elimina el objeto o elemento que se encuentra en el 
  *top de la pila.  
  *@return el objeto que se elimina
  */
  Obj pop(){
  	if(elems->isEmpty()){
  		throw "Pila Vacia";
  	}
  	Obj temp = elems->get(1);
  	elems->remove(1);
  	return temp;
  }
예제 #2
0
void list_remove(linked_list list, const int index){
    struct list_node* n;    // The node to remove

    // Handle head case
    if(index == 0 && list->size > 0){
        n = list->head;
        list->head = list->head->next;
    } else {
        n = get_node_at(list, index-1);
        // Handle tail case
        if(index == list->size - 1){
            list->tail = n;
            n = list->tail->next;
            list->tail->next = NULL;
        } else {
            // Handle normal case, link previous with following node
            struct list_node* to_remove = n->next;
            n->next = to_remove->next;
            n = to_remove;
        }
    }

    // Custom free
    if(list->fun != NULL)
        list->fun(n->elem);
    // Free node and its element
    free(n->elem);
    free(n);

    // Decrease size
    list->size--;
}
예제 #3
0
void list_delete(linked_list list){
    if(list == NULL)
        return;

    // Itearate through each node and free its memory
    struct list_node* next = list->head;
    while(next != NULL){
        struct list_node* temp = next->next;
        // Custom free
        if(list->fun != NULL)
            list->fun(next->elem);

        free(next->elem);
        free(next);
        next = temp;
    }

    // Free list struct memory
    free(list);
}
예제 #4
0
int deserialize(linked_list<T> &list, std::fstream& in) {
    if(!in.is_open())
        throw serialize_exception();
    
    // check #1: check serial ID
    long id;
    deserialize(id, in);
    if(id != LINKEDLIST_SERIAL_ID)
        throw serialize_exception();

    // check #2: get byte count to check later.
    int content_byte_count;
    deserialize(content_byte_count, in);
    
    // clear the current linked list
    list.clear(); // leaves the dummy node
    list.modified = true; // traversal is interrupted
 
    // keep track of bytes read for checking the content_byte_count
    int byte_count = 0;

    // get list size
    byte_count += deserialize(list.list_size, in);
    
    if(list.list_size > 0 ) {
        typename linked_list<T>::link* builder = list.head; // builder starts at the header     
        for(int i=0; i<list.list_size; i++) {
            builder->next = new typename linked_list<T>::link;
            builder = builder->next;
            byte_count += deserialize(builder->element, in);
        }
        builder->next = NULL;
        list.tail = builder;
    }

    // complete the content byte count check
    if(byte_count != content_byte_count)
        throw serialize_exception();
    
    return content_byte_count + sizeof(int) + sizeof(long); // content + header
}
예제 #5
0
 void printStack(){
 	elems->toString();
 } 
예제 #6
0
 /**
  *Elimina todos los elementos existentes en la pila.
  */
  void popAll(){
  	elems->removeAll();
  }
예제 #7
0
 /**
  *Devuelve el objeto que se enuentra en el tope de la pila.
  *@return objeto al inicio de la pila.
  */
  Obj peek(){
  	if(elems->isEmpty())
  		throw "Pila Vacia";
  	return elems->get(1);
  }
예제 #8
0
 /**
  *Insterta un objeto en el tope de la pila
  *@param el objeto que se insertara.
  */
  void push(Obj obj){
  	elems->add(1,obj);
  }
예제 #9
0
 /**
  *Devuelve el valor logico que indica tiene o no elementos
  *@return true si esta vacia.
  */
  bool isEmpty(){
  	return elems->isEmpty();
  }
예제 #10
0
 /**
  *Devuelve el tamaño actual de la pila.
  *@return int con el tamaño de la pila.
  */
  int size(){
  	return elems->size();
  }
 *
 * Run only the tests for the size() method:
 * $ ./unit_tests [size]
 *
 * Run only the tests for the insert() method:
 * $ ./unit_tests [insert]
 *
 * Run only the tests for the remove() method:
 * $ ./unit_tests [remove]
 */

typedef int test_type; // change this to test other types

TEST_CASE("item_at method", "[item_at]")
{
	linked_list<test_type> l;

	SECTION("when list is empty")
	{
		REQUIRE_THROWS(l.item_at(0));
		REQUIRE_THROWS(l.item_at(5));
		REQUIRE_THROWS(l.item_at(-1));
	}

	SECTION("existing list")
	{
		l.insert(0, 1);
		l.insert(1, 2);
		l.insert(2, 3);
		l.insert(3, 4);
예제 #12
0
    void rquicksort(linked_list<int>& ll)
    {
        cout << "rquicksort: TOP: ll = " << endl;
        ll.dump();
        int pivot = ll.get_last();
        cout << "rquicksort: pivot = " << pivot << endl;
        linked_list<int> lm;
        linked_list<int> ln;
        while (ll.get_first() != -1)
        {
            if (ll.get_first() < pivot)
            {
                lm.add_last(ll.get_first());
                ll.remove_first();
            }
            else if (ll.get_first() > pivot)
            {
                ln.add_last(ll.get_first());
                ll.remove_first();
            }
            else if (ll.get_first() == pivot)
            {
                ll.remove_first();
            }

        }
        cout << "rquicksort: lm = " << endl;
        lm.dump();
        cout << "rquicksort: ln = " << endl;
        ln.dump();

        if (lm.get_first() != lm.get_last())
        {
            cout << "calling recursively with lm..." << endl;
            rquicksort(lm);
        }
        if (ln.get_first() != ln.get_last())
        {
            cout << "calling recurisvely with ln..." << endl;
            rquicksort(ln);
        }
        cout << "rquicksort: after recursive calls, concatenating...!" << endl;
        while (lm.get_first() != -1)
        {
            cout << "adding " << lm.get_first() << " from lm" << endl;
            ll.add_last(lm.get_first());
            lm.remove_first();
        }
        ll.add_last(pivot);
        cout << "adding " << pivot << " from pivot" << endl;
        while (ln.get_first() != -1)
        {
            cout << "adding " << ln.get_first() << " from ln" << endl;
            ll.add_last(ln.get_first());
            ln.remove_first();
        }
        cout << "rquicksort: done concatenating, ll =" << endl;
        ll.dump();

    }
예제 #13
0
 T pop()
 {
     T tmp = top();
     ll.remove_first();
     return tmp;
 }
예제 #14
0
 T top()
 {
     T tmp = 0;
     tmp = ll.get_first();
     return tmp;
 }
예제 #15
0
 void push(T d)
 {
     ll.add_first(d);
 }