Trouble with priority queue?

I am supposed to insert names and priorities into a priority queue, however I am having difficulties with accepting a string from a user. It says you have to convert from a string to an int. Can someone explain how I would do that? Any help is appreciated :)

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;

struct node
{
	int priority;
	int info;
	struct node *link;
};

class Priority_Queue
{
private:
	node *front;
public:
	Priority_Queue()
	{
		front = NULL;
	}
	
	void insert(string *item, int priority)
	{
		node *tmp, *q;
		tmp = new node;
		tmp->info = item;
		tmp->priority = priority;
		if (front == NULL || priority < front->priority)
		{
			tmp->link = front;
			front = tmp;
		}
		else
		{
			q = front;
			while (q->link != NULL && q->link->priority <= priority)
				q = q->link;
			tmp->link = q->link;
			q->link = tmp;
		}
	}

	void del()
	{
		node *tmp;
		if (front == NULL)
			cout << "Queue Underflow" << endl;
		else
		{
			tmp = front;
			cout << "Deleted item: " << tmp->info << endl;
			front = front->link;
			free(tmp);
		}
	}

	void display()
	{
		node *ptr;
		ptr = front;
		if (front == NULL)
			cout << "Queue is empty\n";
		else
		{
			cout << "Queue is :\n";
			cout << "Priority       Item\n";
			while (ptr != NULL)
			{
				cout << ptr->priority << "                 " << ptr->info << endl;
				ptr = ptr->link;
			}
		}
	}
};

int main()
{
	int choice, priority;
	string item = "";
	Priority_Queue pq;
	do
	{
		cout << "1.Insert" << endl;
		cout << "2.Delete" << endl;
		cout << "3.Display" << endl;
		cout << "4.Quit" << endl;;
		cout << "Enter your choice : ";
		cin >> choice;
		switch (choice)
		{
		case 1:
			cout << "Input the name : ";
			cin >> item;
			cout << "Enter its priority : ";
			cin >> priority;
			pq.insert(item, priority);
			break;
		case 2:
			pq.del();
			break;
		case 3:
			pq.display();
			break;
		case 4:
			break;
		default:
			cout << "Not a choice" << endl;
		}
	} while (choice != 4);
	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct node
{
	int priority;
	// int info;
        std::string info ;
	struct node *link;
};

// ...

class Priority_Queue
{
private:
	node *front;
public:
	Priority_Queue()
	{
		front = NULL;
	}
	
	// void insert(string *item, int priority)
        void insert( string item, int priority)
	{
              // .... 
thank you so much!
Line 58: use the operator delete instead of calling the function free()
1
2
// free(tmp); // line 58
delete tmp ; // *** created with new, so use delete  

See: https://isocpp.org/wiki/faq/freestore-mgmt#mixing-malloc-and-delete
Topic archived. No new replies allowed.