compiles there are no errors but it only outputs part of the desired output and
then exits in a segmentation fault
This is the output its printing out

Input is: 25 
10 

- 
100 


115 


- 
- 
300 


2 
5 
* 
/ 
/ 

Contents of input queue: 25 
10 
- 
100 
115 
- 
- 
300 
2 
5 
* 
/ 
/ 
Segmentation fault//After this is should say input reversed

This is the code:

#ifndef MY_QUEUE_H
#define MY_QUEUE_H

#include<iostream>
#include<string>
using namespace std;




template<typename T>
class MyQueue
{
public:
	MyQueue();
	T pop_front();
	void push_back(T item);
	bool isEmpty() const;
	void print() const;
	
private:
	//const int MAXI = 100;<----that didn't work just place the 
	//literal value 100	
	T queue[100];
	int front;
	int back;
	
};//End 

//Create an empty queue
template<typename T>
MyQueue<T>::MyQueue()
{
	front = 0;
	back = 0;
}
//remove and return the first item in the queue
template<typename T>
T MyQueue<T>:: pop_front()
{
	T contain = queue[100];
	front++;
	front = front % 100;
	return contain;
}
//add item to the end of queue
template<typename T>
void MyQueue<T>::push_back(T item)
{
	
	queue[back] = item;
	back++;
	back = back % 100;
	
}
//returns true if the queue is empty
template<typename T>
bool MyQueue<T>::isEmpty() const
{
	if(front == back)
		return true;
	else
		return false;
	
}
//print put the elements of the queue from front to back speratied by one space
template<typename T>
void MyQueue<T>::print()const
{
	for(int i = front; i != back; i = (i + 1) % 100)
		cout << queue[i] << " " << "\n";
	
}//end print()
#endif
#ifndef MYSTACK_H
#define MYSTACK_H

#include <iostream>
//#include <stack>
using namespace std;

template <typename T>
class MyStack
{
public:
    MyStack();
	T pop(); 
    void push(T item);
    bool isEmpty() const;
private:
	int top;
	T* items;
    int MAX;
	
	
};

template <typename T>
MyStack<T>::MyStack()
{
	int size=100;
	MAX=size;
	top=-1;
	items=new T[MAX];
	
}
template <typename T>
T MyStack<T>::pop()
{
	if(isEmpty())
	{
		cout<< "" <<endl;
		// exit(1);
	}
	return items[top--];
}

template <typename T>
void MyStack<T>::push(T item)
{
    if(!isEmpty())
    {
		cout<<"" <<endl;
		// exit(1);
    }
	
    items[++top] = item;
	
}
template <typename T>
bool MyStack<T>::isEmpty() const
{ 
    return top == -1;
}
#endif

Recommended Answers

All 3 Replies

I think the problem lies in the queues section but i cant figure it out for the life of me
please help

Shall we start with MyQueue::pop_front() being completely nonsensical? You always return queue[100], and 100 isn't even a valid index in the array.

I changed this code T contain = queue[100]; to T contain=queue[front]
and it worked but there must be something else in the code wrong because
it says results =nan instead of 1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.