case 1 work fine but case 2 and 3 just stop the loop and the program and i cannot choose any other case after that as! i wonder why it stop the loop from going as choice here would never equal 0 which i believe it's the only reason would stop the loop from going forward! thanks in advance.
i also checked the functions and all of them seems fine to me i'm not sure if the problem could be with them
#include "stdafx.h"
#include <iostream>
using namespace std;
void insert_node(int new_data);
void print_node();
void delete_node();
int new_data;
char choice;
struct node {
int data;
node* next;
};
node* head = NULL;
void main()
{
do
{
cout << "Enter 1 to insert a new node \n";
cout << "Enter 2 to disply the list \n";
cout << "Enter 3 to delete the last node \n";
cin >> choice;
switch (choice)
{
case '1':
cout << "Enter the data \n";
cin >> new_data;
insert_node(new_data);
break;
case '2':
if (head != NULL)
print_node();
else
cout << "SORRY, your list is empty \n";
break;
case '3':
delete_node();
break;
default:
cout << "Invalid entry \n";
}
} while (choice != '0');
}
void insert_node(int new_data)
{
node* NewNode = new node;
NewNode->data = new_data;
if (head == NULL)
head = NewNode;
else
{
NewNode->next = head;
head = NewNode;
}
}
void print_node()
{
node* printer=head;
do
{
cout << printer->data<<" - ";
printer = printer->next;
} while (printer != NULL);
}
void delete_node()
{
if (head == NULL)
cout << "no node to be deleted \n";
else
{
node* curr = head;
node* prev = NULL;
while (curr->next != NULL)
{
prev = curr;
curr = curr->next;
}
if (prev == NULL)
{
delete(curr);
head = NULL;
return;
}
prev->next = NULL;
delete(curr);
}
}