0

Okay so the deal is, our instructor wrote what the class is supposed to look like (will be included with the code) and also gave us the Input, Preconditions, Process, Output, and Postcondtions of each function that is supposed to be used. I'm having trouble with an insert, delete, and clear functions dealing with the array. There is no USER input, we're just supposed to test the functions within the main function. Here's the code I've written thus far. Any help would be much appreciated!! Thanks for your time.

Code:

#include <iostream>
using namespace std;
#define DataType int
#define Boolean bool
#define ARRAYSIZE 0

class SeqList
{
private:
	int size;
	int listitem[ARRAYSIZE];
	
	
public:
	SeqList(void);

	int ListSize(void)const;
	Boolean ListEmpty(void) const;
	Boolean Find(DataType& item) const;
	DataType GetData(int pos) const;

	void Insert(const DataType& item);
	void Delete(const DataType& item);
	DataType DeleteFront(void);
	void ClearList(void);
};

SeqList::SeqList()
{
	size = 0;
}

int SeqList::ListSize() const
{
	return size;
}

Boolean SeqList::ListEmpty() const
{
	if(size == 0)
		return true;
	else
		return false;
}

Boolean SeqList::Find(DataType& item) const
{
if(item == listitem[size])
{
return true;
}
else 
return false;
}

void SeqList::Insert(const int& item)//Input: Item to insert in the list, no preconditions, Process: //Add the item at the rear of the list., No output, Postconditions: The list has a new item; its size //is increased by one
{
	size++;
	listitem[size] = item;
			
}
DataType SeqList::GetData(int pos) const
{
	if(pos >= size) 
		abort();
	else
		return pos;
}
void SeqList::Delete(const int& item)//Input: Value to delete from the list, No preconditions, 
//Process: Scan the list and remove the first occurrence of the item in the list. Take no action if //the item is not in the list, no output, Post conditions: If a match occurs, the list has one fewer //items
{
	if(item != listitem[size])
	{
		abort();
	}
	else
		for(int i = 0; i < size; i++)
		{
			listitem[size]=listitem[size-1];
		}

}

//DataType DeleteFront(void)//No input, Preconditions: List must not be empty, Process: //Remove the first item  from the list, output: Return the value of the item that is removed
//Post conditions: The list has one fewer items
//{
//**Code for delete only with the index listitem[0]
//}
void ClearList(void)//No input, no preconditions, process: Remove all elements from the list and //set the list size to 0
{

}
int main()
{
	SeqList A;
	A.Insert(12);
	
	cout << A.ListSize()<<endl;
	if(A.ListEmpty()) cout<< "The List Is Empty" << endl;
	else 
		cout<< "The List Is Not Empty" << endl;

	return 0;
}

Each time I try to write the delete function, I get a runtime error. Stack around A is corrupt. Not sure what to do.

iTsweetie

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by nucleon
0

You're defining your array with a size of zero. (ARRAYSIZE is 0.)

And even if you fix that, your code often refers to listitem, which is outside the array bounds.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.