**This is a singly linked list and the professor wants me 2 find max min from define limit of singly list list ( similar 2 find array but use singly linked list instead ) however when i want 2 remove the number in singly linked list even if i'm not finish with it it get struck here is my code thank you for the contribution i'm a newbie and i don't know many things just a university student. and the number before menu is how many numbers that singly linked list can hold **
# include<bits/stdc++.h>
using namespace std;
class Node
{
public:
int n;
Node*next;
};
bool isEmpty(Node*head)
{
if (head==NULL)return true;
else return false;
}
char menu()
{
char choice;
cout<<"Menu\n";
cout<<"1.Add an item.\n";
cout<<"2.Remove an item.\n";
cout<<"3.Show The List.\n";
cout<<"4.Exit.\n";
cin>>choice;
return choice;
}
void insert(Node*&head,Node*&last,int n)
{
Node*node = new Node(); // allocate memory 2 node let node be an abstract data
node->n = n; // define data in the new node as new data (saving data define in there)
node->next = NULL; // Let next of the new node as head
head = node; // let pointer name head point new node
last = node;
}
void append(Node*&head,Node*&last,int n)
{
if(isEmpty(head)) insert(head,last,n);
else
{
Node*node = new Node(); // allocate memory 2 node let node be abstract data
node->n = n; // define data in the new node as new data (saving data define in there)
node->next = NULL; // This new node is going to be the last node, so make next of it as NULL
last->next = node; // Change the next of last node
last = node;
}
}
void remove(Node*&head,Node*&last)
{
if(isEmpty(head))cout<<"The List is Empty.\n";
else if (head==last)
{
delete head;
head == NULL;
last == NULL;
}
else
{
Node *node=head;
head =head->next;
delete node;
}
}
void print(Node*node) // Function Prints Content of the singly linked list
{
if(isEmpty(node))cout<<"The list is empty\n";
else
{
cout << "The List contains: \n";
while (node!= NULL)
{
cout<<node->n<<endl;
node = node->next;
}
}
}
int main()
{
Node* head = NULL; // Start with empty List
Node* last = NULL;
char choice;int i,n,x;
cin>>x;
for(i=0;i<x;i++)
{
choice = menu();
switch(choice)
{
case '1': cout<<"Please enter a number: ";
cin>>n;
append(head,last,n);
break;
case '2': remove(head,last);
break;
case '3': print(head);
break;
default: cout<< "System exit\n";
}
} while(choice!='4'); return 0;
}
**Reality Output : 3 ( This is the number of data that singly-linked list can hold) Menu
- Add an item.
- Remove an item.
- Show the List.
- Exit. 1 Please enter a number: 22 Menu
- Add an item.
- Remove an item
- Show The List.
- Exit. 1 Please enter a number: 12 Menu
- Add an item.
- Remove an item
- Show The List.
- Exit. 2
- ( THE CURSOR COULDN'T DO ANYTHING ONLY BLINK ON THE BLANK BLACK OUTPUT) Expected Output 3 Menu
- Add an item.
- Remove an item.
- Show the List.
- Exit. 1 Please enter a number: 22 Menu
- Add an item.
- Remove an item
- Show The List.
- Exit. 1 Please enter a number: 12 Menu
- Add an item.
- Remove an item
- Show The List.
- Exit. 2 Menu
- Add an item.
- Remove an item
- Show The List.
- Exit. 3 The List contains 12 Menu
- Add an item.
- Remove an item
- Show The List.
- Exit. 1 Please enter a number: 22 Menu
- Add an item.
- Remove an item
- Show The List.
- Exit. 1 Please enter a number: 34 Menu
- Add an item.
- Remove an item
- Show The List.
- Exit. 3 The List contains 12 22 34 Max = 34 Min = 12
P.S. I'M THANK U EVERYONE WHO COMES ANSWER ON THIS QUESTION BECAUSE I TRY WITH THIS QUESTION FOR 2 DAYS THIS 1 IS HEAVY FOR ME