I am trying to reverse a given string using stacks. I am using linked lists as it takes up less memory compared to arrays. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 100
struct Stack{
char ele;
struct Stack *next;
};
struct Stack* next_node(char element){
struct Stack *node=(struct Stack *)malloc(sizeof(struct Stack));
node->ele=element;
node->next=NULL;
return node;
}
int isEmpty(struct Stack *node){
return node==NULL;
}
void push(struct Stack **node, char element){
struct Stack *temp=next_node(element);
temp->next=*node;
*node=temp;
}
char pop(struct Stack **node){
if(isEmpty(*node)){
return 'a';
}
struct Stack *temp=*node;
*node=(*node)->next;
free(temp);
}
void rev(char str[]){
int i;
int n=strlen(str);
struct Stack *s=(struct Stack *)malloc(sizeof(struct Stack));
for(i=0;i<n;i++)
push(&s, str[i]);
for(i=0;i<n;i++)
str[i]=pop(&s);
printf("The reversed string is: %s\n", str);
}
int main()
{
char string[M], op[1];
do{
printf("Enter the string to be reversed: ");
scanf("%s", string);
rev(string);
printf("Do you want to go again?(Y/N): ");
scanf("%s", op);
}while(op[0]=='Y');
}
However, I do not get any output, it simply says, "The reversed string is: "
I tried a slightly different code by replacing
node->ele=element;
with
strcpy(node->ele, element);
But this gives me a warning, which says:
warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast
I can't wrap my head around why such things is happening. Any help is appreciated! :-)