1,105,556 Community Members

Pointer based Lists

Member Avatar
biogig
Newbie Poster
3 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

guys I've been trying to write this lab for my comp sci class for three days now. i've got to the point where it compiles but when I try to run it a table pops up that's says there's an error somewhere in the program that causes it to abort. the program is a simple one, it uses pointer based lists. Basically I'm supposed to write a code for a parking lot with two lanes. When a car arrives I have to find a spot for it on either of the two lanes. And if a car departs I remove it from whichever lane it is located in. I'm in desperate need of help can someone please give it a look :?:

#include <cstddef>     // for NULL
#include <new>         // for bad_alloc
#include<iostream>
#include "ListP.h" // header file

using namespace std;

// definitions of methods follow:
//   . . .
List::List():head(NULL),size(0) {
}

List::List(const List& aList)
: size(aList.size)
{
	if (aList.head == NULL)
		head = NULL;  // original list is empty
	
	else
	{  // copy first node
		head = new ListNode;
		head->item = aList.head->item;
		
		// copy rest of list
		ListNode *newPtr = head;  // new list pointer
		// newPtr points to last node in new list
		// origPtr points to nodes in original list
		for (ListNode *origPtr = aList.head->next;origPtr != NULL;origPtr = origPtr->next)
		{  newPtr->next = new ListNode;
		newPtr = newPtr->next;
		newPtr->item = origPtr->item;
		}  // end for
		
		newPtr->next = NULL;	//After the for statements is evaluated as invalid because origPtr->next=NULL; we assigne the last node to point to NULL!
	}  // end if
}  // end copy constructor

List::~List()
{
	while (!isEmpty())
		remove(1);
}  // end destructor

bool List::isEmpty() const
{
	return size == 0;
}  // end isEmpty

int List::getLength() const
{
	return size;
}  // end getLength

List::ListNode *List::find(int index) const
{
	if ( (index < 1) || (index > getLength()) )	//getLength()=size;
		return NULL;
	
	else  // count from the beginning of the list.
	{  ListNode *cur = head;
	for (int skip = 1; skip < index; ++skip)
		cur = cur->next;
	return cur;
	}  // end if
}  // end find

ListItemType List::retrieve(int index) const
throw(ListIndexOutOfRangeException)
{
	ListItemType dataItem;
	if ( (index < 1) || (index > getLength()) )
		throw (ListIndexOutOfRangeException)("ListIndexOutOfRangeException: retrieve index out of range");
	else
	{  // get pointer to node, then data in node
		ListNode *cur = find(index);
		dataItem = cur->item;
	} 
	return dataItem;// end if
}  // end retrieve

void List::insert(int index, const ListItemType& newItem)
throw(ListIndexOutOfRangeException, ListException)
{
	int newLength = getLength() + 1;
	
	if ( (index < 1) || (index > 10) )
		throw (ListIndexOutOfRangeException)("ListIndexOutOfRangeException: insert index out of range");
	else
	{  // try to create new node and place newItem in it
		try
		{
			ListNode *newPtr = new ListNode;
			size = newLength;
			newPtr->item = newItem;
			
			// attach new node to list
			if (index == 1)
			{  // insert new node at beginning of list
				newPtr->next = head;
				head = newPtr;
			}
			else
			{  ListNode *prev = find(index-1);
            // insert new node after node
            // to which prev points
            newPtr->next = prev->next;
			prev->next = newPtr;
			}  // end if
		}  // end try
		catch (bad_alloc e)
		{
			throw (ListException)("ListException: memory allocation failed on insert");
		}  // end catch
	}  // end if
}  // end insert

void List::remove(int index) throw(ListIndexOutOfRangeException)
{
	ListNode *cur;
	
	if ( (index < 1) || (index > 10) )
		throw (ListIndexOutOfRangeException)(
		"ListIndexOutOfRangeException: remove index out of range");
	else
	{  --size;
	if (index == 1)
	{  // delete the first node from the list
		cur = head;  // save pointer to node
		head = head->next;
	}
	
	else
	{  ListNode *prev = find(index - 1);
	// delete the node after the node to which prev points
	cur = prev->next;  // save pointer to node
	prev->next = cur->next;
	}  // end if
	
	// return node to system
	cur->next = NULL;
	delete cur;
	cur = NULL;
	}  // end if
}  // end remove

int main ()
{
	List garage1,garage2;
	ListItemType licensePlate;
	

	char car;

	cout<<"Please enter A/a if you want to park your car in our garage"<<endl;
	cout<<"Or enter B/b if you want to find and take your car from our garage"<<endl;
	cin>>car;
	if (car=='A'||car=='a'&&garage1.getLength()<10)
	{
		
				cout<<"Please enter the license plate of the car you wish to park in the first garage"<<endl;
				 cin>>licensePlate;
				 garage1.insert(garage1.getLength()+1,licensePlate);

				 for(int i=0;i<garage1.getLength()+1;i++)
				 cout<<garage1.retrieve(i)<<"   ";
				 cout<<endl;

	}

	else if(car=='A'||car=='a'&&garage1.getLength()==10&&garage2.getLength()<10)
	{
		cout<<"First Garage is full!"<<endl;
		

		
			cout<<"Please enter the license plate of the car you wish to park in the second garage"<<endl;
			cin>>licensePlate;
			garage2.insert(garage2.getLength()+1,licensePlate);

			for(int i=0;i<garage2.getLength()+1;i++)
			cout<<garage2.retrieve(garage2.getLength())<<"    ";
			cout<<endl;
	}
	else if(car=='A'||car=='a'&&garage1.getLength()==10&&garage2.getLength()==10)
		cout<<"Sorry both our garages are full"<<endl;
	

	else if(car=='B'||car=='b')
	

 {cout<<"Please enter the license plate of your car"<<endl;
		cin>>licensePlate;
		for(int i=0;i<garage1.getLength()+1;i++)
		{
				if(garage1.retrieve(i)==licensePlate)
					{cout<<"Car found in garage1. Removing it!"<<endl;
				garage1.remove(i);
				}
		
				else if (garage2.retrieve(i)==licensePlate)
		{	
			cout<<"Car found in garage 2. Removing it!"<<endl;
			garage2.remove(i);}
		}
 }
		

	else 
	{
		cout<<"We're sorry! We couldn't find your car!"<<endl;

			for(int i=0;i<garage1.getLength()+1;i++)
				cout<<"Garage1:  "<<garage1.retrieve(i)<<"    ";
			cout<<endl;

			for(int i=0;i<garage2.getLength()+1;i++)
				cout<<"Garage2:  "<<garage2.retrieve(i)<<"    ";
			cout<<endl;
			
		}


	return 0;
}
Member Avatar
daviddoria
Posting Virtuoso
1,968 posts since Feb 2008
Reputation Points: 334 [?]
Q&As Helped to Solve: 204 [?]
Skill Endorsements: 8 [?]
Featured
 
-1
 

I would very much suggest you hard code values into the program as you debug. Once you're convinced it's working, then you can add in the user input. This will also help us help you - it would be very complicated to explain exactly the sequence of input values you used - whereas if you could post compilable code that demonstrates the problem that would be much easier. However, even if you post compilable and run-albe code, it is unlikely that anyone is going to debug your 200 lines. You really should attempt to narrow the problem down so we can look at a specific function or something like that.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: