User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,944 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,901 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1901 | Replies: 1
Reply
Join Date: Dec 2004
Posts: 1
Reputation: pacbeach is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pacbeach pacbeach is offline Offline
Newbie Poster

Help Help w/ dynamic list funtction definitions

  #1  
Dec 15th, 2004
I'm having trouble defining the functions get_last, delete_last, and add_item. I've been working on the program for days and can't get the function definitions to work at all, so i left them blank. COULD SOMEBODY PLEASE HELP!!!

This program is suppose to define a List class that holds a list of values of type double. The class contains a default constructor that initializes the list to empty with a default maximum size, a constructor with a single argument of type int that specifies the maximum number of entries in the list (the list is initialized to empty), a member function add_item that adds an item to the end of the list, a member function full that returns true if the list is full and false otherwise, a member function get_size that returns the number of values in the list, a member function get_last that returns the value of the last item in the list, and a member function delete_last that deletes the last item on the list. The operator << is overloaded to print objects of type List. The List is implemented using dynamic arrays. Furthermore, the list is itself dynamic in that if there is an attempt to add to a full list the maximum size of the list is doubled, and if deletion causes the size to drop below half of the maximumnumber of entries the maximum size is reduced to half of the original.
#include "stdafx.h"
#include <iostream>
#include <cstdlib>

using namespace std;


const int MAX_LIST_SIZE = 50;

typedef double* ArrayPtr;

class List
{
public:
  List();
  // Initializes the List object to an empty list with size MAX_LIST_SIZE

  List(int maxSize);
  // Initializes the List object to be a list with at most
  // maxSize entries.

  void add_item(double number);
  // Precondition: number has a value
  // Postcondition: The argument number has been added to
  // the list. If the list was full before adding, its maximum
  // size is double what it was before adding.

  bool full() const;
  // Returns true if the list if full; false otherwise.

  friend ostream& operator <<(ostream& outs, const List& theObject);
  // Overloads the << operator so it can be used to output
  // values of type List.
  // Precondition: If outs is a file stream then outs has
  // already been connected to a file.

  int get_size() const;
  // Returns the number of values in the list.

  int get_max() const;
  // Returns the maximum number of entries in the list.

  double get_last ();
  // Precondition: The List object is defined and non-empty.
  // Returns the last value in the list.

  void delete_last();
  // Precondition: The List object is defined.
  // Postcondition: If the list was nonempty, the last item has
  // been deleted from the list array and size has been decremented;
  // otherwise the list remains empty. If deletion of the item
  // caused the size of the list to drop below half the maximum,
  // then the maximum is reduced to half the original and the
  // amount of space used to store the array is half what it was.


private:
  ArrayPtr list;
  int max;   // maximum number of elements
  int size;  // number of array positions filled
};


int main()
{
  //
  // Variable declarations
  // 
  double num;        // a number to be added to the list
  char ans;          // loop control
  int size;

  cout << endl << "Testing the List Class" << endl << endl;

  //
  // Get the maximum number of entries and create a List object
  //
  cout << "What is the maximum number of entries in the list? ";
  cin >> size;
  cout << endl;

  List testList(size);

  //
  // Let the user test add_item, delete_last, and get_last as long
  // as he/she wishes
  //
  do 
    {
      cout << endl;
      cout << "Enter a to add to the list, d to delete the last entry, " << endl;
      cout << "p to print the last item (without deleting), or s to stop: "; 
      cin >> ans;
      
      if (ans == 'a' || ans == 'A')
	{
	  //
	  // Get the number to be added, add it, then output the list
	  //
	  cout << "Enter a number to add to the list: ";
	  cin >> num;
	  testList.add_item(num);
	}
      else if (ans == 'd' || ans == 'D')
	testList.delete_last();
      else if (ans == 'p' || ans == 'P')
	cout << endl << "The last item in the list is " 
	     << testList.get_last() << endl;
      else if (ans != 's' && ans != 'S')
	cout << "Not a valid response - try again!" << endl;  
      
      cout << endl << "Current List" << endl;
      cout << "Capacity: " << testList.get_max() << " Size: "
	   << testList.get_size() << endl;
      cout << "List entries: " << testList << endl;
      
    }
  while (ans != 's' && ans != 'S');
  
  return 0;
}

List::List()
{
  size = 0;
  max = MAX_LIST_SIZE;
}


List::List (int maxSize)
{
  max = maxSize;
  size = 0;
  list = new double[maxSize];
}
  void add_item(double number)
  {
//???????????????
  }

 double get_last()
  {
//??????????????
  }

 void delete_last()
  {
//??????????????
  }

bool List::full() const
{
  return (size == max);
}


ostream& operator <<(ostream& outs, const List& theObject)
{
  for (int i = 0; i < theObject.size; i++)
    outs << theObject.list[i] << "  ";
  return outs;
}


int List::get_size() const
{
  return size;
}


int List::get_max() const
{
  return max;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: Devon - UK
Posts: 420
Reputation: 1o0oBhP is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Help w/ dynamic list funtction definitions

  #2  
Dec 18th, 2004
i have a post on the c/c++ snippets about linked lists. it might help!
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC