hey, i came across this program linked linear list about nodes and all and i m literaaly flustered @ understanding it(only the code in LLLIST.CPP----i mention below)....specially with the POINTERS...
SO PLZ CAN ANYONE EXPLAIN ME THE WORKING OF THE CODE IN LLLIST.CPP, THAT I HAVE MENTIONED BELOW:icon_cool:....
MAYBE I NEED THE UNDERSTANDING OF POINTERS:icon_lol:
JUST NEED THE UNDERSTANDING OF LLLIST.CPP AND IN THAT JUST NEED THE UNDERSTANDING OF THE WORKING OF ADD FUNCTION AND REST I WILL UNDERSTAND...
//NODE.CPP
#define NULL 0
class Node
{
private:
int info;
Node *next;
public:
Node()
{
next=NULL;
}
Node(int x)
{
info=x;
next=NULL;
}
void setInfo(int info)
{
this->info=info;
}
int getInfo()
{
return info;
}
void setNext(Node *next)
{
this->next=next;
}
Node *getNext()
{
return next;
}
};
////////////////////LLLIST.CPP
class LList
{
private:
Node *head;
public:
LList()
{
head=NULL;
}
void addInfo(int x)
{
Node *n = new Node(); n->setInfo(x);
if(head==NULL)
{
head=n;
return;
}
Node *c=head;
while(c->getNext() !=NULL)
c=c->getNext();
c->setNext(n);
}
int removeFirst()
{
if(!isEmpty())
{
Node * c=head;
head=head->getNext();
int i=c->getInfo();
delete c;
return i;
}
}
bool removeInfo(int x)
{
if(head==NULL)
return false;//empty list
if(head->getInfo()==x)
{
head=head->getNext();
}
Node *c=head->getNext(), *p=head;
while((c->getInfo()!=x)&&(c->getNext()!=NULL))
{
p=c;
c=c->getNext();
}
if(c->getInfo()==x)
{
p->setNext(c->getNext());
delete c;
return true;
}
else
return false;
}
bool isEmpty()
{
if (head==NULL)
return true;
else
return false;
}
};
////////////////LLMAIN.CPP
#include <iostream>
using namespace std;
void main()
{
LList l;
int x;
char yesno='y';
while(true)
{
cout<<"Want to add info(integer value) to list(y/n) ? ";
cin>>yesno;
if((yesno=='y')||(yesno=='Y'))
{
cout<<"\n Input info : ";
cin>>x;
l.addInfo(x);
}
else
break;
}
while(!l.isEmpty())
cout<<l.removeFirst()<<" ";
}
AGAIN>>>>>>>>>>>JUST NEED THE UNDERSTANDING OF LLLIST.CPP AND IN THAT JUST NEED THE WORKING OF ADD FUNCTION AND REST I WILL UNDERSTAND...
void addInfo(int x)
{
Node *n = new Node();
n->setInfo(x);
if(head==NULL)
{
head=n;
return;
}
Node *c=head;
while(c->getNext() !=NULL)
c=c->getNext();
c->setNext(n);
}
SPECIALLY THIS LOOP....HOW DOES THIS LOOP COMPUTE FORMING A CHAIN OF LINKS
while(c->getNext() !=NULL)
c=c->getNext();