Palindrome Checker in C Using Stacks and Queues
Classified in Computers
Written on in
English with a size of 4.23 KB
C Program Implementation for Palindrome Checking
This program demonstrates how to utilize Stacks and Queues to determine if a specific word is a palindrome. By leveraging dynamic memory allocation and linked list structures, the code compares characters in both forward and reverse order.
Header Files and Structure Definitions
First, we include the necessary libraries for standard input/output, memory management, and string manipulation. We define the nodop, node, and nodoPILA structures to manage our data.
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct nodop {
char palabra[15];
};
struct node {
struct nodop pal;
struct node *next;
};
typedef struct node *COLA;
struct nodoPILA {
struct nodop pal2;
struct nodoPILA *siguiente2;
};
typedef struct nodoPILA *PILA;Initialization and Utility Functions
The following functions handle the creation of the Queue (COLA) and the Stack (PILA), as well as checking if the structures are empty.
void create(COLA *start, COLA *final) {
(*start) = NULL;
(*final) = NULL;
}
void crearPILA(PILA *start) {
(*start) = NULL;
}
int empty(COLA home) {
if (home == NULL)
return 1;
else
return 0;
}Data Entry and Manipulation
We implement functions to enter data into the queue and ingresar data into the stack. These functions ensure that characters are stored correctly for later comparison.
void enter(COLA *start, COLA *final, char word) {
COLA nuevo;
nuevo = (COLA)malloc(sizeof(struct node));
nuevo->pal.palabra[0] = word;
nuevo->next = NULL;
if ((*final) == NULL) {
(*final) = nuevo;
(*start) = nuevo;
} else {
(*final)->next = nuevo;
(*final) = nuevo;
}
}
void ingresarPILA(PILA *start, char word) {
PILA nuevo;
nuevo = (PILA)malloc(sizeof(struct nodoPILA));
nuevo->pal2.palabra[0] = word;
nuevo->siguiente2 = NULL;
if ((*start) == NULL) {
(*start) = nuevo;
} else {
nuevo->siguiente2 = (*start);
(*start) = nuevo;
}
}Display and Retrieval Functions
The display and mostrarPILA functions retrieve characters from the queue and stack, respectively, while freeing the allocated memory.
char display(COLA *start, COLA *final) {
COLA q;
char a;
a = (*start)->pal.palabra[0];
if ((*start)->next == NULL) {
free(*start);
(*start) = NULL;
(*final) = NULL;
} else {
q = (*start);
(*start) = (*start)->next;
q->next = NULL;
free(q);
}
return a;
}
char mostrarPILA(PILA *start) {
PILA q;
char b;
b = (*start)->pal2.palabra[0];
if ((*start)->siguiente2 == NULL) {
free(*start);
(*start) = NULL;
} else {
q = (*start);
(*start) = (*start)->siguiente2;
q->siguiente2 = NULL;
free(q);
}
return b;
}Main Execution Logic
The main function coordinates the user input, populates the data structures, and performs the final comparison to determine if the word is a palindrome.
void main() {
struct nodop pal;
COLA start, final;
PILA inicio2;
char a, b;
int i = 0, e = 0, equal = 0;
clrscr();
create(&start, &final);
crearPILA(&inicio2);
printf("Enter word: ");
gets(pal.palabra);
e = strlen(pal.palabra);
do {
a = pal.palabra[i];
enter(&start, &final, a);
ingresarPILA(&inicio2, a);
i++;
} while (i < e);
while (!empty(start) && equal == 0) {
a = display(&start, &final);
b = mostrarPILA(&inicio2);
printf("\na = %c - b = %c", a, b);
if (a != b)
equal = 1;
}
if (equal == 1)
printf("\nIt is NOT a palindrome");
else
printf("\nIt IS a palindrome");
getch();
}