How do I insert a string into a linked list in c language?










-1














I am trying to insert a string userId if it doesn't already exist in the linked list. This is my code:



Node *insertLL(Node **ppHead, User user)

Node *pPrecedes;
Node *pNew;
Node *pNext;
Node *pFind;
int pHead;
Fillup fillup; //file with user information

//searches for the user
pFind= searchLL(*ppHead, fillup.szUserId, &pPrecedes);

if(pFind == NULL)

return allocateNode(user);

if (strcmp(pPrecedes->user.szUserId, pFind)!= 0) //pFind seems wrong
pNew->pNext = pPrecedes->pNext;
else
pNew->pNext = pPrecedes->pNext;
pPrecedes->pNext = pNew;


return pNew;










share|improve this question



















  • 3




    Not much point in having code after the return statement. It won't be executed.
    – stark
    Nov 13 '18 at 4:47






  • 1




    and the question is?
    – jwenting
    Nov 13 '18 at 5:06










  • Welcome to stackoverflow. Please take the tour, then read this: How to Ask and this: Minimal, Complete, and Verifiable example. And then edit your question, as it stands here it is imposisble to answer. All we can say now is: "there might be a bug somewhere in your code".
    – Jabberwocky
    Nov 13 '18 at 7:20
















-1














I am trying to insert a string userId if it doesn't already exist in the linked list. This is my code:



Node *insertLL(Node **ppHead, User user)

Node *pPrecedes;
Node *pNew;
Node *pNext;
Node *pFind;
int pHead;
Fillup fillup; //file with user information

//searches for the user
pFind= searchLL(*ppHead, fillup.szUserId, &pPrecedes);

if(pFind == NULL)

return allocateNode(user);

if (strcmp(pPrecedes->user.szUserId, pFind)!= 0) //pFind seems wrong
pNew->pNext = pPrecedes->pNext;
else
pNew->pNext = pPrecedes->pNext;
pPrecedes->pNext = pNew;


return pNew;










share|improve this question



















  • 3




    Not much point in having code after the return statement. It won't be executed.
    – stark
    Nov 13 '18 at 4:47






  • 1




    and the question is?
    – jwenting
    Nov 13 '18 at 5:06










  • Welcome to stackoverflow. Please take the tour, then read this: How to Ask and this: Minimal, Complete, and Verifiable example. And then edit your question, as it stands here it is imposisble to answer. All we can say now is: "there might be a bug somewhere in your code".
    – Jabberwocky
    Nov 13 '18 at 7:20














-1












-1








-1







I am trying to insert a string userId if it doesn't already exist in the linked list. This is my code:



Node *insertLL(Node **ppHead, User user)

Node *pPrecedes;
Node *pNew;
Node *pNext;
Node *pFind;
int pHead;
Fillup fillup; //file with user information

//searches for the user
pFind= searchLL(*ppHead, fillup.szUserId, &pPrecedes);

if(pFind == NULL)

return allocateNode(user);

if (strcmp(pPrecedes->user.szUserId, pFind)!= 0) //pFind seems wrong
pNew->pNext = pPrecedes->pNext;
else
pNew->pNext = pPrecedes->pNext;
pPrecedes->pNext = pNew;


return pNew;










share|improve this question















I am trying to insert a string userId if it doesn't already exist in the linked list. This is my code:



Node *insertLL(Node **ppHead, User user)

Node *pPrecedes;
Node *pNew;
Node *pNext;
Node *pFind;
int pHead;
Fillup fillup; //file with user information

//searches for the user
pFind= searchLL(*ppHead, fillup.szUserId, &pPrecedes);

if(pFind == NULL)

return allocateNode(user);

if (strcmp(pPrecedes->user.szUserId, pFind)!= 0) //pFind seems wrong
pNew->pNext = pPrecedes->pNext;
else
pNew->pNext = pPrecedes->pNext;
pPrecedes->pNext = pNew;


return pNew;







c string pointers linked-list nodes






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 4:49









Swordfish

8,96711335




8,96711335










asked Nov 13 '18 at 4:41









Su.she

1




