Beispiel #1
0
int main(int argc, char ** argv)
{
    if (argc != 3) 
	{
	    printf("Usage: ./pa05 <input file 1> <input file 2>\n");
	    return EXIT_FAILURE;
	}

    // Input file processed, ready to build the sparse-array
    Node * array1 = NULL, * array2 = NULL;
    int okay = TRUE;
    okay = okay && readSparseArrayFile(argv[1], &array1);
    okay = okay && readSparseArrayFile(argv[2], &array2);

    if(okay)
	{
	    FILE * out = stdout; // This is where we output results
    
	    printf("*******************************\n");
	    printf("Array 1:\n");
	    printf("*******************************\n");

		List_print(out, array1);  

	    printf("\n*******************************\n");
	    printf("Array 2:\n");
	    printf("*******************************\n");

	    List_print(out, array2); 

	    printf("\n*******************************\n");
	    printf("Copy of Array 1:\n");
	    printf("*******************************\n");

	    Node * clone_array = List_copy(array1);
	    List_print(out, clone_array);

	    printf("\n*******************************\n");
	    printf("Merged array:\n");
	    printf("*******************************\n");

	    Node * array_new = List_merge(array1, array2);
	    List_print(out, array_new);

			printf("\n\nArray destroy sequence 1\n\n");
	   // List_destroy(array_new);
	  //  List_destroy(clone_array);
	}
	printf("\n\nArray destroy sequence 2\n\n");
	fflush(stdout);
   // List_destroy(array1);
   // List_destroy(array2);

    return EXIT_SUCCESS;
}
Beispiel #2
0
int main()
{
    List *list = NULL;
	char c;
	int i;
	while ((c = getchar()) != 'q')
		switch (c)
		{
			case 'a':
			{
				scanf("%i", &i);
				if (List_add(&list, i) == 1)
					printf("Error\n");
				break;
			}
			case 'r':
			{
				scanf("%i", &i);
				if (List_remove(&list, i) == 1)
					printf("Not found =(\n");
				break;
			}
			case 'p':
			{
				List_print(&list);
				break;
			}
		}
	List_delList(&list);
	return 0;
}
Beispiel #3
0
int main (int argc, char * argv[])
{
  printf("make a node and print it");
  Node * test = List_create(5,1);
  List_print(stdout, test);

  printf("destroying it");
  List_destroy(test);
  List_print(stdout, test);
  
  printf("linkingit again");
  Node * newnode =NULL;
  Node * newnode2 = NULL;
  int values[4] = {2,3,1,2};
  int indexs[4] = {1,4,2,3};
  // int i;
  int length = 4;
  printf("this is newnode");
  newnode = List_build(values, indexs, length);
  //printf("this is the newnode");  
  //List_print(stdout, newnode);

  //printf("address:%p", newnode->next);
  //printf("address:%p", newnode->next->next);
  newnode2 = List_copy(newnode);
  printf("copy of new node");
  List_print(stdout, newnode2);  
  //List_print(stdout, newnode);
  Node * merged = NULL;
  merged = List_merge(newnode, newnode2);
  List_print(stdout, merged);  
// printf("copy linked list");
    
  /*
  while (newnode!= NULL)
    {
      List_print(stdout, newnode);
      printf("this is the address : %p", newnode->next);  
    }
  */   
//newnode = List_insert_ascend(newnode -> next,3,4);
  //List_print(stdout, newnode);
  //printf("this is the address : %p", newnode->next);

  //List_print(stdout, newnode -> next); 
  return 0;
}
Beispiel #4
0
void bubble_sort(List *list)
{
    int count = List_Node_count(list);
    printf("Number of elements in list is %d\n", count);

    List_print(list);
    int i;
    Node *current;
    Node *after_current;
    for(i=1; i<count; i++)
    {
        current = list->first;
        while(current->next!=NULL)
        {
            after_current = current->next; 
            //compare the adjacent entries of list
            if(current->data>after_current->data)
            {
                current->next = after_current->next;
                if(after_current->next!=NULL)
                {
                    Node *ab = after_current->next;
                    ab->previous = current;
                }
                if(current->previous!=NULL)
                {
                    Node *cd = current->previous;
                    cd->next = after_current;
                }
                after_current->previous = current->previous;
                current->previous = after_current;
                after_current->next = current;

                if(current==list->first)
                {
                    list->first = after_current;
                }
            }
            else{
                current = after_current;
            }
        }
    }

    printf("List after bubble sort\n");
    List_print(list);
}
Beispiel #5
0
int main(int argc, char * argv[])
{
  Node * head = NULL; /* must initialize it to NULL */
  head = List_insert(head, 917);
  head = List_insert(head, -504);
  head = List_insert(head, 326);
  List_print(head);
  head = List_delete(head, -504);
  List_print(head);
  head = List_insert(head, 138);
  head = List_insert(head, -64);
  head = List_insert(head, 263);
  List_print(head);

  if (List_search(head, 138) != NULL)
    {
      printf("138 is in the list\n");
    }
  else
    {
      printf("138 is not in the list\n");
    }

  if (List_search(head, 987) != NULL)
    {
      printf("987 is in the list\n");
    }
  else
    {
      printf("987 is not in the list\n");
    }
    

  /* delete the first Node */
  head = List_delete(head, 263);
  List_print(head);

  /* delete the last Node */
  head = List_delete(head, 917);
  List_print(head);

  /* delete all Nodes */
  List_destroy(head);
  return EXIT_SUCCESS;
}
Beispiel #6
0
void Staff_manage()
{
	char dirname[] = "./Date/Staff.txt";
	FILE *fp;
	pList head;
	int n,count;
	fp = File_open(dirname);
	head = File_read(fp,sizeof(struct staff_t));
	count = List_count(head);
	while(1)
	{	 
		system("cls");
		printf("\n\n\n\t\t\t员工管理界面\n\n");
		printf("\t\t\t1、增加员工\n\n"); 
		printf("\t\t\t2、删除员工\n\n");
		printf("\t\t\t3、修改员工\n\n"); 
		printf("\t\t\t4、查询员工\n\n"); 
		printf("\t\t\t5、返回\n\n"); 
		printf("\t\t\t请输入要操作的功能:");
		n = glb_putString(NULL,'1','5',1,2);
		switch(n)
		{
		case 1:	
			List_print(head,staff_print);
			File_add(fp,head,sizeof(struct staff_t),staff_add);
			List_print(head,staff_print);		
			break;
		case 2:
			List_print(head,staff_print);
			File_remove(fp,head,sizeof(struct staff_t),staff_remove);
			head = File_read(fp,sizeof(struct staff_t));
			List_print(head,staff_print);			
			break;
		case 3:
			List_print(head,staff_print);
			File_updata(fp,head,sizeof(struct staff_t),staff_updata);
			head = File_read(fp,sizeof(struct staff_t));
			List_print(head,staff_print);		
			break;
		case 4:  
			staff_search(head);			
			break;
		case 5:
			break;
		}
		if (n == 5)
		{
			break;
		}
		else
		{
            printf("\n按<Esc>键返回...");
			while(getch() != 27)
			{}
		}
		
		
	}
	List_free(head);
}
Beispiel #7
0
void Menu_manage()
{
	char dirname[] = "./Date/Menu.txt";
	FILE *fp;
	pList head;
	int n;
	fp = File_open(dirname);
	head = File_read(fp,sizeof(struct Menu_t));
	while(1)
	{	 
		system("cls");
		printf("\n\n\n\t\t\t菜谱管理界面\n\n");
		printf("\t\t\t1、增加菜谱\n\n"); 
		printf("\t\t\t2、删除菜谱\n\n");
		printf("\t\t\t3、修改菜谱\n\n"); 
		printf("\t\t\t4、查询菜谱\n\n"); 
		printf("\t\t\t5、返回\n\n"); 
		printf("\t\t\t请输入您要操作的功能:");
	    n = glb_putString(NULL,'1','5',1,2);
		switch(n)
		{
		case 1:	
			List_print(head,Menu_print);
			File_add(fp,head,sizeof(struct Menu_t),Menu_add);
			head = File_read(fp,sizeof(struct Menu_t));
			List_print(head,Menu_print);
			break;
		case 2:
			List_print(head,Menu_print);
			File_del(fp,dirname,head,sizeof(struct Menu_t),Menu_del);
			List_print(head,Menu_print);
			break;
		case 3:
			List_print(head,Menu_print);
			File_updata(fp,head,sizeof(struct Menu_t),Menu_updata);
		    head = File_read(fp,sizeof(struct Menu_t));
			List_print(head,Menu_print);
			break;
		case 4: 
	
		    Menu_search(head);
			break;
		case 5:
			break;
		}
		if (n == 5)
		{
			break;
		}
		else
		{
            printf("\n按<Esc>键返回...");
			while(getch() != 27)
			{}
		}
		
	}
	  List_free(head);
}
Beispiel #8
0
void Table_manage()
{
	char dirname[] = "./Date/Table.txt";
	FILE *fp;
	pList head;
	int n;
	fp = File_open(dirname);
	head = File_read(fp,sizeof(struct Table_t));
	while(1)
	{	 
		system("cls");
		printf("\n\n\n\t\t\t台桌管理界面\n\n");
		printf("\t\t\t1、增加台桌\n\n"); 
		printf("\t\t\t2、删除台桌\n\n");
		printf("\t\t\t3、修改台桌\n\n"); 
		printf("\t\t\t4、查询台桌\n\n"); 
		printf("\t\t\t5、返回\n\n"); 
		printf("\t\t\t请输入您要操作的功能:");
	    n = glb_putString(NULL,'1','5',1,2);
		switch(n)
		{
		case 1:	
			List_print(head,Table_print);
			File_add(fp,head,sizeof(struct Table_t),Table_add);
			head = File_read(fp,sizeof(struct Table_t));
			List_print(head,Table_print);			
			break;
		case 2:
			List_print(head,Table_print);
			File_remove(fp,head,sizeof(struct Table_t),Table_remove);
			head = File_read(fp,sizeof(struct Table_t));
			List_print(head,Table_print);
			break;
		case 3:
			List_print(head,Table_print);
			File_updata(fp,head,sizeof(struct Table_t),Table_updata);
		    head = File_read(fp,sizeof(struct Table_t));
			List_print(head,Table_print);
			break;
		case 4: 
		    Table_search(head,3,4);
			break;
		case 5:
			break;
		}
		if (n == 5)
		{
			break;
		}
		else
		{
            printf("\n按<Esc>键返回...");
			while(getch() != 27)
			{}
		}
		
	}
	  List_free(head);
}
Beispiel #9
0
int main(int argc, char * * argv)
{
   printf("---------------------Testing Answer08-----------------------\n");
   // Create  two linked lists
   List * head1 = NULL;
   head1 = List_Insert(head1, "a");
   head1 = List_Insert(head1, "c");
   head1 = List_Insert(head1, "e");
   List_print(head1);
   List * head2 = NULL;
   head2 = List_Insert(head2, "b");
   head2 = List_Insert(head2, "d");
   head2 = List_Insert(head2, "f");
   head2 = List_Insert(head2, "g");
   List_print(head2);
   List * head3 = NULL;
   head3 = List_Insert(head3, "x");
   head3 = List_Insert(head3, "i");
   head3 = List_Insert(head3, "f");
   head3 = List_Insert(head3, "r");
   List_print(head3);
   printf("List length of 1 is: %d\n", List_length(head1));
   printf("List length of 2 is: %d\n", List_length(head2));
   printf("---------------------Testing Merge-----------------------\n");
   List * newlist;
   newlist = List_merge(head1, head2, strcmp);
   List_print(newlist);

   printf("---------------------Testing Merge-----------------------\n");
   List * sorted;
   sorted = List_sort(head3, strcmp);
   List_print(sorted);

   return EXIT_SUCCESS;

}
Beispiel #10
0
void List_print(Node * front)
{
	if(front == NULL)
	{
		return;
	}
	printf("%d \n", front->data);
	List_print(front->next);

NOde * List_delete(Node * front, int val)
{
  if(front == NULL)
    {
      return NULL;
    }
  if((front->val) == val)
    {
      Node * p = front->next; //p may be NULL
      free(front);
      return p;
    }
  front->next = List_delete(front->next, val);
  return front;
}

void List_destroy

  /*
 List_insert4 (Node ** front, ....)
{
  *front = ...
  */


main
{
	front = List_insert(front, 5);
	front = List_insert(front, 8);
	front = List_insert(front, 9);
	front = List_insert(front, 11);

	front2 = List_insert2(front2, 5);
	front2 = List_insert2(front2, 8);
	front2 = List_insert2(front2, 9);
	front2 = List_insert2(front2, 11);
       
       return EXIT_SUCCESS;
}
Beispiel #11
0
void test_list_insert_ascend()
{
		printf("running tests on list-insert-ascend\n");
		
	  Node * head = List_create(100, 2);
		head -> next = List_create(100, 4);
		head -> next -> next = List_create(100, 6);
		head -> next -> next -> next = List_create(100, 8);
		head -> next -> next -> next -> next = List_create(100, 10);
		
		
		
	  //printf("Printing start-list\n");
	  //List_dump(head);
		List_print(stdout, head);
	  List_destroy(head);
}
Beispiel #12
0
int main(void)
{
    List *list = NULL;
    list = List_create();

    // Int Data
    int n1 = 1;
    int n2 = 2;
    int n3 = 3;

    List_push(list, &n1);
    List_push(list, &n2);
    List_push(list, &n3);
    List_print(list, Int_printer);

    List_destroy(list);
}
static int test_merge(List * lhs, List * rhs, 
		      int (*compar)(const char *, const char *))
{
    int success = TRUE; // until proven otherwise

    // First make sure our lists are sorted...
    lhs = List_sort_sol(lhs, compar);
    rhs = List_sort_sol(rhs, compar);

    // What are we testing?
    printf("Testing List_merge(lhs, rhs, compar), where:\n\n");
    printf("+ compar is: ");
    print_compar(compar);
    printf("\n");
    printf("+ lhs is: ");
    List_print(lhs);
    printf("\n");
    printf("+ rhs is: ");
    List_print(rhs);
    printf("\n");

    // Check that the student doesn't reallocate nodes... store lhs/rhs pointers
    int len = List_length_sol(lhs) + List_length_sol(rhs);
    List * * nodeArr = malloc(len * sizeof(List *));
    int ind = 0;
    List * node = NULL;
    node = lhs;
    while(node != NULL) {
	nodeArr[ind++] = node;
	node = node->next;
    }
    node = rhs;
    while(node != NULL) {
	nodeArr[ind++] = node;
	node = node->next;
    }
    
    // Clone lhs, rhs, and get the solution
    List * solution = List_merge_sol(List_clone(lhs), List_clone(rhs), compar);

    // Run the students code
    List * merged = List_merge(lhs, rhs, compar);

    printf("\nmerged is  : ");
    List_print(merged);
    printf("\nsolution is: ");
    List_print(solution);
    printf("\n");
    
    // Are we the correct length?
    int merged_len = List_length_sol(merged);
    if(merged_len != len) {
	printf("Error: merged solution has length %d, but it should be %d\n",
	       merged_len, len);
	success = FALSE;
    }

    // Check the solution is in order, and that every Node pointer is in nodeArr
    List * sol_node = solution;
    node = merged;
    while(node != NULL && sol_node != NULL) {
	int found = FALSE;
	for(ind = 0; ind < len && !found; ++ind)
	    found = (nodeArr[ind] == node);
	if(!found) {
	    printf("Error: merged list contained a node with pointer %p; \n"
		   "however, this pointer was not in either of the inputs.\n"
		   "(i.e., 'lhs', or 'rhs'.) You must not create or destroy\n"
		   "any nodes when writing the merge function.\n", node);
	    success = FALSE;
	}
	if(strcmp(node->str, sol_node->str) != 0) {
	    printf("Error: merged list not in order (!)\n");
	    success = FALSE;
	}
	sol_node = sol_node->next;
	node = node->next;
    }

    // Cleanup.
    free(nodeArr);
    List_destroy_sol(solution);
    List_destroy_sol(merged);
    return success;
}