Please Help Linked list problems!

Discussion in 'C Programming' started by RBCC, Jan 20, 2011.

  1. RBCC

    RBCC Guest

    Please Help!

    I am using this structure :

    typedef struct Sname
    {
    char *pSzfirst;
    char *pSzLast;
    struct name *Snext;
    }

    person* FindMin (person *pSHead)
    {
    person *pSMin;
    person *pSCurr;

    pSMin=pSHead;
    pSCurr=pSHead;

    while (pScurr!= NULL)
    {
    if (strcmp(pScurr->pszfirst,pSMin->pszfirst)<0)
    {
    pSMin=pSCurr;

    }

    pSCurr=pScurr->Snext;
    }

    return psMin;
    } //Error:Expected a declaration! What can I do to make this work?

    Could somebody please write a piece of code that will walk the list?
    Thank you, John
    --
    comp.lang.c.moderated - moderation address: -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.
    RBCC, Jan 20, 2011
    #1
    1. Advertising

  2. RBCC

    osmium Guest

    "RBCC" wrote:

    > Please Help!
    >
    > I am using this structure :
    >
    > typedef struct Sname
    > {
    > char *pSzfirst;
    > char *pSzLast;
    > struct name *Snext;
    > }
    >
    > person* FindMin (person *pSHead)
    > {
    > person *pSMin;
    > person *pSCurr;
    >
    > pSMin=pSHead;
    > pSCurr=pSHead;
    >
    > while (pScurr!= NULL)
    > {
    > if (strcmp(pScurr->pszfirst,pSMin->pszfirst)<0)
    > {
    > pSMin=pSCurr;
    >
    > }
    >
    > pSCurr=pScurr->Snext;
    > }
    >
    > return psMin;
    > } //Error:Expected a declaration! What can I do to make this work?
    >
    > Could somebody please write a piece of code that will walk the list?


    When all else fails, try being honest. You are trying to con us in to
    believing you have a linked list which needs a "few final touches". Provide
    a *program* (not an extract) that shows some promise as to becoming a linked
    list. Then you will likely get some help in walking the list.

    I suggest you write a print function to aid in debugging, you may well
    discard it when the thing is working. There is no sin in writing code that
    is only used for debugging.
    osmium, Jan 20, 2011
    #2
    1. Advertising

  3. On Jan 20, 6:00 pm, RBCC <> wrote:
    ....
    > I am using this structure :
    >
    > typedef struct Sname
    > {
    >   char *pSzfirst;
    >   char *pSzLast;
    >  struct name *Snext;
    >
    > }

    ....
    > } //Error:Expected a declaration!  What can I do to make this work?


    You have a typing error, it should be "struct Sname *Snext;" instead
    of "struct name *Snext;". Therefore when you type struct name the
    compiler is expecting to declare the new structure type.
    --
    comp.lang.c.moderated - moderation address: -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.
    Cristian-Matei Toader, Jan 24, 2011
    #3
  4. RBCC

    Eric Sosman Guest

    On 1/20/2011 1:00 PM, RBCC wrote:
    > Please Help!
    >
    > I am using this structure :
    >
    > typedef struct Sname
    > {
    > char *pSzfirst;
    > char *pSzLast;
    > struct name *Snext;


    This is perfectly all right, but almost certainly not what
    you intended. I strongly suspect that you mean `struct Sname'
    here, or else that you meant `struct name' above. I also strongly
    suspect that the code you have shown us is not the code that's
    giving you trouble; it follows that our diagnoses and suggestions
    may be wide of the (hidden) mark, and if so, that you have no one
    but yourself to blame for our inaccuracies.

    > }


    Where is the identifier the `typedef' declares? And where is
    the semicolon that concludes the declaration? I'm going to proceed
    on the assumption that `person;' comes after the `}', and if my
    assumption is wrong -- well, again, it's your fault.

    > person* FindMin (person *pSHead)
    > {
    > person *pSMin;
    > person *pSCurr;
    >
    > pSMin=pSHead;
    > pSCurr=pSHead;
    >
    > while (pScurr!= NULL)
    > {
    > if (strcmp(pScurr->pszfirst,pSMin->pszfirst)<0)


    This shakes my faith in my assumption that `struct Sname' and
    `person' are the same thing. The problem is that `struct Sname' has
    no element named `pszfirst', so the compiler would have complained.
    But perhaps `person' is something entirely different, something that
    *does* have a `pszfirst' element? Perhaps. But my bet is that you
    are showing us something other than your actual code.

    > {
    > pSMin=pSCurr;
    >
    > }
    >
    > pSCurr=pScurr->Snext;


    This is the basis of my guess that `person' is an alias for
    `struct Sname'. If it were not, the compiler would complain here --
    unless both `person' *and* `struct Sname' have an element named
    `Snext', which is possible but not especially plausible.

    > }
    >
    > return psMin;


    What is `psMin'? You've shown us a `pSMin', but that's not the
    same thing at all.

    > } //Error:Expected a declaration! What can I do to make this work?
    >
    > Could somebody please write a piece of code that will walk the list?


    I could certainly write code that will walk *a* list, but since
    I don't see solid evidence in yours of the existence of anything one
    might call *the* list, then no: I cannot write the code. If you need
    assistance, please try again -- with the *exact* and *complete* code
    that's giving you trouble, and not with an inaccurate paraphrase.

    --
    Eric Sosman
    lid
    --
    comp.lang.c.moderated - moderation address: -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.
    Eric Sosman, Jan 24, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrej Hocevar
    Replies:
    1
    Views:
    480
    Nick Austin
    Aug 18, 2003
  2. fool
    Replies:
    14
    Views:
    504
    Barry Schwarz
    Jul 3, 2006
  3. Replies:
    6
    Views:
    318
    Amedeo Verita
    Dec 7, 2005
  4. joshd
    Replies:
    12
    Views:
    666
    John Carson
    Oct 2, 2006
  5. RBCC

    Please Help Linked list problems!

    RBCC, Jan 20, 2011, in forum: C Programming
    Replies:
    0
    Views:
    252
Loading...

Share This Page