I'm supposed to write a function that scan input from user till enter and enter it to linked list. I cant scan it into a String and than put it in a list.
I wrote this code and doesn't enter the chars to linked list and doesn't exit from the console till I press Exit.
typedef struct chNode
{
char data;
struct chNode *next;
}chNode;
chNode * createCharList(char data)
{
chNode *temp = (chNode*)malloc(sizeof(chNode));
temp->data = data;
temp->next = NULL;
return temp;
}
chNode * addCharToLast(chNode *head, char data)
{
chNode *p = head;
chNode *temp = createCharList(data);
if (head == NULL)
return temp;
while (p->next != NULL)
p = p->next;
p->next = temp;
return head;
}
chNode* insert_Charlist()
{
char ch;
chNode *Head = NULL;
printf("Enter chars For Linked-List Till 'Enter':\n");
scanf_s("%c", &ch);
while (ch != '\n')
{
Head = addCharToLast(Head, ch);
scanf_s("%c", &ch);
}
return Head;
}
void main()
{
chNode *Orignial_list = NULL;
Orignial_list = insert_Charlist(); // Function that imports a list
printf("You Entered This linked-list:\n");
printf_CharList(Orignial_list); //Printing the linked list
getch();
}