Have issues determining if array is sorted - c++

I trying to figure out if an array set is sorted or not using sentinal-terminated sequences.
In attempting I have tried to check if the array is ascending, descending, or not sorted.
#define isNaN(X) (X != X)
#define NaN std::numeric_limits<float>::quiet_NaN()
enum sortType { ASCENDING, DESCENDING, UNKNOWN, UNSORTED };
I think there is an error in my bool inSorted function and I believe the issue is with for checking the NaN value at the end.
bool isSorted(const float data[], const int currentDataItem, const sortType typeOfSort) {
switch(typeOfSort) {
case ASCENDING:
if(currentDataItem == 0){
return isSorted(data, (currentDataItem + 1), ASCENDING);
} else if(data[currentDataItem] > data[currentDataItem+1]){
return false;
} else if(data[currentDataItem] == data[currentDataItem]){
return isSorted(data, (currentDataItem+1), ASCENDING);
} else {
return true;
}
case DESCENDING:
if(currentDataItem == 0){
return isSorted(data, (currentDataItem + 1), DESCENDING);
} else if(data[currentDataItem] < data[currentDataItem+1]){
return false;
} else if(data[currentDataItem] == data[currentDataItem]){
return isSorted(data, (currentDataItem+1), DESCENDING);
} else {
return true;
}
}
}
The isSorted is then called by bool sorted
bool sorted(const float data[]) {
bool ascending = isSorted(data, 0, ASCENDING);
bool descending = isSorted(data, 0, DESCENDING);
if(!ascending && !descending){
return false;
}
return true;
}
Wrapped up by main
int main(const int argc, const char* const argv[]) {
float data[] = {1, 2, 4, 5, 6, NaN};
if (sorted(data))
cout << "Data is sorted" << endl;
else
cout << "Data is not sorted" << endl;
return 0;
}

When implementing a recursive function, you need to ask yourself the following questions explicitly:
What is the base case of your recursive function?
What is the recursive case of your recursive function?
Answers:
If the current array position compares against NaN, then we are sorted.
If our position is not sorted, we know the list isn't sorted. If our position is sorted, then the array is sorted if the recursive condition holds on the next pair.
Therefore:
case ASCENDING:
// Empty list
if (std::isnan(data[currentDataItem]))
return true;
// One element list.
else if (std::isnan(data[currentDataItem + 1])
return true;
// Recursive case: we've found a location where the data isn't sorted.
else if (data[currentDataItem] > data[currentDataItem + 1])
return false;
// This location is sorted, check the next location.
else
return isSorted(data, currentDataItem + 1, typeOfSort);
Note that I only directly return true; on the base cases. Otherwise, we either fail the function, or we recurse.

Related

Delete and insert an element from/to array bag. Why boolean array instead of int?

I am implementing bag using array in C++. I can not figure out how to let the deleteElement function work. It is suppose to delete given element from the array.
I also don't understand why the array is initialized with bool and how the insert function works.
So, I got three questions:\
How to make the deleteElement function work?
Why is the array initialized with bool?
How does the insert function work? It looks like it only adds true value to the array, but when this program prints the array, you will see that the x value is printed out, I can not figure this out.
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
// cin -> add 0 qry 0 del 0 qry 0 quit
// cout -> TF
// add x -> Adds the number x to the bag
// qry x -> Checks if x belongs to the bag then output T, otherwise output F
// del x -> If there is an element x in the bag, remove it, otherwise do nothing.
// quit -> Stops the program
// Exercise: Fun with bags 1 (Here the bag is a set of int values).
/*
Example:
input: add 1 add 2 add 1 del 1 qry 1 qry 2 quit
output: FT
*/
// enumeration type for actions on the bag
enum action {add, qry, del, quit, none};
// translation of strings into actions
action str2action(string s) {
if (s=="add") return add;
if (s=="qry") return qry;
if (s=="del") return del;
if (s=="quit") return quit;
return none;
}
#define BAG_AS_ARRAY_SIZE 10
struct bag {
bool as_array[BAG_AS_ARRAY_SIZE]; // using arrays
};
// Simple function to initialise the bag
void initialise(bag &b){
// Array
for(int i=0; i<BAG_AS_ARRAY_SIZE; i++){
b.as_array[i] = false;
}
}
// function to display the content of the bag
void display_bag(bag b) {
cout << "The bag is : " << endl;
// Array
cout << " - (A) - : " ;
for(int i=0; i<BAG_AS_ARRAY_SIZE; i++){
if(b.as_array[i])
cout << i << " " ;
}
cout << endl;
return;
}
void insert(bag &b,unsigned int x){ //add
// Array
b.as_array[x] = true;
}
void check(bag &b,unsigned int x) //qry
{
bool q = false;
for(int i = 0; i < BAG_AS_ARRAY_SIZE; i++)
{
if(b.as_array[x])
{
q = true;
}
}
if(q == true)
{
cout << "T";
}
if(q == false)
{
cout << "F";
}
cout << endl;
}
void DeleteElement(bag &b, unsigned int x) //del
{
int i;
for (i=0; i<BAG_AS_ARRAY_SIZE; i++)
if (b.as_array[i] == x)
break;
if (i < BAG_AS_ARRAY_SIZE)
{
for (int j=i; j<BAG_AS_ARRAY_SIZE; j++)
b.as_array[j] = b.as_array[j+1];
}
}
// this function deals with actions on a bag
void update(bag &b, action a, unsigned int x){
switch(a){
case add:
insert(b,x);
break;
case qry:
check(b,x);
break;
case del:
DeleteElement(b,x);
break;
case quit:
break;
case none:
break;
default:
break;
}
return;
}
int main()
{
bag my_bag; //We create an array of boolean type.
string my_act_str;
unsigned int x;
initialise(my_bag); //The array is initialised with False, which is 0
bool go_on = true;
while(go_on)
{
display_bag(my_bag);
cout << "What's next? (actions = add x ,qry x ,del x ,quit)" << endl;
cin >> my_act_str;
action act = str2action(my_act_str);
if(act == quit)
{
go_on = false;
}
if(act == add)
{
cin >> x;
update(my_bag,act,x);
}
if(act == qry)
{
cin >> x;
update(my_bag,act,x);
}
if(act == del)
{
cin >> x;
update(my_bag,act,x);
}
}
return 0;
}
Edit:
I found out solution for the delete function. It is very easy one:
void delete_element(bag &b, unsigned int x)
{
b.as_array[x] = false;
}
Your three questions actually come from the fact that this is not really a bag. What you have here is more like a "Boolean mask" that indicates if numbers from zero to BAG_AS_ARRAY_SIZE - 1 are true or false. That is why you have a Boolean array as the storage and all elements in it are initialized with false. That is, the mask is not set for any of the numbers from zero to BAG_AS_ARRAY_SIZE - 1.
Your deleteElement function then only needs to set the corresponding array position of the mask to false to "delete" that number and "inserting" a number corresponds to setting that specific position in the mask to true.
In the display_bag function, notice that you are not print the content of the array (which obviously can only be either true or false), but the index of the positions in the array that have a true value.

Incorrect count output / Having difficulty trying to create a HashTable/Set using open addressing

I'm trying to create a program that opens a .txt file containing a speech and assigns each word to a space in the array/set based on the hash value. Collisions are accounted for using open addressing method. The program should be able to perform the following functions: add(), remove(), find(), count() which keeps count of the elements IN the array/set, and loadfactor(). A template header.h file was provided that required some filling in, but my unfamiliarity with that style of coding was making it difficult for me to understand it. Below I have provided the code I have so far, and everything seems to be working except the mCount. The speech contains about 300 words but when I run the code, the count output shows 17. I'm assuming the error is in my resizing function but I am unsure.
//hashset.h file
#pragma once
#include <cmath>
#include <functional>
#include <vector>
template <typename TValue, typename TFunc>
class HashSet
{
private:
// Unlike Java, the C++ hashtable array won't be full of null references.
// It will be full of "Entry" objects, each of which tracks its current state
// as EMPTY, FULL (with a value), or NIL (value was once here, but was removed).
class Entry
{
public:
enum EntryState { EMPTY, FULL, NIL };
TValue value;
EntryState state;
Entry() : value(), state(EMPTY) {}
Entry(const TValue& v) : value(v), state(EMPTY) {}
};
TFunc mHash;
std::vector<Entry> mTable;
std::size_t mCount;
public:
// Constructs a hashtable with the given size, using the given function for
// computing h(k).
// hash must be a callable object (function, functor, etc.) that takes a parameter
// of type TValue and returns std::size_t (an integer).
HashSet(int size, TFunc hash) : mHash(hash)
{
// initialize count
mCount = 0;
// hashtable array cannot be same data type as that of what is being stored (cannot be string)
// requirement #4 - if user inputs array size that is not a power of 2, constructor must round to nearest power of 2 value
size = pow(2, (int(log(size - 1) / log(2)) | 1));
mTable.resize(size); // resizes the vector to have given size.
// Each element will be default-constructed to have state EMPTY.
}
void resize(int new_size) {
HashSet aux{ new_size, mHash }; //double the size, same hash function
for (const auto& entry : mTable)
if (entry.state == Entry::FULL && entry.state == Entry::EMPTY && entry.state == Entry::NIL) //there is an element
aux.add(entry.value); //insert it on the new set
*this = aux;
}
// Inserts the given value into the set.
void add(const TValue& value)
{
// Use the type std::size_t for working with hash table indices.
// Invoke the mHash function, passing the key to calculate h(k), as in
// size_t hashCode = mHash(value);
// Mod down to size.
// Go to the table at that index and do the insertion routine.
// Note, if table is full when trying to add an element, it should double in size
// to keep table size a power of 2
if (double(mCount) / mTable.size() > 0.8) // load factor comparison
this->resize(2 * mTable.size()); // call resize function if array is too small to accommodate addition
size_t hashCode = mHash(value) % mTable.size(); // mod value by table size to get starting index
if (mTable[hashCode].state == Entry::EMPTY || mTable[hashCode].state == Entry::NIL) { // NIL space CAN be replaced with value
mTable[hashCode].value = value; // store value in vector index specified by hashCode
mCount++; // increment counter when word is added
}
else {
for (std::size_t i = 1; i < mTable.size(); i++) {
// use open addressing to find next open space
if (mTable[hashCode].state != Entry::EMPTY) {
hashCode = ((mHash(value) % mTable.size()) + ((int)(pow(i, 2) + i) >> 1)) % mTable.size(); // h(k) + f(i) or h(k) + ((i^2 + i)) / 2
}
else if (mTable[hashCode].value == value) { // account for duplicates
break; // exit for-loop
}
else if (mTable[hashCode].state == Entry::EMPTY || mTable[hashCode].state == Entry::NIL) { // NIL space CAN be replaced with value
mTable[hashCode].value = value; // store value in vector index specified by new hashCode
mCount++; // increment counter when word is added
break; // exit for-loop
}
else
break; // exit for-loop
}
}
}
// Returns true if the given value is present in the set.
bool find(const TValue& key)
{
size_t hashCode = mHash(key) % mTable.size(); // mod value by table size to get starting index to do retrace
if (mTable[hashCode].value == key)
return true;
else if (mTable[hashCode].state != Entry::EMPTY || mTable[hashCode].state == Entry::NIL) { // check that set is not empty or has a NIL state
for (std::size_t i = 1; i < mTable.size(); i++) {
// use open addressing again to find key
if (mTable[hashCode].value != key)
hashCode = ((mHash(key) % mTable.size()) + ((int)(pow(i, 2) + i) >> 1)) % mTable.size();
else if (mTable[hashCode].value == key) {
return true; // value found at speecified location
break; // exit for-loop as first instance of value has been found
}
//else if (i == mTable.size()) // end of table reached, element not in set
//return false;
}
}
else // end of table reached, element was not in set
return false;
}
// Removes the given value from the set.
void remove(const TValue& key)
{
size_t hashCode = mHash(key) % mTable.size(); // mod value by table size to get starting index to do retrace
if (mTable[hashCode].value == key) {
mTable[hashCode].value = Entry::NIL; // replace value with NIL so find() op does not return a false when searching for element
mCount--; // decrement element counter
}
else if (mTable[hashCode].state != Entry::EMPTY || mTable[hashCode].state != Entry::NIL) { // check that there is a value to be removed
for (std::size_t i = 1; i < mTable.size(); i++) {
// use open addressing again to find key
if (mTable[hashCode].value != key) {
hashCode = ((mHash(key) % mTable.size()) + ((int)(pow(i, 2) + i) >> 1)) % mTable.size();
}
else {
mTable[hashCode].value = Entry::NIL; // if found after open addressing, replace with NIL
mCount--; // decrement element counter
}
}
}
}
int count() {
return mCount;
}
double loadFactor() {
double a = double(mCount) / mTable.size();
return a;
}
};
// main function
#include "hashset.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string testline;
vector<string> word;
HashSet<std::string, std::hash<std::string> > obj1{ 50, std::hash<std::string>{} };
ifstream Test("speech.txt");
if (!Test)
{
cout << "There was an error opening the file.\n";
return 0;
}
//store words in vector
while (Test >> testline) {
word.push_back(testline);
//obj1.add(testline);
}
//output whole vector with position numbers for each entry
cout << "Array contents:\n";
for (int i = 0; i < word.size(); i++) {
obj1.add(word[i]);
cout << word[i] << "(" << i << ")" << endl;
}
cout << "current count: " << obj1.count() << endl;
obj1.add("abcd"); // should hash to 4
if (obj1.find("abcd"))
cout << "abcd is in the set " << endl;
else
cout << "abcd is not in set " << endl;
obj1.add("adcb"); // should hash to 4 then 5 after probing
if (obj1.find("adcb"))
cout << "adcb is in the set " << endl;
else
cout << "adcb is not in set " << endl;
obj1.add("acde"); // should hash to 4 then 7 after probing
if (obj1.find("acde"))
cout << "acde is in the set " << endl;
else
cout << "acde is not in set " << endl;
obj1.remove("adcb"); // 5 should have NIL
if (obj1.find("adcb"))
cout << "adcb is in the set " << endl;
else
cout << "adcb is not in set " << endl;
if (obj1.find("acde"))
cout << "acde is still in the set " << endl;
else
cout << "acde is not in set " << endl;
cout << "final count: " << obj1.count() << endl;
system("pause");
exit(0);
}
}
The errors around NIL are because the enum defining NIL is part of the Entry class. You need to prefix NIL with the class name so the compile knows where the keyword comes from.
else if (mTable[hashCode] != NULL || mTable == Entry::NIL) { // getting error NIL identifier not found
The HashSet variable declaration is complaining because you are passing the wrong types. HashSet constructor takes a size and and a hash function. You are passing it a size and a string. Note the comment above the HashSet constructor
// hash must be a callable object (function, functor, etc.) that takes a parameter
// of type TValue and returns std::size_t (an integer).
This is your clue how to construct a HashSet object.

The Difference Between Calling and Returning a Recursive Function in C++

Can someone please explain with a simple example what precisely is the difference between calling and returning a recursive function in c++?
Here is my code which seeks to find a character in a string recursively. It works fine when I just call find(); the function displays an integer value, But, when I code return find(letter,word), it gives the correct result as either a 1 or 0. Thanks
bool find(char f,string str)
{
static int len = str.length() - 1;
static int count = 1;
if (len<0)
{
return false;
}
else
{
if (str[len] == f)
{
return true;
}
else
{
len--;
return find(f, str);
}
}
}
You don't return the function you return the result of the call.
Here a basic example of recursion :
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
If we call the function with n == 2 the program will do:
1) factorial (2) => return 2 * factorial(1); // It calls factorial with n == 1, do the multiplication and then return the result.
2) factorial(1) => return 1 * factorial(0); // same here with n == 0
3) factorial(0) => return 1; // from here the program will come back from the calls with the results
4) factorial(1) => return 1 * 1; => return 1;
5) factorial(2) => return 2 * 1; => return 2;
Few other things : be aware that your code will only work once as you use static int len, and count is a useless variable.
The function signature indicates a return value, so you need to embed the function call in a return statement for a value to be returned by the function, especially when neither of the if-conditionals are true.
Consider the following code (a variant of what the OP provided):
#include <iostream>
using namespace std;
int find(char f,string str) {
static int len = str.length() - 1;
static int count = 1;
int temp = 0;
if (len < 0) {
cout << count << "\n";
return -99;
}
else
if ( str[len] == f) {
return len;
}
len--;
count++;
temp = find( f, str );
cout << temp << "\n";
return temp;
}
int main() {
char ch = 'z';
int res = find(ch,"I");
if (res < 0) {
cout << "Letter '" << ch << "' was not found";
}
return 0;
}
See demo
Note that find() has a return value of -99 when the letter is not found. That value is captured by temp in find() and the function then returns the value of temp.
Now, consider main() -- its signature also indicates a return value, although it is discarded. If you attempt to execute the code without returning some kind of an integer in main(), the execution will be flawed.
So, whether you use a function recursively or not, if the function's signature indicates a return value then you need to return a value of the expected type. Since find() may return -99 or the position of the found letter, when it executes it will evaluate as one of those two values which will be returned by means of the return statement.

Sorted list: must have class/struct/union

so i have been working on a code for over two weeks and its not going too well. here are the instructions and the code is below it, as well as errors:
Task 1: Create one instance of this class. (the sorted list; he also had other instructions on HOW to start the code, but its already been done by me below in the code such as typedef...) You also need to read in data from one data file: float.dat, which contains the following numbers:
5.5
6.2
7.1
8.0
9.0
10.0
1.0
2.0
3.3
4.4
Data in float.dat contains floating numbers, which should be inserted into the object of SortedList. Note that you do not have any prior knowledge about data values in float.dat, but we assume that there are 10 elements in the data file.
Task 2: Use GetNextItem( ) to print out all the elements in the list in sorted sequence on computer screen.
Task 3: Use GetNextItem( ) to output all the elements in the list in sorted sequence onto a data file, output.dat.
Task 4: Design your test cases to demonstrate InsertItem( ), DeleteItem( ) and RetrieveItem( ) are working as expected.
here is the code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define MAX_ITEMS 10
typedef float ItemType;
class SortedList
{
private:
int length;
ItemType values[MAX_ITEMS];
int currentPos;
enum RelationType { LESS, GREATER, EQUAL };
public:
SortedList() {length = 0; currentPos = -1;}
int getLength() {return length;}
RelationType ComparedTo(ItemType x)
{
if (length > x.getLength())
return LESS;
else if (length == x.getLength())
return GREATER;
else
return EQUAL;
}
void MakeEmpty() {length = 0;}
void InsertItem(ItemType x)
{
int first = 0, last = length --;
bool moreToSearch = (first <= last);
int location = 0;
int midpoint= (first + last) / 2;
while (moreToSearch)
{
switch (x.ComparedTo(values[location]))
{
case LESS: //search in 1st half
moreToSearch = (first <= last);
break;
case GREATER:
location++;
moreToSearch = (location < length);
break;
}
}
for (int index = length; length > location; index--)
{
values[index] = values[index - 1];
}
values[location] = x;
length++;
}
void DeleteItem(ItemType x)
{
int location = 0;
while (x.ComparedTo(values[location]) != EQUAL)
location++;
for (int index = location ++; index < length; index++)
values[index --] = values[index];
length--;
}
void RetrieveItem(ItemType &x, bool & found)
{
int midpoint;
int first = 0, last = length - 1;
bool moreToSearch = (first <= last);
found = false;
int index = 0;
while (moreToSearch && !found)
{
midpoint = (first + last) / 2;
switch (x.ComparedTo(values[index++]))
{
case LESS: //search in 1st half
moreToSearch = (first <= last);
last = midpoint - 1;
break;
case GREATER: //Search in 2nd half
first = midpoint + 1;
moreToSearch = (first <= last);
break;
case EQUAL: //x has been found
found = true;
break;
}
}
}
int LengthIs() {return length;}
void ResetList() {currentPos = -1;}
bool IsFull()
{
if (length < 9)
return false;
else
return true;
}
void GetNextItem(ItemType &x)
{
currentPos++;
x = values[currentPos];
cout << x;
}
};
int main()
{
SortedList x;
ifstream inFile; ofstream output;
string line;
bool allAboutLists;
int i = 0;
int size = 0;
inFile.open("float.txt");
float values[10];
while (!inFile.eof()) // write or read data from inFile into values
{
inFile >> values[i];
i++;
size++; // this will count how many values there are in the array
x.InsertItem(values[i]);
++i;
}
x.ResetList();
cout << "The following is the list that's been made:" << endl << endl;
x.InsertItem(64);
//x.printlist();
cout << endl;
x.DeleteItem(64);
//x.printlist();
x.RetrieveItem(7.1, allAboutLists);
cout << endl;
cout << endl << "The length is: "; x.LengthIs(); cout << endl;
cout << "Is the list full?: " << boolalpha << x.IsFull() << endl;
cout << "The next item is: ";
for (int i = 0; i < 10; i++)
{
cout << x.GetNextItem << endl;
}
x.ResetList();
inFile.close();
output.open("output.txt");
for (int f = 0; f < 10; f++)
{
output << x.GetNextItem << endl;
}
system("pause");
return 0;
}
and the compiler keeps saying this:
(25) error C2228: left of '.getLength' must have class/struct/union [they mean the x. its red lined under, same for the rest of those left of etc..]
(27) error C2228: left of '.getLength' must have class/struct/union
(44) error C2228: left of '.ComparedTo' must have class/struct/union
(66): error C2228: left of '.ComparedTo' must have class/struct/union
-and also, 7.1 in main has something about refernce type mistake.
I am in extereme hurry as i have been working on it for 2 weeks now and its driving me crazy ! I have the code done as seen and more than wnough and just need to know what to change exactly because I am following everything I have been searching and researching yet its no good. so precise details or code specifically taken from mine and fixed would be appreciated.
Thanks!
You are passing x as ItemType which is a float.
float doesn't have those methods... looks like you wanted to pass it as a SortedList
The compare function needs two parameters in order to do a compare. Instead of ComparedTo, you may want to call it CompareToLocation.
RelationType CompareToLocation(ItemType x, size_t location){
if(x < values[location]) return LESS;
if(x == values[location]) return EQUAL;
return GREATER;}
An example usage would be:
result = CompareToLocation(x, location);
// ...
You defined ComparedTo as a method for SortedList, yet everytime you call that function, you call it on ItemType objects, which are actually floats.
As you can see in the definition of the method, you are trying to use, once again, SortedList methods on float Objects:
RelationType ComparedTo(ItemType x)
{
if (length > x.getLength())
return LESS;
else if (length == x.getLength())
return GREATER;
else
return EQUAL;
}
Your problem is not really a compiling one, but a conceptual one, since you don't seem to grasp what your are actually coding.
I'd recommend have your declarations and implementations separate, so you can see at a glance how does your class work.
Your class declaration should look something like this:
class SortedList
{
private:
int length;
ItemType values[MAX_ITEMS];
int currentPos;
enum RelationType { LESS, GREATER, EQUAL };
public:
SortedList();
int getLength();
RelationType ComparedTo(ItemType x) ;
void MakeEmpty();
void InsertItem(ItemType x) ;
void DeleteItem(ItemType x);
void RetrieveItem(ItemType &x, bool & found);
int LengthIs();
void ResetList();
bool IsFull();
void GetNextItem(ItemType &x);
};
You should focus on each method, making clear what each one of them is trying to achieve, and what does it need to achieve it (parameters).
For example:
RelationType ComparedTo(ItemType x) ;
Your SortedList class has this function, which receives an ItemType (float) as a parameter.
What is this trying to achieve? How do you compare a whole ordered list to a single element?
How can a single number be greater, less or EQUAL to a set of numbers?
Maybe what you really want to do is compate parameter X with an element inside the list?
If this is the case, how do you know which element in the list must be compared to parameter X? You should add another parameter telling you which element inside your ordered list to compare X to.
I quess this doesn't really solve your problem, but at least I hope this helps you understand better what your problem is.

Wrong output C++ ArrayList

When i make ArrayList with size 5 it gives wrong result but when it becomes bigger than 5 it becomes correct!
#include <iostream>
#include <string>
using namespace std;
class list
{
typedef int ListElemtype;
private:
ListElemtype listArray[6];
public:
bool insert(ListElemtype e)
{
if (numberOfElements == 5) //It turns wrong result when ListArray equal 5??
{
cout << "Can't do the insertion" << endl;
return false;
} //Why it return false? I know why i but = instead of == :D
else
{
listArray[numberOfElements + 1] = e;
numberOfElements++;
cout << "Done perfectly!" << numberOfElements << endl;
return true;
}
};
bool first(ListElemtype &e);
bool next(ListElemtype &e);
int numberOfElements;
int CurrentPosition;
void LIST ()
{
CurrentPosition=-1;
numberOfElements=0;
return;
}
};
int main()
{
list A;
A.LIST();
A.insert(10);
A.insert(20);
A.insert(30);
A.insert(40);
A.insert(50);
A.insert(60);
system("pause");
return 0;
}
Arrays are indexed from zero, not from one. So listArray[numberOfElements+1]=e; should be listArray[numberOfElements]=e;. The first inserted element goes into listArray[0].
Your listArray size is 6 therefore array index would start from 0 till 5. When you have numberOfElements==5 with listArray[numberOfElements + 1] you are trying to store at index 6 which you don't have.
As you may know, C bases its arrays at 0 and not 1. Thus,
else { listArray[numberOfElements+1]=e;
writes to the end of the array contained within list A, when numberOfElements is equal to 5 or higher.