Thread: list function / test linked lists

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    3

    Question list function / test linked lists

    Hi, I need help with this task. I have difficulties coding the test_list_function to test each function to manipulate the list.
    I have to test with a processlist of Elements 'pid' from 0-4. The list has to be printed after each manipulation.

    Here my code:
    Code:
    #include <stdlib.h> 
    #include <stdio.h> 
    #include "dll.h" 
     
    struct proc_info { 
      struct list_head head; 
      int pid; 
      int counter; 
      int priority; 
    }; 
     
    /* initialize "shortcut links" for empty list */ 
    void 
    list_init(struct list_head *head) 
    { 
        head->next = head; 
        head->prev = head; 
        return (head); 
    } 
     
    /* insert new entry after the specified head */ 
    void  
    list_add(struct list_head *new, struct list_head *head) 
    { 
        new->prev = head; 
        new->next = head->next; 
        new->prev->next = new; 
        new->next->prev = new; 
    } 
     
    /* insert new entry before the specified head */ 
    void  
    list_add_tail(struct list_head *new, struct list_head *head) 
    { 
        new->next = head; 
        new->prev = head->prev; 
        new->prev->next = new; 
        new->next->prev = new; 
    } 
     
    /* deletes entry from list and reinitialize it, returns pointer to entry */ 
    struct list_head*  
    list_del(struct list_head *entry) 
    { 
        entry->prev->next = entry->next; 
        entry->next->prev = entry->prev; 
        entry->next = entry; 
        entry->prev = entry; 
    } 
     
    /* delete entry from one list and insert after the specified head */ 
    void  
    list_move(struct list_head *entry, struct list_head *head) 
    { 
        list_del (entry); 
        list_add (entry, head); 
    } 
     
    /* delete entry from one list and insert before the specified head */ 
    void  
    list_move_tail(struct list_head *entry, struct list_head *head) 
    { 
        list_del (entry); 
        list_add_tail (entry, head); 
    } 
     
    /* tests whether a list is empty */ 
    int  
    list_empty(struct list_head *head) 
    { 
        return (head->next == head); 
    } 
     
     
     
    void test_list_functions() 
    { 
      /* write test code for list functions */
    } 
     
    int 
    main() 
    { 
      test_list_functions(); 
      exit(0); 
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Perhaps you could start by fixing the errors such as

    > return (head);
    But the function is declared void.

    > /* deletes entry from list and reinitialize it, returns pointer to entry */
    > struct list_head*
    > list_del(struct list_head *entry)
    But it has no return statement.

    > I have difficulties coding the test_list_function to test each function to manipulate the list.
    Well not showing us you've at least made an attempt (ie, not left the function completely empty) would be a start.

    How about creating a new empty list and checking that list_empty() does indeed return true?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    3
    Thank you for your help! So I corrected the return statements.
    I actually didn't want to confuse you with my attempt cause it makes no sense at all, but I sure can post it now.
    The print_all function is obviously wrong, but I have no Idea how to print the list. Also I'm not sure how to fill the actual list with values so I got confused and this is what happend:

    Code:
    void test_list_functions() 
    { 
      /* write test code for list functions */ 
        /* Init new list */ 
        struct list_head *head = list_init(head); 
        struct list_head *pid = head; 
        int i; 
     
        /* fill with pit 1-4 */ 
        for(i = 0; i <= 4; i++){ 
            pid = list_init(i); 
        } 
        /* print list */ 
        print_all(head); 
        print_all(pid); 
     
        pid = list_add(pid, head);  
        print_all(pid); 
     
        pid = list_add_tail(pid, head); 
        print_all(pid); 
     
        pid = list_del(pid);  
        print_all(pid); 
     
        pid = list_move(pid, head); 
        print_all(pid); 
     
        pid = list_move_tail(pid, head); 
        print_all(pid); 
     
        pid = list_empty(head); 
        print_all(head); 
     
        return 0; 
    }
    and here is the part I corrected:
    Code:
    /* initialize "shortcut links" for empty list */ 
    void 
    list_init(struct list_head *head) 
    { 
        head->next = head; 
        head->prev = head; 
    }
    
    /* deletes entry from list and reinitialize it, returns pointer to entry */ 
    struct list_head*  
    list_del(struct list_head *entry) 
    { 
        entry->prev->next = entry->next; 
        entry->next->prev = entry->prev; 
        entry->next = entry; 
        entry->prev = entry;
        return(entry); 
    }
    Last edited by sonexa; 06-06-2016 at 11:08 AM.

  4. #4
    Registered User
    Join Date
    Jun 2016
    Posts
    3
    Please, just some help? I'm absolutely new to programming... I tried to solve it by myself put all the info I found on this topic doesn't help at all.

  5. #5
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    as far as I can see you are not allocating memory for your items. here is function to create an empty item
    Code:
    struct list_head* new_item(){
      return (list_head *) malloc(sizeof(list_head));
    }
    struct list_head*
    list_init(struct list_head *head) 
    
    { 
    
        head->next = head; 
    
        head->prev = head; 
        return head;
    
    }
    
    
    
    void test_list_functions() 
    
    { 
    
        struct list_head *head = list_init(new_item());     struct list_head *pid = head; 
    
        int i; 
    
      
    
        /* fill with pit 1-4 */
    
    //    for(i = 0; i <= 4; i++){ 
    
    //        pid = list_init(i); 
    
    //    } //this is wrong list_init 
    
        /* print list */
    
        print_all(head); 
        print_all(pid); 
      
        pid = list_add(pid, head);  
        print_all(pid); 
    
      
    
        pid = list_add_tail(pid, head); 
        print_all(pid); 
      
    
        pid = list_del(pid);  
    
        print_all(pid); 
    
      
    
        pid = list_move(pid, head); 
        print_all(pid); 
      
        pid = list_move_tail(pid, head); 
    
        print_all(pid); 
      
        pid = list_empty(head); 
        print_all(head); 
      
        return 0; 
    }
    The logic on the rest of the functions look ok if you ignore the fact that there is nowhere code to handle the data of the list only the item's links or any sanity checks but I only looked at your code for a few seconds. It would be useful on describing what you expect to happen and what actually happens for example you are using the list_init function in two different ways in one you pass a list item to be initialized in the other you pass an integer value to be used how exactly?
    Last edited by taazz; 06-06-2016 at 05:02 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    You should get rid of the pid variable.

    Everything should be
    head = function(head); // this function modifies the list
    or
    function(head); // this function doesn't modify the list
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked list... nitpick and test if possible
    By manasij7479 in forum C++ Programming
    Replies: 4
    Last Post: 10-09-2011, 03:33 AM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM
  4. linked list function test
    By mickle in forum C Programming
    Replies: 1
    Last Post: 02-13-2003, 01:26 AM
  5. A problem with my linked lists test.
    By valar_king in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:03 PM

Tags for this Thread