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.
#include <iomanip>
#include <iostream>
#include <typeinfo>
#include <cstdlib>
#include <string>
usingnamespace 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;
}
elseif(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();
}