In: Computer Science
C++ Code! This code was written/implemented using the "class format." How would I go about in converting it to the "struct format?"
#include <iostream>
#include <iomanip>
using namespace std;
class ListNode{
public:
string course_name;
string course_number;
string course_room;
ListNode* next;
ListNode(){
this->next = NULL;
}
ListNode(string name, string number, string room){
this->course_name = name;
this->course_number = number;
this->course_room = room;
this->next = NULL;
}
};
class List{
public:
ListNode* head;
List(){
this->head = NULL;
}
void insert(ListNode* Node){
if(head==NULL){
head = Node;
}
else{
ListNode* temp = head;
while(temp->next!=NULL){
temp = temp->next;
}
temp->next = Node;
}
}
void print(){
ListNode* temp = head;
while(temp!=NULL){
cout << left << setw(20) << temp->course_name
<< setw(10) << temp->course_number
<< setw(10) << temp->course_room << "\n";
temp = temp->next;
}
}
};
int main() {
List l;
ListNode* n1 = new ListNode("Calculus 1","Math 1","SCMA 221");
ListNode* n2 = new ListNode("C++ Programming","CSP 31B","SCMA 150");
ListNode* n3 = new ListNode("Discrete Math","CS 55","SCMA 127");
ListNode* n4 = new ListNode("Java Programming","CS 26A","SCMA 352");
l.insert(n1);
l.insert(n2);
l.insert(n3);
l.insert(n4);
cout << "Courses for Tim Apple:\n";
l.print();
return 0;
}
structures:
structure is a collection of heterogeneous type of data means different data types under a single name.
and stucture is similar to class , both holds a collection of different data types.
syntax: struct structure_name
{
type variable_name;
type variable_name;
................
} structure_variable;
#include <iostream>
#include <iomanip>
using namespace std;
struct ListNode {
string course_name;
string course_number;
string course_room;
ListNode* next;
ListNode(){
this->next = NULL;
}
ListNode(string name, string number, string room) {
this->course_name = name;
this->course_number = number;
this->course_room = room;
this->next = NULL;
} };
struct List {
ListNode* head;
List() {
this->head = NULL;
}
void insert(ListNode* Node) {
if(head==NULL) {
head = Node;
}
else {
ListNode* temp = head;
while(temp->next!=NULL) {
temp = temp->next;
}
temp->next = Node;
}
}
void print() {
ListNode* temp = head;
while(temp!=NULL){
cout << left << setw(20) << temp->course_name
<< setw(10) << temp->course_number
<< setw(10) << temp->course_room << "\n";
temp = temp->next;
}
}
};
int main() {
List l;
ListNode* n1 = new ListNode("Calculus 1","Math 1","SCMA 221");
ListNode* n2 = new ListNode("C++ Programming","CSP 31B","SCMA 150");
ListNode* n3 = new ListNode("Discrete Math","CS 55","SCMA 127");
ListNode* n4 = new ListNode("Java Programming","CS 26A","SCMA 352");
l.insert(n1);
l.insert(n2);
l.insert(n3);
l.insert(n4);
cout << "Courses for Tim Apple:\n";
l.print();
return 0;
}