Пример #1
0
/*  Delete the list

    param:  heap    pointer to the list
    post:   The tasks from the list are removed and their occupied memories are freed

*/
void deleteList(DynArr *heap)
{

	Task* task;
	while(sizeDynArr(heap) > 0)
	{
	  	/* get the task */
		task = getMinHeap(heap);
		/* remove the task */
		removeMinHeap(heap);
		/* free the task */
		free(task);
	}
	/* free the heap */
	deleteDynArr(heap);
}
Пример #2
0
/*  Print the list

    param:  heap    pointer to the list
    pre:    the list is not empty
    post:   The tasks from the list are printed out in priority order.
			The tasks are not removed from the list.
*/
void printList(DynArr *heap)
{
  /* FIXME: Write this */

  // create a new copy of the heap to prevent removal
  DynArr *tmpHeap = createDynArr(sizeDynArr(heap));
  copyDynArr(heap, tmpHeap);
  
  while (sizeDynArr(tmpHeap) > 0)
    {
      // get first value of min priority
      TYPE min = getMinHeap(tmpHeap);
      print_type(min);
      removeMinHeap(tmpHeap); // remove min, to get next
    }

  freeDynArr(tmpHeap);
}
Пример #3
0
float getShortestPathUtil(graph* gr, heap* h, float vert[],int destination)
{

	edge_st* temp_ed;
	int temp_id;
	int temp_dest;
	float temp_len;
	graphArrnode* head;

	while(h->size)
	{
		temp_ed = getMinHeap(h);
		temp_id = temp_ed->source;
#ifdef DEBUG
		printf("getShortestPathUtil: HeapSource: %d\n",temp_id);
#endif
		head = gr->ArrList[temp_id].head;
		while(head)
		{
			temp_dest = head->dest;
#ifdef DEBUG
			printf("getSHortestPathUtil: Link Source %d\n",temp_dest);
#endif
			temp_len = head->len+temp_ed->length;
			modifyHeap(h,temp_dest,temp_len);
			head = head->next;
		}
		vert[temp_id] = temp_ed->length;

		deleteMinHeap(h);
		if(temp_id == destination)
		{
			return vert[temp_id];
		}

	}
	return vert[destination];


}
Пример #4
0
/*  Print the list
 
 param:  heap    pointer to the list
 pre:    the list is not empty
 post:   The tasks from the list are printed out in priority order.
 The tasks are not removed from the list.
 */
void printList(DynArr *heap)
{
    DynArr *temp;
    TaskP task;
    assert(sizeDynArr(heap) > 0);
    
    temp = createDynArr(sizeDynArr(heap));
    /* copy the main list to a temp list
     * so that tasks can be printed out and removed.
     */
    copyDynArr(heap, temp);
    while(sizeDynArr(temp) > 0)
    {
        /* get the task */
        task = getMinHeap(temp);
        /* print the task */
        printf("%d:  %s\n\n", task->priority, task->description);
        /* remove the task */
        removeMinHeap(temp);
    }
    /* free the temp list */
    deleteDynArr(temp);
}
Пример #5
0
/*  Print the list in priority order.  This requires that either we sort it...or make a copy and pull
off the smallest element one at a time.  That's what we've done here.

    param:  heap    pointer to the list
    pre:    the list is not empty
    post:   The tasks from the list are printed out in priority order.
			The tasks are not removed from the list.
*/
void printList(DynArr *heap)
{
  DynArr *temp;
  TaskP task;
  assert(sizeDynArr(heap) > 0);

  temp = createDynArr(sizeDynArr(heap));
  /* copy the main list to a temp list
   * so that tasks can be printed out and removed.
   */
  copyDynArr(heap, temp);
  while(sizeDynArr(temp) > 0)
    {
      /* get the task */
      task = getMinHeap(temp);

      /* print the task */
      printf("%d:  %s\n\n", task->priority, task->description);
      /* remove the task , but let's not free up the memory it's pointing to since old Arr is using it!*/
      removeMinHeap(temp, compare);
    }
  /* free the temp list */
  deleteDynArr(temp);
}
Пример #6
0
int main(int argc, const char * argv[])
{
	char cmd = ' ';
	DynArr* mainList = createDynArr(10);
	char filename[100]; //filename buffer
	FILE* file = NULL; //input/output file

	printf("\n\n** TO-DO LIST APPLICATION **\n\n");

	do
	{
		printf("Press:\n"
			"'l' to load to-do list from a file\n"
			"'s' to save to-do list to a file\n"
			"'a' to add a new task\n"
			"'g' to get the first task\n"
			"'r' to remove the first task\n"
			"'p' to print the list\n"
			"'e' to exit the program\n"
			);
		/* get input command (from the keyboard) */
		cmd = getchar();
		/* clear the trailing newline character */
		while (getchar() != '\n');

		switch (cmd)
		{
		case 'l':
		{
			//get filename
			printf("Enter filename: ");
			scanf("%s", filename);
			getchar();

			//if a file is already open, then close it
			if (file != NULL)
			{
				fclose(file);
			}

			file = fopen(filename, "r"); //open the file for reading

			if (file != NULL) //if the file was opened sucessfully...
			{
				//empty the main list
				if (!isEmptyDynArr(mainList))
				{
					int size = sizeDynArr(mainList);
					for (int i = 0; i < size; i++)
					{
						removeMinHeap(mainList, compare);
					}
				}

				//load the file and print a confirmation
				loadList(mainList, file); 
				printf("List has been loaded.\n\n");
			}
			else //otherwise print and error message
			{
				printf("Could not open file.\n\n");

			}
			break;
		}
		case 's':
		{
			//get filename
			printf("Enter filename: ");
			scanf("%s", filename);
			getchar();

			file = fopen(filename, "w"); //open the file for writing

			if (file != NULL) //if the file was opened sucessfully
			{
				//save the list and print a confirmation
				saveList(mainList, file);
				printf("List has been saved.\n\n");
			}
			else //otherwise print an error message
			{
				printf("Could not create file.\n\n");

			}
			break;
		}
		case 'a':
		{
			int priority; //stores the task priority
			char desc[100]; //sotres the task description

			//get the task description
			printf("Enter a brief description: ");
			fgets(desc, 100, stdin);
			desc[strcspn(desc, "\n")] = 0; //removes trailing newline character

			//get the task priority
			printf("Enter a task priority (0-999): ");
			scanf("%d", &priority);
			getchar();

			TYPE newTask = createTask(priority, desc); //create a new task
			addHeap(mainList, newTask, compare); //add that taks to the heap
			printf("The task '%s' has been added to your list.\n\n", desc); //print a confirmation
			break;
		}
		case 'g':
		{
			if (!isEmptyDynArr(mainList)) //make sure the list is not empty
			{
				struct Task* task = (struct Task*)getMinHeap(mainList); //get the value at the heap's root node
				printf("Your first task is: %s\n\n", task->description); //print the description of that value
			}
			else
			{
				printf("Your to-do list is empty!\n\n"); 
			}
			break;
		}
		case 'r':
		{
			if (!isEmptyDynArr(mainList)) //make sure the heap is not empty
			{
				struct Task* task = (struct Task*)getMinHeap(mainList); //get the value at the heap's root node
				printf("Your first task '%s' has been removed from the list.\n\n", task->description); //print the description of that value
				removeMinHeap(mainList, compare); //remove that value from the heap
				free(task);
				task = NULL;
				
			}
			else
			{
				printf("The list is empty.\n\n");
			}
			break;
		}
		case 'p':
		{
			if (!isEmptyDynArr(mainList)) //make sure the heap is not empty
			{
				printList(mainList); //then print the contents of the heap from highest to lowest priority
			}
			else
			{
				printf("The list is empty.\n\n");
			}
			break;
		}
		case 'e':
		{
			//if a file has been opened, then close that file
			if (file != NULL)
			{
				fclose(file);
			}
			break;
		}
		default:
			break;
		}
		/* Note: We have provided functions called printList(), saveList() and loadList() for you
		to use.  They can be found in toDoList.c */
	} while (cmd != 'e');
	/* delete the list */
	deleteDynArr(mainList);

	return 0;
}
Пример #7
0
int main(int argc, const char * argv[])
{
  Task *task1, *task2, *task3, *task4, *task5, *task6, *task7, *task8, *task9, *task10;
  Task *task11, *task12, *task13, *task14, *task15, *task16, *task17, *task18, *task19, *task20;
	DynArr *mainList;
	int i;
	mainList = createDynArr(10);

	/* create tasks */
	task1 = createTask(9, "task 1");
	task2 = createTask(3, "task 2");
	task3 = createTask(2, "task 3");
	task4 = createTask(4, "task 4");
	task5 = createTask(5, "task 5");
	task6 = createTask(7, "task 6");
	task7 = createTask(8, "task 7");
	task8 = createTask(6, "task 8");
	task9 = createTask(1, "task 9");
	task10 = createTask(10, "task 10");
    task11 = createTask(19, "task 11");
	task12 = createTask(13, "task 12");
	task13 = createTask(12, "task 13");
	task14 = createTask(14, "task 14");
	task15 = createTask(15, "task 15");
	task16 = createTask(17, "task 16");
	task17 = createTask(18, "task 17");
	task18 = createTask(16, "task 18");
	task19 = createTask(3, "task 19");
	task20 = createTask(110, "task 20");

	/* add tasks to the dynamic array */
	addHeap(mainList, task1);
	addHeap(mainList, task2);
	addHeap(mainList, task3);
	addHeap(mainList, task4);
	addHeap(mainList, task5);
	addHeap(mainList, task6);
	addHeap(mainList, task7);
	addHeap(mainList, task8);
	addHeap(mainList, task9);
	addHeap(mainList, task10);
    addHeap(mainList, task11);
	addHeap(mainList, task12);
	addHeap(mainList, task13);
	addHeap(mainList, task14);
	addHeap(mainList, task15);
	addHeap(mainList, task16);
	addHeap(mainList, task17);
	addHeap(mainList, task18);
	addHeap(mainList, task19);
	addHeap(mainList, task20);

#ifdef TESTHEAP
    printf("Print heap\n");
	for(i = 0; i < sizeDynArr(mainList);i++)
		print_type(getDynArr(mainList,i));


	while(!isEmptyDynArr(mainList))
	{
		TYPE v;
		v = getMinHeap(mainList);
		 printf("Removing: ");
		print_type(v);
		removeMinHeap(mainList);

        for(i = 0; i < sizeDynArr(mainList);i++)
            print_type(getDynArr(mainList,i));
	}
#endif

#ifdef TESTSORT

	printf("Before Sort Called \n");
	for(i = 0; i < sizeDynArr(mainList);i++)
			printf("DynArr[%d] = %d\n", i, ((Task*)getDynArr(mainList,i))->priority);


	/* sort tasks */
	sortHeap(mainList);
	printf("After Sort Called \n");

	/* print sorted tasks from the dynamic array */
	for(i = 0; i < sizeDynArr(mainList);i++)
				printf("DynArr[%d] = %d\n", i, ((Task*)getDynArr(mainList,i))->priority);

	return 0;

#endif
}
Пример #8
0
int main (int argc, const char * argv [])
{
    TYPE newTask, firstTask;
    char desc [ TASK_DESC_SIZE ], filename [ 50 ], *nlptr;
    int priority;
    char cmd = ' ';
    FILE *filePointer;
    DynArr * mainList = createDynArr(10);

    printf("\n\n** TO-DO LIST APPLICATION **\n\n");

    do
        {
        printf("Press:\n"
               "'l' to load to-do list from a file\n"
               "'s' to save to-do list to a file\n"
               "'a' to add a new task\n"
               "'g' to get the first task\n"
               "'r' to remove the first task\n"
               "'p' to print the list\n"
               "'e' to exit the program\n"
               );
        /* get input command (from the keyboard) */
        cmd = getchar();
        /* clear the trailing newline character */
        while( getchar() != '\n' ) ;

        switch( cmd )
            {
        case 'a': /* add new task */
            printf("Please enter the task description: ");
            /* get task description from user input (from keyboard) */
            if( fgets(desc, sizeof( desc ), stdin) != NULL )
                {
                /* remove trailing newline character */
                nlptr = strchr(desc, '\n');
                if( nlptr )
                    *nlptr = '\0';
                }
            /* get task priority from user input (from keyboard) */
            do {
                printf("Please enter the task priority (0-999): ");
                scanf("%d", &priority);
                } while( !( priority >= 0 && priority <= 999 ) );

            /* clear the trailing newline character */
            while( getchar() != '\n' ) ;

            /* create task and add the task to the heap */
            newTask = createTask(priority, desc);
            addHeap(mainList, newTask);
            printf("The task '%s' has been added to your to-do list.\n\n", desc);
            break;

        case 'g': /* get the first task */
            if( sizeDynArr(mainList) > 0 )
                {
                firstTask = getMinHeap(mainList);
                printf("Your first task is: %s\n\n", firstTask.description);
                }
            else
                printf("Your to-do list is empty!\n\n");

            break;

        case 'r': /* remove the first task */
            if( sizeDynArr(mainList) > 0 )
                {
                firstTask = getMinHeap(mainList);
                removeMinHeap(mainList);
                printf("Your first task '%s' has been removed from the list.\n\n", firstTask.description);
                }
            else
                printf("Your to-do list is empty!\n\n");

            break;

        case 'p': /* print the list */
            if( sizeDynArr(mainList) > 0 )
                {
                printList(mainList);
                }
            else
                printf("Your to-do list is empty!\n\n");

            break;


        case 's': /* save the list to file */
            if( sizeDynArr(mainList) > 0 )
                {
                /* get filename from user input (from keyboard) */
                printf("Please enter the filename: ");
                if( fgets(filename, sizeof( filename ), stdin) != NULL )
                    {
                    /* remove trailing newline character */
                    nlptr = strchr(filename, '\n');
                    if( nlptr )
                        *nlptr = '\0';
                    }
                /* open the file */
                filePointer = fopen(filename, "w");
                if( filePointer == NULL ) {
                    fprintf(stderr, "Cannot open %s\n", filename);
                    break;
                    }
                /* save the list to the file */
                saveList(mainList, filePointer);
                /* close the file */
                fclose(filePointer);
                printf("The list has been saved into the file successfully.\n\n");
                }
            else
                printf("Your to-do list is empty!\n\n");

            break;

        case 'l': /* load the list from the file */
            printf("Please enter the filename: ");
            /* get filename from user input (from keyboard) */
            if( fgets(filename, sizeof( filename ), stdin) != NULL )
                {
                /* remove trailing newline character */
                nlptr = strchr(filename, '\n');
                if( nlptr )
                    *nlptr = '\0';
                }
            /* open the file */
            filePointer = fopen(filename, "r");
            if( filePointer == NULL ) {
                fprintf(stderr, "Cannot open %s\n", filename);
                break;
                }
            /* load the list from the file */
            loadList(mainList, filePointer);
            /* close the file */
            fclose(filePointer);
            printf("The list has been loaded from file successfully.\n\n");
            break;

        case 'e': /* exit the program */
            printf("Bye!\n\n");
            break;

        default:
            printf("What is your command anyway?\n\n" );
            break;
            }
        }
    while( cmd != 'e' );
    /* delete the list */
    deleteDynArr(mainList);

    return 0;
}
Пример #9
0
int main(int argc, const char * argv[])
{
  Task *task1, *task2, *task3, *task4, *task5, *task6, *task7, *task8, *task9, *task10;
	DynArr *mainList;
	int i;
	mainList = createDynArr(10);

	/* create tasks */
	task1 = createTask(9, "task 1");
	task2 = createTask(3, "task 2");
	task3 = createTask(2, "task 3");
	task4 = createTask(4, "task 4");
	task5 = createTask(5, "task 5");
	task6 = createTask(7, "task 6");
	task7 = createTask(8, "task 7");
	task8 = createTask(6, "task 8");
	task9 = createTask(1, "task 9");
	task10 = createTask(0, "task 10");

	/* add tasks to the dynamic array */
	addHeap(mainList, task1);
	addHeap(mainList, task2);
	addHeap(mainList, task3);
	addHeap(mainList, task4);
	addHeap(mainList, task5);
	addHeap(mainList, task6);
	addHeap(mainList, task7);
	addHeap(mainList, task8);
	addHeap(mainList, task9);
	addHeap(mainList, task10);

#ifdef TESTHEAP
	for(i = 0; i < sizeDynArr(mainList);i++)
		printf("DynArr[%d] = %d\n", i, getDynArr(mainList,i).priority);


	while(!isEmptyDynArr(mainList))
	{
		TYPE v;
		v = getMinHeap(mainList);
		printf("Val = %s ___%d\n", v->description, v->priority);
		removeMinHeap(mainList);
	}
#endif

#ifdef TESTSORT

	printf("Before Sort Called \n");
	for(i = 0; i < sizeDynArr(mainList);i++)
			printf("DynArr[%d] = %d\n", i, ((Task*)getDynArr(mainList,i))->priority);


	/* sort tasks */
	sortHeap(mainList);
	printf("After Sort Called \n");

	/* print sorted tasks from the dynamic array */
	for(i = 0; i < sizeDynArr(mainList);i++)
				printf("DynArr[%d] = %d\n", i, ((Task*)getDynArr(mainList,i))->priority);

	return 0;

#endif
}
Пример #10
0
Файл: main.c Проект: dot1q/Misc
int main (int argc, const char * argv[])
{
  char cmd = ' ';
  int priority;
  char str [256];
  DynArr* mainList = createDynArr(10);
  FILE *fp;

  printf("\n\n** TO-DO LIST APPLICATION **\n\n");

  do
    {
      printf("Press:\n"
             "'l' to load to-do list from a file\n"
             "'s' to save to-do list to a file\n"
             "'a' to add a new task\n"
             "'g' to get the first task\n"
             "'r' to remove the first task\n"
             "'p' to print the list\n"
             "'e' to exit the program\n"
             );
      /* get input command (from the keyboard) */
      cmd = getchar();
      /* clear the trailing newline character */
      while (getchar() != '\n');

	switch(cmd){
	case 'l':
		//load
		fp = fopen("todo.txt", "r");
		if(fp == NULL){
			printf("error opening file\n");
		}
		loadList(mainList, fp);
		fclose(fp);
		printf("List is loaded!\n");
		break;
		
	case 's':
		//save
		fp = fopen("todo.txt", "w");
		if(fp == NULL){
			printf("Error opening file!\n");
		}
		saveList(mainList, fp);
		fclose(fp);
		printf("List is saved!\n");
		break;
	case 'a':
                printf("Enter a description: ");
		fgets(str, 256, stdin);
		if ((strlen(str)>0) && (str[strlen (str) - 1] == '\n'))
			str[strlen (str) - 1] = '\0';
		printf("Enter the priority: ");
		scanf("%d", &priority);
                TYPE taskTemp = createTask(priority, str);
                addHeap(mainList, taskTemp, compare);
		printf("Item added! \n\n");
		while (getchar() != '\n');
		break;
	case 'g':
		if(sizeDynArr(mainList) > 0){
			print_type(getMinHeap(mainList));
		}else{
			printf("List is empty!\n");
		}
		break;
	case 'r':
		if(sizeDynArr(mainList) > 0){
			removeMinHeap(mainList, compare);
		}else{
			printf("No items in list!\n");
		}
		break;
	case 'p':
		if(sizeDynArr(mainList) > 0){
			printList(mainList);
		}else{
			printf("List has no items!\n");
		}
		break;
	case 'e':
		break;
	default:
		printf("Please enter a proper value!\n");
		break;
	}
	

    }
  while(cmd != 'e');
  /* delete the list */
  deleteDynArr(mainList);

  return 0;
}
Пример #11
0
int main() {
    struct task task1, task2, task3, task4, task5, task6, task7, task8, task9, task10;
	struct DynArr taskList;
	initDynArr(&taskList, 10);

    /* create tasks */
	task1 = createTask(10, "Clean house");
	task2 = createTask(6, "Eat dinner");
	task3 = createTask(2, "Do laundry");
	task4 = createTask(4, "Do dishes");
	task5 = createTask(5, "Grocery shopping");
	task6 = createTask(7, "Change oil");
	task7 = createTask(13, "Do taxes");
	task8 = createTask(8, "Sleep");
	task9 = createTask(1, "Do other homework");
	task10 = createTask(0, "Finish heap homework");

	/* add tasks to the dynamic array */
	addHeap(&taskList, task1);
	addHeap(&taskList, task2);
	addHeap(&taskList, task3);
	addHeap(&taskList, task4);
	addHeap(&taskList, task5);
	addHeap(&taskList, task6);
	addHeap(&taskList, task7);
	addHeap(&taskList, task8);
	addHeap(&taskList, task9);
	addHeap(&taskList, task10);

	printf("Beginning testing... \n");

  /* Should print: heap size: 10 */
  printf("heap size: %d \n", sizeDynArr(&taskList));

  /* Should print - Priority: 0 - Description: Finish heap homework */
  printTask(getMinHeap(&taskList));

  /* Remove top node from head */
	removeMinHeap(&taskList);

  /* Should print - Priority: 1 - Do other homework */
	printTask(getMinHeap(&taskList));

	int i;
	printf("printing heap... \n");
	/* Should print - 1 2 6 4 5 7 13 10 8 9 */
	for(i = 0; i <sizeDynArr(&taskList);i++) {
        printf("%d ", getDynArr(&taskList, i).priority);
	}
	printf("\n");

    /* Sort heap */
	sortHeap(&taskList);

    printf("printing sorted heap... \n");
    /* Should print (without descriptions) : 1, 2, 4, 5, 6, 7, 8, 10, 13 */
		/*Not sure how to both print "Priority: 1 - Do Other homework" like you asked
			above and in this order ascending, using the same function.
			Doesn't really make sense.*/
	for (i = 0; i < taskList.size; i++)	{
	  	printTask(getDynArr(&taskList, i));
	}
    printf("\n");

	return 0;
}
Пример #12
0
int main (int argc, const char * argv[])
{
    char cmd[1] = " ";
    char  filename[128];
    FILE *a_file;

    int priority;
    char task[128];
    struct Task * tempTask;
    DynArr* mainList = createDynArr(10);

    printf("\n\n** TO-DO LIST APPLICATION **\n\n");

    do
        {
            printf("Press:\n"
                 "'l' to load to-do list from a file\n"
                 "'s' to save to-do list to a file\n"
                 "'a' to add a new task\n"
                 "'g' to get the first task\n"
                 "'r' to remove the first task\n"
                 "'p' to print the list\n"
                 "'e' to exit the program\n"
                 );
          /* get input command (from the keyboard) */
            scanf("%s", cmd);
          /* clear the trailing newline character */
            while (getchar() != '\n');

          /* Fixme:  Your logic goes here! */
            if(cmd[0] == 'l'){
                printf("Please enter a file name to load:");
                scanf("%s", filename);
                a_file = fopen(filename, "r");
                loadList(mainList, a_file);
                printf("File loaded\n");
                }

            else if(cmd[0] == 's'){
                printf("Please enter the file name:");
                scanf("%s", filename);
                a_file = fopen(filename, "w");
                saveList(mainList, a_file);
                printf("File saved.\n");
                }

            else if(cmd[0] == 'a'){
                printf("Please enter the task name: ");
                scanf("%s", task);
                printf("Please enter the task priority: ");
                scanf("%d", &priority);
                tempTask = createTask(priority, task);

                addHeap(mainList, tempTask, compare);
                }

            else if(cmd[0] == 'g'){
                if (sizeDynArr(mainList) > 0){
                    tempTask = getMinHeap(mainList);
                    printf("The first task on the list is: %s\n", tempTask->description);
                }
                else
                    printf("Your to-do list is empty.\n");
            }

            else if(cmd[0] == 'r'){

                if (sizeDynArr(mainList) > 0){
                    tempTask = getMinHeap(mainList);
                    removeMinHeap(mainList, compare);
                    printf("The first task (%s) has been removed from the list.\n", tempTask->description);
                    free(tempTask);
                  }
                else
                    printf("Your to-do list is empty, so nothing was removed.\n");
                }

            else if(cmd[0] == 'p'){
                if (sizeDynArr(mainList) > 0){
                    printDynArr(mainList, print_type);
                    }
                else
                    printf("Your to-do list is empty.\n");
                }
            } while(cmd[0] != 'e');
          /* Note: We have provided functions called printList(), saveList() and loadList() for you
             to use.  They can be found in toDoList.c */


      /* delete the list */
      deleteDynArr(mainList);

      return 0;
}