Sunday, August 7, 2016

C++ List

List is almost the same as array, but array is limited and list is not (well except for the memory limitation, but you rarely used that much)

Defining Node

to create list it is necessary to define a node first..
lets say a note is a storage for everything you want to contain in a list, in this sample I use a single integer to be contained there.
struct node {
  node* next;
  int value;
};

Defining List

and then next we define the list with some basic function
this is the definition
class list {
private:
  node* root = NULL;
  void add(int value, node* p);
public:
  // add value to the last part
  void add(int value);
  // print values
  void print();
};
and this is the implementation
void list::add(int value, node * p)
{
  if (p->next != NULL) {
    add(value, p->next);
  }
  else {
    node* newNode = new node;
    newNode->value = value;
    newNode->next = NULL;
    p->next = newNode;
  }
}

void list::add(int value)
{
  if (root == NULL) {
    node* newNode = new node;
    newNode->value = value;
    newNode->next = NULL;
    root = newNode;
  }
  else {
    add(value, root);
  }
}

void list::print()
{
  if (root == NULL) {
    cout << "Empty List" << endl;
  } else {
    node* temp = root;
    while (temp != NULL) {
      cout << temp->value << " ";
      temp = temp->next;
    }
    cout << endl;
  }
}
and this is how to test it
int main() {
  list* l = new list();
  l->print();

  l->add(1);
  l->add(2);
  l->add(3);
  l->print();

  l->add(4);
  l->add(5);
  l->print();

  int temp;
  cin >> temp;
}
the result will be as follow

Complete Codes

and finally here's the complete codes
#include < stdio .h="" >
#include < iostream >

using namespace std;

struct node {
 node* next;
 int value;
};

class list {
private:
 node* root = NULL;
 void add(int value, node* p);
public:
 // add value to the last part
 void add(int value);
 // print values
 void print();
};

void list::add(int value, node * p)
{
 if (p->next != NULL) {
  add(value, p->next);
 }
 else {
  node* newNode = new node;
  newNode->value = value;
  newNode->next = NULL;
  p->next = newNode;
 }
}

void list::add(int value)
{
 if (root == NULL) {
  node* newNode = new node;
  newNode->value = value;
  newNode->next = NULL;
  root = newNode;
 }
 else {
  add(value, root);
 }

}

void list::print()
{
 if (root == NULL) {
  cout << "Empty List" << endl;
 } else {
  node* temp = root;
  while (temp != NULL) {
   cout << temp->value << " ";
   temp = temp->next;
  }
  cout << endl;
 }
}

int main() {
 list* l = new list();
 l->print();

 l->add(1);
 l->add(2);
 l->add(3);

 l->print();

 l->add(4);
 l->add(5);

 l->print();

 int temp;
 cin >> temp;
}

Implement Get Function


Next we will try to implement get function, which purpose is to retrieve value from a certain index
The we define 2 function, the private one is the recursive part, while the public one is the function that activates the recursive part
here's the definition
class list {
private:
 node* root = NULL;
 void add(int value, node* p);
 int get(int index, int count, node* p);
public:
 // add value to the last part
 void add(int value);
 // print values
 void print();
 // get values in position (index)
 int get(int index);
};
and here's the implementation
// the recursive part
int list::get(int index, int count, node * p)
{
 if (index == count) {
  return p->value;
 }
 else {
  if (p->next == NULL)
   throw std::out_of_range("index out of range");
  else
   return get(index, count + 1, p->next);
 }
}
// and the public function
int list::get(int index)
{
 try {
  return get(index, 0, root);
 }
 catch (exception ex) {
  throw ex;
 }
}

Complete Code (Again) plus the test part

#include <stdio .h="" >
#include <iostream >
using namespace std;

struct node {
 node* next;
 int value;
};

class list {
private:
 node* root = NULL;
 void add(int value, node* p);
 int get(int index, int count, node* p);
public:
 // add value to the last part
 void add(int value);
 // print values
 void print();
 // get values in position (index)
 int get(int index);
};

void list::add(int value, node * p)
{
 if (p->next != NULL) {
  add(value, p->next);
 }
 else {
  node* newNode = new node;
  newNode->value = value;
  newNode->next = NULL;
  p->next = newNode;
 }
}

int list::get(int index, int count, node * p)
{
 if (index == count) {
  return p->value;
 }
 else {
  if (p->next == NULL)
   throw std::out_of_range("index out of range");
  else
   return get(index, count + 1, p->next);
 }
}

void list::add(int value)
{
 if (root == NULL) {
  node* newNode = new node;
  newNode->value = value;
  newNode->next = NULL;
  root = newNode;
 }
 else {
  add(value, root);
 }

}

void list::print()
{
 if (root == NULL) {
  cout << "Empty List" << endl;
 } else {
  node* temp = root;
  while (temp != NULL) {
   cout << temp->value << " ";
   temp = temp->next;
  }
  cout << endl;
 }
}

int list::get(int index)
{
 try {
  return get(index, 0, root);
 }
 catch (exception ex) {
  throw ex;
 }
}

int main() {
 list* l = new list();
 l->print();

 l->add(0);
 l->add(1);
 l->add(2);
 l->add(3);
 
 cout << "get index 2 :" << l->get(2) << endl;

 l->print();

 l->add(4);
 cout << "get index 1 :" << l->get(1) << endl;
 cout << "get index 0 :" << l->get(0) << endl;
 l->add(5);
 try {
  l->get(10);
 }
 catch (exception &ex) {
  cout << "error getting index 10 : " << ex.what() << endl;
 }
 l->print();

 int temp;
 cin >> temp;
}
And the result will be as follows

No comments :

Post a Comment