-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBUBBLES0RT.cpp
More file actions
163 lines (152 loc) · 3.87 KB
/
Copy pathBUBBLES0RT.cpp
File metadata and controls
163 lines (152 loc) · 3.87 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Group1.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//this is a code block, everything inside gets tabulations
struct node {
int number;
string name;
node *next;
};
//End of the code block
bool isEmpty(node *head);
void menu();
void insert1(node *&head, node *&last, string name, int number);
void insert2(node *&head, node *&last, string name, int number);
void remove(node *&head);
void showList(node *current);
void bubbleSort(struct node *head);
void swap(struct node *a, struct node *b);
//Function, it is a code block...
bool isEmpty(node *head)
{
if(head == NULL) //Next line depends of this, we add spaces
return true;
else //This line doesn't depend from above
return false; //this depends from above line
}
//Code block ends...
//now look: this is an idented function
void menu() {
cout << "Menu\n" ; //Menu
cout << "1. Add the patient name.\n" ; //To add patient name
cout << "2. Add patient number.\n" ;
cout << "3. Delete patient data.\n" ;
cout << "4. Display patient list.\n" ;
cout << "5. Sort by name.\n" ;
cout << "6. Exit.\n" ;
//return choice;
}
//Next function have identation, always is easier to read...
void insertNode(node *&head, string name, int number) {
node *temp = new node;
if(isEmpty(head)) {
temp->number = number;
temp->name = name;
temp->next = NULL;
head = temp;
} else {
temp->number = number;
temp->name = name;
temp->next = NULL;
head->next = temp;
}
}
void remove(node *&head) {
if(isEmpty(head))
cout <<"This is already empty.\n" ;
else if (head == head) {
delete head;
head = NULL;
} else {
node *temp = head;
head = head ->next;
delete temp;
}
}
void showList(node *current) {
if(isEmpty(current))
cout << "This patient list is empty \n" ;
else
cout << "The patient list contains : \n" ;
while (current->next != NULL)
cout << current->number ;
cout << current->name ;
current = current-> next;
}
void bubbleSort(struct node *head)
{
int swapped, i;
struct node *name = NULL;
/* Checking for empty list */
if (name == NULL)
return;
do
{
swapped = 0;
name = head;
while (name->next != name)
{
if (name->name > name->next->name)
{
swap(name, name->next);
swapped = 1;
}
name = name->next;
}
name = name;
}
while (swapped);
}
/* function to swap data of two nodes a and b*/
void swap(struct node *a, struct node *b)
{
string temp = a->name;
a->name = b->name;
b->name = temp;
}
int main() {
node *head = NULL;
node *last = NULL; //I think this variable can disapear...
char choice;
int number;
string name;
int running = 1;
while (running) {
menu();
cin >> choice;
cin.ignore();
std::cout << "You selected " << choice << " option\n";
switch(choice) {
case '1': cout <<"Please enter patient name:" ;
std::getline (std::cin, name);
cout << "you entered " << name << " to the list\n";
break;
case '2': cout <<"Please enter a patient number:" ;
std::getline (std::cin, name);
cout << "you entered " << name << " to the list\n";
//cin >> number;
break;
case '3': remove(head);
cout <<"You have removed the patient data" << choice << "option\n";
break;
case '4': showList(head);
cout <<"All the patient data";
break;
case '5': bubbleSort(head) ;
cout <<"Sort by patient name";
case '6':
cout <<"System exit\n" ;
running = 0;
break;
//default: cout << "sorry, what? (" << choice << ")\n";
}
if (name != "" && number > 0) {
insertNode(head, name, number);
name = "";
number = -1;
}
}
return 0;
}