As an update to previous code previous code I've submitted:
Improvements this time are mainly having everything moved to a class, before I plough on and add more functionality (I've come back to C++ after a few years away and this is a revision exercise).
#include <iostream>
#include "LinkedList.h"
class LinkedList
{
public:
LinkedList(void);
~LinkedList(void);
void traverseList();
int length();
void push(int data);
private:
struct node{
int data;
node *pointee;
};
node *head;
node *last;
int count;
};
LinkedList::LinkedList(void)
{
head = new node();
last = new node();
head->data = 0;
head->pointee = last;
last->data = 0;
last->data = NULL;
count = 2;
}
LinkedList::~LinkedList(void)
{
}
/*
To be replaced with an iterator and an overloaded print operator for output
*/
void LinkedList::traverseList(){
for(node *iterator = LinkedList::head ; iterator ; iterator = iterator->pointee)
{
std::cout << iterator->data << std::endl;
}
}
int LinkedList::length(){
return LinkedList::count;
}
void LinkedList::push(int data){
node *newNode = new node();
newNode->data = data;
newNode->pointee = NULL;
last = newNode;
++count;
}
int main(){
LinkedList list;
for(int i=1 ; i<4 ; i++)
{
list.push(i);
}
int length = list.length();
std::cout << length << std::endl;
list.traverseList();
return 0;
}
As an update to previous code I've submitted:
Improvements this time are mainly having everything moved to a class, before I plough on and add more functionality (I've come back to C++ after a few years away and this is a revision exercise).
#include <iostream>
#include "LinkedList.h"
class LinkedList
{
public:
LinkedList(void);
~LinkedList(void);
void traverseList();
int length();
void push(int data);
private:
struct node{
int data;
node *pointee;
};
node *head;
node *last;
int count;
};
LinkedList::LinkedList(void)
{
head = new node();
last = new node();
head->data = 0;
head->pointee = last;
last->data = 0;
last->data = NULL;
count = 2;
}
LinkedList::~LinkedList(void)
{
}
/*
To be replaced with an iterator and an overloaded print operator for output
*/
void LinkedList::traverseList(){
for(node *iterator = LinkedList::head ; iterator ; iterator = iterator->pointee)
{
std::cout << iterator->data << std::endl;
}
}
int LinkedList::length(){
return LinkedList::count;
}
void LinkedList::push(int data){
node *newNode = new node();
newNode->data = data;
newNode->pointee = NULL;
last = newNode;
++count;
}
int main(){
LinkedList list;
for(int i=1 ; i<4 ; i++)
{
list.push(i);
}
int length = list.length();
std::cout << length << std::endl;
list.traverseList();
return 0;
}
As an update to previous code I've submitted:
Improvements this time are mainly having everything moved to a class, before I plough on and add more functionality (I've come back to C++ after a few years away and this is a revision exercise).
#include <iostream>
#include "LinkedList.h"
class LinkedList
{
public:
LinkedList(void);
~LinkedList(void);
void traverseList();
int length();
void push(int data);
private:
struct node{
int data;
node *pointee;
};
node *head;
node *last;
int count;
};
LinkedList::LinkedList(void)
{
head = new node();
last = new node();
head->data = 0;
head->pointee = last;
last->data = 0;
last->data = NULL;
count = 2;
}
LinkedList::~LinkedList(void)
{
}
/*
To be replaced with an iterator and an overloaded print operator for output
*/
void LinkedList::traverseList(){
for(node *iterator = LinkedList::head ; iterator ; iterator = iterator->pointee)
{
std::cout << iterator->data << std::endl;
}
}
int LinkedList::length(){
return LinkedList::count;
}
void LinkedList::push(int data){
node *newNode = new node();
newNode->data = data;
newNode->pointee = NULL;
last = newNode;
++count;
}
int main(){
LinkedList list;
for(int i=1 ; i<4 ; i++)
{
list.push(i);
}
int length = list.length();
std::cout << length << std::endl;
list.traverseList();
return 0;
}
C++ Linked Listlist implemented as classes, not imperative code
As an update to previous code I've submitted:
Improvements this time are mainly having everything moved to a class, before I plough on and add more functionality (I've come back to C++ after a few years away and this is a revision excerciseexercise).
#include <iostream>
#include "LinkedList.h"
class LinkedList
{
public:
LinkedList(void);
~LinkedList(void);
void traverseList();
int length();
void push(int data);
private:
struct node{
int data;
node *pointee;
};
node *head;
node *last;
int count;
};
LinkedList::LinkedList(void)
{
head = new node();
last = new node();
head->data = 0;
head->pointee = last;
last->data = 0;
last->data = NULL;
count = 2;
}
LinkedList::~LinkedList(void)
{
}
/*
To be replaced with an iterator and an overloaded print operator for output
*/
void LinkedList::traverseList(){
for(node *iterator = LinkedList::head ; iterator ; iterator = iterator->pointee)
{
std::cout << iterator->data << std::endl;
}
}
int LinkedList::length(){
return LinkedList::count;
}
void LinkedList::push(int data){
node *newNode = new node();
newNode->data = data;
newNode->pointee = NULL;
last = newNode;
++count;
}
int main(){
LinkedList list;
for(int i=1 ; i<4 ; i++)
{
list.push(i);
}
int length = list.length();
std::cout << length << std::endl;
list.traverseList();
return 0;
}
C++ Linked List implemented as classes, not imperative code
As an update to previous code I've submitted:
Improvements this time are mainly having everything moved to a class, before I plough on and add more functionality (I've come back to C++ after a few years away and this is a revision excercise)
#include <iostream>
#include "LinkedList.h"
class LinkedList
{
public:
LinkedList(void);
~LinkedList(void);
void traverseList();
int length();
void push(int data);
private:
struct node{
int data;
node *pointee;
};
node *head;
node *last;
int count;
};
LinkedList::LinkedList(void)
{
head = new node();
last = new node();
head->data = 0;
head->pointee = last;
last->data = 0;
last->data = NULL;
count = 2;
}
LinkedList::~LinkedList(void)
{
}
/*
To be replaced with an iterator and an overloaded print operator for output
*/
void LinkedList::traverseList(){
for(node *iterator = LinkedList::head ; iterator ; iterator = iterator->pointee)
{
std::cout << iterator->data << std::endl;
}
}
int LinkedList::length(){
return LinkedList::count;
}
void LinkedList::push(int data){
node *newNode = new node();
newNode->data = data;
newNode->pointee = NULL;
last = newNode;
++count;
}
int main(){
LinkedList list;
for(int i=1 ; i<4 ; i++)
{
list.push(i);
}
int length = list.length();
std::cout << length << std::endl;
list.traverseList();
return 0;
}
Linked list implemented as classes, not imperative code
As an update to previous code I've submitted:
Improvements this time are mainly having everything moved to a class, before I plough on and add more functionality (I've come back to C++ after a few years away and this is a revision exercise).
#include <iostream>
#include "LinkedList.h"
class LinkedList
{
public:
LinkedList(void);
~LinkedList(void);
void traverseList();
int length();
void push(int data);
private:
struct node{
int data;
node *pointee;
};
node *head;
node *last;
int count;
};
LinkedList::LinkedList(void)
{
head = new node();
last = new node();
head->data = 0;
head->pointee = last;
last->data = 0;
last->data = NULL;
count = 2;
}
LinkedList::~LinkedList(void)
{
}
/*
To be replaced with an iterator and an overloaded print operator for output
*/
void LinkedList::traverseList(){
for(node *iterator = LinkedList::head ; iterator ; iterator = iterator->pointee)
{
std::cout << iterator->data << std::endl;
}
}
int LinkedList::length(){
return LinkedList::count;
}
void LinkedList::push(int data){
node *newNode = new node();
newNode->data = data;
newNode->pointee = NULL;
last = newNode;
++count;
}
int main(){
LinkedList list;
for(int i=1 ; i<4 ; i++)
{
list.push(i);
}
int length = list.length();
std::cout << length << std::endl;
list.traverseList();
return 0;
}
C++ Linked List implemented as classes, not imperative code
As an update to previous code I've submitted:
Improvements this time are mainly having everything moved to a class, before I plough on and add more functionality (I've come back to C++ after a few years away and this is a revision excercise)
#include <iostream>
#include "LinkedList.h"
class LinkedList
{
public:
LinkedList(void);
~LinkedList(void);
void traverseList();
int length();
void push(int data);
private:
struct node{
int data;
node *pointee;
};
node *head;
node *last;
int count;
};
LinkedList::LinkedList(void)
{
head = new node();
last = new node();
head->data = 0;
head->pointee = last;
last->data = 0;
last->data = NULL;
count = 2;
}
LinkedList::~LinkedList(void)
{
}
/*
To be replaced with an iterator and an overloaded print operator for output
*/
void LinkedList::traverseList(){
for(node *iterator = LinkedList::head ; iterator ; iterator = iterator->pointee)
{
std::cout << iterator->data << std::endl;
}
}
int LinkedList::length(){
return LinkedList::count;
}
void LinkedList::push(int data){
node *newNode = new node();
newNode->data = data;
newNode->pointee = NULL;
last = newNode;
++count;
}
int main(){
LinkedList list;
for(int i=1 ; i<4 ; i++)
{
list.push(i);
}
int length = list.length();
std::cout << length << std::endl;
list.traverseList();
return 0;
}