Link list using C++ improved version 2021
Link list using C++ improved version 2021
source code:
#include<iostream>
using namespace std;
class Node
{
private:
int object;
Node * nextNode;//address of next node
public:
int get() { return object; };
void set(int object) { this->object = object; };
Node * getNext() { return nextNode; };
void setNext(Node * nextNode) { this->nextNode = nextNode; };
};
////////////////////list class//////////////////
/* The List class */
class List
{
public://methods or function
//declare the functions
List(); //constructor
void add (int addObject); //add new object
int get(); //get node
bool next();
friend void traverse(List list);
friend List addNodes(); //add new node
////
// position currentNode and lastCurrentNode at first element
void start()
{ ////headNode currentNode lastCurrentNode
lastCurrentNode = headNode;
currentNode = headNode;
}
////////////
void remove()
{
if( currentNode != NULL && currentNode != headNode)
{
lastCurrentNode->setNext(currentNode->getNext()); //(step 1)
delete currentNode; //(step 2)
currentNode = lastCurrentNode;//(step 3)
size--; //(step 4)
}
}
////
// returns the size of the list
int length()
{
return size;
}
private: //data members
int size;
Node * headNode;
Node * currentNode;
Node * lastCurrentNode;
};//end of class
/* Constructor */
//define the function: work
List::List()
{
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
lastCurrentNode = NULL;
size = 0;
}
/* add() class method */
void List::add (int addObject)
{
Node * newNode = new Node();
newNode->set(addObject);
if( currentNode != NULL )
{
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode );
lastCurrentNode = currentNode;
currentNode = newNode;
}
else //when currentNode == NULL
{newNode->setNext(NULL);
headNode->setNext(newNode);
lastCurrentNode = headNode;
currentNode = newNode;
}
size ++;
}
/* get() class method */
int List::get()
{
if (currentNode != NULL)
return currentNode->get();
}
/* next() class method */
bool List::next()
{
if (currentNode == NULL) return false;
lastCurrentNode = currentNode;
currentNode = currentNode->getNext();
if (currentNode == NULL || size == 0)
return false;
else
return true;
}
/* Friend function to traverse linked list */
void traverse(List list)
{ //show the elements
Node* savedCurrentNode = list.currentNode;
list.currentNode = list.headNode;
for(int i = 1; list.next(); i++)
{
cout << "\n Lists Element " << i << " :" << list.get();
}
list.currentNode = savedCurrentNode;
}
/* Friend function to add Nodes into the list */
List addNodes()
{
List list; //create a simple object
list.add(2);
list.add(6);
list.add(8);
list.add(7);
list.add(1);
//List *p;//pointer type object
//p->add(5);
cout << "\n List size = " << list.size <<'\n';
return list;
}
main()
{
// List list = addNodes();
//traverse(list);
cout << "\n ******** Link List C++ Example ********" <<'\n';
List list; // creating a list object
// adding values to the list
list.add(10);
list.add(11);
list.add(12);
list.add(13);
list.add(14);
list.add(15);
list.add(16);
// calling the start method of the list
list.start();
// printing all the elements of the list
while (list.next())
cout << "Elements of list container : "<< list.get()<<endl;
cout << "\n ********** By Hafiz Muhammad Umar Hayat ******** : "<< list.get()<<endl;
system("pause");
}
****************Result****************

Comments
Post a Comment