1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
using namespace std;
struct node
{
string member;
node *next;
};
bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, string member);
void insert(node *&head, node *&last, string name);
void remove(node *&head, node *&last);
void display(node *¤t);
bool isEmpty(node *head){
if(head == NULL)
return true;
else
return false;
}
char menu(){
char choice;
cout << "MENU.\n";
cout << "1. Add\n";
cout << "2. remove\n";
cout << "3. display list\n";
cout << "4. Exit\n";
cin >> choice;
return choice;
}
void insertAsFirstElement(node *&head, node *&last, string member){
node *temp = new node;
temp-> member = member;
temp->next =NULL;
head = temp;
last = temp;
}
void insert(node *&head, node *&last, string member){
if(isEmpty(head))
insertAsFirstElement(head , last , member);
else{
node *temp = new node;
temp-> member = member;
temp->next = NULL;
last->next = temp;
last=temp;
}
}
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 *temp=head;
head = head->next;
delete temp;
}
}
void display(node *¤t){
if(isEmpty(current))
cout << "the list is empty.\n";
else{
while(current != NULL)
{
cout << current ->member <<endl;
current=current->next;
}
}
}
int main()
{
node *head = NULL;
node *last = NULL;
char choice;
string member;
do{
choice= menu();
switch(choice){
case '1' : cout <<"enter a member:";
cin >> member;
insert(head , last ,member);
break;
case '2': remove(head, last);
break;
case '3' : display(head);
break;
default : cout << "System exit\n";
}
}while(choice !='4');
return 0;
}
| |