GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC 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 28-Apr-2008, 03:30
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road
Arrow

Cicrular Linked list insertion


I am totally lost on this one. I am stuck on populating a circular link list with random generated ints. I cannot get the list ordered. My biggest problem is with inserting a value greater than the front. Any advice would be gratefully received....

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, *place;
	for ( i = 0; i<numOfInts; i++)
	{ int rnum = rand();
	  rnum = rnum % 100;
	  cout<< "num = "<<rnum<<endl;
	  nodetype *newnode = new nodetype(rnum,NULL);
		if( front == NULL && rear == NULL)
		{cout<<"in null if"<<endl;
			front = rear = newnode;
			cout<<"front data "<<front->data;
		}
		if(rnum<front->data)
	            {
		  tempnode = front;
		  front = newnode;
		  front->next = tempnode;
		  rear->next = front;
		  cout<<"inside if loop"<<endl;
		  newnode->data = rnum;
		  cout<<"in rnum<front loop"<<endl;
		  cout<<"tempnode data"<<tempnode->data<<endl;
		}
		else
		{
                            cout<<"inside else"<<endl;
		  place = front;
		   while ( place != rear )
		   {
		     if (rnum< place->data)
		     {
		        tempnode = place;
		        place = newnode;
		        newnode->data = rnum;
		        newnode->next = tempnode;
		     }
		 place = place->next;
		
		 }
		
	}
         }
cin.ignore();
cin.ignore();
}

/*void linklist::insert( int insertvalue)
{

}*/

void linklist::print() const
{
	nodetype *traverse = front;
	while( traverse != rear)
	{
		cout<<traverse->data<<" ";
		traverse = traverse->next;
	}
	cin.ignore();
	cin.ignore();
}
  #2  
Old 29-Apr-2008, 12:38
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

Re: Cicrular Linked list insertion


I received some guidance from my professor last night on this project. So, please disregard this post. I may still have questions and if so, will clean up and post a tighter, more concise question.

Best regards,

Paul
 
 

Recent GIDBlog2nd Week of IA Training 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 20:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Linked list: differing results on same code. Howard_L C Programming Language 17 28-Jun-2007 23:36
search linked list itsmecathys CPP / C++ Forum 20 18-Apr-2005 01:34

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

All times are GMT -6. The time now is 15:20.


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