Priority Queue

I am losing data. somewhere and im not sure where. I am pretty sure it is in the pushqueue function.....it is suppose to put the symbols in order by its priority.
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
#include <iomanip>
#include <iostream>
#include <typeinfo>
#include <cstdlib>
#include <string>

using namespace std;

// Specification file for a linked list abstract data type class.
// This linked list is actually a stack, since all additions and
// removals are at the head.
template<class type>
struct node // Each node has two fields:
{
        type data; // a data field,
        node<type> *next; // and a pointer field.
        type priority;
};

template<class type>
class stack
{
        private:
                node<type> *head; // Pointer to the first cell.

        public:

                stack();
                void push(type item);
                void pushprior(type item, type priority);
                type pop();
                void view();
                void pushqueue(type item, type priority);
};

template<class type>
stack<type>::stack()
{
        head=NULL;
}

template<class type>
void stack<type> :: push (type item)
{
        node<type> *t = new node<type>;
 t -> data = item;
        t -> next = head;
        head = t;

}
//adds the int into the list and then sorts it to where it should be located
template<class type>
void stack<type>::pushqueue (type item,type priorityt)
{
        node<type> *t = new node<type>;
        node<type> *tmp = new node<type>;
        node<type> *trail = new node<type>;
        //trail = NULL;

        if(head == NULL) //Startig the list
        {
                t-> data = item;
                t->priority=priorityt;
                t -> next = head;
                head = t;
        }
        else if(head != NULL)
        {
                t = head;
                while(t != NULL && priorityt > t->priority){ //Puts inside the list
                trail = t;
                t = t->next;
                }
        tmp->data = item;
        tmp->priority=priorityt;
        tmp->next = t;
        trail->next = tmp;
        }
}
// Function to remove the first element from the stack and
// return it to the caller.
template<class type>
type stack<type> :: pop ()
{

        node<type>* cur;
        cur = head;
        if(cur == NULL)
        {
                cout<<"Nothing to pop"<<endl;;
}

        else
        {
                head = cur -> next;
                return cur->data;
                delete cur;
        }
}
// Accessor functions.
// Function to see whether an element is on the list.
template<class type>
void stack<type> :: view ()
{
        node<type>* tmp = head;

        while(tmp != NULL){
                cout << tmp -> data;
                tmp = tmp -> next;
                cout<<endl;
        }
        cout<<endl;
}

int main()
{

stack<char> b;
stack<int> c;

b.pushqueue('6',3);
cout<<endl;
b.pushqueue('@',4);
cout<<endl;
b.pushqueue('b',2);

b.view();
}



Output right now:

6
@
Last edited on
And could you not post the same topic multiple times?
Topic archived. No new replies allowed.