queue and stack issues

alright im supposed to make a "quack" class (combination of queue and stack). the underlying data struct is a linked list. ive created the quackadt class and override the functions for both other classes (queue class and stack class).
#1) is what im doing so far definitively accurate to the instructions?
#2) getting a lot of errors that i cant debug... ive had a long break in between classes so im rusty and this is due tomorrow, if someone could help point out what im doing wrong thatd be great :))
#3) also my cout function is getting a not defined or declared in this scope even when i include iostream?



this is my "quack" class

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


#ifndef QUACKADT_H
#define	QUACKADT_H

#include <iostream>
#include <cstdlib>


template<class type>
struct node{
    type data;
    node<type> *link;
};

template<class type>
class quack{
public:
    quack();
    ~quack();
    //virtual void initialize()=0;
    virtual bool empty() const=0;
    virtual void push(const type& element)=0;
    //virtual type top() const=0;
    virtual type pop()=0;
    virtual void enqueue(const type& element)=0;
    virtual type dequeue()=0;
private:
    node<type> *first;
    node<type> *last;
};


template<class type>
quack<type>::quack(){
    first=NULL;
}

template<class type>
quack<type>::~quack(){
    node<type> *temp;
    while(first!=NULL){
        temp=first;
        first=first->link;
        delete temp;
    }
};

template<class type>
bool quack<type>::empty() const{
    return (first==NULL);
}

template<class type>
void quack<type>::push(const type& element){
    node<type> *temp;
    temp=new node<type>;
    temp->data=element;
    temp->link=first;
    first=temp;
}

template<class type>
type quack<type>::pop(){
    node<type> *temp, *trail;
    if(first!=NULL){
        temp=first;
        trail=first;
        first=first->link;
        delete temp;
        return trail;
    }
//    else
//        cout<<"The list is empty;"<<endl;
}

template<class type>
void quack<type>::enqueue(const type& element){
    node<type> *temp;
    temp=new node<type>;
    temp->data=element;
    temp->link=NULL;
    
    if (first==NULL){
        first=temp;
        last=temp;
    }
    else{
        last->link=temp;
        last=last->link;
    }
}

template<class type>
type quack<type>::dequeue(){
    node<type> *temp, *trail;
    
    if(!empty()){
        temp=first;
        trail=first;
        first=first->link;
        delete temp;
        
        if (first==NULL)
            last==NULL;
        
        return trail;
    }
}

#endif	/* QUACKADT_H */





declaring an instance i.e.: quack<int> q; results in these errors when i build:

main.cpp:19:16: error: cannot declare variable ‘q’ to be of abstract type ‘quack<int>’
quackADT.h:22:12: note: because the following virtual functions are pure within ‘quack<int>’:
quackADT.h:55:6: note: bool quack<type>::empty() const [with type = int]
quackADT.h:60:6: note: void quack<type>::push(const type&) [with type = int]
quackADT.h:69:6: note: type quack<type>::pop() [with type = int]
quackADT.h:83:6: note: void quack<type>::enqueue(const type&) [with type = int]
quackADT.h:100:6: note: type quack<type>::dequeue() [with type = int]
Last edited on
This is the best I could do to syntactically salvage your code; I didn't check the logic, as it is up to you:

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
template<class type>
struct node
{
    type data;
    node<type> *link;
};

template<class type>
class quackadt
{
public:
    virtual bool empty() const;
    virtual void push(const type&);
    virtual type pop();
    virtual void enqueue(const type&);
    virtual type dequeue();
private:
    node<type> *top;
    node<type> *back;
};

template<class type>
bool quackadt<type>::empty() const
{
    return (top == NULL);
}

template<class type>
void quackadt<type>::push(const type& item)
{
    node<type> *temp;
    temp = new node<type>;
    temp->data = item;
    temp->link = top;
    top = temp;
}

template<class type>
type quackadt<type>::pop()
{
    node<type> *temp, *trail;

    if(top != NULL)
    {
        temp = top;
        trail = top;
        top = top->link;
        delete temp;
        return trail;
    }
}

template<class type>
void quackadt<type>::enqueue(const type& element)
{
    node<type> *temp;
    temp = new node<type>;
    temp->data = element;
    temp->link = NULL;
    
    if (top == NULL)
    {
        top = temp;
        back = temp;
    }
    else
    {
        back->link = temp;
        back = back->link;
    }
}

template<class type>
type quackadt<type>::dequeue()
{
    return pop();
}
yeah ive worked out the fact that i dont need 3 classes so far but im getting new errors can you check the updated post?
dequeue and pop isnt the same because one is last in first out and one is first in first out... do the functions of my class even need to be virtual??
Last edited on
Push places an element at the front of a list, and enqueue at the back. However, both dequeue and pop remove an element from the front. Work this logic out by hand and convince yourself that it holds.
either way that isnt my place of error. idk i feel that my logic is mostly correct, i cant see what the problem is and the error messages arent making any sense i think its just a technicality with virtual functions that im ignorant of
Purely virtual functions are marked by =0 in the prototype, which you have done.
These are different from regular virtual functions because they should not be defined. And any class with purely virtual functions becomes "abstract" (but derived classes can define them). You cannot create instances of abstract classes. After all, what would happen if you called a purely virtual function, a function with no definition, if you could create an object of an abstract class?

Simply remove "=0" to make the function just virtual.
Last edited on
thanks ^^ this was the problem :/
Topic archived. No new replies allowed.