can someone please fix this error in the stack file at line 18 (
class Stack2:public Stack
{
)
this is thew error ----> expected class name before '{' token
Queue.h -->
#include "Stack.hpp"
template <typename ITEM>
class Queue
{
private:
ITEM item;
Stack <ITEM> * s1= new Stack<ITEM>;
Stack <ITEM> * s2= new Stack<ITEM>;
public:
Queue(){};
bool enqueue (ITEM item);
bool dequeue (ITEM &item);
bool check = true;
~Queue();
}; //this is the class which contains the functions of the queue class which I will use the stack to implement it.
template <typename ITEM>
Queue<ITEM>::~Queue(){}; //this is the destructor
template <typename ITEM>
bool Queue<ITEM>::enqueue (ITEM item)
{
if(check)
{
s1->push(item);
}
else
{
Stack <ITEM> * temp = new Stack<ITEM>;
temp=s1;
s1=s2;
s2=temp;
s1->push(item);
check = false;
}
}
template <typename ITEM>
bool Queue<ITEM>::dequeue (ITEM &item)
{
if(check)
{
Stack <ITEM> * temp = new Stack<ITEM>;
temp=s1;
s1=s2;
s2=temp;
s2->pop(item);
check=false;
}
else
s2->pop(item);
}
Stack.hpp --->
#include "ListLinkedList.hpp"
template <typename ITEM>
class Stack
{
private:
ListLinkedList <ITEM> l;
public:
Stack(){};
virtual bool push (ITEM item){return l.append(item);};
virtual bool pop (ITEM &item){ return l.removeEnd(item);};
~Stack(){};
};
template <typename ITEM>
class Stack2:public Stack
{
private:
ListLinkedList <ITEM> l;
public:
Stack2(){};
virtual bool push (ITEM item){return l.append(item);};
virtual bool pop (ITEM &item){return l.removeAt(0);};
~Stack2(){};
};
ListLinkedList.hpp --->
#include <iostream>
using namespace std;
template <typename ITEM>
class Node
{
private:
ITEM item;
public:
Node <ITEM> * back;
Node <ITEM> * next;
Node ();
Node(ITEM p_item);
void setNext(Node <ITEM> * n);
void setBack(Node <ITEM> * b);
Node<ITEM> * getNext();
Node<ITEM> * getBack();
ITEM & getItem();
~Node();
};
template <typename ITEM> Node<ITEM>::Node () { next = back = NULL;}
template <typename ITEM> Node<ITEM>::Node(ITEM p_item):Node() { item = p_item; }
template <typename ITEM> void Node<ITEM>::setNext(Node <ITEM> * n){ next = n;}
template <typename ITEM> void Node<ITEM>::setBack(Node <ITEM> * b) { back = b; }
template <typename ITEM> Node<ITEM> * Node<ITEM>::getNext() { return next; }
template <typename ITEM> Node<ITEM> * Node<ITEM>::getBack(){ return back; }
template <typename ITEM> ITEM & Node<ITEM>::getItem() { return item;}
template <typename ITEM> Node<ITEM>::~Node(){cout<<"deleting" << item <<endl;}
template <typename ITEM>
class ListLinkedList
{
private:
int size;
Node<ITEM> * getIndex(int index);
Node<ITEM> * findItem(ITEM item);
Node<ITEM> * head;
Node<ITEM> * tail;
public:
ListLinkedList();
void emptyList(); // O(n)
void printList(); // O(n)
int removeAt (int index); // O(n)
ITEM * getItemAt (int index);
int find(ITEM item);
bool insert(ITEM item,int index);
int remove(ITEM item); // O(n)
int append(ITEM item); // O(1)
bool removeEnd(ITEM & item); // O(1)
bool removeStart (ITEM & item); //O(1)
int getSize (); // O(1)
bool isEmpty(); // O(1)
~ListLinkedList();
};
template <typename ITEM>
Node<ITEM> * ListLinkedList<ITEM>::getIndex (int index)
{
int i = 0;
for (Node <ITEM> * cur = head ;cur != NULL; cur = cur->getNext(), i++)
if ( i == index) return cur;
return NULL;
}
template <typename ITEM>
ITEM * ListLinkedList<ITEM>::getItemAt (int index) {
int i = 0;
for (Node <ITEM> * cur = head ;cur != NULL; cur = cur->getNext(), i++)
if ( i == index) return &(cur->getItem()); return NULL;
}
template <typename ITEM>
Node<ITEM> * ListLinkedList<ITEM>::findItem (ITEM item)
{
for (Node <ITEM> * cur = head ;cur != NULL; cur = cur->getNext())
if ( cur->getItem() == item ) return cur;
return NULL;
}
template <typename ITEM>
int ListLinkedList<ITEM>::removeAt (int index) {
Node <ITEM> * cur = getIndex(index); if ( cur != NULL)
{
if ( cur != tail ) cur->getNext()->setBack(cur->getBack()); else tail = cur->getBack();
if ( cur != head ) cur->getBack()->setNext(cur->getNext()); else head=cur->getNext();
delete (cur);
size --;
}
return size;
}
template <typename ITEM>
ListLinkedList<ITEM>::ListLinkedList ()
{
head = tail = NULL;
size = 0;
}
template <typename ITEM>
void ListLinkedList<ITEM>::emptyList() {
if (isEmpty()) return;
Node <ITEM> * cur = NULL;
while (head != NULL)
{
cur = head;
head = head->getNext();
delete cur;
}
head = tail = NULL;
}
template <typename ITEM>
void ListLinkedList<ITEM>::printList ()
{
for (Node <ITEM> * cur = head; cur != NULL; cur = cur->getNext())
cout << cur->getItem() << endl;
}
template <typename ITEM>
int ListLinkedList<ITEM>::find (ITEM item)
{
int i = 0;
for (Node <ITEM> * cur = head ;cur != NULL; cur = cur->getNext(), i++) if ( cur->getItem() == item ) return i;
return -1;
}
template <typename ITEM>
bool ListLinkedList<ITEM>::insert (ITEM item,int index)
{
if ( isEmpty()) {
head=tail=new Node <ITEM> (item);
size++;
return true;
}
Node <ITEM> * cur = getIndex(index);
if ( cur != NULL) {
Node <ITEM> * newNode = new Node <ITEM> (item);
newNode->setNext(cur);
newNode->setBack(cur->getBack());
cur->setBack(newNode);
if ( cur == head)
head = head->getBack();
else
newNode->getBack()->setNext(newNode);
size++;
return true;
}
else return false;
}
template <typename ITEM>
int ListLinkedList<ITEM>::remove (ITEM item)
{
Node <ITEM> * cur = findItem(item);
if ( cur != NULL) {
if ( cur != tail ) cur->getNext()->setBack(cur->getBack());
else tail = cur->getBack();
if ( cur != head ) cur->getBack()->setNext(cur->getNext());
else head=cur->getNext();
delete (cur);
size --;
}
return size;
}
template <typename ITEM>
int ListLinkedList<ITEM>::append(ITEM item)
{
if ( isEmpty()) {
head=tail=new Node <ITEM> (item);
size++;
return true;
}
tail->setNext(new Node <ITEM> (item));
tail->getNext()->setBack(tail);
tail = tail->getNext();
size++;
return size;}
template <typename ITEM>
bool ListLinkedList<ITEM>::removeEnd(ITEM & item)
{
if ( isEmpty()) return false;
Node <ITEM> * cur = tail;
if ( head != tail){
tail = tail->getBack();
tail->setNext(NULL);
} else {
cur = head;
head=tail=NULL;
}
item = cur->getItem();
delete(cur);
return true;
}
template <typename ITEM>
bool ListLinkedList<ITEM>::removeStart (ITEM & item)
{
if ( isEmpty()) return false;
Node <ITEM> * cur = head;
if ( tail != head){
head = head->getNext();
head->setBack(NULL);
} else {
cur = tail;
head=tail=NULL;
}
item = cur->getItem();
delete(cur);
return true;
}
template <typename ITEM>
int ListLinkedList<ITEM>::getSize ()
{
return size;
}
template <typename ITEM>
bool ListLinkedList<ITEM>::isEmpty()
{
if ( head == NULL)
return true;
else return false;
}
template <typename ITEM>
ListLinkedList<ITEM>::~ListLinkedList ()
{
emptyList();
}
main.cpp --->
#include"Queue.h"
int main()
{
char a;
int b;
cout<<"This is a Queue using stack program"<<endl;
Queue<int> x;
while(true)
{
cout<<"Please Enter y to enter and n to print"<<endl;
cin>>a;
if((a=='y')||(a=='Y'))
{
cout<<"Please enter the number you want to add the the Queue"<<endl;
cin>>b;
x.enqueue(b);
cout<<"Data entered"<<b<<endl;
}
else
{
cout<<"printing the data from queue"<<endl;
x.dequeue(b);
}
}
}This all program is correct.but there is one error in line 18.
Which is " expected class name before '{' token"
template <typename ITEM>
class Stack2:public Stack
{
Here you have typed wrong syntax.
Here you derived your class Stack2 from Stack which template or generic class so you have to write syntax like this,
Correct syntax is
template <typename ITEM>
class Stack2:public Stack<ITEM>
{
you forgot to write datatype <ITEM> in your syntax.
if you have any query then feel free to ask in comment.
can someone please fix this error in the stack file at line 18 ( class Stack2:public...
C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...
C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes: -head: first node in the list -tail: last node in the list #include "circDLLNode.h" template class { public: //Default constructor: creates an empty list (); //Destructor: deallocate memory ~(); ...
C++ Error. I'm having trouble with a pointer. I keep getting the
error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)"
The objective of the assignment is to revise the public method
add in class template LinkedBag so that the new
node is inserted at the end of the linked chain instead of at the
beginning.
This is the code I have:
linkedBag-driver.cpp
BagInterface.hpp
LinkedBag.hpp
Node.hpp
int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...
CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node { char c; Node *next; }; class LinkedString { Node *head; public: LinkedString(); LinkedString(char[]); LinkedString(string); char charAt(int) const; string concat(const LinkedString &obj) const; bool isEmpty() const; int length() const; LinkedString substring(int, int) const; //added helper function to add to linekd list private: void add(char c); };...
What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 { private Node head, tail; private int size; public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...
Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...
- implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...