#include <iostream>
#include <string>
using namespace std;
struct node
{
int num;
node*next;
};
bool isEmpty(node *head);
char menu();
void insertasfirstelement(node *&head, node *&last, int num);
void insert(node *&head, node *&last, int num);
void remove(node *&head, node *&last);
void showlist(node*c);
bool isEmpty(node*head)
{
if(head == NULL)
return true;
else
return false;
}
char menu()
{
char choice;
cout << "\n\nMenu:\n";
cout << "\n1. Add an item";
cout << "\n2. Remove an item";
cout << "\n3. Show the list";
cout << "\n4. Exit" <<endl;
cin >> choice;
return choice;
}
void insertasfirstelement(node *&head, node*&last, int num)
{
node * temp = new node;
temp ->num=num;
temp ->next=NULL;
head = temp;
last = temp;
}
void insert(node *&head, node *&last, int num)
{
if(isEmpty(head))
insertasfirstelement(head,last,num);
else
{
node *temp = new node;
temp ->num=num;
temp ->next=NULL;
last ->next= temp;
last = temp;
}
}
void remove(node *&head, node *&last)
{
if(isEmpty(head))
cout << "List is empty\n";
else if(head == last)
{
delete head;
head = NULL;
last = NULL;
}
else
{
node *temp = head;
head = head -> next;
delete temp;
}
}
void showlist(node*c)
{
if(isEmpty(c))
cout <<"The list is empty\n";
else
{
cout << "The values are: \n";
while(c !=NULL)
{
cout << c -> num << endl;
c = c -> next;
}
}
}
int main()
{
node *head=NULL;
node *last=NULL;
char choice;
int num;
do{
choice = menu();
switch(choice)
{
case '1': cout << "Please enter a number: ";
cin >> num;
insert(head, last, num);
break;
case '2': remove(head,last);
break;
case '3': showlist(head);``
break;
default: cout << "System exit\n";
}
} while(choice != '4');
}
Таким образом, мне удалось получить рабочий список ссылок. Но я не смог понять, как его привести в порядок. Я также не знаю, как это сделать, поэтому я могу удалить число, которое я вставил. Я пытаюсь понять, как связанные списки работают лучше, поэтому, если вы можете оказать некоторую помощь, мы будем очень благодарны.Как сделать связанный список упорядоченным в C++?
я полностью видеть, что вы делаете Вот. Я пытался сравнить q-указатель на int value num, чтобы попытаться выполнить итерацию по списку, вместо того, чтобы делать num-point для чего-то. Я не кодировался как год, поэтому я немного ржавый, я пытаюсь понять все, что я могу сделать на этом языке. Я планирую попытаться разработать и закодировать игры. –