I am trying to write a program that finds all the ")"
in an expression and puts them in a linked list, always adding at the beginning of the list. The problem is that when I try to place a new element into the list, the program stops working.
With a sample user input 865)987
:
#include <stdio.h>
#include <stdlib.h>
typedef struct element {
char data;
struct element *next;
} ELEMENT;
int main(void)
{
ELEMENT *first = NULL;
ELEMENT *new_a;
char input[30];
int x=0;
printf("Input expression: ");
scanf("%s", &input);
while(input[x]!='\0'){
if (input[x]==')'){
printf("%c", input[x]); //This works just fine.
new_a->data = input[x]; //Here, the program stops working.
new_a->next = first;
first = new_a;
}
x++;
}
}
What am I doing wrong?