void addLitToBack(SLIST *strList, char *lit)
{
	struct String *newStr = createStr();
	SNODE *newNode = (SNODE *)wrp_calloc(1, sizeof(SNODE));
	appendLit(newStr, lit);
	initStrNode(newNode, newStr);
	addToBack(strList, newNode);
}
Exemple #2
0
Memory * removeProcess(Memory * memoryList,Disk * diskList, int loc) {
    int i;
    int age;

    age = -1;
    //printf("\nBeginning check for oldest Program");
    //getchar();
    for(i=loc; i < memoryList->totalSize; i++) {
        if(memoryList->memoryArr[i] != NULL) {
            if(age == -1) {
                age = memoryList->memoryArr[i]->age;
            }
            if(memoryList->memoryArr[i]->age < age) {
                //printf("\nNew oldest age was found:");
                age = memoryList->memoryArr[i]->age;
                //printf(" %d",age);
            }
        }
    }
    //printf("\nBeginning erase, searching for an age value of %d", age);
    for(i=loc; i < memoryList->totalSize; i++) {
        if(memoryList->memoryArr[i] != NULL) {
            //printf("\n@slot %d, filled by process %s, whose age value is %d ",i,memoryList->memoryArr[i]->label,memoryList->memoryArr[i]->age);
            if(memoryList->memoryArr[i]->age == age) {
                //printf("\nage value found, deleting");
                if(memoryList->memoryArr[i]->version == 0) {
                    memoryList->memoryArr[i]->cycle--;
                    if(memoryList->memoryArr[i]->cycle > 0) {
                        diskList = addToBack(diskList,memoryList->memoryArr[i]);
                        //printf("\nProcess %s is being moved to disk.",memoryList->memoryArr[i]->label);
                        //getchar();
                    } else {
                        //printf("\nProcess %s has completed.",memoryList->memoryArr[i]->label);
                        //getchar();
                    }
                    memoryList->memoryArr[i] = NULL;
                } else {
                    memoryList->memoryArr[i] = NULL;
                }
            }
        }
    }
    //printf("\nfinished!");
    //getchar();

    return(memoryList);
}
void NewUnit(Unit * toAdd, unitTypes type, UnitListHeader * myList) {
    toAdd->unitType = type;
    
    if (toAdd->unitType == unit1) { //determines unit type
        toAdd->health = unit1Health;
    } else if (toAdd->unitType == unit2) {
        toAdd->health = unit2Health;
    } else if (toAdd->unitType == unit3) {
        toAdd->health = unit3Health;
    } else if (toAdd->unitType == unit4) {
        toAdd->health = unit4Health;
    } else if (toAdd->unitType == unit5) {
        toAdd->health = unit5Health;
    }
    
    toAdd->next = NULL;
    toAdd->pathLocation = -1 - myList->size;
    addToBack(toAdd, myList);
}
Exemple #4
0
MusicRec * CreateList(char * fileName)
{
    MusicRec * head;
    MusicRec * toAdd;
    FILE * file;
    char fileContents[200];
    char * line;
    bool headExists;

    /* open the text file and read it */
    file = fopen(fileName, "r");
    if(file == NULL)
    {
        printf("The file specified does not exist. Try again.\n");
        exit(0);
    }
    /* start without a head */
    headExists = false;
    while(fgets(fileContents, sizeof(fileContents), file) != NULL)
    {
        /* split the text file by new lines */
        line = strtok(fileContents, "\n");
        
        /* if the line does not contain at least 1 letter between commas it is too short */
        if(strlen(fileContents) < 8)
            continue;

        /* if we are parsing the first element, we create the head */
        if(!headExists)
        {
            /* create the node to add */
            toAdd = ParseLine(line);

            /* if all went well and it is set, set it to the head */
            if(toAdd != NULL)
            {
                headExists = true;
                head = toAdd;
            }
        }
        else
        {
            toAdd = ParseLine(line);

            if(toAdd != NULL)
            {    
                if(toAdd->type == 'a')
                {
                    /* add it to the front */
                    head = addToFront(head, toAdd);
                }
                else
                {
                    /* add it to the back */
                    head = addToBack(head, toAdd);
                }
            }
        }   
    }
    /* close the file */
    fclose(file);
    
    return head;
}
Exemple #5
0
int main(int argc, char * argv[])
{
	FILE * file;
	char * name;
	char * group;
	char line[900][256];
	char ch;
	char type;
	char temp;
	double calories;
	double calTotal = 0;
	double vegTotal = 0, meatTotal = 0, dairyTotal = 0, grainsTotal = 0, fatTotal = 0;
	double vegTot = 0, meatTot = 0, dairyTot = 0, grainsTot = 0, fatTot = 0;
	int i;

	Food * head = (Food *)malloc(sizeof(Food));
	Food * ptr = (Food *)malloc(sizeof(Food));

	head = NULL;
	ptr = NULL;

	if (argc < 2)
	 {
		printf("No argument inputted. Exiting...\n");
		exit(0);
	}

	file = fopen(argv[1], "r");
	if (file == NULL)
	 {
		printf("Error opening file. Exiting...\n");
		exit(0);
	}
	while((ch = fgetc(file)) != EOF)
	 {
		line[i][0] = ch;
		while((ch = fgetc(file)) != '\n')
		 {
			line[i][strlen(line[i])] = ch;
			if (line[i][strlen(line[i])-1] == ',' && isdigit(line[i][strlen(line[i])-2]))
			 {
				temp = fgetc(file);
			}
		}
		name = strtok(line[i], ",");
		group = strtok(NULL, ",");
		calories = atoi(strtok(NULL, ","));
		calTotal += calories;
		type = temp;
		ptr = createRecord(name, group, calories, type);
		if (head == NULL)
		 {
			head = ptr;
		}
		else if (type == 'j')
		 {
			head = addToBack(head, ptr);
		}
		else if (type == 'h')
		 {
			head = addToFront(head, ptr);
		}

		if (strcmp(group, "vegfruit") == 0)
		 {
                vegTotal += calories;
                vegTot += 1;
		}
		else if (strcmp(group, "grains") == 0)
         {
                grainsTotal += calories;
                grainsTot += 1;
        }
		else if (strcmp(group, "dairy") == 0)
         {
                dairyTotal += calories;
                dairyTot += 1;
        }
		else if (strcmp(group, "meat") == 0)
         {
                meatTotal += calories;
                meatTot += 1;
        }
		else if (strcmp(group, "fat") == 0)
         {
                fatTotal += calories;
                fatTot += 1;
        }

		i++;
	}
	printf("%d\n", (int)calTotal);
	printf("%.0f\n", vegTotal / vegTot);
	printf("%.0f\n", meatTotal / meatTot);
	printf("%.0f\n", dairyTotal / dairyTot);
	printf("%.0f\n", grainsTotal / grainsTot);
	printf("%.0f\n", fatTotal / fatTot);
	printList(head);
	destroyList(head);

	return 0;
}
void addStrToBack(SLIST *strList, struct String *s)
{
	SNODE *node = (SNODE *)wrp_calloc(1, sizeof(SNODE));
	initStrNode(node, s);
	addToBack(strList, node);
}
void addNode(SLIST *strList, SNODE *node)
{
	SNODE *prevNode;
	//Add node to empty list
	if (strList->length == 0)
	{
		strList->head = node;
		strList->tail = node;
		strList->length++;
	}
	//non-empty list
	else
	{
		//new node should appear alphabetically before strList->head
		if (compareStr(node->str, strList->head->str) == -1)
		{
			addToFront(strList, node);
		}
		//new node is equal to strList->head
		else if (compareStr(node->str, strList->head->str) == 0)
			strList->head->count++;
			
		//node should appear alphabetically after strList->tail
		else if ((compareStr(node->str, strList->tail->str)) == 1)
		{
			addToBack(strList, node);
		}
		//new node is equal to strList->tail
		else if (compareStr(node->str, strList->tail->str) == 0)
			strList->tail->count++;
		//new node is not smaller than strList->head nor greater than strList->tail
		else
		{
			//Start at node after head
			strList->cur = strList->head;
			while (strList->cur != NULL)
			{
				//new node contains the same string as strList->cur
				if (compareStr(node->str, strList->cur->str) == 0)
				{
					strList->cur->count++;
					break;
				}			
				//node->str should appear alphabetically after cur->str
				else if (compareStr(node->str, strList->cur->str) == 1)
				{
					strList->cur = strList->cur->next;					
				}
				//node->str should appear alphabetically before cur->str
				else
				{
					prevNode = strList->cur->prev;
					//First have node point to forwards to current and backwards to previous node
					node->next = strList->cur;
					node->prev = prevNode;
					prevNode->next = node; //Point old previous node to new node
					strList->cur->prev = node;
					strList->length++;
					break;
				}
			}
		}
	}
}