Beispiel #1
0
Type CTECList<Type>:: removeFromIndex(int index)
{
	Type thingToRemove;

	assert(size > 0);
	assert(index >= 0);
	assert(index < size);
	assert(size > 0 && index >= 0 && index < size); //Same as the three above

	ArrayNode<Type> * previous, deleteMe, newNext;
	if(index == 0)
	{
		thingToRemove = removeFromFront();
	}
	else if(index == size - 1)
	{
		thingToRemove = removeFromEnd();
	}
	else
	{
		for(int spot = 0; spot < index + 1; spot++)
		{

		}
	}

	this -> calculateSize();
	return thingToRemove;
}
Beispiel #2
0
Type CTECList<Type>::removeFromEnd()
{
	//defencive code
	assert(this->size > 0);
	Type returnValue;
	ArrayNode<Type> * newTail = new ArrayNode<Type>();
	ArrayNode<Type> * current = ArrayNode<Type>();

	if(size == 1)
	{
		ArrayNode<Type> * toRemove = tail;
		returnValue = removeFromFront();
		tail = nullptr;
	}

	else
	{
ArrayNode<Type> * current = head;
for(int index = 0; index < size-1; index ++)
{
	current = current->getNext();
}


	returnValue = tail->getValue();
		delete tail;
		current = newTail;
		calculateSize();
	current->setNext(nullptr);
	}



	return returnValue;
}
Beispiel #3
0
Type CTECList<Type>::removeFromBack()
{
	assert(size > 0);
	Type returnValue;

	if(size == 1)
	{
		ArrayNode<Type> * toRemove = end;
		returnValue = removeFromFront();
		end = nullptr;
		head = nullptr;
		delete toRemove;

	}
	else
	{
		ArrayNode<Type> * current = head;
		for(int index = 0; index < size - 1; index++)
		{
			current = current->getNext();
		}

		returnValue = end->getValue();
		delete end;
		current = end;
		current->setNext(nullptr);
	}

	calculateSize();

	return returnValue;


}
Beispiel #4
0
int pop(Stack * stack)
{
    int removedElement;
    removedElement = firstValue(stack -> top);
    removeFromFront(stack -> top);
    return(removedElement);
}
Beispiel #5
0
// Function to insert a new node in complete binary tree
void treeInsert(struct node **root, int data, struct Queue *queue)
{
    // Create a new node for give data
    struct node *temp = newNode(data);

    // If the tree is empty, initialize the root with a new node
    if (!*root)
        *root = temp;
    else
    {
        // get the front node of the queue
        struct node *front = getFront(queue);

        // If the left child of this front node doesn't exist,
        // set the left child as the new node
        if (!front->left)
            front->left = temp;
        // If the right child of this front node doesn't exist;
        // set the right child as the new node
        else if (!front->right)
            front->right = temp;
        // If the front node has both children, dequeue it
        if (hasBothChildren(front))
            removeFromFront(queue);
    }
    // Enqueue the new node for later insertions
    enqueue(temp, queue);
}
bool List :: removeFromList( char value )
{
	ListNode *deletePtr; // Pointer will point to node will be deleted
	ListNode *prevPtr; // Pointer that will point to previous node

	deletePtr = firstPtr;

	while( deletePtr != NULL ) // Traverse through the list
	{
		if( deletePtr->data != value ) // Matching pointer data with value
		{
			deletePtr = deletePtr->nextPtr;
		}

		else
			break;
	}

	if( deletePtr == NULL )
	{
		cout << endl << "That node is not in the list." << endl << endl;
		return false;
	}

	prevPtr = firstPtr;

	if( deletePtr == firstPtr )
	{
		// Calling removeFromFront if user wants to delete first node 
		removeFromFront();
		return true;
	}

	if( deletePtr == firstPtr ) // if one element remains
	{
		if ( firstPtr == lastPtr )
			firstPtr = lastPtr = NULL; // no nodes remain after removal

		delete deletePtr; // reclaim previous front node
		return true; // delete successful
	}
	
	while( prevPtr->nextPtr != deletePtr )
	{
		prevPtr = prevPtr->nextPtr;
	}

	prevPtr->nextPtr = deletePtr->nextPtr;
	delete deletePtr;
	return true;
} // end function removeFromList  
Type CTECList<Type> :: removeFromIndex(int index)
{
    assert(this->size > 0);
    assert(index >= 0 && index < size);
    Type thingToRemove;
    Arraynode<Type> * previous,deleteMe,newNext;

    if(index == 0)
    {
        thingToRemove = removeFromFront();
    }
    else if(index == size-1)
    {
        thingToRemove = removeFromEnd();
        p
    }
Beispiel #8
0
// Standard level-order traversal to test
void levelOrder(struct node *root)
{
    struct Queue *queue = createQueue(SIZE);
    enqueue(root, queue);
    while (!queueIsEmpty(queue))
    {
        struct node *temp = removeFromFront(queue);
        printf("%d\n", temp->data);
        if (temp->left)
            //printf("adding temp->left to queue\n");
            enqueue(temp->left, queue);
        if (temp->right)
            //printf("adding temp->right to queue\n");
            enqueue(temp->right, queue);
    }
}
Type CTECList<Type>:: removeFromEnd()
{
	//Check for size == 1 it is a special case
	//Loop over size
	//or
	//Loop until getNext()->getNext() == nullptr.
	//Grab the value from the last node
	//set the next to last node as end
	//delete the old last node
	//Before return the variable call calculateSize().
	assert(size > 0);
	Type thingToRemove;

	if(size == 1)
	{
		thingToRemove = removeFromFront();
		end = nullptr;
		calculateSize();
		return thingToRemove;
	}
	else
	{
	ArrayNode<Type> * current = head;

	for(int spot = 0; spot < size -1; spot++)
	{
		current = current -> getNext();
	}

//Another way to do this
//	ArrayNode<Type> * pointer = head;
//	while(pointer -> getNext() -> getNext() != nullptr)
//	{
//		pointer = pointer -> getNext();
//	}

	thingToRemove = current -> getNext() -> getValue();
	end = current;
	delete current -> getNext();

	this -> calculateSize();
	return thingToRemove;
	}
}
Beispiel #10
0
Type CTECList<Type>::removeFromIndex(int index)
{

	assert(index >= 0);
	assert(index < size);
	assert(size > 0);
	ArrayNode<Type> * deleteMe = new ArrayNode<Type>();
	Type thingToRemove;
	ArrayNode<Type> * newNext = new ArrayNode<Type>();
	ArrayNode<Type> * current = new ArrayNode<Type>();
	ArrayNode<Type> * previous = new ArrayNode<Type>();

	if(index == 0)
	{

		thingToRemove = removeFromFront();
	}

	else if(index == size-1)
	{
		thingToRemove = removeFromEnd();
	}
	else

	{


		for(int index = 0; index < size  ; index++)
		{

			current = current->next();

		}
		ArrayNode<Type> * previous = newNext;
		delete deleteMe;
		this->calculateSize;
		return deleteMe;
	}
}
// main crawler function
int main(int argc, char* argv[]) {

    // local variables
    FILE *fp; // file pointer for html files
    char *nextURL; // pointer to the next URL found on the seed page
    char *newURL; // pointer to the next URL in the while loop

    // check command line arguments
    if (argc != 4) {
        printf("Incorrect number of arguments provided.");
        exit(1);
    }
    // check that the second argument is a directory
    stat(argv[2],&statbuffer);
    if S_ISDIR(statbuffer.st_mode) { }
    else {
        printf("Error, you did not supply a valid directory");
        exit(1);
    }

    // get arguments
    char *seedURL = argv[1];
    int filename_len = strlen(argv[2])+21;

    // get the directory
    char*filename = calloc(filename_len,sizeof(char));

    // check the maxDepth
    int value = is_numeric(argv[3]);
    if (value != 0) {
        sscanf(argv[3],"%i",&maxDepth);
    }
    else {
        printf("Error! maxDepth must be a number");
        exit(1);
    }

    // init curl
    curl_global_init(CURL_GLOBAL_ALL);

    // initialize data structures/variables

    // initialize hashtable
    HashTable *table = malloc(sizeof(HashTable));
    memset(table,0,MAX_HASH_SLOT);

    // initialize linked list
    List *WebPageList;
    WebPageList = createList();

    // setup seed page

    // get seed webpage
    // if it fails, report and exit
    if (NormalizeURL(seedURL) == 0) {
        printf("Error, bad URL");
        exit(1);
    }
    // write seed file

    // create WebPage object by allocating memory
    WebPage *seedPage = malloc(sizeof(WebPage));

    // assign values to each part of the struct
    seedPage->url = seedURL;
    seedPage->html = NULL;
    seedPage->html_len = 0;
    seedPage->depth = 0;

    // try to get the webpage up to MAX_TRY times
    if (!GetWebPage(seedPage)) {
        for (tries = 0; tries < MAX_TRY; tries++) {
            if (GetWebPage(seedPage)) {
                break;
            }
        }
    }

    // write html contents to a file "1" in the given directory
    sprintf(filename,"%s/%d",argv[2],1);
    fp = fopen(filename,"w");
    fputs(seedURL,fp);
    fputs("\n",fp);
    fprintf(fp,"%d\n",seedPage->depth);
    fputs(seedPage->html,fp);

    // close the file and wipe the filename
    fclose(fp);
    memset(filename,'\0',filename_len);

    // add seed page to hashtable
    add(table,seedURL);

    // extract urls from seed page

    // while there are still URLs in the seed page's html
    while ((pos = GetNextURL(seedPage->html,pos,seedPage->url,&nextURL)) > 0) {

        // only visiting them if it wouldn't exceed maxDepth
        if ((seedPage->depth+1) > maxDepth) {
            free(seedPage);
            exit(1);
        }

        // ensure it's a valid url
        if (NormalizeURL(nextURL) != 0) {

            // also check if its in the right domain
            if (strncmp(URL_PREFIX,nextURL,strlen(URL_PREFIX)) == 0) {

                // if it is added to the hashtable it is a unique URL that
                // hasn't been visited before, add it to the linked list
                // of URLs to visit
                if (add(table,nextURL)) {
                    // create a new webpage object
                    WebPage *pages = malloc(sizeof(WebPage));
                    pages->url = nextURL;
                    pages->html = NULL;
                    pages->html_len = 0;
                    pages->depth = 1;

                    // try to get the webpage up until the MAX_TRY
                    tries = 0;
                    if (!GetWebPage(pages)) {
                        for (tries = 0; tries < MAX_TRY; tries++) {
                            if (GetWebPage(pages)) {
                                break;
                            }
                        }
                    }

                    // add it to the linked list
                    addToEnd(WebPageList,pages);
                }
            }
        }
    }

    // while there are urls to crawl
    while (WebPageList->head != NULL) {
        // get next url from list
        WebPage *nextPage = malloc(sizeof(WebPage));
        nextPage = removeFromFront(WebPageList);

        // try to get the webpage up until the MAX_TRY
        tries = 0;
        if (!GetWebPage(nextPage)) {
            for (tries = 0; tries < MAX_TRY; tries++) {
                if (GetWebPage(nextPage)) {
                    break;
                }
            }
        }

        // write page file
        sprintf(filename,"%s/%d",argv[2],docNum);
        fp = fopen(filename,"w");
        fputs(nextPage->url,fp);
        fputs("\n",fp);
        fprintf(fp,"%d\n",nextPage->depth);
        fputs(nextPage->html,fp);

        // close the file and wipe the filename (to be used next time)
        fclose(fp);
        memset(filename,'\0',filename_len);

        // increment the doc num
        docNum++;

        // check if visiting the URLs on this page will exceed maxDepth
        if ((nextPage->depth+1) > maxDepth) {
            free(nextPage);
            continue;
        }
        pos = 0;
        // iterate through all the URLs on the page
        while ((pos = GetNextURL(nextPage->html,pos,nextPage->url,&newURL))>0) {
            // check to ensure that the URLs are the proper format
            if (NormalizeURL(newURL) != 0 ) {
                // check to ensure that they are in the right domain
                if (strncmp(URL_PREFIX,newURL,strlen(URL_PREFIX)) == 0) {
                    // making sure to only add new ones to the list
                    if (add(table,newURL) != 0) {
                        // create a new WebPage object
                        WebPage *page = malloc(sizeof(WebPage));
                        page->url = newURL;
                        page->html = NULL;
                        page->html_len = 0;
                        page->depth = nextPage->depth + 1;
                        GetWebPage(page);

                        // try to get the webpage up until the MAX_TRY
                        tries = 0;
                        if (!GetWebPage(page)) {
                            for (tries = 0; tries < MAX_TRY; tries++) {
                                if (GetWebPage(page)) {
                                    break;
                                }
                            }
                        }

                        // add the page to the linked list
                        addToEnd(WebPageList,page);
                    }
                }
            }
        }
        // Sleep for a bit to avoid annoying the target
        sleep(INTERVAL_PER_FETCH);

        // Free resources
        free(nextPage);

    }

    // cleanup curl
    curl_global_cleanup();

    // free resources
    // free hashtable
    hash = JenkinsHash(seedURL,MAX_HASH_SLOT);
    HashTableNode *freer = table->table[hash];
    HashTableNode *tempHash = NULL;
    while (freer != NULL) {
        tempHash = freer;
        freer = freer->next;
        free(tempHash);
    }
    free(table);

    // free linked list
    free(WebPageList);

    // free WebPage and filename pointer
    free(seedPage);
    free(filename);
    return 0;
}
Beispiel #12
0
// Heap things go here
void maxHeapify(struct node *root)
{
    struct Queue *queue = createQueue(SIZE);
    enqueue(root, queue);
    while (!queueIsEmpty(queue))
    {
        struct node *temp = removeFromFront(queue);
        /*
         printf("Dequeue'd node->data: %d\n",temp->data);
        // If left > data AND left > right, replace data with left
        if ((temp->left->data > temp->data) && (temp->left->data > temp->right->data))
        {
            printf("Left child %d was greater than %d. Moving %d to back of queue...\n", temp->left->data, temp->data, temp->data);
            struct node *swapA = newNode(temp->left->data);
            printf("swapA->data = %d\n", swapA->data);
            if (temp->left)
            {
                swapA->left = temp;
                printf("temp->left copied to swapA->left. swapA->left value is %d\n", swapA->left->data);
            }
            if (temp->right)
            {
                swapA->right = temp->right;
                printf("temp->right copied to swapA->right. swapA->right value is %d\n", swapA->right->data);
            }
            temp->data = temp->left->data;
            printf("New temp->data: %d\n", temp->data);
            enqueue(swapA, queue);
            printf("Successfully enqueue'd swapA\n");
        }
        // If right > data AND right > left, replace data with right
        else if ((temp->right->data > temp->data) && (temp->right->data > temp->left->data))
        {
            printf("Right child %d was greater than %d. Moving %d to back of queue...\n", temp->right->data, temp->data, temp->data);
            struct node *swapB = newNode(temp->right->data);
            printf("swapB->data = %d\n", swapB->data);
            if (temp->left)
            {
                swapB->left = temp->left;
                printf("temp->left copied to swapB->left. swapB->left value is %d\n", swapB->left->data);
            }
            if (temp->right)
            {
                swapB->right = temp;
                printf("temp->right copied to swapB->right. swapB->right value is %d\n", swapB->right->data);
            }
            temp->data = temp->right->data;
            printf("New temp->data: %d\n", temp->data);
            enqueue(swapB, queue);
            printf("Successfully enqueue'd swapB\n");
        }
        */
        if (temp->left)
        {
            printf("Enqueue'ing %d...\n", temp->left->data);
            enqueue(temp->left, queue);
        }
        if (temp->right)
        {
            printf("Enqueue'ing %d...\n", temp->right->data);
            enqueue(temp->right, queue);
        }
        //if (temp->left == NULL && temp->right == NULL)
            //printf("Node with %d value has no children\n", temp->data);
    }
    printf("%d\n", removeFromRear(queue)->data);
}