C++ List Program, Study notes of Data Structures and Algorithms

The implementation of a simple C++ program for creating and managing a list using an array with a maximum size of 100. The program includes a constructor, destructor, methods for adding and printing items, and a main function for testing the functionality.

Typology: Study notes

2019/2020

Uploaded on 10/09/2021

Syazzz
Syazzz 🇲🇾

4 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PROGRAM 5.1.1
// List.h
// Declaration of class List
#ifndef LIST_H
#define LIST_H
const int maxSize = 100;
class List
{
public:
List();
~List();
int numberOfItem(); // Return number of item
void addItem(int item); // Add item
void printItem(); // Print item in the list
private :
int size; // size shows the number of item in the list
int array[maxSize]; // array that keep the item
};
#endif
// List.cpp
// Define the implementation of all methods in class List
#include <iostream>
using namespace std;
#include "List.h"
List::List() // Constructor
{ size = 0; }
List::~List() // Destructor
{}
int List::numberOfItem() // Return the size of the list
{
return size;
}
void List::addItem(int item) // Add new item
{
if (size == maxSize)
{
cout <<"***There is no more place left****\n";
return;
}
array[size] = item;
size++; // Add 1 to variable size
}
pf3
pf4
pf5

Partial preview of the text

Download C++ List Program and more Study notes Data Structures and Algorithms in PDF only on Docsity!

PROGRAM 5.1.

// List.h // Declaration of class List #ifndef LIST_H #define LIST_H const int maxSize = 100; class List { public: List(); ~List(); int numberOfItem(); // Return number of item void addItem(int item); // Add item void printItem(); // Print item in the list private : int size; // size shows the number of item in the list int array[maxSize]; // array that keep the item }; #endif // List.cpp // Define the implementation of all methods in class List #include using namespace std; #include "List.h" List::List() // Constructor { size = 0; } List::~List() // Destructor {} int List::numberOfItem() // Return the size of the list { return size; } void List::addItem(int item) // Add new item { if (size == maxSize) { cout <<"***There is no more place left****\n"; return; } array[size] = item; size++; // Add 1 to variable size }

void List::printItem() // Print items in list { for (int i = 0; i < size; i++) cout << array[i] << " "; cout << "\n"; } // ListMain.cpp // Implementation // Create object and method implementation #include using namespace std; #include "List.h" void main() { int item; List ItemList; cout << "Now there are : " << ItemList.numberOfItem() << " item in the list\n\n"; cout << "Enter a digit : "; cin >> item; for (int i = 0; i < 10; i++) { ItemList.addItem(item); cout <<"Item " << item++ << " is added."; } cout << "Now there are : " << ItemList.numberOfItem() << " item in the list\n\n"; cout << "The list are : \n"; ItemList.printItem(); }

PROGRAM 5.

// List.h // Declaration of class List #ifndef LIST_H #define LIST_H const int maxSize = 100; class List { public: List(); ~List(); int numberOfItem(); // Return number of item void addItem(int item); // Add item void printItem(); // Print item in the list private : int size; // size shows the number of item in the list int array[maxSize]; // array that keep the item }; #endif // List.cpp // Define the implementation of all methods in class List #include using namespace std; #include "List.h" List::List() // Constructor { size = 0; } List::~List() // Destructor {} int List::numberOfItem() // Return the size of the list { return size; } void List::addItem(int item) // Add new item { if (size == maxSize) { cout <<"***There is no more place left****\n"; return; } array[size] = item; size++; // Add 1 to variable size

void List::printItem() // Print items in list { for (int i = 0; i < size; i++) cout << array[i] << " "; cout << "\n"; } //ListMain.cpp //Implementation //Create object and method implementation #include using namespace std; #include "List.h" void main() { int item; List ItemList; cout << "Now there are : " << ItemList.numberOfItem() << " item in the listt\n\n"; cout << "Enter a digit:"; cin >> item; for (int i = 0; i < 10; i++) { ItemList.addItem(item); cout << "Item " << item << " is added."; item += 2; } cout << "Now there are: " << ItemList.numberOfItem() << " item in the list\n\n"; cout << "The list are: \n"; ItemList.printItem(); } OUPUT: