예제 #1
0
int main(){
    List list;
    List list2;
    int i;
    
    init_list( &list );
    printf( "Empty: %d\n", empty_list( list ) );
    for( i = 1; i <= 5; i++ ){
        append_node( &i, sizeof( int ), list );
        printf( "Length: %d | ", length( list ) );
        print_list( list );
    }
    for( i = 6; i <= 10; i++ ){
        insert_front( &i, sizeof( int ), list );
        printf( "Length: %d | ", length( list ) );
        print_list( list );
    }
    printf( "Empty: %d\n", empty_list( list ) );
    printf( "First: %d\n", get_int( list_op_first( list ) -> data ) );
    printf( "Last:  %d\n", get_int( list_op_last( list ) -> data ) );
    printf( "Second to last: %d\n", get_int( list_op_prev( list_op_last( list ), list ) -> data ) );
    printf( "Second element: %d\n", get_int( get_elem_at( 1, list ) -> data ) );
    
    place_after( &i, sizeof( int ), get_elem_at( 1, list ), list );
    print_list( list );
    i++;
    insert_at( &i, sizeof( int ), 2, list );
    print_list( list );
    i++;
    insert_at( &i, sizeof( int ), 0, list );
    print_list( list );
    
    init_list( &list2 );
    for( i = 100; i > 95; i-- ){
        insert_front( &i, sizeof( int ), list2 );
    }
    append_list( list, &list2 );
    free( list2 );
    print_list( list );
    
    printf( "Erasing 5th element...\n" );
    erase_at( 4, list );
    print_list( list );
    printf( "Erasing 1st element...\n" );
    erase_at( 0, list );
    print_list( list );
    printf( "Erasing last element...\n" );
    erase_node( list_op_last( list ), list );
    print_list( list );
    printf( "Erasing 3rd element...\n" );
    erase_node( get_elem_at( 2, list ), list );
    print_list( list );
    
    clear_list( &list );
    
    return 0;
}
예제 #2
0
song* insert_ordered(song* list, char* n, char* a) {
  if (list == NULL) {
    return insert_front(list, n, a);
  } else if (strcmp(a, list->artist) < 0) {
    return insert_front(list, n, a);
  } else if (strcmp(a, list->artist) == 0 &&
	     strcmp(n, list->name) <0) {
    return insert_front(list, n, a);
  }
  list->next = insert_ordered(list->next, n, a);
  return list;
}
int main() {

  // Test reverse of 4 item list.
  struct ll* myList = NULL;
  myList = insert_front(myList, 7);
  myList = insert_front(myList, 3);
  myList = insert_front(myList, 6);
  myList = insert_front(myList, 10);
  print(myList);
  myList = reverse(myList);
  print(myList);

  return 0;
}
예제 #4
0
int main(){

  printf("\n");

  node a,b,c;

  printf("Testing node initialization and print_list: \n");

  a=*make_node('a',&b);
  b=*make_node('b',&c);
  c=*make_terminating_node('c');
  printf("Data in a: %c",a.data);
  printf("\n");
  printf("Linked list beginning with a: ");
  print_list(&a);

  printf("\n\n");


  node x,y,z;

  printf("Testing insert_front: \n");

  z=*insert_front(&a,*make_terminating_node('z'));

  printf("Linked list beginning with z: ");
  print_list(&z);
  printf("\n");

  y=*insert_front(&z,*make_terminating_node('y'));

  printf("Linked list beginning with y: ");
  print_list(&y);
  printf("\n");

  x=*insert_front(&y,*make_terminating_node('x'));

  printf("Linked list beginning with x: ");
  print_list(&x);
  printf("\n");

  printf("\n");

  printf("--re: free_list-- \n--I suppose I did something wrong.  Probably in allocating memory in the first place (in my make_node function, which I now doubt is even necessary).  Temporarily, please enjoy the following error.-- \n\n");

  free_list(&x);

  printf("\n");

}
예제 #5
0
int main()
{
  // Variables
  struct node* head = 0;
  int i;

  // Print List (Start)
  printf("Creating head node...done\n");
  print_list(head);
  printf("\n");

  // Add Nodes to Linked List
  for (i = 5; i >= 0; i--)
    head = insert_front(head, i);

  // Print List (Add)
  printf("Adding elements to list...done\n");
  print_list(head);
  printf("\n");

  // Clearing List
  head = free_list(head);

  // Print List (Clear/End)
  printf("Freeing memory for List...done\n");
  print_list(head);
  printf("\n");

  // End
  printf("END OF PROGRAM\n");

  return 0;
}
예제 #6
0
void main()
{
    N *start=NULL;
    //start=getnode();

    int ch,item;
    for(;;)
    {
        printf("\n1. Insert Front");
        printf("\n2. Delete Front");
        printf("\n3. Delete Front");
        printf("\n4. Delete Rear");
        printf("\n Enter ur choice:");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: printf("\n Enter the element to be inserted:");
                    scanf("%d",&item);
                    start=insert_front(item,start);
                    display(start);
                    break;
            case 2: start=delete_front(start);
                    display(start);
        }

    }
}
예제 #7
0
int main(){
    Item* listptr;
    int i;
    listptr = new_item(0);
    for (i=1; i < 6; i++){
        listptr=insert_front(listptr, i);
    }
    for (i=0; i < 6; i++){
        printf("i %d Item %d\n", i, get(listptr, i));
    }
    listptr = remove_item(listptr, 3);
    for (i=0; i <= 5; i++){
        printf("i %d Item %d\n", i, get(listptr, i));
    }
    printf("Index for 3 %d\n", get_index(listptr, 3));
    
    for (i=1; i < 6; i++){
        listptr=insert_back(listptr, i);
    }
    for (i=0; i < 12; i++){
        printf("i %d Item %d\n", i, get(listptr, i));
    }
    set_item(listptr, 9, 15);
    for (i=0; i < 12; i++){
        printf("i %d Item %d\n", i, get(listptr, i));
    }
    set_item(listptr, 10, 15);
    free_all(listptr);
}
int main(int argc, char** argv) {
	struct node* node = init_node(9);
	struct node *root = insert_front(node, 2);
	root = insert_front(root,3);
	root = insert_front(root, 4);
	root = insert_front(root, 5);

	//struct node* root2 = init_node(7);
//	root2 = insert_front(root2, 6);
	root = mergesort_sll(root, node);
	struct node* curr = root;
	while(curr) {
		printf("%d\n", curr->value);
		curr = curr->next;
	}
	struct node* median = get_median(root);
	printf("\n\n%d\n", median->value);
	
	
}  	 
예제 #9
0
//------------------------------------------------------------------
// Insert many nodes consecutively on either side. 1 front, 0 end
// Returns 0 if successful; -1 otherwise.
//------------------------------------------------------------------
int insert_many(struct node **head, char **str, int cnt, bool side){
 int i;

 if(side){
   for( i = 0; i < cnt; i++ )
     if((insert_front(head, str[i])) == -1)
       return -1;
 }
 else{
   for( i = 0; i < cnt; i++ )
     if((insert_end(head, str[i])) == -1)
       return -1;
 }
 return 0;
}
예제 #10
0
const LinkedList<T>& LinkedList<T>:: operator= (const LinkedList<T>& rhs)
{
	if( this != &rhs)
	{
		
		const LinkedList<T>* p = &rhs;
		if(m_next != NULL){clear();}
		while(p->m_next != NULL)
		{
			//rhs.getAtPtr(static_cast<int>(size));
			insert_front(p->m_data);
			p=p->m_next;
		}
		
	}
	return(*this);
}
예제 #11
0
int main(void)
{
	int ch,item,pos;	
	while(1)
	{
		printf("enter ur choice;\n");
		printf("\n1:insert the number in the front\n2:insert the element in the end\n3:append in the mid\n4:display the elements\n");
		printf("5:delet the node w.r.t pos\n6:delet the node with respect to data\n7:exit\n");
		scanf("%d",&ch);
		switch(ch)
		{
		case 1:
			printf("enter the number\n");
			scanf("%d",&item);
			insert_front(item);
			break;
		case 2:
			printf("enter the number:\n");
			scanf("%d",&item);
			insert_end(item);
			break;
		case 3:
			printf("enter the number and the postision: \n");
			scanf("%d %d",&item, &pos);
			insert_mid(item, pos);
			break;

		case 4:
			display();
			break;
		case 5:
			printf("enter the pos:\n");
			scanf("%d",&pos);
			delet_pos(pos);
			break;
		case 6:
			printf("enter the data\n");
			scanf("%d",&item);
			delet_data(item);
			break;
		case 7:
			exit(0);
		}
	}
	return 0;
}
예제 #12
0
Item* remove_item(Item* listptr, int value) {
    int count = 0;
    int i;
    Item* p;
    Item* q; 
    for(p = listptr; p!= NULL; p = p->rest){
        if (p->value==value){
            q = p->rest;
            for(i=count-1; i>=0; i--){
                q = insert_front(q,get(listptr,i));
            }
            return q;
        }
        count++;
        
    }
    return NULL;
}
예제 #13
0
파일: linklist.c 프로젝트: sduncan/Code
/*======== struct link * insert_ordered() ==========
Inputs:  struct link *p
         char *n
         char *e 
Returns: Pointer to the beginning of the list
Inserts a new link with name n and email e in it's 
lexicographically correct position based on name

10/16/09 13:07:23
jdyrlandweaver
====================*/
struct link * insert_ordered(struct link *p, char *n, char *e) {
  
