Linked list movies

My current code is where I enter in how many movies there (integer "max") are and enter in the names of said movies. After that I've been trying to ask the ticket sales for each movies, but there is a slight problem. I obviously want to enter in the ticket sales corresponding to the number of movies playing, but the only way to do so is to be redundant and ask again about how many movies are playing to get the max for my main function. Is there a way in which I can transfer the info of "max" from line 42 to my main function without be redundant? If not should I just wipe everything and start from scratch?
P.S. I did linked list simply so I can brush up on it and doubt it's practical in this instance, so that why I'm wondering if I should just wipe.
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
 #include <iostream>
#include <string>
#include <vector>
using namespace std;

class Node
{
public:
	Node() 
	{
		
	};
	void add(int ticket);
	bool isempty();
	void del();
	void display();
private:
	int data;
	Node* front = NULL;
	Node* rear = NULL;
	Node* link;
};

class movies
{
public:
	movies()
	{
		
	}
	void movie();
	vector<string> getmovie();
private:
	vector<string> names;
	string name;
	int max;
};

void movies::movie()
{
	cout << "How many movies are playing?" << endl;
	cin >> max;
	cout << "Enter in the name(s) of the movie(s)" << endl;
	for (int i = -1; i < max; i++)
	{
		getline(cin, name);
		names.push_back(name);
	}
}

vector<string> movies::getmovie()
{
	for (auto i = names.begin(); i != names.end(); ++i)
	{
		cout << *i << endl;
	}
	return names;
}

void Node::add(int ticket)
{
	Node* ptr = new Node;//new node created
	ptr->data = ticket;//the ticket is now part of the new data for the new node
	ptr->link = NULL;//end of first node
	if (front == NULL)//entering the first node
	{
		front = ptr;//both point to the first value entered
		rear = ptr;
	}
	else
	{
		rear->link = ptr;//now linking to new node
		rear = ptr;//moving rear to begining of new node
	}
}

bool Node::isempty()
{
	if (front == NULL && rear == NULL)
	{
		return true;
	}
	else
		return false;
}

void Node::del()
{
	if (isempty())
	{
		cout << "Node is empty" << endl;
	}
	else if (front == rear)//there is only one element in the queue
	{
		free(front);//freeing the memory of the only present element
		front = rear = NULL;//making sure that everything is empty
	}
	else
	{
		Node* ptr = front;//ptr is currently at the front 
		front = front->link;//move the ptr to the next node
		free(ptr);//now deleting the first node 
	}
}

void Node::display()
{
	if (isempty())
	{
		cout << "Node is empty" << endl;
	}
	else
	{
		Node* ptr = front;//ptr is at front of the all the data
		while (ptr != NULL)
		{
			cout << ptr->data << " ";
			ptr = ptr->link;//moving ptr to the next node
		}
	}
}

int main()
{
	movies enter2;
	Node enter;
	int ticket = 0;
	int max;
	string name;
	enter2.movie();
	cout<<"--------"<<endl;
	cout << "Movies: " << endl;
	cout << "--------";
	enter2.getmovie();
	cout << "Tickets for each movie:";
	for (int i = 0; i < max; i++)
	{
		cin >> ticket;
	}
}
> Is there a way in which I can transfer the info of "max" from line 42 to my main function without be redundant?
This is pretty much a sign that you may have chosen the wrong data structure(s).

> My current code is where I enter in how many movies there (integer "max")
Give it a better name.

> movies enter2;
> Node enter;
Ditto, better names.

> enter2.getmovie();
An appalling name for something which
- prints data (which would not be obvious from just looking at the function name)
- returns a vector, indicating plural rather than singular.

You ignore the return result, which would give you your answer anyway.
vector<string> playing = enter2.getmovie();
for ( size_t i = 0 ; i < playing.size() ; i++ )


> free(front);//freeing the memory of the only present element
Absolutely NOT.
You created this object using 'new', so you must use 'delete' to dispose of it properly.


> for (int i = -1; i < max; i++)
So why do you go round max+1 times here?

> string name;
There's no reason for this to be a member of the class.
It should be a local variable in the only function which uses it.

Yes, your identifier names are definitely confusing.
If your class Node should work as a linked list, do call it List or a similar name. A node is usually a single element of a linked list.
Following your name choice, your Node::add() method adds an entire new list…

To make a flexible linked list, it’s common to see a struct Node and a class LinkedList that manages such Nodes.

- - -
What should your program do? Simulate a cinema? If so, I think you should start from a class Cinema, which could for example contain Rooms and sell Tickets.
A Movie could be played in more than a one Room, with an extra charge for those which provide 3D…

Making all this classes ‘cooperate’ could soon become challenging.
In addition to what Salem C wrote:
1
2
3
4
5
6
7
class Node
{
...
	Node* front = NULL;
	Node* rear = NULL;
	Node* link;
};

You're making a common mistake of confusing a linked list with an item in the list. You need two classes. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Node {
    int data;
    Node *link;
};

class List
{
public:
	void add(int ticket);
	bool isempty();
	void del();
	void display();
private:
	Node* front = NULL;
	Node* rear = NULL;
};


Do you need a rear pointer? It complicates the code.
I see...Thanks for the constructive criticism. I'm currently creating a new slate with some of all of your suggestions. I might stray from linked list for this program since it's not exactly practical here it appears. As said before, I've been wanting to incorporate a linked list to see if it was doable and was curious. Seeing how things ended up, it's best I scrap that.
Last edited on
Topic archived. No new replies allowed.