I am trying a new approach since my last post, and I am not sure if the logic is right or why my syntax is incorrect.
Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
char data;
struct node *next;
};
int menu();
struct node *insert(struct node *, char);
void print(struct node *);
void main(void)
{
struct node *front;
int optionsResponse;
char response[25];
front = NULL;
do
{
optionsResponse = menu();
switch(optionsResponse)
{
case 1 :
printf ("Enter a new string: ");
scanf("%s", response);
front = insert(front, response);
printf("\n");
break;
case 2 :
print(front);
break;
}
} while(optionsResponse != 8);
}
int menu()
{
int temp;
printf("Please select from one of the following String menu options: \n\n");
printf("1.\tCreate a new string\n");
printf("2.\tPrint the linked list\n");
printf("8.\tQuit\n\n");
printf("I choose option number: ");
scanf("%d", &temp);
return temp;
}
struct node *insert(struct node *f, char response)
{
int i = 0;
struct node *temp = (struct node *)malloc(sizeof(struct node)), *previous, *current;
temp->data=response[i++];
temp->next=NULL;
while (response[i] != '\0')
{
if (f == NULL)
{
return temp;
}
else {
current = f;
while(current != NULL)
{
previous=current;
current=current->next;
}
previous->next=temp;
temp->next=current;
temp->data = response[i++];
return temp;
}
}
}
void print(struct node *f)
{
printf("List currently contains: ");
while(f != NULL)
{
printf("%c", f->data);
f=f->next;
}
printf("\n");
}
My errors are on lines:
Code:
temp->data=response[i++];
while (response[i] != '\0')
temp->data = response[i++];
and I get the error:
subscript requires array or pointer type
Was I better off the way I had it? What do I need to correct?