My program takes in an input file from the command line and converts the string from the file into a linked list and then depending on the command it will manipulate the string to either reverse the list, print the list, or take a character out...I'm having trouble taking a character out, my code compiles fine but doesn't change the string at all
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 1024
typedef struct node
{
char data;
struct node *next;
} node;
int main(int argc, char **argv)
{
char chr;
char str[SIZE];
char key;
char buffer[SIZE];
FILE *file = fopen(argv[1], "r");
if (file == 0)
{
printf("Could not open file\n");
}
fgets(buffer, SIZE, file);
node *new_head = stringToList(buffer);
while(fscanf(file, " %c", &chr) != EOF){
if(chr == '!')
printList(new_head);
if(chr == '~')
new_head = reverseList(new_head);
if(chr == '-')
{
fscanf(file, "%c", &key);
fscanf(file, "%s", str);
new_head = replaceChar(new_head, key, "");
}
}
fclose(file);
return 0;
}
node *stringToList(char *str)
{
node *temp = NULL;
node *tail = NULL;
for(; *str != '\0'; str++)
{
if (temp==NULL)
{
temp=(node*)malloc(sizeof(node));
temp->data = *str;
temp->next = NULL;
tail = temp;
}
else
{
tail->next = (node*)malloc(sizeof(node));
tail = tail->next;
tail->data = *str;
tail->next = NULL;
}
}
return temp;
}
node *replaceChar(node *head, char key, char *str)
{ node *temp;
if(head == NULL)
return NULL;
if(head->data == key)
{
temp = head->next;
free(head);
return temp;
}
head->next = replaceChar(head->next, key, "");
return head;
}
node *reverseList(node *head){
node *temp;
node *rev_head = NULL;
while(head != NULL){
temp = head->next;
head->next = rev_head;
rev_head = head;
head = temp;
}
return rev_head;
}
void printList(node *temp)
{
node *p;
for(p=temp; p != NULL; p = p->next)
{
printf("%c", p->data);
}
printf("\n");
}
The input.txt file has the following
hello
!
~
- e
!