Logo 
Search:

C++ Programming Answers

Ask Question   UnAnswered
Home » Forum » C++ Programming       RSS Feeds
  Question Asked By: Pitso Mike   on Oct 13 In C++ Programming Category.

  
Question Answered By: Pitso Mike   on Oct 13

here is my queue code
#include<iostream>

#include<cstdlib>

using namespace std;

struct node{

int info;

struct node *next;

};

class Queue{

private:

node *rear;

node *front;

public:

Queue();

void enqueue();

void dequeue();

void display();

};

Queue::Queue(){

rear = NULL;

front = NULL;

}

void Queue::enqueue(){

int data;

node *temp = new node;

cout<<"Enter the data to enqueue: ";

cin>>data;

temp->info = data;

temp->next = NULL;

if(front == NULL){

front = temp;

}else{

rear->next = temp;

}

rear = temp;

}

void Queue::dequeue(){

node *temp = new node;

if(front == NULL){

cout<<"\nQueue is Emtpty\n";

}else{

temp = front;

front = front->next;

cout<<"The data Dequeued is "<<temp->info;

delete temp;

}

}

void Queue::display(){

node *p = new node;

p = front;

if(front == NULL){

cout<<"\nNothing to Display\n";

}else{

while(p!=NULL){

cout<<endl<<p->info;

p = p->next;

}

}

}

int main(){

Queue queue;

int choice;

while(true){

cout<<"\n1.Enqueue\n2. Dequeue\n3. Display\n 4.Quit";

cout<<"\nEnter your choice: ";

cin>>choice;

switch(choice){

case 1:

queue.enqueue();

break;

case 2:

queue.dequeue();

break;

case 3:

queue.display();

break;

case 4:

exit(0);

break;

default:

cout<<"\nInvalid Input. Try again! \n";


}
}



here is the stack

#include <cstdlib>
#include <iostream>
#include "stack.h"


Stack::Stack(int size /*= 10*/) {
values = new double[size];
top = -1;
maxTop = size - 1;
}

void Stack::Push(const double x) {
if (IsFull()) // if stack is full, print error
cout << "Error: the stack is full." << endl;
else
values[++top] = x;
}

double Stack::Pop() {
if (IsEmpty()) { //if stack is empty, print error
cout << "Error: the stack is empty." << endl;
return -1;
}
else {
return values[top--];
}
}

double Stack::Top() {
if (IsEmpty()) {
cout << "Error: the stack is empty." << endl;
return -1;
}
else
return values[top];
}

void Stack::DisplayStack() {
cout << "top -->";
for (int i = top; i >= 0; i--)
cout << "\t|\t" << values[i] << "\t|" << endl;
cout << "\t|---------------|" << endl;
}


Share: 

 
 
Didn't find what you were looking for? Find more on detecting palindromes Or get search suggestion and latest updates.


Tagged: