コード例 #1
0
// Copy constructor
LinkedList::LinkedList(const LinkedList &obj){

    Node *cursor; // For navigating through the obj list
    Node *current; // Current link in the chain to be added

     // Clear the pointers
    cursor = head = NULL;
    // Now copy all the links from input into this object
    cursor = obj.head;

    // Set the size of the list
    vectorSize = obj.vectorSize;

    if(cursor){
        try{
            // Create the head node and set its data
            Node *link = new Node;
            link->data=cursor->data;
            link->next = NULL;
            current = head = link;
            // Go to the next node to copy if any
            cursor=cursor->next;
        }
        catch(std::bad_alloc){
            memError();
        }
    }

    // Traverse the rest of the nodes in obj
    while(cursor){

        try{
            // Create a new link and set its data
            Node *link = new Node;
            link->data=cursor->data;
            // Now link it with the other links in the list
            link->next = NULL;
            // Connect the new link to the existing links
            current->next = link;
            // Now set the new link as the current link
            current=link;
            // Go to the next link in obj list
            cursor = cursor->next;
        }
        catch(std::bad_alloc){
            memError();
        }
    }
}
コード例 #2
0
ファイル: buffer.c プロジェクト: ZeyuMi/MiLex
int initializeIntBuffer(){
	intEntry *temp = intBufferList;
	intEntry *pre = NULL;
	while(NULL != temp){
		pre = temp;
		temp = temp->next;
	}
	temp = malloc(sizeof(intEntry));
	if(NULL == temp)
		memError();
	temp->id = ++intNum;
	temp->buffersize = DEFAULT;
	temp->bufp = temp->buffer = malloc(sizeof(int) * DEFAULT);
	if(NULL == temp->bufp)
		memError();
	temp->next = NULL;
	if(NULL == intBufferList)
		intBufferList = temp;
	else
		pre->next = temp;
	return intNum;
}
コード例 #3
0
ファイル: buffer.c プロジェクト: ZeyuMi/MiLex
int initializeCharBuffer(){
	charEntry *temp = charBufferList;
	charEntry *pre = NULL;
	while(NULL != temp){
		pre = temp;
		temp = temp->next;
	}
	temp = malloc(sizeof(charEntry));
	if(NULL == temp)
		memError();
	temp->id = ++charNum;
	temp->buffersize = DEFAULT;
	temp->bufp = temp->buffer = malloc(DEFAULT);
	if(NULL == temp->bufp)
		memError();
	temp->next = NULL;
	if(NULL == charBufferList)
		charBufferList = temp;
	else
		pre->next = temp;
	return charNum;
}
コード例 #4
0
void pushs(stack * st, char * value)
{
	stacke * aux1 = (stacke *)malloc(sizeof(stacke));
	
	if(!aux1)
	{
		memError("pushs");
	}
	
	aux1->type = 2;
	aux1->value.literalS = value;
	StackPushS(st, aux1);
	free(aux1);
}
コード例 #5
0
void pushf(stack * st, float value)
{
	stacke * aux1 = (stacke *)malloc(sizeof(stacke));
	
	if(!aux1)
	{
		memError("pushf");
	}
	
	aux1->type = 1;
	aux1->value.literalF = value;
	StackPushF(st, aux1);
	free(aux1);
}
コード例 #6
0
void pushi(stack * st, int value)
{
	stacke * aux1 = (stacke *)malloc(sizeof(stacke));
	
	if(!aux1)
	{
		memError("pushi");
	}
		
	aux1->type = 0;
	aux1->value.literalI = value;
	StackPushI(st, aux1);
	free(aux1);
}
コード例 #7
0
ファイル: buffer.c プロジェクト: ZeyuMi/MiLex
void addIntElement(int id, int i){
	intEntry *temp = findIntEntry(id);
	if(NULL == temp)
		return;

	if(temp->bufp - temp->buffer >= temp->buffersize){
		temp->buffersize *= 2;
		int *old = temp->buffer;
		temp->buffer = malloc(sizeof(int) * temp->buffersize);
		if(temp->buffer == NULL)
			memError();
		memcpy((void *)temp->buffer, (void *)old, sizeof(int) * (temp->buffersize)/2);
		free(old);
		temp->bufp = temp->buffer + ((temp->buffersize)/2);
	}
	*((temp->bufp)++) = i;
}
コード例 #8
0
ファイル: buffer.c プロジェクト: ZeyuMi/MiLex
void addCharElement(int id, char c){
	charEntry *temp = findCharEntry(id); 
	if(NULL == temp)
		return;

	if(temp->bufp - temp->buffer >= temp->buffersize){
		temp->buffersize *= 2;
		char *old = temp->buffer;
		temp->buffer = malloc(temp->buffersize);
		if(temp->buffer == NULL)
			memError();
		memcpy((void *)temp->buffer, (void *)old, (temp->buffersize)/2);
		free(old);
		temp->bufp = temp->buffer + ((temp->buffersize)/2);
	}
	*((temp->bufp)++) = c;
}
コード例 #9
0
// Main constructor
LinkedList::LinkedList(const int &input){

    try{
        // Initialize the size to 1
        vectorSize = 1;
        // Create a new node, set its data element
        // to the input and its pointer to null
        Node *link = new Node;
        link->data = input;
        link->next = NULL;

        // Now set the head pointer to this new link
        // in the list/chain
        head = link;
    }
    catch(std::bad_alloc){
        memError();
    }
}
コード例 #10
0
/* lti: 0;
   lei: 1;
   gti: 2;
   gei: 3;
   eq:  4;
   neq: 5; */
int comp_generic(stack * st, int type)
{
	stacke * aux1, * aux2;
	int result;

	aux1 = (stacke *)malloc(sizeof(stacke));
	aux2 = (stacke *)malloc(sizeof(stacke));

	if(aux1 == NULL || aux2 == NULL)
	{
		memError("addi");
	}

	if(debug)
		fprintf(stdout, "--debugFunction-- Comparing %d and %d.\n", aux2->value.literalI, aux1->value.literalI);

	
	StackPopI(st, aux1);
	StackPopI(st, aux2);
	
	if(aux2->type == aux1->type)
	{
		if(aux2->type == 0)
		{
			switch(type)
			{
				case 0: /* lti */
					if(aux2->value.literalI < aux1->value.literalI)
						result = 1;
					else
						result = 3;
					break;
				case 1: /* lei */
					if(aux2->value.literalI <= aux1->value.literalI)
						result = 1;
					else
						result = 3;
					break;
				case 2: /* gti */
					if(aux2->value.literalI > aux1->value.literalI)
						result = 1;
					else
						result = 3;
					break;
				case 3: /* gei */
					if(aux2->value.literalI >= aux1->value.literalI)
						result = 1;
					else
						result = 3;
					break;
				case 4: /* eq */
					if(aux2->value.literalI == aux1->value.literalI)
						result = 1;
					else
						result = 3;
					break;
				case 5: /* neq */
					if(aux2->value.literalI != aux1->value.literalI)
						result = 1;
					else
						result = 3;
					break;
			}
		}
		else if(aux2->type == 1)
		{
			switch(type)
			{
				case 0: /* lti */
					if(aux2->value.literalF < aux1->value.literalF)
						result = 1;
					else
						result = 3;
					break;
				case 1: /* lei */
					if(aux2->value.literalF <= aux1->value.literalF)
						result = 1;
					else
						result = 3;
					break;
				case 2: /* gti */
					if(aux2->value.literalF > aux1->value.literalF)
						result = 1;
					else
						result = 3;
					break;
				case 3: /* gei */
					if(aux2->value.literalF >= aux1->value.literalF)
						result = 1;
					else
						result = 3;
					break;
				case 4: /* eq */
					if(aux2->value.literalF == aux1->value.literalF)
						result = 1;
					else
						result = 3;
					break;
				case 5: /* neq */
					if(aux2->value.literalF != aux1->value.literalF)
						result = 1;
					else
						result = 3;
					break;
			}
		}
		else if(aux2->type == 2)
		{
			printf("UNIMPLEMENTED comparer for strings\n");
			exit(6);
		}
	}
	else
	{
		printf("UNIMPLEMENTED types differ\n");
		exit(6);
	}

	free(aux1);
	free(aux2);	

	return result;
}
コード例 #11
0
void operate(stack * st, int type)
{
	stacke * aux1, * aux2;
	char * resultString;
	
	aux1 = malloc(sizeof(stacke));	
	aux2 = malloc(sizeof(stacke));

	if(aux1 == NULL || aux2 == NULL)
	{
		memError("arithmetic operation");
	}
	
	
	switch(type)
	{
		case 0:
			StackPopI(st, aux1);
			StackPopI(st, aux2);
			aux2->value.literalI += aux1->value.literalI;
			StackPushI(st, aux2);
			break;
		case 1:
			StackPopI(st, aux1);
			StackPopI(st, aux2);
			aux2->value.literalI -= aux1->value.literalI;
			StackPushI(st, aux2);
			break;
		case 2:
			StackPopI(st, aux1);
			StackPopI(st, aux2);
			aux2->value.literalI *= aux1->value.literalI;
			StackPushI(st, aux2);
			break;
		case 3:
			StackPopI(st, aux1);
			StackPopI(st, aux2);
			aux2->value.literalI /= aux1->value.literalI;
			StackPushI(st, aux2);
			break;
		case 4:
			StackPopF(st, aux1);
			StackPopF(st, aux2);
			aux2->value.literalF += aux1->value.literalF;
			StackPushF(st, aux2);
			break;
		case 5:
			StackPopF(st, aux1);
			StackPopF(st, aux2);
			aux2->value.literalF -= aux1->value.literalF;
			StackPushF(st, aux2);
			break;
		case 6:
			StackPopF(st, aux1);
			StackPopF(st, aux2);
			aux2->value.literalF *= aux1->value.literalF;
			StackPushF(st, aux2);
			break;
		case 7:
			StackPopF(st, aux1);
			StackPopF(st, aux2);
			aux2->value.literalF /= aux1->value.literalF;
			StackPushF(st, aux2);
			break;
		case 8:
			StackPopS(st, aux1);
			StackPopS(st, aux2);
			resultString = (char *)malloc(strlen(aux2->value.literalS)+strlen(aux1->value.literalS)+1);
			strcpy(resultString, aux2->value.literalS);
			strcat(resultString, aux1->value.literalS);
			aux2->value.literalS = resultString;
			StackPushS(st, aux2);
			break;
	}

	free(aux1);
	free(aux2);
}
コード例 #12
0
ファイル: tree.c プロジェクト: bjnix/pedogogy
	/* Insert:
	 * Takes username( %s ) and an address ( %s )
	 * and inserts them into the given SearchTree
	 */
        SearchTree Insert( char* usr, char* a, SearchTree T )
        {
		int found = 0;
		
		nodeAddr X;
		nodeAddr Y;
		
		
/**		fprintf(stderr,"inserting... usr: %s, address: %s\n",usr,a);
**/		if( !T )
		{
			/* Create and return a one-node tree */
		  T = malloc( sizeof(* T));
		  T->A = malloc( sizeof(* T->A)); 
			if((!T)||(!T->A))
				{ memError(T); }
			else
			{	

/*				fprintf(stderr,"creating new treenode\n");
*/				strcpy(T->A->addr, a);
				T->A->nLogins = 1;
				T->A->next = 0;
				strcpy(T->name, usr);
				T->Left = T->Right = 0;
/*				fprintf(stderr,"inserted\n");
*/			}
		}
		else if(strcmp(usr, T->name) < 0 )
		{
/*			fprintf(stderr,"going left\n");
*/			T->Left = Insert( usr, a, T->Left );/*its not here, take a left*/
		}
		else if(strcmp(usr, T->name) > 0 )
		{
/*			fprintf(stderr,"going right\n");
*/			T->Right = Insert( usr, a, T->Right );/*nope not here either, take a right*/
		}		
		else if(strcmp(usr,T->name) == 0)/*found it!  Update count for logins or create new address node*/
		{	
/*			fprintf(stderr,"found matching treenode\n");
*/			X = T->A;
			do{
				if( strncmp(a, X->addr,32) == 0)
				{
					found = 1;
					 X->nLogins ++;
/*					fprintf(stderr,"found matching address %s, %d logins\n", X->addr, X->nLogins);
*/					}
				X =  X->next;
			}while((X)&&(!found));
			if(!found)
			{
				X=T->A;
/*				 fprintf(stderr,"no matching address found, creating new %a ...");
*/				if(!(Y=malloc( sizeof(struct address))))
					{ memError(T); }
				else
				{
				printf("%p\n",X->addr);
					strcpy(Y->addr, a );
					Y->nLogins = 1;
					Y->next = X->next;
					 X->next = Y;
/*					fprintf(stderr,"created and inserted\n");
*/				}
			}
		}
		else
		{
			fprintf(stderr,"ERROR INSERT FAIL");
			return 0;
		}
		return T;  
        }/*end Insert*/
コード例 #13
0
void LinkedList::push(const int& input){

    int totalPush=0;// Reset the count

    // 1 boolean
    totalPush++;

    // Test to see if we have added any items to the vector
    if(head){

        // 1 assignment
        totalPush++;

        Node *cursor; // For navigating through the list
        // Go through the list until the last node, then
        // add the new link there
        cursor = head;

        while(cursor->next){
            // 1 assignment and 1 boolean
            totalPush+=2;

            // Keep going until we get to the end.
            cursor = cursor->next;
        }

        try{
            // Create a new node, set its data element
            // to the input and its pointer to null
            Node *link = new Node;
            link->data = input;
            link->next = NULL;
            // Add the new link to the last link we found
            cursor->next = link;

            // 4 assignment
            totalPush+=4;

        }
        catch(std::bad_alloc){
            memError();
        }

        // 1 increment
        totalPush++;

        // Increase the size by 1
        vectorSize++;
    }

    // If the vector does not have any items
    else{
        // Create a new node, set its data element
        // to the input and its pointer to null
        Node *link = new Node;
        link->data = input;
        link->next = NULL;

        // Now set the head pointer to this new link
        // in the list/chain
        head = link;
        // Initialize the size to 1
        vectorSize = 1;

        // 5 assignment =, 1 memory access new
        totalPush+=5;
    }

    std::cout<<"Total operations in push: "<< totalPush << std::endl;

}
コード例 #14
0
// Overload = operator declaration
LinkedList& LinkedList::operator =(const LinkedList &input){

    // Test to make sure the passed object is not the same
    // as calling object e.g. input = input
    if(this!=&input){
        Node *cursor; // For navigating through the obj list
        Node *current; // Current link in the chain to be added

        // Clear out the data in the existing list
        while(head){
            // Set the cursor to the current head
            cursor = head;
            // Set the head pointer to the next link
            head=head->next;
            // Dealloate the current link
            delete cursor;
        }
        // Clear the pointers
        cursor = head = NULL;
        // Now copy all the links from input into this object
        cursor = input.head;

        // Set the size of the list
        vectorSize = input.vectorSize;

        if(cursor){
            try{
                // Create the head node and set its data
                Node *link = new Node;
                link->data=cursor->data;
                link->next = NULL;
                current = head = link;
                // Go to the next node to copy if any
                cursor=cursor->next;
            }
            catch(std::bad_alloc){
                memError();
            }
        }

        // Traverse the rest of the nodes in obj
        while(cursor){

            try{
                // Create a new link and set its data
                Node *link = new Node;
                link->data=cursor->data;
                // Now link it with the other links in the list
                link->next = NULL;
                // Connect the new link to the existing links
                current->next = link;
                // Now set the new link as the current link
                current=link;
                // Go to the next link in obj list
                cursor = cursor->next;
            }
            catch(std::bad_alloc){
                memError();
            }
        }        
    }
    // Just return a reference to
    return *this;
}