1







  • 3




    Not much point in having code after the return statement. It won't be executed.
    – stark
    Nov 13 '18 at 4:47






  • 1




    and the question is?
    – jwenting
    Nov 13 '18 at 5:06










  • Welcome to stackoverflow. Please take the tour, then read this: How to Ask and this: Minimal, Complete, and Verifiable example. And then edit your question, as it stands here it is imposisble to answer. All we can say now is: "there might be a bug somewhere in your code".
    – Jabberwocky
    Nov 13 '18 at 7:20













  • 3




    Not much point in having code after the return statement. It won't be executed.
    – stark
    Nov 13 '18 at 4:47






  • 1




    and the question is?
    – jwenting
    Nov 13 '18 at 5:06










  • Welcome to stackoverflow. Please take the tour, then read this: How to Ask and this: Minimal, Complete, and Verifiable example. And then edit your question, as it stands here it is imposisble to answer. All we can say now is: "there might be a bug somewhere in your code".
    – Jabberwocky
    Nov 13 '18 at 7:20








3




3




Not much point in having code after the return statement. It won't be executed.
– stark
Nov 13 '18 at 4:47




Not much point in having code after the return statement. It won't be executed.
– stark
Nov 13 '18 at 4:47




1




1




and the question is?
– jwenting
Nov 13 '18 at 5:06




and the question is?
– jwenting
Nov 13 '18 at 5:06












Welcome to stackoverflow. Please take the tour, then read this: How to Ask and this: Minimal, Complete, and Verifiable example. And then edit your question, as it stands here it is imposisble to answer. All we can say now is: "there might be a bug somewhere in your code".
– Jabberwocky
Nov 13 '18 at 7:20





Welcome to stackoverflow. Please take the tour, then read this: How to Ask and this: Minimal, Complete, and Verifiable example. And then edit your question, as it stands here it is imposisble to answer. All we can say now is: "there might be a bug somewhere in your code".
– Jabberwocky
Nov 13 '18 at 7:20













3 Answers
3






active

oldest

votes


















0














things wrong with the posted code that are certainly not helping



if(pFind == NULL) 

return allocateNode(user); <<< == nothing will be executed after this line

if (strcmp(pPrecedes->user.szUserId, pFind)!= 0) //pFind seems wrong
pNew->pNext = pPrecedes->pNext;
else
pNew->pNext = pPrecedes->pNext;
pPrecedes->pNext = pNew; <<<<< == should this line be dependant on the else above. The indentation says yes but there are no braces




and yes I know, I will get down voted cos this is 'not an answer' - whatever, I will take the hit just to see if I can help out






share|improve this answer




















  • I am just glad that you took the time to reply!
    – Su.she
    Nov 14 '18 at 6:02


















0














I have figured out the correct code for this!!!I should have mentioned this but I had another function searchLL that already compared and searched for the user so strcmp was unnecessary. I had to make 2 new if-statements. One where if the string already exists it just returns that string. The other in case the string has to be inserted at the beginning where it doesn't have a preceding string. I also had to open up some space for my new string.
Node *insertLL(Node **ppHead, User user)



Node *pPrecedes;
Node *pNew;
Node *pNext;
Node *pFind;

//searches the user //searchLL is a differen't function
pFind= searchLL(*ppHead, user.szUserId, &pPrecedes);

if (pFind!= NULL) //if pFind already exists
return pFind;

pNew=allocateNode(user);//get space for pNew

if(pFind == NULL) //if pFind doesn't exist insert pFind

//insert at the beginning
if(pPrecedes == NULL) //if there is no pPrecedes

pNew->pNext = *ppHead; //pNew's next will be the head
*ppHead = pNew; //pNew is now the new head

//insert in the middle
else

pNew->pNext = pPrecedes->pNext;
pPrecedes->pNext = pNew;


return pNew;







share|improve this answer




























    -1














    In line 19 (If i counted correctly) else has no but has 2 lines of code. Second statement is being executed even if strcmp is correct, change it and see if it works.






    share|improve this answer






















    • Please add more detail to your answer.
      – Cheche
      Nov 13 '18 at 17:19










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273930%2fhow-do-i-insert-a-string-into-a-linked-list-in-c-language%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    things wrong with the posted code that are certainly not helping



    if(pFind == NULL) 

    return allocateNode(user); <<< == nothing will be executed after this line

    if (strcmp(pPrecedes->user.szUserId, pFind)!= 0) //pFind seems wrong
    pNew->pNext = pPrecedes->pNext;
    else
    pNew->pNext = pPrecedes->pNext;
    pPrecedes->pNext = pNew; <<<<< == should this line be dependant on the else above. The indentation says yes but there are no braces




    and yes I know, I will get down voted cos this is 'not an answer' - whatever, I will take the hit just to see if I can help out






    share|improve this answer




















    • I am just glad that you took the time to reply!
      – Su.she
      Nov 14 '18 at 6:02















    0














    things wrong with the posted code that are certainly not helping



    if(pFind == NULL) 

    return allocateNode(user); <<< == nothing will be executed after this line

    if (strcmp(pPrecedes->user.szUserId, pFind)!= 0) //pFind seems wrong
    pNew->pNext = pPrecedes->pNext;
    else
    pNew->pNext = pPrecedes->pNext;
    pPrecedes->pNext = pNew; <<<<< == should this line be dependant on the else above. The indentation says yes but there are no braces




    and yes I know, I will get down voted cos this is 'not an answer' - whatever, I will take the hit just to see if I can help out






    share|improve this answer




















    • I am just glad that you took the time to reply!
      – Su.she
      Nov 14 '18 at 6:02













    0












    0








    0






    things wrong with the posted code that are certainly not helping



    if(pFind == NULL) 

    return allocateNode(user); <<< == nothing will be executed after this line

    if (strcmp(pPrecedes->user.szUserId, pFind)!= 0) //pFind seems wrong
    pNew->pNext = pPrecedes->pNext;
    else
    pNew->pNext = pPrecedes->pNext;
    pPrecedes->pNext = pNew; <<<<< == should this line be dependant on the else above. The indentation says yes but there are no braces




    and yes I know, I will get down voted cos this is 'not an answer' - whatever, I will take the hit just to see if I can help out






    share|improve this answer












    things wrong with the posted code that are certainly not helping



    if(pFind == NULL) 

    return allocateNode(user); <<< == nothing will be executed after this line

    if (strcmp(pPrecedes->user.szUserId, pFind)!= 0) //pFind seems wrong
    pNew->pNext = pPrecedes->pNext;
    else
    pNew->pNext = pPrecedes->pNext;
    pPrecedes->pNext = pNew; <<<<< == should this line be dependant on the else above. The indentation says yes but there are no braces




    and yes I know, I will get down voted cos this is 'not an answer' - whatever, I will take the hit just to see if I can help out







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 18:12









    pm100

    25k1557104




    25k1557104











    • I am just glad that you took the time to reply!
      – Su.she
      Nov 14 '18 at 6:02
















    • I am just glad that you took the time to reply!
      – Su.she
      Nov 14 '18 at 6:02















    I am just glad that you took the time to reply!
    – Su.she
    Nov 14 '18 at 6:02




    I am just glad that you took the time to reply!
    – Su.she
    Nov 14 '18 at 6:02













    0














    I have figured out the correct code for this!!!I should have mentioned this but I had another function searchLL that already compared and searched for the user so strcmp was unnecessary. I had to make 2 new if-statements. One where if the string already exists it just returns that string. The other in case the string has to be inserted at the beginning where it doesn't have a preceding string. I also had to open up some space for my new string.
    Node *insertLL(Node **ppHead, User user)



    Node *pPrecedes;
    Node *pNew;
    Node *pNext;
    Node *pFind;

    //searches the user //searchLL is a differen't function
    pFind= searchLL(*ppHead, user.szUserId, &pPrecedes);

    if (pFind!= NULL) //if pFind already exists
    return pFind;

    pNew=allocateNode(user);//get space for pNew

    if(pFind == NULL) //if pFind doesn't exist insert pFind

    //insert at the beginning
    if(pPrecedes == NULL) //if there is no pPrecedes

    pNew->pNext = *ppHead; //pNew's next will be the head
    *ppHead = pNew; //pNew is now the new head

    //insert in the middle
    else

    pNew->pNext = pPrecedes->pNext;
    pPrecedes->pNext = pNew;


    return pNew;







    share|improve this answer

























      0














      I have figured out the correct code for this!!!I should have mentioned this but I had another function searchLL that already compared and searched for the user so strcmp was unnecessary. I had to make 2 new if-statements. One where if the string already exists it just returns that string. The other in case the string has to be inserted at the beginning where it doesn't have a preceding string. I also had to open up some space for my new string.
      Node *insertLL(Node **ppHead, User user)



      Node *pPrecedes;
      Node *pNew;
      Node *pNext;
      Node *pFind;

      //searches the user //searchLL is a differen't function
      pFind= searchLL(*ppHead, user.szUserId, &pPrecedes);

      if (pFind!= NULL) //if pFind already exists
      return pFind;

      pNew=allocateNode(user);//get space for pNew

      if(pFind == NULL) //if pFind doesn't exist insert pFind

      //insert at the beginning
      if(pPrecedes == NULL) //if there is no pPrecedes

      pNew->pNext = *ppHead; //pNew's next will be the head
      *ppHead = pNew; //pNew is now the new head

      //insert in the middle
      else

      pNew->pNext = pPrecedes->pNext;
      pPrecedes->pNext = pNew;


      return pNew;







      share|improve this answer























        0












        0








        0






        I have figured out the correct code for this!!!I should have mentioned this but I had another function searchLL that already compared and searched for the user so strcmp was unnecessary. I had to make 2 new if-statements. One where if the string already exists it just returns that string. The other in case the string has to be inserted at the beginning where it doesn't have a preceding string. I also had to open up some space for my new string.
        Node *insertLL(Node **ppHead, User user)



        Node *pPrecedes;
        Node *pNew;
        Node *pNext;
        Node *pFind;

        //searches the user //searchLL is a differen't function
        pFind= searchLL(*ppHead, user.szUserId, &pPrecedes);

        if (pFind!= NULL) //if pFind already exists
        return pFind;

        pNew=allocateNode(user);//get space for pNew

        if(pFind == NULL) //if pFind doesn't exist insert pFind

        //insert at the beginning
        if(pPrecedes == NULL) //if there is no pPrecedes

        pNew->pNext = *ppHead; //pNew's next will be the head
        *ppHead = pNew; //pNew is now the new head

        //insert in the middle
        else

        pNew->pNext = pPrecedes->pNext;
        pPrecedes->pNext = pNew;


        return pNew;







        share|improve this answer












        I have figured out the correct code for this!!!I should have mentioned this but I had another function searchLL that already compared and searched for the user so strcmp was unnecessary. I had to make 2 new if-statements. One where if the string already exists it just returns that string. The other in case the string has to be inserted at the beginning where it doesn't have a preceding string. I also had to open up some space for my new string.
        Node *insertLL(Node **ppHead, User user)



        Node *pPrecedes;
        Node *pNew;
        Node *pNext;
        Node *pFind;

        //searches the user //searchLL is a differen't function
        pFind= searchLL(*ppHead, user.szUserId, &pPrecedes);

        if (pFind!= NULL) //if pFind already exists
        return pFind;

        pNew=allocateNode(user);//get space for pNew

        if(pFind == NULL) //if pFind doesn't exist insert pFind

        //insert at the beginning
        if(pPrecedes == NULL) //if there is no pPrecedes

        pNew->pNext = *ppHead; //pNew's next will be the head
        *ppHead = pNew; //pNew is now the new head

        //insert in the middle
        else

        pNew->pNext = pPrecedes->pNext;
        pPrecedes->pNext = pNew;


        return pNew;








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 6:14









        Su.she

        1




        1





















            -1














            In line 19 (If i counted correctly) else has no but has 2 lines of code. Second statement is being executed even if strcmp is correct, change it and see if it works.






            share|improve this answer






















            • Please add more detail to your answer.
              – Cheche
              Nov 13 '18 at 17:19















            -1














            In line 19 (If i counted correctly) else has no but has 2 lines of code. Second statement is being executed even if strcmp is correct, change it and see if it works.






            share|improve this answer






















            • Please add more detail to your answer.
              – Cheche
              Nov 13 '18 at 17:19













            -1












            -1








            -1






            In line 19 (If i counted correctly) else has no but has 2 lines of code. Second statement is being executed even if strcmp is correct, change it and see if it works.






            share|improve this answer














            In line 19 (If i counted correctly) else has no but has 2 lines of code. Second statement is being executed even if strcmp is correct, change it and see if it works.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 18:01









            Cheche

            848218




            848218










            answered Nov 13 '18 at 17:08









            Kralhex Notex

            1




            1











            • Please add more detail to your answer.
              – Cheche
              Nov 13 '18 at 17:19
















            • Please add more detail to your answer.
              – Cheche
              Nov 13 '18 at 17:19















            Please add more detail to your answer.
            – Cheche
            Nov 13 '18 at 17:19




            Please add more detail to your answer.
            – Cheche
            Nov 13 '18 at 17:19

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273930%2fhow-do-i-insert-a-string-into-a-linked-list-in-c-language%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            2013 DreamHack Counter-Strike: Global Offensive Championship