Using Queue/Stacks with classes and Nodes
Oct 26, 2010 at 1:12am UTC
so basically this programs works, but with 1 problem and i know how to fix it, but don't know how to put the code in.
the error i get when i put the code i want in is
Cannot use template 'Node1<T>' without specifying specialization parameters
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.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
class Node1{
public :
int data1;
Node1 *next1;
};
template <class T>
class Queue1{
protected :
T data1;
Node1 *front1, *rear1;
T size1;
T full;
public :
Queue1(T n1=10){size1=0; full=n1;};
~Queue1(){};
int isEmpty(){return !(size1);};
// int isFull(){return !(full-size1);};
void enqueue(T );
int dequeue();
void peekAll();
void peekTop();
};
template <class T>
void Queue1<T>::enqueue(T num){
// if(isFull())
// cout << "\n\n\tList is Full..";
//else{
Node1 *new1 = new Node1;
new1->data1=num;
new1->next1=NULL;
if (isEmpty()){
front1=new1;
rear1=new1;
}
else {
rear1->next1=new1;
rear1=new1;
}
size1+=1; // }
}
template <class T>
void Queue1<T>::peekAll(){
Queue1 temp(size1);
T num;
T size=size1;
cout << "\tList: H-> " ;
for (int i=0; i<size; i++){
num = Queue1::dequeue();
cout << num << " " ;
temp.enqueue(num);
}
for (int i=0; i<size; i++){
num= temp.dequeue();
Queue1::enqueue(num);
}
}
template <class T>
int Queue1<T>::dequeue(){
T value;
if (isEmpty()) cout << "\n\n\tEmpty...." ;
else {
Node1 *temp;
temp=front1;
value=front1->data1;
front1=front1->next1;
delete temp; size1-=1;
}
return value;
}
template <class T>
void Queue1<T>::peekTop(){
if (! isEmpty() ) cout << Queue1::front1->data1;
else cout << "\n\n\tList <Empty>" ;
}
cout << "\n\n\tSelect queue type (defualt=int)" ;
cout << "\n\n\t1.int 2.char 3.double 0.exit >> " ;
cin >> gogo;
if (gogo==1){
int size=0;
int num1;
Queue1<int >s1(num1);
cout << "\n\n" ;
for (int i=0; i<10; i++){
cout << "\tEnter integer <0 to quit> >> " ;
cin >> num1;
if (num1==0) break ;
s1.enqueue(num1);
size++;
}
cout << "\n\tEnqueue complete-----------" ;
cout << "\n" ; s1.peekAll();
cout << "\n\tDequeue-stack---------------------" …
for (int i=0; i<size-1; i++){
cout << "\n\t" ; s1.peekTop(); s1.dequeue(); s1.peekAll();
}
cout << "\n\t" ; s1.peekTop();
}
else if (gogo==2){
char c1;
int size=0;
Queue1<char >s2(c1);
cout << "\n\n" ;
for (int i=0; i<10; i++){
cout << "\tEnter character <0 to quit> >> " ;
cin >> c1;
if (c1=='0' ) break ;
s2.enqueue(c1);
size++;
}
cout << "\n\tEnqueue complete-----------" ;
cout << "\n" ; s2.peekAll();
cout << "\n\tDequeue-stack---------------------" …
for (int i=0; i<size-1; i++){
cout << "\n\t" ; s2.peekTop(); s2.dequeue(); s2.peekAll();
}
cout << "\n\t" ; s2.peekTop();
}
in this working code i am able to run it and it works, but when i select the character option i can put in the letters, but it wont read the tops because of the very top class. here is the code with the error, but i know
here is the code im using that doesn't has the error, but i know is the right theory code because when i changed the 'int data' to 'char data' it would let me use the peektop function to look at the last letter. so here is the code
1 2 3 4 5 6
template <class T>
class Node1{
public :
int data1;
Node1 *next1;
};
Cannot use template 'Node1<T>' without specifying specialization parameters
so how would i can i fix the node1 class and use template
Oct 26, 2010 at 1:39am UTC
1 2 3 4 5 6
template <class T>
class Node1{
public :
int data1;
Node1<T> *next1;
};
Last edited on Oct 26, 2010 at 1:40am UTC
Topic archived. No new replies allowed.