Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct node{
int data;
struct node* next;
};
struct node* head;
void insert(int data,int n)
{
 node* temp1=new node();
 temp1->data= data;
 temp1->next=NULL;
 if (n==1){
    temp1->next=head;
    head=temp1;
    return;
 }
 node* temp2=head;
 for(int i=0;i<n-2;i++){
    temp2=temp2->next;
}
temp1->next=temp2->next;
temp2->next= temp1;
};
void print(){
node* temp=head;
while(temp!= NULL)
    cout<<temp->data<<"\n";
}
int main()
{
 head=NULL;
 insert(2,1);
 insert(7,2);
 insert(8,1);
 print();
}


What I have tried:

C++
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct node{
int data;
struct node* next;
};
struct node* head;
void insert(int data,int n)
{
 node* temp1=new node();
 temp1->data= data;
 temp1->next=NULL;
 if (n==1){
    temp1->next=head;
    head=temp1;
    return;
 }
 node* temp2=head;
 for(int i=0;i<n-2;i++){
    temp2=temp2->next;
}
temp1->next=temp2->next;
temp2->next= temp1;
};
void print(){
node* temp=head;
while(temp!= NULL)
    cout<<temp->data<<"\n";
}
int main()
{
 head=NULL;
 insert(2,1);
 insert(7,2);
 insert(8,1);
 print();
}
Posted
Updated 21-Jul-20 2:50am

Think about your print code
void print(){
node* temp=head;
while(temp!= NULL)
    cout<<temp->data<<"\n";
}
.. after you've printed a node, do you think you might want to advance to the next node (hint)
void print(){
node* temp=head;
while(temp!= NULL)
    cout<<temp->data<<"\n";
    temp = ??????????
}
 
Share this answer
 
v2
Comments
Critical Fist 21-Jul-20 7:49am    
hey thank you buddy!!!!
OriginalGriff 21-Jul-20 7:51am    
Umm ... curly brackets? :laugh:
Garth J Lancaster 21-Jul-20 9:40am    
:rollseyes:
Look at your print method:
C++
void print(){
    node* temp=head;
    while(temp!= NULL)
        cout<<temp->data<<"\n";
}
You don't move the temp node pointer inside the loop ...
 
Share this answer
 
Comments
Critical Fist 21-Jul-20 7:49am    
hey thank you buddy!!!!!
OriginalGriff 21-Jul-20 7:52am    
You're welcome!

I'd strongly suggest that you start looking at the debugger for your system - it can make finding problems like this almost trivial.
Critical Fist 21-Jul-20 8:36am    
i might well debugg on my own so i can improve my skills and then look for a debugger.
OriginalGriff 21-Jul-20 8:54am    
No, learn to use the debugger - it gives you information on what is happening in your app while it's running, including variable contents and being able to execute you code step by step.

It's not a replacement for "manual debugging" - it's a tool which makes manual debugging a lot more effective.

For example, running that code through the debugger would have shown you that temp wasn't changing very quickly. Quicker in fact than posting a question here and waiting for an answer!
Critical Fist 21-Jul-20 8:59am    
does it come along with complier or should i download it manually
Quote:
Why am I getting 8s repeatedly where I should be getting 827 as output for the following code?

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.
 
Share this answer
 
Comments
Critical Fist 21-Jul-20 8:58am    
kindoff wanted to debugg myself to understand how computer behaves
ok guys i got to know of my mistake and hope people will learn from this. we should add the while statments inside brackets.
 
Share this answer
 

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