Thread: Question about linked lists.

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    11

    Question Question about linked lists.

    Hi everyone,
    I'm studying for my final exam and I am stuck on a review question.

    The question is as follows:

    Consider this code fragment and answer the questions, which follow.

    NODE *p1, *p2,*p3;
    NODE *p, *pFirst, *pNew, *pTemp;

    Question:
    Write the code to allocate memory for nodes at p1, p2, and p3. Do not declare any new variables. Assume that your code will appear after the above fragment, and write only the necessary statements.

    This is what I have come up with. Let me know if I am right or where I went wrong.

    pFirst = NULL;


    pNew = new NODE;
    pNew->link = NULL;
    if (pFirst == NULL)
    {
    pFirst = pNew;
    pTemp = pNew;
    }
    else
    {
    pTemp->link = pNew;
    pTemp = pNode;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Write the code to allocate memory for nodes at p1, p2, and p3.
    All this question asks you to do is allocate memory for those three pointers:
    Code:
    try {
      p1 = new NODE;
      p2 = new NODE;
      p3 = new NODE;
    }
    catch ( bad_alloc ) {
      cerr<<"Memory allocation failure\n";
    }
    Done.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Question about linked lists.

    Originally posted by cheeisme123
    Code:
    pFirst = NULL;
    
    if(pFirst == NULL)
    {
       ...
    }
    Why check if it's NULL in this case where it's always NULL?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    11
    Write additional statements to make the nodes at p1, p2, p3 into a forrward-linked list, such that p3 is the first node, p1 is the second, and p2 is last. Assign the address of the first node to pFirst.

    How does this sound:

    p3->link=pFirst;
    p3->link->link=p1;
    p1->link=p2;

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    11

    Another linked question

    I'm studying for my final exam and I am stuck on a review question.

    The question is as follows:

    Consider this code fragment and answer the questions, which follow.

    NODE *p1, *p2,*p3;
    NODE *p, *pFirst, *pNew, *pTemp;

    try {
    p1 = new NODE;
    p2 = new NODE;
    p3 = new NODE;
    }
    catch ( bad_alloc ) {
    cerr<<"Memory allocation failure\n";
    }


    QUESTION:

    Write additional statements to make the nodes at p1, p2, p3 into a forrward-linked list, such that p3 is the first node, p1 is the second, and p2 is last. Assign the address of the first node to pFirst.

    How does this sound:

    p3->link=pFirst;
    p3->link->link=p1;
    p1->link=p2;

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Write additional statements to make the nodes at p1, p2, p3 into a forrward-linked list, such that p3 is the first node, p1 is the second, and p2 is last.
    Code:
    p3->next = p1; // p3 points to p1
    p1->next = p2; // p1 points to p2
    >Assign the address of the first node to pFirst.
    Code:
    pFirst = p3; // pFirst points to the start, p3
    -Prelude
    My best code is written with the delete key.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How does this sound:
    It sounds like you should keep the same questions to a single thread. Double posting is rude, please don't do it.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding comparing two linked lists
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2007, 04:28 AM
  2. Linked Lists Question
    By panfilero in forum C Programming
    Replies: 4
    Last Post: 11-22-2005, 01:33 AM
  3. Linked Lists Question
    By SlyMaelstrom in forum C++ Programming
    Replies: 12
    Last Post: 11-12-2005, 12:03 PM
  4. eof in fstream & singly linked lists
    By mazo in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2002, 09:50 AM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM