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 10-Mar-2007, 22:54
baka_yaro baka_yaro is offline
New Member
 
Join Date: Mar 2007
Posts: 4
baka_yaro is on a distinguished road

Help with some functions


I need help writing a few functions. I'm haveing difficulties with them functions: bag bag::operator=(const bag& b), bool bag::operator==(const bag& b), bool bag::erase(const value_type& target).
Thank you for any help.

CPP / C++ / C Code:
// FILE: bag1.h
// CLASS PROVIDED: bag 
//
// TYPEDEF and MEMBER CONSTANTS for the bag class:
//   typedef ____ value_type
//     bag::value_type is the data type of the items in the bag. It may be any of
//     the C++ built-in types (int, char, etc.), or a class with a default
//     constructor, an assignment operator, and operators to
//     test for equality (x == y) and non-equality (x != y).
//
//   typedef ____ size_type
//     bag::size_type is the data type of any variable that keeps track of how many items
//     are in a bag.
//
//   static const size_type CAPACITY = _____
//     bag::CAPACITY is the maximum number of items that a bag can hold.
//
// CONSTRUCTOR for the bag class:
//   bag( )
//     Postcondition: The bag has been initialized as an empty bag.
//
//  MODIFICATION MEMBER FUNCTIONS for the bag class:
//  INSERT_TOP
//   void insert_top(const value_type& entry)
//     Postcondition: A new copy of entry has been added to the top of the bag.
//
//
//  INSERT BOTTOM
//  bool insert_bottom(const value_type& entry)
//	Postcondition: A new copy of entry has been inserted at the end/bottom
//		of the bag
//
//  ERASE
//   bool erase(const value_type& target);
//   Postcondition: All copies of target have been removed from the bag.
//   The return value is the number of copies removed (which could be zero).
//
//
//  ITERATORS
// Set current to the first item
//void bag::begin(); 
//
// Set current to the next item on the list
//bool bag::next()
//
// Set current to the previous item on the list
//bool bag::previous()
//
// Set current to to the last item on the list
//void bag::end()

// GET
// Return the current item on the list
//value_type get_current_item(); 
    
// BOOL
// True if current points to first item, false otherwise
//bool at_beginning() 
//
// true if current points to the last item, false otherwise
//bool at_end()
//
// true if current points to a valid item in the bag
//bool valid_current() 
//
// CONSTANT MEMBER FUNCTIONS for the bag class:
//   size_type size( ) const
//     Postcondition: The return value is the total number of items in the bag.
//

#ifndef MAIN_SAVITCH_BAG1_H
#define MAIN_SAVITCH_BAG1_H
#include <cstdlib>  // Provides size_t
#include "cr_circuits_entry.h"

typedef cr_circuits_entry value_type; // USER_SPECIFIED_TYPE must be defined before we et here

class bag
{ 
	// TYPEDEFS
	typedef struct node
	{
		value_type entry;
		node * next; 
	}Node;
	
	// VAR DEFS
	Node * head;  // head, tail, current
	Node * tail;
	Node * current;
	size_type used;     //Num items on the list
	    
	bool delete_flag; 
	    
	public: 
    // CONSTRUCTOR
    bag( );  
    // COPY CONSTRUCTOR
    bag(const bag& b);  
    
    // OPERATORS
    // ASSIGNMENT
    bag operator=(const bag& b); 
    // EQUAL
    bool operator==(const bag& b); 
    
    // MODIFICATION MEMBER FUNCTIONS
    // erase one item in the bag with the specified target
    bool erase(const value_type& target); 
    // erase all items in the bag
    void erase(); 
    
	// insert at the top of the list
    bool insert_top(const value_type& entry); 
    // insert bottom
    bool insert_bottom(const value_type& entry); 
    
    // make copy of bag list
    Node * make_copy_of_list() const; 
    
    // Find
    Node * find_last_node() const; 
    Node * find_next_to_last_node();
    
   // delete the top Node
   bool delete_top(); 
   // delete the bottom Node
   bool delete_bottom();
    
    // ITERATORS
    void begin(); // Set current to the first item
    bool next(); // Set current to the next item on the list if there is a  next item
    bool previous(); // Set current to the previous item on the list
    void end(); // Set current to to the last item on the list
    
    // GET
    value_type get_current_item(); // Return the current item on the list 
    Node * get_current() const; // return the contents of the current ptr.
    bag  get_cr_circuit_calculations() const; // return the bag and its items 
    
    // BOOL
    bool at_beginning(); // True if current points to first item, false otherwise
    bool at_end(); // true if current points to the last item, false otherwise
    bool valid_current();
    
    // CONSTANT MEMBER FUNCTIONS
    size_type size( ) const;
};
// NONMEMBER FUNCTIONS for the bag class
bag operator +(const bag& b1, const bag& b2);
#endif

CPP / C++ / C Code:
// FILE: bag1.cpp
// CLASS IMPLEMENTED: bag (see bag1.h for documentation)
// INVARIANT for the bag class:
//   The number of items in the bag is in the member variable used;


#include <algorithm> // Provides copy function
#include <cassert>   // Provides assert function
#include <iostream>
#include <string> 
#include "bag1.h"
using namespace std;



// CONSTRUCTOR
bag::bag()
{
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////
	head = tail = current = NULL; 
	used =0;
///////////////////////////  HERE  ////////////////////////////// 
}

// COPY CONSTRUCTOR
bag::bag(const bag& b)
{
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////
	head = b.make_copy_of_list(); 
	tail = b.find_last_node(); 
	current = b.get_current(); 
	used = b.size();
///////////////////////////  HERE  //////////////////////////////
}


// OPERATORS
// ASSIGNMENT
bag bag::operator=(const bag& b)
{
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////
return *this;
///////////////////////////  HERE  //////////////////////////////
}


// EQUAL
bool bag::operator==(const bag& b)
{
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////

///////////////////////////  HERE  ////////////////////////////// 
}



// MODIFICATION MEMBER FUNCTIONS
bool bag::insert_top(const value_type& entry)
{   
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////
	Node * new_node = new Node;
	if ( ! new_node) {
			cout<<"Insert: Out of Memory"<<endl;
			return false;
	};
	new_node->entry = entry;
	new_node->next = head;
	if(! head) tail=new_node;
	head=new_node;
	current=new_node;
	return true;


///////////////////////////  HERE  //////////////////////////////
}

bool bag::insert_bottom(const value_type& entry)
{
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////
	Node * new_node = new Node;
	if ( ! new_node) {
			cout<<"Insert: Out of Memory"<<endl;
			return false;
	}
	Node * last_node = find_last_node();
	new_node-> entry = entry;
	if(last_node){
		last_node->next = new_node;
		current = tail = new_node;}
	else
		head = current = tail = new_node;
	
	new_node->next = 0;
	return true;
///////////////////////////  HERE  //////////////////////////////
}


// makes copy of bag list
bag::Node * bag::make_copy_of_list() const
{
	
	if(!head)
		return NULL; 
	
	Node * new_head = new Node;
	if(!new_head)
	{
		cerr << "bag::make_copy_of_list - Out of memory.." << endl;
		return NULL;
	}
	
	Node * wrk_ptr = new Node; 
	Node * new_wrk_ptr = new_head; 
	while(wrk_ptr)
	{
		new_wrk_ptr->entry = wrk_ptr->entry;
		new_wrk_ptr->next = NULL;  // set to null just in case there are no more Nodes

		if(wrk_ptr->next)
		{
			new_wrk_ptr->next = new Node;
			if(!new_wrk_ptr->next)
			{
				cerr << "bag::make_copy_of_list(1) - Out of memory." << endl; 
				return new_head; 
			}
			
			new_wrk_ptr = new_wrk_ptr->next;
			new_wrk_ptr->next = NULL; // set to null just in case there are no more Nodes
			wrk_ptr = wrk_ptr->next;
		}
	}
	
	return head; 
}



// Removes the item from the bag that has an entry that 
// is equal to the specified target 
bool bag::erase(const value_type& target)
{
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////
	
	return true;
///////////////////////////  HERE  //////////////////////////////
}


void bag::erase()
{
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////
	while (head != NULL){
		Node *wrk_ptr;
		wrk_ptr = head;
		head=head->next;
		delete wrk_ptr;
	}
///////////////////////////  HERE  //////////////////////////////
}



bool bag::delete_top()
{
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////
	if ( ! head) return false;
	Node * wrk_ptr = head;
	head = head->next;
	delete wrk_ptr;
	return true; 
///////////////////////////  HERE  //////////////////////////////
}



bool bag::delete_bottom()
{
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////
	if (! head) return false;
	Node *wrk_ptr = find_last_node();
	if( wrk_ptr == head){
		delete head;
		head = NULL;
		return true;}
	delete wrk_ptr->next;
	wrk_ptr->next = NULL;
	return true;
///////////////////////////  HERE  //////////////////////////////
}


bag::Node * bag::find_last_node() const
{
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////
	Node *wrk_ptr = head;
	while(wrk_ptr && wrk_ptr->next){
		wrk_ptr=wrk_ptr->next;
	}
	return wrk_ptr;
///////////////////////////  HERE  //////////////////////////////
}



bag::Node * bag::find_next_to_last_node()
{
/////////////// WRITE YOUR CODE BETWEEN HERE AND ///////////////
	Node *wrk_ptr = head;
	while(wrk_ptr && wrk_ptr->next && wrk_ptr->next->next){
		wrk_ptr=wrk_ptr->next;
	}
	return wrk_ptr;
///////////////////////////  HERE  //////////////////////////////

}



// ITERATORS
// Set current to the first item
void bag::begin()
{
	current = head; 
}

// Set current to the next item on the list
bool bag::next()
{
	if(current && current->next)
	{
		current = current->next; 
		return true;
	}
	
	return false;
}


// Set current to the previous item on the list
bool bag::previous()
{
	if(current == head) return false; // already at top, there is no previous 
	
	Node * wrk_ptr = head;
	while(wrk_ptr && (wrk_ptr->next == current))
		wrk_ptr = wrk_ptr->next; 
		
	current = wrk_ptr;
	return true; 
}


// Set current to to the last item on the list
void bag::end()
{
	current = tail; 
}


// GET
value_type bag::get_current_item()
{
	if(current)
		return current->entry; 
	
	return (*new value_type); // return empty item
}


bag::Node * bag::get_current() const
{
	return current; 
}


bag bag::get_cr_circuit_calculations() const
{
	return *this; 
}

// BOOL
bool bag::at_beginning()
{
	return current == head;
}


bool bag::at_end()
{
	return current == tail;
}


bool bag::valid_current()
{
	return  current != NULL;
}


// Return the number of items in the bag
size_type bag::size() const
{
	return used; 
}
  #2  
Old 12-Mar-2007, 22:30
baka_yaro baka_yaro is offline
New Member
 
Join Date: Mar 2007
Posts: 4
baka_yaro is on a distinguished road

Re: help with some functions


Is my question too abstract or too difficult to answer?
  #3  
Old 13-Mar-2007, 01:45
Gamer_2k4's Avatar
Gamer_2k4 Gamer_2k4 is offline
Member
 
Join Date: Apr 2005
Location: Wisconsin
Posts: 117
Gamer_2k4 will become famous soon enough

Re: help with some functions


For the == operator, compare each variable in the two objects. For example:
CPP / C++ / C Code:
if (head == b.head) //&& var1 == b.var1, etc.
    return true;

return false;

Of course, that only works if you've overloaded the == operator for the Node class and every other class you're comparing.

As for the = operator, why would it be any different from the copy constructor?

BTW, does "head = tail = current = NULL;" actually work? I thought I tried it in one of my programs and received an error.
__________________
Gamer_2k4
  #4  
Old 14-Mar-2007, 06:56
InfiniteLoop InfiniteLoop is offline
New Member
 
Join Date: Mar 2007
Posts: 20
InfiniteLoop is on a distinguished road

Re: help with some functions


Quote:
BTW, does "head = tail = current = NULL;" actually work? I thought I tried it in one of my programs and received an error

Assuming that they are all the same data type, yes.

Now for the OP questions. I encounter this problem alot with people that I work with. Lets do an example:

CPP / C++ / C Code:
//For our purposes class ClassName contains one member which is int num
//It can be accessed using getNum() and setNum(int)
//lets say in out main we want to do:
ClassName obj1(3); 
ClassName obj2(5);

ClassName obj3 = obj1

//then in our class definition, we need to define our overloaded =
void Bag::operator=(const Bag &copy)
{
//in here we copy all relevant data from one object (obj1) to another
//which in this case is (obj3)
    setNum(copy.getNum());
}

gamer_2k4 already hit on the other one, and go ahead and try the last one and let us know what you get.
 
 

Recent GIDBlogReview: Gel laptop cooling pad 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
Windows C++ Problem with Class and Functions Alloishus C++ Forum 3 28-Sep-2006 19:34
friend functions error CaptnB C++ Forum 2 12-Jun-2006 14:39
Please help with functions... brookeville C++ Forum 36 05-Nov-2004 01:23
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 09:11
Understanding functions tommy69 C Programming Language 15 15-Mar-2004 18:59

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

All times are GMT -6. The time now is 21:16.


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