Learn Computer Science programming - C# and C++ Algorithms

 
Linked List C++
This is a simple Data Structure that create a linked list of nodes and point one to the other from head to tail. It  consists of a sequence of data records such that in each record there is a field that contains a reference to another node.
Picture
Code
linkedlist.zip
File Size: 2634 kb
File Type: zip
Download File

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


void LinkList::m_addItem (int d)
{
Link* newItem = new Link;
newItem->p_next = first;
newItem->i_data = d;
first = newItem;
}


Link *LinkList::m_FindElement (int value)
{
Link *myLink = first;

while (myLink)
{
wcout << L"I am in Element " << myLink->i_data  << endl;
if (myLink->i_data  == value)
{
return myLink;
}
myLink = myLink->p_next;
}
// No element found
delete (myLink);
return NULL;
}


void LinkList::m_display()
{
Link *current = first;
while (current)
{
//Output data for the current link
wcout << L"Current Link data: "  << current->i_data;


//Identify and comment if this is the first link - for clarity only
if (current->i_data == first->i_data )
{
wcout << L" -- FIRST" << endl;
}
else
{
wcout << endl ;
}


wcout << L"Current Link Address " << current->p_next << endl;
current = current->p_next;
}
}


void LinkList::m_reverse()
{
Link* current = first;
    first = NULL;


    while (current) {
        Link* save = current;
        current = current->p_next;
        save->p_next = first;
        first = save;
}
// *** Method 2 *** //
  //Link* p1, *p2, *p3;
  //  
  //   if (first == NULL ) return; // empty
  //   if (first->p_next == NULL) return; // single Link
  //   
  //   p1 = first;
  //   p2 = p1->p_next;
  //   p1->p_next = NULL; // make the first as the last
  //   while (p2) {
  //         p3 = p2->p_next;
  //         p2->p_next = p1;
  //         p1 = p2;
  //         p2 = p3;
  //   }
  //   first = p1;
}
void LinkList::m_removeItem (Link *deleteMe)
{
Link* currentToRemove = first;
// If head is to be deleted
if (first == deleteMe)
{
first = first->p_next ;
free(deleteMe);
}
//If deleting other nodes ..
while (currentToRemove)
{
if (currentToRemove->p_next  == deleteMe)
{
currentToRemove->p_next = deleteMe->p_next;
free(deleteMe);
}
currentToRemove = currentToRemove->p_next;
}
}


Link *LinkList::m_GetItem (int d)
{
Link *currentLink = first;
while (currentLink)
{
if (currentLink->i_data == d)
            return currentLink;
currentLink= currentLink->p_next;
}
}
void LinkList::removeFirst() 
{
Link *deleteMe = first; //Create a new link - Make it equal to first
    first = first->p_next; // Assign the link to Element 2 to be designated as first
deleteMe = NULL; // Delete newly created element
}


void LinkList::m_addToBegining (int d)
{
Link *newItemBegin = new Link;
newItemBegin->i_data = d;
newItemBegin->p_next = first;
first = newItemBegin;
}


void LinkList::m_addToEnd (int d)
{
Link *newItemEnd = new Link; // Create a new item of type Link
newItemEnd->i_data = d; // Assign the desired value to it
newItemEnd->p_next = NULL; // Assign it's link to NULL since it will be the last element

Link *currentTemp = first; // Create a temporary element called current and give it the value of first
while (currentTemp->p_next)
{
currentTemp = currentTemp->p_next;
}


currentTemp->p_next = newItemEnd;
}