예제 #1
0
파일: DLList.cpp 프로젝트: lovodi/CSCI-21
//inserts new nodes into the linked list and will be moved to the right position in list
void DLList::insert(int newContents)
{
    if (head == NULL)
        pushFront(newContents);
    else if (head -> getNext() == NULL)
    {
        if (newContents < head -> getContents())
            pushFront(newContents);
        else
            pushBack(newContents);
    }
    else
    {
        DLNode* trailer = NULL;
        DLNode* spot = head;
        while (spot -> getNext() != NULL && newContents > spot -> getContents())
        {
            trailer = spot;
            spot = spot -> getNext();
        }
        if (spot -> getNext() == NULL && newContents > spot -> getContents())
            pushBack(newContents);
        else
        {
            DLNode* nodee = new DLNode(newContents);
            nodee -> setNext(spot);
            if (trailer != NULL)
                trailer -> setNext(nodee);
            else
                head = nodee;
            count++;
        }
    }
}
static char *test_removeElementFromList_lastElement() {
    Person *testPerson1 = (Person *) malloc(sizeof(Person));
    Person *testPerson2 = (Person *) malloc(sizeof(Person));
    Person *testPerson3 = (Person *) malloc(sizeof(Person));

    testPerson1->first_name = "Zbigniew";
    testPerson1->last_name = "Ostatek";

    testPerson2->first_name = "Wladimir";
    testPerson2->last_name = "Srodkowy";

    testPerson3->first_name = "Andrzej";
    testPerson3->last_name = "Pierwszy";

    Node *testListHead = initList(testPerson1);
    testListHead = pushFront(testListHead, testPerson2);
    testListHead = pushFront(testListHead, testPerson3);

    Node *secondNode = testListHead->next;

    Node *nodeToRemove = (Node *) malloc(sizeof(Node));
    Person *removePerson = (Person *) malloc(sizeof(Person));
    removePerson->first_name = "Zbigniew";
    removePerson->last_name = "Ostatek";
    nodeToRemove->person = removePerson;
    nodeToRemove->previous = secondNode;

    Node *listAfterRemoval = removeElementFromList(testListHead, nodeToRemove);
    mu_assert("error in testRemoveElementFromList_lastElement",
              listAfterRemoval->next->next == NULL);
    removeList(listAfterRemoval);
    free(nodeToRemove);
    free(removePerson);
    return 0;
}
예제 #3
0
void DLList::insert(int newValue) {
  if (head == NULL)
    pushFront(newValue);
  else if (newValue <= head->getContents())
    pushFront(newValue);
  else if (newValue >= tail->getContents())
    pushBack(newValue);
  else {
    DLNode* spot = head;

    while (spot->getNext() != NULL && newValue > spot->getContents())
      spot = spot->getNext();

    if (spot->getNext() == NULL && newValue >= spot->getContents())
      pushBack(newValue);
    else {
      DLNode* newNode = new DLNode(newValue);
      DLNode* trailer = spot->getPrevious();
      trailer->setNext(newNode);
      newNode->setNext(spot);
      newNode->setPrevious(trailer);
      spot->setPrevious(newNode);
      size++;
    }

  }
}
static char *test_findElement_ElementDoesNotExist() {
    Person *testPerson = (Person *) malloc(sizeof(Person));
    Person *testPerson2 = (Person *) malloc(sizeof(Person));
    Person *testPerson3 = (Person *) malloc(sizeof(Person));

    Person *elementToFind = (Person *) malloc(sizeof(Person));
    testPerson->first_name = "Abdel";
    testPerson->last_name = "Akacja";
    testPerson2->first_name = "Boguslaw";
    testPerson2->last_name = "Bluszcz";
    testPerson3->first_name = "Cezary";
    testPerson3->last_name = "Czomber";

    elementToFind->first_name = "Dionizy";
    elementToFind->last_name = "Dab";

    Node *list = initList(testPerson);
    list = pushFront(list, testPerson2);
    list = pushFront(list, testPerson3);

    mu_assert("error in findElement_ElementDoesNotExist", findElement(list, elementToFind) == 0);
    removeList(list);
    free(elementToFind);
    return 0;
}
static char *test_removeElementFromList_firstElement() {
    Person *testPerson1 = (Person *) malloc(sizeof(Person));
    testPerson1->first_name = "John";
    testPerson1->last_name = "Kowalsky";
    Person *testPerson2 = (Person *) malloc(sizeof(Person));
    testPerson2->first_name = "Wladimir";
    testPerson2->last_name = "Nowak";

    Node *testListHead = initList(testPerson1);
    testListHead = pushFront(testListHead, testPerson2);

    Node *secondNode = testListHead->next;

    Person *removePerson = (Person *) malloc(sizeof(Person));
    removePerson->first_name = "Wladimir";
    removePerson->last_name = "Nowak";
    Node *nodeToRemove = (Node *) malloc(sizeof(Node));
    nodeToRemove->person = removePerson;
    nodeToRemove->previous = NULL;
    nodeToRemove->next = secondNode;

    Node *listAfterRemoval = removeElementFromList(testListHead, nodeToRemove);
    mu_assert("error in removeElementFromList_firstElement",
              strcmp(listAfterRemoval->person->first_name ,"John") == 0);
    removeList(listAfterRemoval);
    free(removePerson);
    return 0;
}
예제 #6
0
//create new DLNode with newContents and insert in ascending (based on newContents) order
void DLList::insert (int newContents)
{
    if(head == NULL || newContents < head->getContents())
    {
        pushFront(newContents);
        return;
    }
    
    DLNode* temp = head;
    
    while(temp->getNext() != NULL)
    {
        DLNode* nextTemp = temp->getNext();
        if(nextTemp->getContents() > newContents)
        {
            DLNode* inserted = new DLNode(newContents);
            
            inserted->setPrevious(temp);
            inserted->setNext(nextTemp);
            
            temp->setNext(inserted);
            nextTemp->setPrevious(inserted);
            
            size++;
            return;
        }
        temp = temp->getNext();
    }
    pushBack(newContents);
}
예제 #7
0
파일: tt_liste01.c 프로젝트: cebor/prog1
void testTeil1 (t_Listenkopf *li)
{
	int i;
	char s[LAENGE];

	// Testfall 1:
	printf("Testfall 1: push und print\n");
	for (i = 0; i < N; i++)
		pushFront(li, testListe[i]);

	printListe(li);
	Assert(li->anzahlElemente == N, "falsche Elementezahl");
#ifdef TEST
	printf("Press Enter...\n");
	while (getchar() != '\n');
#endif

	// Testfall 2
	printf("Testfall 2: pop einzeln\n");

	for (i = 0; i < N; i++)
	{
		popFront(li, s);
		printf("pop: %s  Rest: \n",s);
		Assert(strcmp(s, testListe[N-i-1])==0, "falsche Reihenfolge");// prüfen Folge
		Assert(li->anzahlElemente == (N-i-1), "falsche Elementezahl");//Anzahl
		printListe (li);
#ifdef TEST
	printf("Press Enter...\n");
	while (getchar() != '\n');
#endif
	}
	Assert(li->anzahlElemente == 0, "falsche Elementezahl");
}
예제 #8
0
/**
 * create new DLNode with newContents and attach at tail
 */
 void DLList::pushBack(int newContents) 
 {
    /**
     * Checks if the list is empty, if so calls the InsertHead() function
     */
    if(head_ != NULL)
    {
      /**
       * Creates a new node of the specified value and a iterator node, then
       * sets the iterator node to head_
       */
      DLNode* temp_node, *newNode = new DLNode(newContents);
      temp_node = head_;
      /**
       * Loops through the list to check if the next node is null, if not it
       * iterates to the next node
       */
      while(temp_node->getNext() != NULL)
      {
        temp_node = temp_node->getNext();
      }
      /**
       * Inserts the node at the end of the list, points the tail to it and
       * increases the size variable by 1
       */
      temp_node->setNext(newNode);
      //head_->setPrevious(newNode);
      tail_ = newNode;
      count_ = count_ + 1;
    } else 
    {
      pushFront(newContents);
    }
 }
예제 #9
0
/**
 * Insert function that creates a new node and
 * inserts it in an appropriate location in the list.
 *@param string contents the contents of the new node.
 */
void DLList::insert(string contents)
{
    //Checks if head is null or contents is < head_->getContents();
    if (head_ == NULL)
    {
        pushFront(contents);
    } else if ( tail_->getContents() < contents)
    {
      //  cout << " got it: " << contents << endl;
     //   cout << ToString();
        pushBack(contents);
    } else 
    {
        //creates iterator and newnode
        DLNode* iterator;
        iterator = head_;
        while (iterator->getNext() != tail_ && contents > iterator->getNext()->getContents())
        {
        iterator = iterator->getNext();
        }
        if (iterator != tail_)
        {
            DLNode *newNode = new DLNode(contents);
            newNode->setNext(iterator->getNext());
            iterator->setNext(newNode);
            size_ +=1;
        } else
        {
            pushBack(contents);
        }
    }
}
예제 #10
0
파일: mymem.c 프로젝트: turu/sysopy
int myfree(void * blockPtr) {
    if (!_initialized)
        return -1;

    DescriptorNode * node = findDescriptor(blockPtr, _usedList);
    //printf("Chunk to free: blocks=%d, ptr=%d\n", node->value->blockCount, node->value->memory);

    size_t nodeSize = node->value->blockCount * (BLOCK_SIZE << 10);

    if (node == NULL)
        return 0;

    removeFromList(node, &_usedList);
    pushFront(node, &_freeList);

    _usedCount--;
    _freeCount++;
    _totalFreeSize += nodeSize;

    if (nodeSize < _minFreeSize)
        _minFreeSize = nodeSize;
    if (nodeSize > _maxFreeSize)
        _maxFreeSize = nodeSize;

    return 1;
}
예제 #11
0
void List::insertByIndex(double inData, uint index)
{
    if (index == 0)
    {
        pushFront(inData);
    }
    else if (index == _count)
    {
        pushBack(inData);
    }
    else 
    {
        Node* element = elementByIndex(index);
        if (element != NULL)
        {
            _count++;
            Node* newElement = new Node(inData);
            newElement->setNext(element);
            newElement->setPrevious(element->getPrevious());

            element->getPrevious()->setNext(newElement);
            element->setPrevious(newElement);
        }
    }
}
예제 #12
0
//function insert element at position "pos";
void insertAt(int pos,int p)
{
	int i=(int)0;
	if(pos<=(int)0){	//add element at the beginning of list;
		pushFront(p);
	}else if(pos>=size)
	{
		pushBack(p);	//add element at the and of list;
	}else{
		DEQ* bef=front;
		DEQ* after;
		DEQ* pel=new DEQ;
		pel->data=p;
		while(i<pos){	//find element, befor which insert new element;
			bef=bef->next;
			i++;
		}
		after=bef->prev;
		bef->prev=pel;
		pel->next=bef;
		pel->prev=after;
		after->next=pel;
		size++;
	}
}
예제 #13
0
/**
 * create new DLNode with newContents and insert in ascending (based on 
 * newContents) order
 */
 void DLList::insert(int newContents) 
 {
    /**
     * Checks to see if head_ is null or if the contents of head_ are greater
     * then the parameter being inserted. If it is then calls the 
     * pushFront() function, otherwise continues to next check
     */
    if(head_ == NULL || head_->getContents() > newContents)
    {
        pushFront(newContents);
      /**
       * Checks if the contents of tail_ are less then the parameter being
       * inserted.  If so, calls pushBack() function, otherwise continues
       * to next check
       */
    } else if (tail_->getContents() < newContents)
    {
      pushBack(newContents);
      /**
       * Inserts the parameter into the correct location in the list
       */
    } else 
    {
      DLNode* iterator;
      iterator = head_;
      /**
       * Checks whether the contents of the next node is less then the parameter
       * and that the next node is not the tail.  If they are not, then it 
       * advances the iterator to the next node in the list
       */
      while(newContents > iterator->getNext()->getContents() && iterator->getNext() != tail_)
      {
        iterator = iterator->getNext();
      }
      /**
       * Checks if the iterator is located at the tail
       */
      if(iterator != tail_)
      {
        /**
         * If the iterator is not located at the tail, it creates a new node
         * and inserts it at the iterators current location, then increases
         * the size variable by 1
         */
        DLNode* new_node = new DLNode(newContents);
        new_node->setNext(iterator->getNext());
        //new_node->setPrevious(iterator);
        iterator->setNext(new_node);
        count_ = count_ + 1;
      } else
      {
        /**
         * If the iterator is located at the tail, then calls the InsertTail()
         * function
         */
        pushBack(newContents);
      }
    }
 }
예제 #14
0
void DLList::pushBack(int value) {
  if (head == NULL) {
    pushFront(value);
  } else {
    DLNode* newTail = new DLNode(value);
    newTail->setPrevious(tail);
    tail->setNext(newTail);
    tail = newTail;
    size++;
  }
}
static char *test_pushFront_newPersonToEmptyList() {
    Person *testPerson = (Person *) malloc(sizeof(Person));
    testPerson->first_name = "John";
    testPerson->last_name = "Kowalsky";
    testPerson->address = "Walbrzych";
    Node *testListHead = pushFront(NULL, testPerson);

    mu_assert("error in pushFront_newPersonToEmptyList",
              strcmp(testListHead->person->first_name, "John") == 0 );
    removeList(testListHead);
    return 0;
}
예제 #16
0
void DLList::pushBack(int newContents){
        if(head == NULL){
                pushFront(newContents);
        }
        else{
                DLNode* node = new DLNode(newContents);
                DLNode* b = head;
                while(b->getNextNode() != NULL){
                    b = b->getNextNode();
                }
                b->setNextNode(node);
                size++;
        }
}
예제 #17
0
void DLList::insert(int newContents) {
    if (head == NULL) {
        pushFront(newContents);
    } else {
        count++;
        DLNode* newNode = new DLNode(newContents);
        if (head->getContents() > newContents){
            DLNode* oldHead = head;
            head = newNode;
            head->setNext(oldHead);
            oldHead->setPrevious(head);
            oldHead = NULL;
        } else {
            if (head->getNext() == NULL) {
                head->setNext(newNode);
                newNode->setPrevious(head);
                tail = newNode;
            } else {
                DLNode* nodeMark = head->getNext();
                DLNode* lagMark = NULL;
                for (unsigned int i = 1; i < count; i++) {
                    if (nodeMark->getContents() < newContents && nodeMark->getNext() != NULL) {
                        lagMark = nodeMark;
                        nodeMark = nodeMark->getNext();
                    }
                }
                if ((lagMark == NULL) && (nodeMark->getNext() != NULL || nodeMark->getContents() > newContents)) {
                    head->setNext(newNode);
                    newNode->setNext(nodeMark);
                    nodeMark->setPrevious(newNode);
                    newNode->setPrevious(head);
                    
                } else if (nodeMark->getNext() == NULL && newContents > nodeMark->getContents()) {
                    nodeMark->setNext(newNode);
                    newNode->setPrevious(nodeMark);
                    tail = newNode;
                } else {
                    lagMark->setNext(newNode);
                    newNode->setNext(nodeMark);
                    nodeMark->setPrevious(newNode);
                    newNode->setPrevious(lagMark);
                }
                nodeMark = NULL;
                lagMark = NULL;
            }
        }
        newNode = NULL;
    }
}
예제 #18
0
int _tmain(int argc, _TCHAR* argv[])
{
	for(int i=(int)0;i<4;i++){
		pushBack(i*i);
	}
	puts("print()");
	print();
	getchar();

	puts("insertAt(1,-100)");	//insert element;
	insertAt((int)1,(int)-100);
	print();
	getchar();

	puts("popFront()");		//delete first element; 
	popFront();
	print();
	getchar();

	puts("popBack()");		//delete last element;
	popBack();
	print();
	getchar();

	puts("pushFront(-1)");	//insert element at first position;
	pushFront((int)-1);
	print();
	getchar();

	printf("at(3)=%d\n",at((int)3));//get 3rd element;
	getchar();

	puts("insertAt(size,-3)");	//delete element;
	insertAt(size,(int)-3);
	print();
	getchar();

	puts("deleteAt(1)");	//delete element;
	deleteAt((int)1);
	print();
	getchar();

	puts("Free list");		//delete all elements;
	delDEQ();
	print();
	getchar();
	return 0;
}
예제 #19
0
파일: list.cpp 프로젝트: anuruddhah/DSALib
	void SLList<T>::insert(int pos, const T& data)
	{
		if (pos == 0)
		{
			pushFront(data);
		}
		else
		{
			SNode<T>* prev, *newNode = new SNode<T>(data);
			prev = goTo(pos - 1);
			newNode->next = prev->next;
			prev->next = newNode;
			size++;
		}
	
	}
