I am continually writing a program to improve my knowledge of linked-lists and how they function. I was wondering if some of you could review my code and notice any faults that you may be familiar with, or any errors in general. The functions work for my test functions, but obviously, I have not tested every scenario possible.
// LinkedList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
struct node
{
int value;
node* next;
};
node* push(node*, int);
node* pop(node*);
node* pop(node*, int);
node* insert(node*, int, int);
void printList(const node*);
int _tmain(int argc, _TCHAR* argv[])
{
node* head = NULL;
for(int i = 1; i <= 15; ++i)
{
head = push(head, i);
}
for(int j = 14; j >= 0; j = j - 2)
{
head = pop(head, j);
printList(head);
}
head = pop(head);
head = insert(head, 4, 27);
printList(head);
cin.ignore();
}
node* push(node* read, int val)
{
node* write = read;
if(read == NULL)
{
read = new node;
read->next = NULL;
read->value = val;
cout << "Node Head: " << read->value << endl;
return read;
}
else
{
while(read->next != NULL)
{
read = read->next;
}
read->next = new node;
read->next->next = NULL;
read->next->value = val;
read = read->next;
cout << "Node Link: " << read->value << endl;
return write;
}
}
node* pop(node* read)
{
node* write = read;
if(read->next == NULL)
{
delete read;
read = NULL;
return write;
}
else
{
while(read->next != NULL)
{
if(read->next->next == NULL)
{
cout << "Pop: " << read->next->value << endl;
delete read->next;
read->next = NULL;
}
else
{
read = read->next;
}
}
return write;
}
}
node* pop(node* read, int pos)
{
node* write = read;
if(read->next == NULL)
{
delete read;
return write;
}
else
{
if(pos == 0)
{
node* old = read;
cout << "Pop: " << read->value << endl;
read = read->next;
delete old;
return read;
}
else
{
for(int i = 0; i < (pos - 1); i++)
{
read = read->next;
}
node* old = read->next;
cout << "Pop: " << old->value << endl;
read->next = read->next->next;
delete old;
return write;
}
}
}
node* insert(node* read, int pos, int val)
{
node* write = read;
for(int i = 0; i < (pos - 1); i++)
{
read = read->next;
}
node* ins = new node;
ins->value = val;
cout << "Insert: " << ins->value << endl;
ins->next = read->next;
read->next = ins;
return write;
}
void printList(const node* read)
{
if(read != NULL)
{
cout << "List Item: " << read->value << endl;
printList(read->next);
}
}
/****************OUTPUT*********************
Node Head: 1
Node Link: 2
Node Link: 3
Node Link: 4
Node Link: 5
Node Link: 6
Node Link: 7
Node Link: 8
Node Link: 9
Node Link: 10
Node Link: 11
Node Link: 12
Node Link: 13
Node Link: 14
Node Link: 15
Pop: 15
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 5
List Item: 6
List Item: 7
List Item: 8
List Item: 9
List Item: 10
List Item: 11
List Item: 12
List Item: 13
List Item: 14
Pop: 13
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 5
List Item: 6
List Item: 7
List Item: 8
List Item: 9
List Item: 10
List Item: 11
List Item: 12
List Item: 14
Pop: 11
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 5
List Item: 6
List Item: 7
List Item: 8
List Item: 9
List Item: 10
List Item: 12
List Item: 14
Pop: 9
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 5
List Item: 6
List Item: 7
List Item: 8
List Item: 10
List Item: 12
List Item: 14
Pop: 7
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 5
List Item: 6
List Item: 8
List Item: 10
List Item: 12
List Item: 14
Pop: 5
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 6
List Item: 8
List Item: 10
List Item: 12
List Item: 14
Pop: 3
List Item: 1
List Item: 2
List Item: 4
List Item: 6
List Item: 8
List Item: 10
List Item: 12
List Item: 14
Pop: 1
List Item: 2
List Item: 4
List Item: 6
List Item: 8
List Item: 10
List Item: 12
List Item: 14
Pop: 14
Insert: 27
List Item: 2
List Item: 4
List Item: 6
List Item: 8
List Item: 27
List Item: 10
List Item: 12
*******************************************/
Well, first of all, for general use, you should be using the Standard library :
std:vector, or, if you really need a linked-list,std::list<>.But, since this is a self-teaching exercise, we’ll skip that.Which bring us to the next problem: As a teaching exercise, it really isn’t production code, so what things should we complain about? The cout’s in the middle of functions??
One particular problem I saw was code like this:
The constructor for the node object should handle setting it’s
nextmember to null. For that matter it should handle setting the value also, so that this code should really be:Another problem: In pop(), you have:
Setting
readto null is pointless — it’s a local varaible about to go out of scope. And you are returningwrite, which is equal toread, which has beed deleted.Additionally, you use almost no features of C++ and Object-oreitned programming in the code: If we ignore the cout’s, it’s basically just C code that’s allocating memory via new & delete. As I noted, it could greatly benefit from a constructor. A destructor may be useful as well, plus a List class, which would hold the head node, and would have all your functions as members.