Click here to Skip to main content
14,767,355 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
The following function is a member function in singly linked list class. The function is called with the list containing the integers 1,2,3,4,5,6,7 in the given order. What will be the contents of the list after the function completes execution?

What I have tried:

struct nodeType{
   int info;
   node* next;
};

void doSomething ()
{
    nodeType *p,*q;
    int temp;
    if (head != NULL && head->next != NULL)
    {   
         p=head; 
         q=head->next;
         while(p!=NULL && q !=NULL)
         {
             temp=p->info;     
             p-> info =(q-> info);
             q-> info =temp-1;   
             p=q->next;   
             q=p->next;
         } // End of while loop
    } // End of if

}//End of doSomething function
Posted
Updated 26-Dec-20 19:16pm
v3
Comments
Richard MacCutchan 27-Dec-20 3:28am
   
There will not be any output, as that code will not even compile.

Not sure if that will even compile, it should fail here:
if (head != NULL && head->next != NULL)
What is head?

What the code (that you are trying to write) is doing is that it is traversing the linked list. You are trying to read until you find the end of the list:

p=head; 
q=head->next;
while(p!=NULL && q !=NULL)
This will work until p and q are not NULL. (Chances are that this will not print the "last element", because q = head->next; and that will make be null for the last element.)



Then what happens within the while loop, stays within the while loop. :doh: It is unclear to me why you would want to assign the q->next to p, and not to q itself? That is the purpose of this iteration.

See this for more information: Linked List | Set 1 (Introduction) - GeeksforGeeks[^]
   
Firstly, what is a node object? Your structure has a pointer to one called next.

Why can't you find out for yourself? You could put some print statements in there and find out fairly easily. A function to display the contents of your list could be very useful. Here's one possibility :
struct node
{
   int info;
   node* next;
};

void PrintList( node * pnode )
{
    while( pnode )
    {
        printf( "node at %08X : next is %08X, item is %d\n", pnode, pnode->next, item );
        pnode = pnode->next;
    }
}
that is pretty simple to do and will help you debug your code. If printf if not available then use a suitable output function. I don't write console programs so I have my own called trace.
   
Quote:
What will be the contents of the list after the function completes execution?

Should be obvious, there is an easy way : Run the code and print result !
Just a guess by reading the code: 2,0,4,2,6,4,7.
In case you want to see how it is done, break the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900