// First_VB_Prog.cpp : Defines the entry point for the console application.
//
#include <iostream>
usingnamespace std;
//Simple Linked List composing of a "Body" , "Head" and a "Tail"
struct nodeType
{
int info;
nodeType *link;
};
class linkedList
{
public:
linkedList()
{
Head = NULL;
}
nodeType* addHead()
{
Head = new nodeType;
return Head;
}
private:
nodeType *Head;
};
int main()
{
//declaring Nodes:
nodeType *Body;
Body = new nodeType;
nodeType *upperBody;
upperBody = new nodeType;
nodeType *Tail;
Tail = new nodeType;
//Linking Nodes
linkedList listt;
Body->info = 2; //accessing info on Body Node
listt.addHead()->link = Body; //linking OR joing the head to the body Node
Body->link = upperBody; //linking OR joing the body to the TailNode
lowerBody->info = 89;
lowerBody->link = Tail; //linking OR joing the body to the lowerBody
Tail->link= NULL; //accessing info on Tail Node
cout<<listt.addHead()->link->info;
}
why doesnt "listt.addHead()->link->info" this section of code print out the "info" value of the Body Node of which is :2?
Because you're not printing out the "info" value of the Body node. You're printing out the "info" value of the newly allocated node returned by addHead, which, incidentally, leaks memory and makes no attempt to preserve any existing list structure.
addHead is not a method a linked list should have.
// First_VB_Prog.cpp : Defines the entry point for the console application.
//
#include <iostream>
usingnamespace std;
//Simple Linked List composing of a "Body" , "Head" and a "Tail"
struct nodeType
{
int info;
nodeType *link;
};
int main()
{
//declaring Nodes:
nodeType *Head;
Head = new nodeType;
Head->info = 0;
nodeType *Body;
Body = new nodeType;
Head ->link = Body;
nodeType *upperBody;
upperBody = new nodeType;
nodeType *Tail;
Tail = new nodeType;
//Linking Nodes
//linkedList listt;
Body->info = 2; //accessing info on Body Node
//listt.addHead()->link = Body; //linking OR joing the head to the body Node
Body->link = upperBody; //linking OR joing the body to the TailNode
upperBody->info = 89;
upperBody->link = Tail; //linking OR joing the body to the TailNode
Tail->info = NULL; //accessing info on Tail Node
cout<<Head->link->info;
}