The issue I have is that after entering a value to be used by the variable action, the program immediately ends without doing any of the cases inside the switch. Will post the other functions if needed, but otherwise, I will refrain because it's too long and it will flood the page. Can anyone tell me why this is happening?
Driver code and output are shown below:
int main()
{
// Creation of empty list
struct Node* head = NULL;
bool print = false;
int action, placeNum, numIndex;
while (print == false){
printf("LINKED LIST CREATOR \n");
printf("1. Insert node at the beginning\n");
printf("2. Insert node at the end\n");
printf("3. Insert node at specified position\n");
printf("4. Delete node at the beginning\n");
printf("5. Delete node at the end\n");
printf("6. Delete node at specified position\n");
printf("7. Display the linked list\n");
printf("Please enter the action you want to do: ");
scanf("%d", action);
switch (action)
{
case 1:
printf("Enter the number to be placed at the beginning: ");
scanf("%d", placeNum);
insert_first(&head, placeNum);
break;
case 2:
printf("Enter the number to be placed at the end: ");
scanf("%d", placeNum);
insert_last(&head, placeNum);
break;
case 3:
printf("Enter the index for the number to be placed: ");
scanf("%d", numIndex);
printf("\nEnter the number to be placed at specified index: ");
scanf("%d", placeNum);
insert_middle(numIndex, placeNum, &head);
break;
case 4:
delete_first(&head);
break;
case 5:
delete_last(head);
break;
case 6:
printf("Enter the index of the number to be deleted: ");
scanf("%d", numIndex);
delete_middle(&head, numIndex);
break;
case 7:
printf("\nThe Created Linked List is: ");
printList(head);
print = true;
break;
}
}
return 0;
}
PS C:\Codes> cd "c:\Codes\C++\" ; if ($?) { g++ singlelist.cpp -o singlelist } ; if ($?) { .\singlelist }
LINKED LIST CREATOR
1. Insert node at the beginning
2. Insert node at the end
3. Insert node at specified position
4. Delete node at the beginning
5. Delete node at the end
6. Delete node at specified position
7. Display the linked list
Please enter the action you want to do: 1
PS C:\Codes\C++>