How to create a simple list?

Hello
I very much ask to help me with the decision of one problem. There is a sample of a code.

You can describe a simple list as follows:
• The simple list can hold zero or more items of some particular type.
• You can create an empty list.
• You can add items to the list.
• You can determine whether the list is empty.
• You can determine whether the list is full.
• You can visit each item in the list and perform some action on it.
As you can see, this list really is simple; it doesn’t allow insertion or deletion, for
example.
Design a List class to represent this abstract type. You should provide a list.h header
file with the class declaration and a list.cpp file with the class method implementations.
You should also create a short program that utilizes your design.
The main reason for keeping the list specification simple is to simplify this programming
exercise. You can implement the list as an array or, if you’re familiar with the data type,as a linked list. But the public interface should not depend on your choice. That is, the
public interface should not have array indices, pointers to nodes, and so on. It should be
expressed in the general concepts of creating a list, adding an item to the list, and so on.
The usual way to handle visiting each item and performing an action is to use a function
that takes a function pointer as an argument:
void visit(void (*pf)(Item &));
Here pf points to a function (not a member function) that takes a reference to Item
argument, where Item is the type for items in the list. The visit() function applies this
function to each item in the list. You can use the Stack class as a general guide.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//class Stack
// stack.h -- class definition for the stack ADT
#ifndef STACK_H_
#define STACK_H_
typedef unsigned long Item;
class Stack
{
private:
 enum {MAX = 10}; // constant specific to class
 Item items[MAX]; // holds stack items
 int top; // index for top stack item
public:
 Stack();
 bool isempty() const;
 bool isfull() const;
 // push() returns false if stack already is full, true otherwise
 bool push(const Item & item); // add item to stack
 // pop() returns false if stack already is empty, true otherwise
 bool pop(Item & item); // pop top into item
};
#endif
_____________________________________________
// stack.cpp -- Stack member functions
#include “stack.h”
Stack::Stack() // create an empty stack
{
   top = 0;
}

bool Stack::isempty() const
{
  return top == 0;
}

bool Stack::isfull() const
{
  return top == MAX;
}

bool Stack::push(const Item & item)
{
  if (top < MAX)
   {
     items[top++] = item;
     return true;
   }
    else
     return false;
}

bool Stack::pop(Item & item)
{
   if (top > 0)
    {
      item = items[--top];
      return true;
    }
     else
      return false;
}
____________________________

// stacker.cpp -- testing the Stack class
#include <iostream>
#include <cctype> // or ctype.h
#include “stack.h”
int main()
{
using namespace std;
 Stack st; // create an empty stack
 char ch;
 unsigned long po;
 cout << “Please enter A to add a purchase order,\n”
      << “P to process a PO, or Q to quit.\n”;
while (cin >> ch && toupper(ch) != ‘Q’)
 {
  while (cin.get() != ‘\n’)
  continue;
   if (!isalpha(ch))
    {
      cout << ‘\a’;
      continue;
    }
    switch(ch)
    {
      case ‘A’:
      case ‘a’: cout << “Enter a PO number to add: “;
      cin >> po;
         if (st.isfull())
          cout << “stack already full\n”;
           else
            st.push(po);
             break;
      case ‘P’:
      case ‘p’: if (st.isempty())
                 cout << “stack already empty\n”;
                  else 
                  {
                    st.pop(po);
                    cout << “PO #” << po << “ popped\n”;
                   }
                 break;
 }
 cout << “Please enter A to add a purchase order,\n”
      << “P to process a PO, or Q to quit.\n”;
}
cout << “Bye\n”;
return 0;
}


p.s.I Have tried to create 2 classes, but that that not so..., as it should seems to me with objects to work class functions-members, and here it is necessary that functions were not included into a class. Or it is necessary to use structure?? Please help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//list.h
#ifndef LIST_H_
#define LIST_H_
class Data
{
    private:
        char fullname[35];
        double payment;
    public:
        Data();
        Data(const char fn, double pm);
        ~Data();
        void Rename();
        void Repayment();
};
typedef Data Item;
class List
{
    private:
        enum{MAX = 10};
        Item items[MAX];
        int top;
        double summ;
    public:
        List();
};
 
        void visit(void (*pf)(Item & pa));
        void isempty(const Item & it);
        void isfull(const Item & it);
        void push(const Item & it);
        void pop(Item & it);
        void Funct_Sum(const Item & parStruct);
#endif
____________________________
//list.cpp
#include "list.h"
#include <iostream>
//class Data
Data::Data()
{
    strspy(fullname,"no name")
    payment = 0;
}

Data::Data(const char fn, double pm)
{
    strncpy(fullname,fn,34);
    fullname[34] = '\0';
    payment = pm;
}

Data::~Data()
{
    
}
void Data::Rename(char fn)
{
      strncpy(fullname,fn,34);
      fullname[34] = '\0';
}
void Data::Repayment(double pm)
{
    payment = pm;
}

//class List
List::List()
{
    top = 0;
}
void isempty(const Item & it)
{
    if(top == 0)
     cout<<"List is empty\n";
}
void isfull(const Item & it)
{
    if(top == MAX)
      cout<<"List is full\n;
}
void push(const Item & it)
{
    if(top < MAX)
    {
        items[top++] = it;
        return true;
    }
    else
     return false;
}

void pop(Item & it)
{
    if(top > 0)
    {
        it = items[--top];
        return true;
    }
    else
     return false;
}
void Funct_Sum(const Data & parData)
{
    using namespace std;
    static double summ = 0; 
    summ += parData.payment;
    cout<<" The report on profit:"<<summ<<endl;
}
void visit(void (*pf)(Item & pa))
{
    cout<<(*pf)(Item & pa);
}
___________________________________-
//lister.cpp
#include <cstdlib>
#include <iostream>
#include <cctype>
#include "list.h"

int main(int argc, char *argv[])
{
    using namespace std;
    List st;
    char ch;
    Data object;
        while(toupper(ch) != 'Q')
        {
            while(cin.get() != '\n')
             continue;
             if(!isalpha(ch))
             {
                    cout<<'\a';
                    continue;
             }
                               
                visit(isfull(st));
                visit(push(object));
                visit(isempty(st));
                visit(pop(object));
                visit(Funct_Sum(object));
        }  
        cout<<"Bye\n!";                               
    system("PAUSE");
    return EXIT_SUCCESS;
}
 

I didn't see a question in there. What's your question?
My question and is described in a topic.
And if is is more concrete? That how to realise to me this function?
I like have described a problem, whether not so?
Or I simply not there have placed the top?
I kind a lost here:

1. Isn't that function like
1
2
3
4
5
6
void visit(void (*pf)(Item & pa));
        void isempty(const Item & it);
        void isfull(const Item & it);
        void push(const Item & it);
        void pop(Item & it);


should be a member function instead of free function ?

2. Even if you insist on free function, should it be working on class List, not Data ?
3. How do you expect function like isempty to access member variable top if it isn't even work on instance of class List ?
4. Do you even remotely understood how class work in C++ ?
1
2
3
4
5
void isfull(const Item & it)
{
    if(top == MAX)
      cout<<"List is full\n;
} 


Should be

1
2
3
4
5
void isfull(const Item & it)
{
    if(top == MAX)
      cout<<"List is full\n"; // <-- Closing quote
}


Last edited on
Topic archived. No new replies allowed.