I need help with the following program:
Create a structure STRING which represents a string ( as singly linked list).
Check if the string is a palindrome using stack and function which prototype is int palindrome(STRING *str);
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *info;
}STRING;
typedef struct node
{
STRING str;
struct node *next;
}NODE;
void formList(NODE **head,STRING *str)
{
NODE *node=(NODE *)malloc(sizeof(NODE));
node->str=*str;
node->next=NULL;
if(*head==NULL)
*head=node;
else
{
NODE *temp=*head;
while(temp->next)
temp=temp->next;
temp->next=node;
}
}
void push(NODE **tos,STRING *str)
{
NODE *node=(NODE *)malloc(sizeof(NODE));
node->str=*str;
node->next=*tos;
*tos=node;
}
int palindrome(STRING *str)
{
NODE **head;
NODE *temp=*head;
NODE *temp1=*head;
NODE *tos=NULL;
while(temp!=NULL)
{
push(&tos,str);
temp=temp->next;
}
while(temp1!=NULL)
{
if(temp1==tos)
{
temp1=temp1->next;
tos=tos->next;
}
else
return 0;
}
return 1;
}
void read(STRING *str)
{
printf("info:");
scanf("%s",str->info);
}
int main()
{
NODE *head=0;
STRING str;
read(&str);
formList(&head,&str);
if(palindrome(str.info)==1)
printf("string is a palindrome");
else
printf("string is not a palindrome");
return 0;
}
Segmentation faults:
|75|warning: passing argument 1 of 'palindrome' from incompatible pointer type [enabled by default]|
|39|note: expected 'struct STRING *' but argument is of type 'char *'|
|42|warning: 'head' is used uninitialized in this function [-Wuninitialized]|
How to resolve these errors?