When I tried to build the following code, although the compiler didn't give an error or a warning, I met with the following picture:
Then, I noticed that in the book, there is a newline character(\n) inside the function scanf as follows:
Code:
scanf("\n%c",&item); // QUESTION 1
Because I didn't write '\n' inside the function scanf, I met above picture. But why? I don't understand that the work is done by '\n' inside the function scanf. Can you explain this situtation?
*****************************************(1)
The other thing that I don't understand that the process of adding node. Normally if we first add character B, then if we secondly add character A, we see that;
(Shortly after the function insert)
---------------------------
Then if we add character C, does it have to be in this manner:
(Shortly after the function insert)
So does it have to be output the following:
(1) A -- > C -- > B -- > NULL
But, when program runs, The output is in this way:
(2) A -- > B -- > C -- > NULL
I think it mustn't be as above figure (2). It must be figure (1). Because, in the code there are no parts of code about adding to last of list. There are just parts of code about adding new node at beginning of list, and adding new node between previousPtr and currentPtr. I focused the while statement in the function insert, but result in my head didn't change. If you want to see my processing steps as above picture, look at the next my post here.(At DOWN)
Code:
#include <stdio.h>
#include <stdlib.h>
struct listNode{
char data;
struct listNode *nextPtr;
};
typedef struct listNode ListNode;
typedef ListNode* ListNodePtr;
void insert(ListNodePtr *sPtr,char value);
void instructions(void);
void printList(ListNodePtr currentPtr);
int main()
{
ListNodePtr startPtr=NULL;
char item;
int choice;
instructions();
printf("> ");
scanf("%d",&choice);
while(choice != 2){
switch(choice){
case 1:
printf("Enter a character: ");
scanf("\n%c",&item); // QUESTION 1
insert(&startPtr,item);
printList(startPtr);
break;
default:
printf("\nIncorrect choice.\n");
instructions();
break;
}
printf("> ");
scanf("%d",&choice);
}
printf("End of run.\n");
return 0;
}
void insert(ListNodePtr *sPtr,char value)
{
ListNodePtr newPtr,currentPtr,previousPtr;
newPtr = malloc(sizeof(ListNode));
if(newPtr != NULL) {
newPtr->data = value;
newPtr->nextPtr=NULL;
previousPtr = NULL;
currentPtr = *sPtr;
while(currentPtr != NULL && value > currentPtr->data) {
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if(previousPtr == NULL){
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else{
printf("%c not inserted. No memory available.\n",value);
}
}
void printList(ListNodePtr currentPtr)
{
if(currentPtr == NULL){
printf("List is empty.\n\n");
}
else {
printf("The list is:\n");
while(currentPtr != NULL){
printf("%c --> ",currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
printf("NULL\n\n");
}
}
void instructions(void)
{
printf("\nEnter a choice :\n"
"1 - Insert an element into the list.\n"
"2 - End of run.\n");
}
*****************************************(2)