Logo 
Search:

C++ Programming Forum

Ask Question   UnAnswered
Home » Forum » C++ Programming       RSS Feeds

detecting palindromes

  Asked By: Pitso    Date: Oct 12    Category: C++ Programming    Views: 649
  

Hi,
I want to create a program that detects palindroms in c++ using stack(arrays) and queue(linked list). I have already created my stack and queue, so I want them tho work together... I want to make it this way, when the user inserts a string, character should be inserted into a stack and a queue, one character of the string at a time, then when the user deletes the characters, front and top have to be the same, then we will say its a palindrome. So can anyone help? Thank you

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Pitso Mike     Answered 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;
}


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




Tagged: