Testing palindromes with stack and queue

I'm reading a list of words that is terminated by $$$$$

madam
Madam I'm Adam.
A man, a plan, a canal, Panama!
Never odd or even
Amor, Roma
race car
step on NO pets
Able was I, ere I saw Elba
$$$$$

I want to get each line into a queue and stack to which I will compare if the top of the stack is the same as the front of the queue
1
2
3
4
5
6
7
8
9
10
11
  if (s -> top < q -> front) 
 	pal=-1;
  else if (s -> top > q -> front) 
	pal=1;
  else pal=0;
s->top=s->top->next;
q->front=q->front->next;
		if(pal==0)
		  cout<<words<<" - Is a palindrome"<<endl;
		else
		  cout<<words<<" - Is not a palindrome"<<endl;


My issue is that I'm not properly getting the code to compare each line as I want it. I'm a student at this, and it is required that I use char, stacks and queues only.
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>

using namespace std;
struct Node{
	char data;
	Node *next;
};

struct Stack{
	Node *top;
};

struct Queue{
	Node *front;
	Node *rear;
};

Stack *initStack(){
	Stack *s;
	s=(Stack*)malloc(sizeof(Stack));
	s->top=NULL;
}

Queue *initQueue(){
	Queue *q;
	q=(Queue*)malloc(sizeof(Queue));
	q->front=NULL;
	q->rear=NULL;
return q;
}

bool isEmpty(Queue *q){
	return (q->front==NULL);
}

bool isEmpty(Stack *s){
	return (s->top==NULL);
}

Node *createNode(char c){
	Node *newNode;
	newNode=(Node*)malloc(sizeof(Node));
	newNode->data=c;
	newNode->next=NULL;
return newNode;
}

Node *insertAtHead(Node *top, char c){
	Node *newNode;
	newNode= createNode(c);
	newNode->next=top;
	top=newNode;
return top;
}

Node *deleteAtHead(Node *top){
	Node *curr;
	curr=top;
		if(top==NULL)
			return NULL;
		else{
			curr=top;
			top=top->next;
			free(curr);
			return top;
		}
}

void push(Stack *s, char c){
	s->top=insertAtHead(s->top, c);
}

char pop(Stack *s){
	char c;
	Node *top;
		if(isEmpty(s)){
				cout<<"Stack is Empty";
				return 'X';
			}
		top=s->top;
		c=top->data;
		s->top=deleteAtHead(s->top);
	return c;
}

char peek(Stack *s){
	char c;
	Node *top;
		if(isEmpty(s)){
			cout<<"Stack is Empty";
			return 'X';
		}	
		top=s->top;
		c=top->data;
	return c;
}
void enqueue(Queue *q, char c){
	Node *newNode= createNode(c);
		if(isEmpty(q)){
			q->front=newNode;
			q->rear=newNode;
		}else{
			q->rear->next=newNode;
			q->rear=newNode;
		}

}

char dequeue(Queue *q){
	char hold = q->front->data;
	Node *temp=q->front;
	q->front=q->front->next;
		if(q->front==NULL)
			q->rear=NULL;
	free(temp);
	return hold;
}

bool isUpperCase (char c) {
 if (c >= 'A' && c <= 'Z')
 return true;
 return false;
} 

char toLower(char ch ){
 if (isUpperCase(ch))
 ch = ch + 32;
 return ch;
} 

bool isLetter(char ch) {
 if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
 return true;
 return false;
}


int main(){
    ifstream in;
    in.open("input.txt");
    int i=0;
    int num=0;
    char ch[150];
    char words[150];
    char newCh[150];
    char c;
    
    Queue *q;
    q=initQueue();
    Stack *s;
    s=initStack();
    
int pal=0;
for(int j=0; j<150; j=j+1) {
	in.getline(words, 150, '\n');
    	words[j]=toLower(words[j]);
    	enqueue(q, words[j]);
		push(s, words[j]);
		
			if (s -> top < q -> front) 
 				pal=-1;
	 	
 			else if (s -> top > q -> front) 
		 		pal=1;
		else pal=0;
		s->top=s->top->next;
		q->front=q->front->next;
			
			
			if(pal==0)
				cout<<words<<" - Is a palindrome"<<endl;
			else
			cout<<words<<" - Is not a palindrome"<<endl;	
			
	}


		
	
    return 0;
}
Last edited on
it is required that I use char, stacks and queues only.

Is it possible you’re asked to use std::stack and std::queue?
http://en.cppreference.com/w/cpp/container/stack
http://en.cppreference.com/w/cpp/container/queue

in.getline(words, 150, '\n');
std::basic_istream::getline() reads all the needed characters in a single operation: you may not want that instruction inside your 150-iterations for-loop.

To compare character by character your queue to your stack you don’t need that “int pal”: just stop at the first not matching character.

I think the exercise is about loading the *entire* read line inside a stack and inside a queue and after that comparing them character by character - since stack is a FILO structure and a queue is a FIFO one, that should become straightforward.

If you want to stuck to your code, I think you need to add two functions, one which pushes the entire line inside your stack and one inside your queue.

May I ask you, just out of curiosity, what type of course are you attending? Because your code is mostly C code, with just some C++ features.
Computer Programming III from a Computer Science course
first convert everything to all caps. strip out everything that's not a letter. compare that string to itself. way easier than writing the logic to compare something piecemeal on the unconverted string... If its required that you use certain features, you can use those features anyway, but still take the simplest approach ;-)
Topic archived. No new replies allowed.