Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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;
}
edited body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

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;
}
Tweeted twitter.com/#!/StackCodeReview/status/172057529990381568
Source Link
Tom Kealy
  • 505
  • 2
  • 5
  • 14

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;
}
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /