Thread: Size of Linked List

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    4

    Question Size of Linked List

    Code:
    struct _Link
    {
         int x;
         int y;
         struct _Link *next;
    };
    typedef struct _Link Link;
    
    struct _LinkList
    {
         int size;
         Link *head;
         Link *tail;
    };
    typedef struct _LinkList LinkList;
    
    struct _Stack
    {
        LinkList *l;
    };
    typedef struct _Stack Stack;
    
    
    Stack* stack_init()
    {
        Stack *s;
        LinkList *l;
    
        s = (Stack*)malloc(sizeof(Stack));
        s->l = (LinkList*)malloc(sizeof(LinkList));
    
        s->l->size =0;  
        s->l->head = NULL;
        s->l->tail = NULL;
    
        return s;
    }
    
    
    void stack_check()
    {
       Stack *s;
    
    
       s = stack_init();
    
       if(s==0)
          printf("correct");
    }
    This is part of my project, I'm trying to get the size of the stack (which in this case should be 0) after initialization. After exiting the function, it returns an arbitrary value. Howcome?
    Note, the linked list has already been initialized.
    Any help will be greatly appreciated.
    Last edited by Pana; 09-04-2013 at 02:06 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Can you post a bit more code, please?

    Welcome to the forum.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The stack_init function returns the pointer to your new stack.
    You don't attempt to "get the size" in the code you posted.
    And you should be checking the that the return value from malloc is not NULL.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I've no idea, you need to provide a runnable test case which fails for you.

    Code:
    int main ( ) {
      Stack *s = stack_init();
      printf("Len=%d\n", s->l->size);
      return 0;  
    }
    I just added this and it works as expected.
    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
    Sep 2013
    Posts
    4
    Thanks

    I included more code.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > s = stack_init();
    > if(s==0)
    This isn't checking the size of your stack, it's checking whether you have a stack at all.

    Check my code.
    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.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The code you added does -pretty much- what oogabooga said. Take a look at Salem's post, where he actually checks the size.

    Are you sure you are ok with structs, or do you want a little revision in the very basics of structs?

    Oh and a piece of advice. It is better some times to post your new code in your current post, not editing the old ones, because if the thread is big, people get easily lost. Aliens not. :P
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    4
    Thanks, that makes sense.

    I coded stack_init, stack_check was given to us, but I kept getting incorrect answer.

    std10093- thanks, I will look it. Do you have anything on pointers? And thanks for the advice, much appreciated.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are welcome.

    Functions in C (very short) can provide some info in pointers, but I do not have something that is devoted to pointers. You can see the link in my signature, if you want to search yourself.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Would a global variable help?

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    That's the easy solution. And the when something is way too easy, it means that there is a tradeoff hiding back there.

    When you have a small program of about 200 lines it's ok to use a global variable. You know that this is a global variable, etc.
    When you start writing some bigger projects, you will see that even you get fooled by what it is a global variable and which variable does it shadows, etc.

    Imagine what will happen, when your projects are read/modified by at least two people. You will protect the others from ruining the code, you will save time and you will avoid chaos.

    One can easily say: "Well, ok, let me use global variables now and when the time for BIG projects comes up, I will stop using them.".
    Let me give you an analogy to this:
    Assume we have a runner, who when practicing, runs with his back turned to the direction he goes. Imagine how runners run and rotate that man by π (180 degrees), but let him run to the same direction.
    His coach asks him, why do you do such thing? It's easier for me, because it's fun.
    The coach tells him, that in the finals, he will have to run normally, like all the runners. And the runner says, well ok, let me run normally when the the finals come up.
    When the finals come up, I do not think he is going to be ready for this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Idk, I like globals for counts. Just modify it when you add or remove an element. Doesn't seem that bad or hard

    Another solution is to comtain an index for each element in the list, the front being 0, the next being 1...

  13. #13
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Nevermind, the OP actually seems to have a pretty good soulution or approach as is.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MutantJohn
    Idk, I like globals for counts. Just modify it when you add or remove an element. Doesn't seem that bad or hard
    What if you have multiple linked lists? You won't be able to reuse the structure and its associated functions because they are tied to the notion of a single linked list with a global count.

    Quote Originally Posted by MutantJohn
    Another solution is to comtain an index for each element in the list, the front being 0, the next being 1...
    This has a disadvantage in that adding or removing an element in the middle of the linked list would no longer be a constant time operation since the elements after the insertion/removal point must be re-indexed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    No, I realized those thinfs after posting.

    The OP approach was something I had in mind but thought it might be too much, Idk why.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Linked List Size Limitation?
    By j0seph in forum C Programming
    Replies: 6
    Last Post: 06-27-2005, 09:36 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM