Thread: [HELP] Circular list find item

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    9

    [HELP] Circular list find item

    Hello,

    I have a circular list with items added to it and I want to create a function to go through the list to check if an item exists.

    Printing my list function

    Code:
    void show_int( any d ){
        printf("%3i", *(int *)d);
    }
    void show_list(clist * cl, char * caption) {
        any d;
        printf("\nCAPTION: \"%s\"",caption);
        printf("\n clist_size: %3i", clist_size(cl));
        printf("\n clist_isempty: %3i", clist_isempty(cl));
        printf("\n clist_cursor_inlist: %3i", clist_cursor_inlist(cl));
        printf("\n item at cursor: ");
        d = clist_get_item(cl);
        if (d==NULL)
        printf(" NULL");
        else
        show_int( d );
        printf("\n clist_print: \n");
        clist_print(cl, &show_int);
        NEWLINE
    }

    Main code
    Code:
    int a;    clist *xs;
        int i, d[MAX_INTS];
        for (i=0;i<MAX_INTS;i++) d[i]=i;
        xs = new_clist();
        
        clist_ins_before( xs, &d[1] );
        clist_ins_before( xs, &d[2] );
        clist_ins_before( xs, &d[3] );
        clist_ins_before( xs, &d[4] );
        clist_ins_before( xs, &d[5] );
        clist_ins_before( xs, &d[6] );
        NEWLINE
        show_list(xs, "Test"); // print out the empty list
        NEWLINE

    I have included "clists.c" where it calls the functions in my main.

    I have functions that finds if any of the items in the list are even numbers but I want to be able to call it like this

    clist_find(xs, 10); and then it will find any occurrences of 10 in the list.


    Here is the code for clist_find - It takes in a pointer to my clist and the function "even"

    Code:
    int clist_find(clist *c, pred p){
        assert(c!=NULL);
        clist_goto_head(c);
        while (clist_cursor_inlist(c) && (!p(c->cursor->item)))
            clist_goto_next(c);
        return clist_cursor_inlist(c);    
    }

    I've been trying to figure this out for hours

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Without you posting a complete compilable version of the program, there isn't much to say about your program (except the NEWLINE macro you like is truly awful).

    Who knows where the problem is, maybe the list isn't as circular as you think.
    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
    Dec 2014
    Posts
    9
    Quote Originally Posted by Salem View Post
    Without you posting a complete compilable version of the program, there isn't much to say about your program (except the NEWLINE macro you like is truly awful).

    Who knows where the problem is, maybe the list isn't as circular as you think.
    This is code created by my university tutor so the newline feature wasn't created by me. I could include the whole program but I didn't think someone would go through the effort of compiling the clists Library on their computer and then compiling the main.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    And if the problem isn't self-evident from what you've posted, what are you going to do?

    > I could include the whole program but I didn't think someone would go through the effort of compiling the clists Library on their computer and then compiling the main.
    Most bugs are found by actually running the code in a debugger, waiting for it to catch a crash and then inspecting the wreckage.
    Or by stepping through the code to find out where/how (for example), your search function fails to match what is expected.

    Maybe you're not iterating over the list properly.
    Maybe your even() function is broken.
    Maybe ....

    A peephole at some of the code which has nothing to do with where the real problem(s) are isn't going to help.
    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.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    What is "any" in the show_int function header?

  6. #6
    Registered User
    Join Date
    Dec 2014
    Posts
    9
    Quote Originally Posted by Salem View Post
    And if the problem isn't self-evident from what you've posted, what are you going to do?

    > I could include the whole program but I didn't think someone would go through the effort of compiling the clists Library on their computer and then compiling the main.
    Most bugs are found by actually running the code in a debugger, waiting for it to catch a crash and then inspecting the wreckage.
    Or by stepping through the code to find out where/how (for example), your search function fails to match what is expected.

    Maybe you're not iterating over the list properly.
    Maybe your even() function is broken.
    Maybe ....

    A peephole at some of the code which has nothing to do with where the real problem(s) are isn't going to help.

    Sorry for not going into too much detail, I was on StackOverflow and they didn't like me posting big lines of code.

    I'm very bad at C Programming at the moment but I know this code works because it has been used for all my other work. The even function does work as it picks the first number out in the list that is even, that is the sort of behavior I am wanting to do but instead of finding even numbers I want to pass a number in like so - clist_find(xs, 10); to find any occurrence of 10. I have an iterate function but like before it iterates and alters the list as it goes through instead of searching for a number.

    I can't pin point the problem i'm having being I don't actually know how to make the function as of yet, I think I am 50% there.


    Here is the library that contains all of the functions for the list -
    // Linked implementation of an circular list with a sentinel node // Author: dr - Pastebin.com


    Also here is the full program that I am running, if you look at the bottom you can see the search function I want to call.

    Code:
    #include <stdlib.h>#include <stdio.h>
    #include "any.h"
    #include "clist.h"
    #define MAX_INTS 100
    #define NEWLINE printf("\n");
    
    
    /*************************************************************************************
    * Functions that operate on references to int values
    ************************************************************************************/
    int even_int( any d ) // even(d) ?
    {
        return ((*(int*)d));
    }
    void double_int( any d ) // d *= 2
    {
        *(int *)d = *(int *)d * 2;
    }
    /*************************************************************************************
    * Functions to display the contents of a list
    ************************************************************************************/
    void show_int( any d )
    {
        printf("%3i", *(int *)d);
    }
    void show_list(clist * cl, char * caption) {
        any d;
        printf("\nCAPTION: \"%s\"",caption);
        printf("\n clist_size: %3i", clist_size(cl));
        printf("\n clist_isempty: %3i", clist_isempty(cl));
        printf("\n clist_cursor_inlist: %3i", clist_cursor_inlist(cl));
        printf("\n item at cursor: ");
        d = clist_get_item(cl);
        if (d==NULL)
        printf(" NULL");
        else
        show_int( d );
        printf("\n clist_print: \n");
        clist_print(cl, &show_int);
        NEWLINE
    }
    
    
    
    
    
    
    int main()
    {
    	int a;
    	clist *xs;
    	int i, d[MAX_INTS];
        for (i=0;i<MAX_INTS;i++) d[i]=i;
        xs = new_clist();
    	
    	clist_ins_before( xs, &d[1] );
    	clist_ins_before( xs, &d[2] );
    	clist_ins_before( xs, &d[3] );
    	clist_ins_before( xs, &d[4] );
    	clist_ins_before( xs, &d[5] );
    	clist_ins_before( xs, &d[6] );
    	clist_search(xs, 5);
    	
    	NEWLINE
    
    
    	
    	
    }

  7. #7
    Registered User
    Join Date
    Dec 2014
    Posts
    9
    Quote Originally Posted by gemera View Post
    What is "any" in the show_int function header?
    I do believe it means it will take any type?

    #ifndef ANY_H
    #define ANY_H


    typedef void * any;


    #endif

  8. #8
    Registered User
    Join Date
    Dec 2014
    Posts
    9
    Quote Originally Posted by Salem View Post
    And if the problem isn't self-evident from what you've posted, what are you going to do?

    > I could include the whole program but I didn't think someone would go through the effort of compiling the clists Library on their computer and then compiling the main.
    Most bugs are found by actually running the code in a debugger, waiting for it to catch a crash and then inspecting the wreckage.
    Or by stepping through the code to find out where/how (for example), your search function fails to match what is expected.

    Maybe you're not iterating over the list properly.
    Maybe your even() function is broken.
    Maybe ....

    A peephole at some of the code which has nothing to do with where the real problem(s) are isn't going to help.

    I have made my own function that iterates through the list but instead of printing back the the values I believe its printing the memory address?

    Code:
    int clist_search(clist *c){
    	
    	assert(c!=NULL);
        struct node * n = c->cursor;
        clist_goto_head(c);
        while (clist_cursor_inlist(c))
        {
            printf("%i\n",(c->cursor->item));
            clist_goto_next(c);
        }
        c->cursor = n;
    	
    	
    	
    }


    OUTPUT :
    Code:
    319250292
    319250296
    319250300
    319250304
    319250308
    319250312

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You are correct, it is printing addresses.
    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. Help on circular list!
    By flexo87 in forum C Programming
    Replies: 2
    Last Post: 01-30-2009, 02:35 PM
  2. Replies: 4
    Last Post: 04-25-2008, 02:37 AM
  3. Find the zero-based postition of an item in array
    By Joelito in forum C Programming
    Replies: 2
    Last Post: 12-03-2006, 08:03 PM
  4. circular linked->list help
    By The Brain in forum C++ Programming
    Replies: 8
    Last Post: 10-21-2004, 11:12 AM
  5. Again, Circular Linked list ???
    By yescha in forum C Programming
    Replies: 2
    Last Post: 11-16-2001, 08:35 PM