
July 1st, 2003, 11:00 PM
|
|
Contributing User
|
|
Join Date: Jul 2002
Location: Tallahassee
Posts: 55
  
Time spent in forums: 2 h 57 m 51 sec
Reputation Power: 8
|
|
|
My list class
Hi guys,
Let me start off by saying that this is a project from school. I've been working on this awhile now and I still cannot get it to work. Please tell me what I am doing wrong. I am going to post list.h and list.cpp in this page. Thank you for your concern.
Code:
// Program Description: This is a header file for the list class
#ifndef LIST_H
#define LIST_H
#include <iostream.h>
const int SIZE = 20;
class List{
public:
List();
void Show();
bool Insert(int value);
bool Insert(int value, int pos);
bool Delete(int pos);
int Size();
int Sum();
float Average();
int Odds();
int HowMany(char value);
int Between(int low,int high);
int WhereIs(int value);
void Clear();
private:
int list_array[SIZE];// the container -- an array of 20 integers
int element;// a pointer that keeps track of the position in array
};
#endif
Code:
/*-------------------------------------list.cpp------------------------------------------*/
#include <iostream.h>
#include "list.h"
List::List()
// constructor for list object - has to create the list object and print out message that
// object has been instantiated
{
element = 0;
cout << "Creating List Object\n";
}
void List::Show()
// if list is empty, output message
// else show elements in list
{
if (element <= 0)
cout << "List is empty\n";
else if (element >= SIZE)
cout << "List is full\n";
else{
for (int i = 0; i < element; i++)
cout << list_array[i] << " ";
cout << "\n";
}
}
bool List::Insert(int value)
// Inserts a number at the end of the list(if there is enough room)
{
if (element < SIZE){
list_array[element] = value;
element = element++;
return true;
}
else{
cout << "*** Invalid list position number\n";
return false;
}
}
bool List::Insert(int pos, int value)
// inserts a number in the list, possibly in the middle(if room, and if valid position given)
{
if (pos <= (element+1)){
// In order to insert another value, we need atleast one more free space
if (((element+1) < SIZE) && (pos > 0)){
// starting at element, begin moving every element one space to the right
for(int i = (element+1); i <= pos; i--)
list_array[i + 1] = list_array[i];
// at pos, insert new value;
list_array[pos] = value;
// increment element
element = element + 1;
return true;
}
}
else {
cout << "*** Invalid position number\n";
return false;
}
}
bool List::Delete(int pos)
// delete a value from the list(given a position)
{
if ((element < 0) && (element > SIZE)){
cout << "*** Invalid list position number";
return false;
}
else{
// shift value at pos + 1 and so on to the left
for (int i = element; i > pos; i--)
list_array[i] = list_array[i + 1];
// decrease element by one
element = element - 1;
return true;
}
}
int List::Size()
// returns the size of list
{
return element;
}
int List::Sum()
// returns the sum of the values in the list
{
int sum = 0;
for (int i = 0; i < element; i++)
sum += list_array[ i ];
return sum;
}
float List::Average()
// returns the average of the values in the list
{
float sum = Sum();
float size = Size();
float average = sum/size;
return average;
}
int List::Odds()
// counts up and returns how many odd numbers in the list
{
int odd_count = 0;
for (int i = 0; i <= element; i++){
if ((list_array[i] % 2) == 1){
odd_count++;
}
}
return odd_count;
}
int List::HowMany(char value)
// counts up and returns how many occurences of a given value(should work for any parameter value)
{
int how_many_counter = 0;
for (int i=0; i < element; i++){
if (list_array[i] == value)
how_many_counter++;
}
return how_many_counter;
}
int List::Between(int low,int high){
int between = 0;
int value;
for(int i = 0; i < element; i++){
value = list_array[i];
if((value >= low) && (value <= high))
between++;
}
return between;
}
int List::WhereIs(int value)
// returns the position of the first occurence of a given value(or returns 0, if not found)
{
int num; // This variable tells us what number is in array_list[i]
// This is just a counter to capture the position we are on
int pos_counter = 0;
for (int i = 0; i <= element; i++){
num = list_array[i];
if (num == value)
pos_counter = (i + 1); // capture the array number + 1
}
return pos_counter;
}
void List::Clear()
// resets the list object to represent an empty list
{
element = 0;
}
I just need help on the Insert(int pos, int value) in particular. The Delete() does not work either but I'll get to that later. I just added the rest of the code for the sake of completeness. Thank you so much.
|