Thursday 24 October 2013

Lists on C++

Hey Guys!

I'm tempted to actually call my website Dummies using C++, but it may have been taken already by someone else smarter than me :P. However, all smart people started from scratch and were dummies as well at some point (that's my saying, it keeps me learning new tricks or things from others all the time).

I'm aware that I haven't posted in a while about OOP, many things in my personal life has changed and has "distracted" me of this super intensive subject, but using the time wisely during reading week at school, has allow me to share with you this subject on detail. I read the book twice, then went online to different sites and found this good guy who shows some codes really useful and clear to follow (http://www.youtube.com/user/PaulProgramming?feature=watch).

I have built and compiled the following code with lots of comments so you can easily follow it.

First my list.h:

#ifndef _LIST_H
#define _LIST_H

class List{
private:
   //typedef is a trick to abreviate the work, instead of doing: node *Name ... you simply type: nodeP (That's how I called it )
   typedef struct node
   {
      int data;
      node* next;

   }* nodeP; //after the curly brackets and *, you place the name of how you will call the copy of the struct
  
   nodeP head;
   nodeP curr;
   nodeP temp;

public:
   List();
   void AddNode(int addData);
   void DeleteNode(int delData);
   void PrintList();
};

#endif


THEN my list.cpp

#include "list.h"
#include <cstdlib>
#include <iostream>
using namespace std;

List::List()
{
   head = NULL;
   curr = NULL;
   temp = NULL;
}

void List::AddNode(int addData)
{
   nodeP n = new node; //new node pointer called n, which will be pointing the new created node
   n->next = NULL; //node at the end of the list should point to NULL
   n->data = addData; //filling it with new data
  
   //General case:
   if(head != NULL) //if head is pointing to a subject, we have a list intact
   {
      curr = head; //points to the froint point of the list
      while(curr->next != NULL) //Are we at the end of the list?
      {
         curr = curr->next; //we will point to the following pointer of the list
      }
      curr->next = n; //next element will point to n, N is our new Node pointer
   }
   else //if we dont have a list
   {
      head = n; //n will be the front
   }

}

void List::DeleteNode(int delData)
{
   nodeP delPtr = NULL;
   temp = head;
   curr = head;

    //the data that its on the node that the current node is pointing to is not equal to the data that we are passing
   while(curr != NULL && curr->data != delData)
   {
      temp = curr;
      curr = curr->next;
   }
   if(curr == NULL) //end of the list
   {
      cout << delData << " was not in the list \n" ;
      delete delPtr; //clean our memory
   }
   else
   {
      delPtr = curr;
      //following two lines patches the whole on the list
      curr = curr->next;
      temp->next = curr;
      if(delPtr == head)
      {
         head = head->next;
         temp = NULL;
      }
      //now we remove the node to be deleted
      delete delPtr;
      cout << "The value " << delData << " was deleted\n" ;
   }

}

void List::PrintList()
{
   curr = head;
   while(curr != NULL) //pointing somewhere on the list
   {
      cout << curr->data << endl ;//will output the data of the current element
      curr = curr->next;
   }
}

AND FINALLY my main.cpp to test the code:

#include "list.h"
#include <cstdlib>
using namespace std;

int main (int argc, char** argv)
{
   List Kyno;

   Kyno.AddNode(31);
   Kyno.AddNode(22);
   Kyno.AddNode(15);
  
   Kyno.PrintList();
  
   //Delete a random value
   Kyno.DeleteNode(22);
   Kyno.PrintList();

   //Delete a value that its not on the list
   Kyno.DeleteNode(8);
   Kyno.PrintList();

   //Delete the first value
   Kyno.DeleteNode(31);
   Kyno.PrintList();

   return 0;
}

IF You want to learn about Stack Lists besides the book, take a look at this video, it almost shows the step by step as it is on our book.  http://www.youtube.com/watch?v=KQ8qXi4sLj0

No comments:

Post a Comment