  struct link *tmp, *curr;
  curr = p;
  
  if ( !p || strcmp(n, p->name) < 0 )
    return insert_front(p, n, e);

  while ( curr->next && strcmp(n, curr->next->name) > 0 )
    curr = curr->next; 
    
  tmp = (struct link *)malloc(sizeof(struct link));
  strncpy(tmp->name, n, sizeof(tmp->name));
  strncpy(tmp->email, e, sizeof(tmp->email));
  tmp->next = curr->next;
  curr->next = tmp;

  return p;
}
int main() {
    int n;
    char s[999], *tok;
    list *l = create_list();

    scanf("%d", &n);
    while (n--) {
        scanf(" %[^\n]", s);
        tok = strtok(s, " ");
        switch (tok[2]) {
            case 'i'://inicio i
            {
                tok = strtok(NULL, " ");
                int i = atoi(tok);
                insert_front(l, i);
                break;
            }
            case 'm'://fim i
            {
                tok = strtok(NULL, " ");
                int i = atoi(tok);
                insert_back(l, i);
                break;
            }
            case 'p'://imprimir
            {
                print_list(l);
                break;
            }
            case 'v'://inverter x y
            {
                tok = strtok(NULL, " ");
                int x = atoi(tok);
                tok = strtok(NULL, " ");
                int y = atoi(tok);
                reverse_list(l, x, y);
                break;
            }
        }
    }
    free_list(l);
    return 0;
}
예제 #15
0
파일: 2.c 프로젝트: sgmonusg/DSA_codes
int main()
{
	struct node *list=NULL;
	insert_front(&list,7);
	insert_front(&list,6);
	insert_front(&list,5);
	insert_front(&list,4);
	insert_front(&list,3);
	insert_front(&list,2);
	insert_front(&list,1);
	
	print_list(list);
	
	swap_pairwise(&list);
	print_list(list);
	
	return 0;
}
예제 #16
0
int main() {


	//Creating llist (node)
	node* llist = (node*)malloc(sizeof(node));
	llist->value = 0;
	llist->next = NULL;

	//Printing value of llist using print_list
	printf("This is when llist is just one node: \n");
	printf("(Printing with print_list)");
	print_list(llist);

	//Adding values to linked list with insert_front
	int i;
	for (i = 1; i < 5; i++){
		llist = insert_front(llist, i);
	}

	//Printing new linked list with print_list
	printf("After values are added to llist with insert_front: \n");
	printf("(Printing with print_list)");
	print_list(llist);

	printf("Before free_list: \n");
	if (llist == NULL)
		printf("null\n");
	else
		printf("not null\n");
	//Using free_list to free all of the memory in the linked list
	llist = free_list(llist);
	printf("After free_list: \n");
	if (llist == NULL)
		printf("null\n");
	else
		printf("not null\n");

	return 0;

}
예제 #17
0
int main() {
    int i;
    Item *list = new_item(0);
    printf("Inserting 6 first integers\n");
    for (i=1; i < 6; i++) {
        list=insert_front(list, i);
    }
    printf("Getting 6 first integers\n");
    for (i=0; i < 6; i++) {
        printf("i %d Item %d\n", i, get(list, i));
    }
    printf("Getting all elements with 3 removed\n");
    list = remove_item(list, 3);
    for (i=0; i <= 5; i++) {
        printf("i %d Item %d\n", i, get(list, i));
    }
    printf("Try to retrieve the index of 3\n");
    printf("Index for 3 %d\n", get_index(list, 3));
    free_all(list);
    printf("Check of the free_all function\n");
    printf("%d\n", get(list, 1));
}
예제 #18
0
int main()
{
	list l;
	init(&l);

	printf("Inserting 3, 4, 1, 5, 6 and 2 at the back of the list.\n");
	insert_back(&l, 3);
	insert_back(&l, 4);
	insert_back(&l, 1);
	insert_back(&l, 5);
	insert_back(&l, 6);
	insert_back(&l, 2);
	for_each_forward(&l, print_node, NULL);

	printf("Print list backwards.\n");
	for_each_backward(&l, print_node, NULL);

	printf("Inserting 5 in front of the list.\n");
	insert_front(&l, 5);
	for_each_forward(&l, print_node, NULL);

	printf("Removing tail.\n");
	remove_tail(&l);
	for_each_forward(&l, print_node, NULL);

	printf("Removing head.\n");
	remove_head(&l);
	for_each_forward(&l, print_node, NULL);

	printf("Add 5 to each element in the list.\n");
	int add=5;
	for_each_forward(&l, add_node, &add);
	for_each_forward(&l, print_node, NULL);

	printf("Freeing list.\n");
	for_each_forward(&l, free_node, NULL);
	return 0;
}
예제 #19
0
void main()
{

    int choice,cnt;

    while(1)
    {

        printf("\n---------------------------------Operations On Doubly Linked List-----------------------------------\n");
        printf("\n\n1.Insert-Front\n2.Insert-Back\n3.Count\n4.Dispaly\n5.Delete-Front\n6.Delete-back\n7.Search\n8.Exit\nEnter your Choice   :  ");
        scanf("%d",&choice);

        switch(choice)//Menu selection
        {
            case 1 :insert_front();
                    break;
            case 2: insert_back();
                    break;
            case 3: cnt=noof();
                    printf("\n No of nodes   is %d ",cnt); 
                    break;
            case 4: display();
                    break;
            case 5: delete_front();
                    break;
            case 6: delete_back();
                    break;
            case 7: search();
                    break;
            case 8: exit(0);
            default:printf("Wrong Input.Please try again \n");
        }

    }

}
예제 #20
0
int main(){
        List *list = (List*)0;
        int opt;
        int val;
        int k1,k2;
        do{     //loop
                printf("Select an operation:\n");
                printf("1) Create a list\t2) Insert at front\n3) Insert at end\t4) Delete first");
                printf("\n5) Delete last\t\t6) Display list\n7) Delete list\t\t8) Swap nodes\n9) Exit\n> ");
                scanf("%d",&opt);
                FLUSH_STDIN();
                switch (opt){
                        case 1: //Create list
                                if((create_list(list) == -1))
                                        fprintf(stderr,"\nError: List already created\n");
                                else    fprintf(stdout,"\nList successfully created\n");
                                break;

                        case 2: //Insert front
                                printf("Enter a value(integer) to insert: ");
                                scanf("%d",&val);
                                list = insert_front(list,val);
                                err_check();
                                break;

                        case 3: //Insert end
                                printf("Enter a value(integer) to insert: ");
                                scanf("%d",&val);
                                list = insert_end(list,val);
                                err_check();
                                break;

                        case 4: //Delete first
                                list = delete_front(list);
                                err_check();
                                break;

                        case 5: //Delete last
                                list = delete_end(list);
                                err_check();
                                break;

                        case 6: //Display list
                                if(display_list(list) <= 0)
                                        fprintf(stderr,"\nError: List is empty or not created. Cannot display.\n");
                                break;

                        case 7: //Delete the whole list
                                list = delete_list(list);
                                if(list_err == LIST_DELETED)
                                        fprintf(stderr,"\nThe list has already been deleted or not even created.\n");
                                else
                                        printf("\nList delete successfully.\n");
                                break;

                        case 8: //Swap
                                printf("Enter key values of two nodes to be swapped: ");
                                scanf("%d %d",&k1,&k2);
                                list = swap_nodes(list,k1,k2);
                                err_check();
                                break;

                        case 9: //Exit
                                break;

                        default:        //Invalid option
                                fprintf(stderr,"\nError: Invalid option\n");

                }//switch
        }while(opt != 9);

        return 0;
}
예제 #21
0
파일: main.c 프로젝트: TristanDuck/C
int main(void)
{
	IntNode *head = NULL;  // An empty linked list.
    IntNode *empty = NULL; // Another empty linked list.

    printf("=== Testing insert_front ===\n\n");
	printf("Calling insert_front with list: ");
    print_linked_list(head);
    printf("\nInserting 3.\n");
	head = insert_front(head, 3);
	printf("Expected list: 3\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

	printf("Calling insert_front with list: ");
    print_linked_list(head);
    printf("\nInserting 2.\n");
	head = insert_front(head, 2);
	printf("Expected list: 2 -> 3\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

	printf("Calling insert_front with list: ");
    print_linked_list(head);
    printf("\nInserting 1.\n");
	head = insert_front(head, 1);
	printf("Expected list: 1 -> 2 -> 3\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

    printf("=== Testing contains ===\n\n");

	_Bool found;

	printf("Calling contains with list: ");
    print_linked_list(empty);
    printf("\nSearching for 1.\n");
	found = contains(empty, 1);
	printf("Expected result: false\n");
    printf("Actual result: ");
    print_boolean(found);
	printf("\n\n");

	printf("Calling contains with list: ");
    print_linked_list(head);
    printf("\nSearching for 1.\n");
	found = contains(head, 1);
	printf("Expected result: true\n");
    printf("Actual result: ");
    print_boolean(found);
	printf("\n\n");

	printf("Calling contains with list: ");
    print_linked_list(head);
    printf("\nSearching for 3.\n");
	found = contains(head, 3);
	printf("Expected result: true\n");
    printf("Actual result: ");
    print_boolean(found);
	printf("\n\n");

	printf("Calling contains with list: ");
    print_linked_list(head);
    printf("\nSearching for 6.\n");
	found = contains(head, 6);
	printf("Expected result: false\n");
    printf("Actual result: ");
    print_boolean(found);
	printf("\n\n");

    printf("=== Testing append_rear ===\n\n");

	printf("Calling append_rear with list: ");
    print_linked_list(head);
    printf("\nAppending 4.\n");
	head = append_rear(head, 4);
	printf("Expected list: 1 -> 2 -> 3 -> 4\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

    printf("=== Testing remove_first ===\n\n");

	printf("Calling remove_first with list: ");
    print_linked_list(head);
	head = remove_first(head);
	printf("\nExpected list: 2 -> 3 -> 4\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

    printf("=== Testing remove_last ===\n\n");

	printf("Calling remove_last with list: ");
    print_linked_list(head);
	head = remove_last(head);
	printf("\nExpected list: 2 -> 3\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

	printf("Calling remove_last with list: ");
    print_linked_list(head);
	head = remove_last(head);
	printf("\nExpected list: 2\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

	printf("Calling remove_last with list: ");
    print_linked_list(head);
	head = remove_last(head);
	printf("\nExpected list: empty list\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

    /* Tests for Exercise 1. */
    
    printf("Building linked list 1 -> 1 -> 2 -> 3 -> 3 -> 4 -> 5 -> 5 -> 5\n\n");

    head = NULL;
    head = intnode_construct(5, head);
    head = intnode_construct(5, head);
    head = intnode_construct(5, head);
    head = intnode_construct(4, head);
    head = intnode_construct(3, head);
    head = intnode_construct(3, head);
    head = intnode_construct(2, head);
    head = intnode_construct(1, head);
    head = intnode_construct(1, head);
    // print_linked_list(head);

    printf("=== Testing count ===\n\n");

    int occurrences;

	printf("Calling count with list: ");
    print_linked_list(empty);
    printf("\nCounting 1's.\n");
	occurrences = count(empty, 1);
	printf("Expected result: 0\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(empty);
    printf("\nCounting 7's.\n");
	occurrences = count(empty, 7);
	printf("Expected result: 0\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 1's.\n");
	occurrences = count(head, 1);
	printf("Expected result: 2\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 2's.\n");
	occurrences = count(head, 2);
	printf("Expected result: 1\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 3's.\n");
	occurrences = count(head, 3);
	printf("Expected result: 2\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 4's.\n");
	occurrences = count(head, 4);
	printf("Expected result: 1\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 5's.\n");
	occurrences = count(head, 5);
	printf("Expected result: 3\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 7's.\n");
	occurrences = count(head, 7);
	printf("Expected result: 0\n");
    printf("Actual result: %d\n\n", occurrences);

    /* Tests for Exercise 2. */

    printf("=== Testing index ===\n\n");

    int posn;

	printf("Calling index with list: ");
    print_linked_list(empty);
    printf("\nSearching for 1.\n");
	posn = index(empty, 1);
	printf("Expected result: -1\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 1.\n");
	posn = index(head, 1);
	printf("Expected result: 0\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 2.\n");
	posn = index(head, 2);
	printf("Expected result: 2\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 3.\n");
	posn = index(head, 3);
	printf("Expected result: 3\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 4.\n");
	posn = index(head, 4);
	printf("Expected result: 5\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 5.\n");
	posn = index(head, 5);
	printf("Expected result: 6\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 7.\n");
	posn = index(head, 7);
	printf("Expected result: -1\n");
    printf("Actual result: %d\n\n", posn);

    /* Tests for Exercise 31. */

    printf("=== Testing fetch ===\n\n");

    /* We can't test these cases, because they should cause the function
     * to terminate via assert. 
     *
     * 1. The list is empty; terminate via assert.
     * 2. index < 0 or index >= # of nodes; terminate via assert.
     */

    int value;

	printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 0.\n");
	value = fetch(head, 0);
	printf("Expected result: 1\n");
    printf("Actual result: %d\n\n", value);

	printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 1.\n");
	value = fetch(head, 1);
	printf("Expected result: 1\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 2.\n");
	value = fetch(head, 2);
	printf("Expected result: 2\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 3.\n");
	value = fetch(head, 3);
	printf("Expected result: 3\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 4.\n");
	value = fetch(head, 4);
	printf("Expected result: 3\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 5.\n");
	value = fetch(head, 5);
	printf("Expected result: 4\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 6.\n");
	value = fetch(head, 6);
	printf("Expected result: 5\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 7.\n");
	value = fetch(head, 7);
	printf("Expected result: 5\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 8.\n");
	value = fetch(head, 8);
	printf("Expected result: 5\n");
    printf("Actual result: %d\n\n", value);

    /* Tests for Exercise 4. */

    printf("Building linked list 1 -> 2 -> 3 -> 4\n\n");

    IntNode *list = NULL;
    list = intnode_construct(4, list);
    list = intnode_construct(3, list);
    list = intnode_construct(2, list);
    list = intnode_construct(1, list);

    printf("=== Testing remove_last_one_pointer ===\n\n");

	printf("Calling remove_last_one_pointer with list: ");
    print_linked_list(list);
	list = remove_last_one_pointer(list);
	printf("\nExpected list: 1 -> 2 -> 3\n");
    printf("Actual list: ");
    print_linked_list(list);
	printf("\n\n");

	printf("Calling remove_last_one_pointer with list: ");
    print_linked_list(list);
	list = remove_last_one_pointer(list);
	printf("\nExpected list: 1 -> 2\n");
    printf("Actual list: ");
    print_linked_list(list);
	printf("\n\n");

	printf("Calling remove_last_one_pointer with list: ");
    print_linked_list(list);
	list = remove_last_one_pointer(list);
	printf("\nExpected list: 1\n");
    printf("Actual list: ");
    print_linked_list(list);
	printf("\n\n");

	printf("Calling remove_last_one_pointer with list: ");
    print_linked_list(list);
	list = remove_last_one_pointer(list);
	printf("\nExpected list: empty list\n");
    printf("Actual list: ");
    print_linked_list(list);
	printf("\n\n");
}
예제 #22
0
int main (void)
{
    system ("clear");
    node_t *head = NULL;
    int choice = 0;

    int n;

    while (TRUE)
    {
	print_menu();
	scanf("%d", &choice);

	switch(choice)
	{
	    case 1:
		head = insert_rear(head);
		view_list(head);
		break;
	    case 2:
		head = insert_front(head);
		view_list(head);
		break;
	    case 3:
		head = delete_rear(head);
		view_list(head);
		break;
	    case 4:
		head = delete_front(head);
		view_list(head);
		break;
	    case 5:
		print_reverse(head);
		printf("\n");
		break;
		//	    case 6: 
		//		link_sort1(head);
		//		view_list(head);
		//		break;
	    case 7:
		head = reverse_list_iterative(head);
		view_list(head);
		break;
	    case 8:
		head = reverse_list_recursive(head);
		view_list(head);
		break;
	    case 9:
		head = swap_alternate(head);
		view_list(head);
		break;
	    case 10:
		head = insert_sorted(head);
		view_list(head);
		break;
	    case 11:
		printf ("Enter the Value of n: ");
		scanf ("%d", &n);
		head = reverse_n_list(head, n);
		view_list(head);
		break;

	    case 100:
		view_list(head);
		break;
	    default : printf("Invalid Choice\n");
	}
    }
    return 0;
}