GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-May-2008, 05:29
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

Link list parameter check


Hi,

I am trying to use the function in the program below, check parameter. However, it only works when at least one value has been placed into the list. It is menu option 7 and it works fine with data but crashes when the list is empty. Can anyone tell me where I am going wrong?


CPP / C++ / C Code:
#include <iostream>
#include <stdlib.h>

using namespace std;


struct nodetype
{	int data;
	nodetype * next;
	nodetype ( int, nodetype *);
};

nodetype::nodetype(int n, nodetype * link)
: data(n), next(link)
{}

class linklist
{	public:
	  linklist ();
	  void insert( int insertvalue);
	  void print ( ) const;
	  void fill (int numOfInts );
	  linklist (const linklist & original);
	  void deletenode( int deletevalue);
	  //void destroy( );
	  //~linklist();
	private:
		nodetype * front;
		nodetype * rear;
};
void checkparameter (linklist copy);
int menu();

int main()
{

linklist list;
int choice, num;
	do
	{	system("cls");
		choice = menu( );
		switch(choice)
		{	case 1: cout<<"Enter integer to insert:";
			cin>>num;
			list.insert(num); break;
			case 2: list.print(); break;
			case 3: cout<<"How many random integers?";
					cin>>num;
					list.fill(num); break;
			case 4: {linklist copy (list);
					cout<<"copy: ";
					copy.print();
					}break;
			case 5:	cout<<"Enter the number to delete:";
					cin>>num;
					list.deletenode(num); break;
		//	case 6: list.destroy(); break;
			case 7:	checkparameter(list); break; 
			case 9: break;
			default: cout<<"Invalid choice\n";
				cin.ignore(); cin.ignore();
		}
	} while (choice != 9);


return 0;
}
void checkparameter (linklist copy)
{  //pre: copy is a valid circular linked list (could be empty)
  //post: copy is modified and printed and then erased by operating system

	copy.insert(33);
	copy.insert(55);
	cout<<"copy:";
	copy.print();
}

int menu( )
{
int choice;
system("cls");
cout<<"	LAB 4 MENU"<<endl;
cout<<" 1 Insert a number into the ordered circular list"<<endl;
cout<<" 2 Print the ordered circular list"<<endl;
cout<<" 3 Insert random integers into the ordered circular list"<<endl;
cout<<" 4 Test the copy constructor of the ordered circular list"<<endl;
cout<<" 5 Delete a number from the ordered circular list"<<endl;
cout<<" 6 Destroy the ordered circular list"<<endl;
cout<<" 7 Pass the list as value parameter & check"<<endl;
cout<<" 9 Quit"<<endl;
cin>>choice;
return choice;
}

linklist::linklist()
{
front = NULL;
rear = NULL;
}

void linklist::fill(int numOfInts)
{ 
  int i;
  nodetype *tempnode;
	for ( i = 0; i<numOfInts; i++)
	{ int rnum = rand();
	  rnum = rnum % 100;
	   nodetype *newnode = new nodetype(rnum,NULL);
		if( front == NULL && rear == NULL)
		{
			front = rear = newnode;
			rear->next = front;
		}
		else if(rnum<front->data)
		{
			newnode->next = front;
			front = newnode;
			rear->next = newnode;
		}

		else if(rnum>rear->data)
		{
			newnode->next = front;
			rear->next = newnode;
			rear = newnode;
		}

		else
		{
			tempnode = front;

				while ( tempnode->next != rear && tempnode->next->data < rnum)
				{
					tempnode = tempnode->next;
				}
				
			newnode->next = tempnode->next;
			tempnode->next = newnode;
		}
	}
cin.ignore();
}


void linklist::print() const
{
	nodetype *traverse = front;
	while( traverse != rear)
	{
		cout<<traverse->data<<" ";
		traverse = traverse->next;
	}
	cout<<traverse->data;
		
	cin.ignore();
	cin.ignore();
}

void linklist::insert( int insertvalue )
{
   nodetype *tempnode;

	    nodetype *newnode = new nodetype(insertvalue,NULL);
		if( front == NULL && rear == NULL)
		{
			front = rear = newnode;
			rear->next = front;
			return;
		}
		else if(insertvalue<front->data)
		{
			newnode->next = front;
			front = newnode;
			rear->next = newnode;
			return;
		}

		else if(insertvalue>rear->data)
		{
			newnode->next = front;
			rear->next = newnode;
			rear = newnode;
			return;
		}

		else
		{
			tempnode = front;

				while ( tempnode->next != rear && tempnode->next->data < insertvalue)
				{
						
					tempnode = tempnode->next;
				
				}
				newnode->next = tempnode->next;
				tempnode->next = newnode;
				return;
		}

  cin.ignore();

}

void linklist::deletenode( int deletevalue)
{
  nodetype *garbage;
  nodetype *tempnode = front;
  while (tempnode != rear)
  {

	if (deletevalue == front->data)
	{
			cout<<"bihia"<<endl;
		garbage = front;
		front = front->next;
		delete garbage;
		return;

	}
	if (deletevalue == rear->data)
	{
			
		garbage = rear;
		tempnode = front;
		while (tempnode->next != rear)
		{
			tempnode = tempnode->next;
		}

		rear = tempnode;	
		rear->next = front;

		delete garbage;
		return;

	}
	else
		while( tempnode->next != rear)
		{
			if (deletevalue == tempnode->next->data)
			{
				garbage = tempnode->next;
				tempnode->next = tempnode->next->next;
				delete garbage;
				return;
			}
			
		}
  cout<< "Not in list.  Please press enter to continue...";
  cin.ignore();
  cin.ignore();
  return;

  }
}

linklist::linklist (const linklist & original)
{
	front = original.front;
	rear = original.rear;
	nodetype *copy = new nodetype(front->data,NULL);
	nodetype *traverse = front;
	front = copy;

	while(traverse != original.rear)
	{
		traverse = traverse->next;
		copy->next = new nodetype(traverse->data,NULL);
		copy = copy->next;

	}
rear = copy;
	
}
  #2  
Old 04-May-2008, 08:29
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

Re: Link list parameter check


Sorry. False alarm. I was really stuck but in the end figured out a work around.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Str_Misaligned in Double Link List Peter_APIIT C Programming Language 1 29-Feb-2008 21:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Code Snipet For Doubly Linked List Danny C Programming Language 8 19-Feb-2008 12:37
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 21:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:24.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.