How to create a single linklist in C++
#include<iostream.h>
#include<conio.h>
class node{
private:
int value,size;
node *newnode,*currentnode,*prevnode,*headnode,*lastnode,*nextnode;
public:
node();
void add(int);
int get()
{
return value;
}
void set(int value)
{
this->value=value;
}
node *getnext()
{
return nextnode;
}
void setnext(node *nextnode)
{
this->nextnode=nextnode;
}
node *getprev()
{
return prevnode;
}
void setprev(node *prevnode)
{
this->prevnode=prevnode;
}
};
node::node()
{
headnode=new node();
headnode->setnext(NULL);
currentnode=NULL;
size=0;
}
void node::add(int n)
{
node *newnode=new node();
newnode->set(n);
if(currentnode!=NULL)
{
newnode->setnext(currentnode->getnext());
currentnode->setnext(newnode);
lastnode=currentnode;
currentnode=newnode;
}
else
{
newnode->setnext(NULL);
headnode->setnext(newnode);
lastnode=headnode;
currentnode=newnode;
}size++;
}
void main()
{
node obj;
obj.add(10);
obj.add(20);
getch();
}
#include<conio.h>
class node{
private:
int value,size;
node *newnode,*currentnode,*prevnode,*headnode,*lastnode,*nextnode;
public:
node();
void add(int);
int get()
{
return value;
}
void set(int value)
{
this->value=value;
}
node *getnext()
{
return nextnode;
}
void setnext(node *nextnode)
{
this->nextnode=nextnode;
}
node *getprev()
{
return prevnode;
}
void setprev(node *prevnode)
{
this->prevnode=prevnode;
}
};
node::node()
{
headnode=new node();
headnode->setnext(NULL);
currentnode=NULL;
size=0;
}
void node::add(int n)
{
node *newnode=new node();
newnode->set(n);
if(currentnode!=NULL)
{
newnode->setnext(currentnode->getnext());
currentnode->setnext(newnode);
lastnode=currentnode;
currentnode=newnode;
}
else
{
newnode->setnext(NULL);
headnode->setnext(newnode);
lastnode=headnode;
currentnode=newnode;
}size++;
}
void main()
{
node obj;
obj.add(10);
obj.add(20);
getch();
}
Post a Comment