예제 #20
0
파일: mymem.c 프로젝트: turu/sysopy
void memInit(int blocks) {
    _totalFreeSize = blocks * BLOCK_SIZE * (1 << 10);

    MyDescriptor * descriptor = (MyDescriptor*) malloc(sizeof(MyDescriptor));
    descriptor->memory = malloc(_totalFreeSize);
    _entry = descriptor->memory;
    descriptor->blockCount = blocks;

    _freeList = createDescriptorList(NULL);
    pushFront(createDescriptorList(descriptor), &_freeList);
    _usedList = createDescriptorList(NULL);
    _minFreeSize = _totalFreeSize;
    _maxFreeSize = _minFreeSize;
    _freeCount = 1;
    _initialized = 1;
}
static char *test_pushFront_newPersonToExistingList() {
    Person *testPerson = (Person *) malloc(sizeof(Person));
    testPerson->first_name = "John";
    testPerson->last_name = "Kowalsky";
    testPerson->address = "Walbrzych";
    Node *testListHead = initList(testPerson);

    Person *testPerson2 = (Person *) malloc(sizeof(Person));
    testPerson2->first_name = "Wladimir";
    testPerson2->last_name = "Nowak";
    testPerson2->address = "Wroclaw";

    testListHead = pushFront(testListHead, testPerson2);
    mu_assert("error in pushFront_newPersonToExistingList",
              strcmp(testListHead->person->first_name,"Wladimir") == 0);
    removeList(testListHead);
    return 0;
}
예제 #22
0
파일: DLList.cpp 프로젝트: lovodi/CSCI-21
//inserts new node where the tail is
void DLList::pushBack(int Contents)
{
    if(head == NULL)
    {
        pushFront(Contents); //this will increment the count, no need for count++ etc.
    }
    else
    {
        DLNode* temp(new DLNode(Contents));
        DLNode* i = head;
        while (i -> getNext() != NULL)
        {
            i = i -> getNext();
        }
        i -> setNext(temp);
        count++;
    }
}
예제 #23
0
파일: list.cpp 프로젝트: anuruddhah/DSALib
	void DLList<T>::insert(int pos, const T & data)
	{
		if (pos == 0)
		{
			pushFront(data);
		}
		else
		{
			DNode<T>* newNode = new DNode<T>(data);
			DNode<T>* prev = goTo(pos - 1);
			newNode->prev = prev;
			newNode->next = prev->next;
			prev->next = newNode;
			if (newNode->next != nullptr)
				newNode->next->prev = newNode;
			size++;
		}
	}
static char *test_pushFront_newPersonToExisitingListPointersTests() {
    Person *testPerson = (Person *) malloc(sizeof(Person));
    testPerson->first_name = "John";
    testPerson->last_name = "Kowalsky";
    testPerson->address = "Walbrzych";
    Node *testListHead = initList(testPerson);

    Person *testPerson2 = (Person *) malloc(sizeof(Person));
    testPerson2->first_name = "Wladimir";
    testPerson2->last_name = "Nowak";
    testPerson2->address = "Wroclaw";

    testListHead = pushFront(testListHead, testPerson2);
    mu_assert("error in pushFront_newPersonToExistingListPointersTests",
              testListHead == testListHead->next->previous);
    removeList(testListHead);
    return 0;
}
예제 #25
0
/**
 * creates a new dynamic DLNode with the contents of 
 * the parameter and attaches as the new tail of the list
 * @param string contents
 */
void DLList::pushBack(string contents)
{
    if (head_ != NULL)
    {
        DLNode* temp, *newNode = new DLNode(contents);
        temp = head_;
        while (temp->getNext() != NULL)
        {
            temp = temp->getNext();
        }
        temp->setNext(newNode);
        newNode->setNext(head_);
        tail_ = newNode;
        size_ += 1;
    } else 
    {
        pushFront(contents);
    }
}
예제 #26
0
void LinkedList<T>::insertAt(Node<T>& n, int index) {
	if (index == 0) { //just add it to the front
		pushFront(n);
	}
	else if (index == length) { //just add it to the back
		pushBack(n);
	}
	else if (index<0 || index>length) { //index is outside of valid range
		std::cout << "Invalid index" << endl;
		return;
	}
	else {
		Node<T>* currentAtIndex=first;
		for (int i = 0; i < index-1; i++) { //find the current node at that index
			currentAtIndex = currentAtIndex->getNext();
		}
		n.setNext(currentAtIndex->getNext());
		currentAtIndex->setNext(&n);
		length++;
	}
}
static char *test_removeElementFromList_emptyElementFromExistingList() {
    Node *emptyNode = (Node *) malloc(sizeof(Node));

    Person *testPerson1 = (Person *) malloc(sizeof(Person));
    testPerson1->first_name = "John";
    testPerson1->last_name = "Kowalsky";
    testPerson1->address = "Walbrzych";
    Person *testPerson2 = (Person *) malloc(sizeof(Person));
    testPerson2->first_name = "Wladimir";
    testPerson2->last_name = "Nowak";
    testPerson2->address = "Wroclaw";


    Node *testListHead = initList(testPerson1);
    testListHead = pushFront(testListHead, testPerson2);

    Node *listAfterRemoval = removeElementFromList(testListHead, emptyNode);

    mu_assert("error in removeElementFromList_emptyElementFromExistingList",
              testListHead == listAfterRemoval);
    removeList(listAfterRemoval);
    return 0;
}
예제 #28
0
void DLList::insert(string newContents) {
    // Inserts New Node with newContents and attach in order
    string placeholder = newContents;
    char check_place = newContents[0];
    char check_head;
    char check_tail;
    if(head_node != NULL) {
        string head_contents = head_node->getContents();
        check_head = head_contents[0];
        string tail_contents = tail_node->getContents();
        check_tail = tail_contents[0];
    }
    
    if(head_node == NULL || ((check_place <= check_head) && (head_node != NULL))) {
        cout <<  "    Creating new head node:" << newContents << endl;
        pushFront(newContents);
    }
    else if(tail_node == NULL || ((check_place >= check_tail) && (tail_node != NULL))) {
        cout <<  "    Creating new tail node:" << newContents << endl;
        pushBack(newContents);
    }
    else
    {
        DLNode* temp = head_node;
        DLNode* previous = temp;
        
        while(temp->getContents() < newContents) {
            previous = temp;
            temp = temp->getNext();
        }
        
        DLNode* new_node = new DLNode(newContents);
        new_node->setNext(temp);
        previous->setNext(new_node);
        node_count = node_count + 1;
    }
}
예제 #29
0
MemTrieNode* TrieBranchNode::rejig()
{
	mark();
	byte n = activeBranch();

	if (n == (byte)-1 && m_value.size())
	{
		// switch to leaf
		auto r = new TrieLeafNode(bytesConstRef(), m_value);
		delete this;
		return r;
	}
	else if (n < 16 && m_value.empty())
	{
		// only branching to n...
		if (auto b = dynamic_cast<TrieBranchNode*>(m_nodes[n]))
		{
			// switch to infix
			m_nodes[n] = nullptr;
			delete this;
			return new TrieInfixNode(bytesConstRef(&n, 1), b);
		}
		else
		{
			auto x = dynamic_cast<TrieExtNode*>(m_nodes[n]);
			assert(x);
			// include in child
			pushFront(x->m_ext, n);
			m_nodes[n] = nullptr;
			delete this;
			return x;
		}
	}

	return this;
}
예제 #30
0
void DLList::pushBack(int newContents) {
    if (head == NULL) {
        pushFront(newContents);
    } else {
        DLNode* newTail = new DLNode(newContents);
        if (head->getNext() == NULL) {
            head->setNext(newTail);
            newTail->setPrevious(head);
        } else {
            DLNode* oldTail = head->getNext();
            for (unsigned int i = 1; i < count; i++){
                if (oldTail-> getNext() != NULL){
                    oldTail = oldTail->getNext();
                }
            }
            oldTail->setNext(newTail);
            newTail->setPrevious(oldTail);
            oldTail = NULL;
        }
        tail = newTail;
        newTail = NULL;
        count++;
    }
}