Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with LinkList in C++ Programming:

Status
Not open for further replies.

bhattaroshan

Member level 1
Joined
Sep 28, 2010
Messages
33
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
1,531
When i write this it works fine

Code:
#include<iostream>
#include<conio.h>
#include<stdio.h>

using namespace std;

typedef struct stack
{
        int num;
        stack *lpnext;
};

stack *head=0;

void push_me_back(int num)
{
     stack *tmp,*tmp2;
     
      tmp=new stack;
      tmp->num=num;
      tmp->lpnext=0;
       
     if(head==0)
      {
       head=tmp;
      }
     else
      {
       tmp2=head;
       
       while(tmp2->lpnext)
        tmp2=tmp2->lpnext;

       tmp2->lpnext=tmp;
      }
}

int main()
{
    push_me_back(10);
    push_me_back(20);
    push_me_back(30);
    
    cout<<head->lpnext->num;
    
    getch();
    return 0;
}

But when i make slight change in code as follows code is not working what is the difference. Is there anything difference in the code:

Code:
#include<iostream>
#include<conio.h>
#include<stdio.h>

using namespace std;

typedef struct stack
{
        int num;
        stack *lpnext;
};

stack *head=0;

void push_me_back(int num)
{
     stack *tmp,*tmp2;
     
      tmp=new stack;
      tmp->num=num;
      tmp->lpnext=0;
       
     if(head==0)
      {
       head=tmp;
      }
     else
      {
       tmp2=head->lpnext; //made changes here
       
       while(tmp2)  //made changes here
        tmp2=tmp2->lpnext;

       tmp2=tmp; //made changes here
      }
}

int main()
{
    push_me_back(10);
    push_me_back(20);
    push_me_back(30);
    
    cout<<head->lpnext->num;
    
    getch();
    return 0;
}
 

Code:
Code:
#include<iostream>
#include<conio.h>
#include<stdio.h>

using namespace std;

typedef struct stack
{
        int num;
        stack *lpnext;
};

stack *head=0;

void push_me_back(int num)
{
     stack *tmp,*tmp2;
     
      tmp=new stack;
      tmp->num=num;
      tmp->lpnext=0;
       
     if(head==0)
      {
       head=tmp;
      }
     else
      {
       tmp2=head->lpnext; //made changes here   [COLOR="#FFD700"]since the pointer has null stored in it it flags an error[/COLOR]
       
       while(tmp2)  //made changes here[COLOR="#FF0000"]there is null in tmp2 and so the segment is omitted [/COLOR]
        tmp2=tmp2->lpnext;

       tmp2=tmp; //made changes here[COLOR="#FF0000"]as the above conditions failed this is also flagged as an error [/COLOR]
      }
}

int main()
{
    push_me_back(10);
    push_me_back(20);
    push_me_back(30);
    
    cout<<head->lpnext->num;
    
    getch();
    return 0;
}

my corrections are in colour hope it helps
 

thanks for your reply

if so then why does the first code that i posted runs without an error
even tmp2->lpnext is null pointer.......
 

there you have defined the pointer

there is no memory allocated to the pointer in your program
that is the reason for the error to be flagged

remember that pointer gets defined only when memory is assigned to it
 
Let me write another example exactly as above but with pointer to integer.

Code:
#include<iostream>
#include<conio.h>
#include<stdio.h>

using namespace std;

int main()
{
    int *b,*a,*c;
    
    c=new int;
    
    b=NULL; //it is not pointed any where
    
    a=b;  //according to you this is wrong
    
    while(a); //this is also wrong according to you
    
    a=c; //this is also wrong 
    
    *a=10; //but all these works well
    
    getch();
    return 0;
}

If what you told is correct why does this code run well?

I might not be understanding linklist? If you have any tutorials or site, or anything please help me out.

Thanks for you reply.
 

try out a book by NV publications for data structures and stuff like that

also go for pearson publication's book they are worth trying


the difference in these coding is simple int is a default data type

whereas you are using a abstract datatype in list that makes a huge difference in the applications of them in the programs
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top