You are not allowed to use any build in libraries - you have to build them from
ID: 3662761 • Letter: Y
Question
You are not allowed to use any build in libraries - you have to build them from scratch and they have to be allocaed on the heap.
Create a SSL (singly linked list) datastructure that will store Student objects with two private slots: String::StudentName and String::StudentUserName.
SSL operations:
-> void print() the list content by calling the Student ->print() function
-> void add(Student*) add an instance of a student object
-> ssl* split() will split the list in two halves. If the number of objects saved in the list are odd, then the original list pointer will have the floor(list size / 2) objects and the function returns a pointer to the second half of the list with the student objects. The split function will retan the pointer to the first unchanged - the first half, and the split function will return a pointer to the second half of the list as a new singly linked list.
Student operations:
-> print() will print the content of the student object.
1. Separate the business logic from the payload.
2. Provide a driver that illustrated how you tested your linked list and its functionality.
3. For C++ implementation, make sure you allocate the ssl and student object on the heap and when you terminate the driver's main routine, you correctly deallocate all memory.
Implement the following functionality of this challenge:
-Implementation in both Java and C++
-You cannot use built-in libraries (such as a linked list, vector etc.)
-Read-in unlimited number of students
-Implement all necessary setter, getter, and manipulation methods
-Allocate all data-structures on the heap (not the stack) (C++)
-Graceful program termination: deallocate all allocated memory before exiting application (C++)
Explanation / Answer
Java Solution
Student.java
public class Student {
private String StudentName;
public String getStudentName() {
return StudentName;
}
public void setStudentName(String studentName) {
StudentName = studentName;
}
public String getStudentUserName() {
return StudentUserName;
}
public void setStudentUserName(String studentUserName) {
StudentUserName = studentUserName;
}
private String StudentUserName;
public Student(String name,String userName)
{
this.StudentName=name;
this.StudentUserName=userName;
}
public void Print()
{
System.out.println(this.StudentName+" "+this.StudentUserName);
}
}
SinglyLinkedList.java
public class SinglyLinkedList {
private LinkedListNode head;
private LinkedListNode tail;
private int size;
public SinglyLinkedList()
{
head=null;
tail=null;
size=0;
}
public void add(Student data) {
if(data==null)
{
throw new java.lang.IllegalArgumentException("data should not be null");
}
LinkedListNode node=new LinkedListNode(data);
if(head==null)
{
head=node;
tail=node;
}
else
{
tail.setNext(node);
tail=node;
}
size++;
}
public void print()
{
LinkedListNode temp=head;
//Iterating through the list
while(temp!=tail)
{
Student student=temp.getData();
student.Print();
temp=temp.getNext();
}
Student student=temp.getData();
student.Print();
}
public SinglyLinkedList split()
{
int split_size=size/2;
LinkedListNode temp=this.head;
int i=1;
while(i<split_size)
{
i++;
temp=temp.getNext();
}
//Creating a new list
SinglyLinkedList splitList=new SinglyLinkedList();
//setting the head of new list to the node from where second part of list starts
splitList.head=temp.getNext();
splitList.tail=this.tail;
//Breaking the link between first and second part
temp.setNext(null);
return splitList;
}
}
LinkedListNode.java
public class LinkedListNode {
private Student data;
private LinkedListNode next;
public LinkedListNode(Student data, LinkedListNode next) {
this.data = data;
this.next = next;
}
public LinkedListNode(Student data) {
this(data, null);
}
public Student getData() {
return data;
}
public LinkedListNode getNext() {
return next;
}
public void setNext(LinkedListNode next) {
this.next = next;
}
@Override
public String toString() {
return "Node containing: " + data;
}
}
StudentDriver.java(Driver program to test)
public class StudentDriver {
public static void main(String[] args) {
SinglyLinkedList students=new SinglyLinkedList();
Student a=new Student("Jhon","Jhonny@11");
Student b=new Student("Kevin","Kevin$22");
Student c=new Student("Brad","coolguy@007");
students.add(a);
students.add(b);
students.add(c);
students.print();
SinglyLinkedList second_part=students.split();
second_part.print();
}
}
C++ Solution
Student.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(string name,string userName);
~Student();
string getStudentName(){ return studentName; }
string getStudentUserName(){ return studentUserName; }
void setStudentName(string name){ studentName = name; }
void setStudentUserName(string userName){ studentUserName = userName; }
void print();
private:
string studentName;
string studentUserName;
};
SSLNode.h
#include "Student.h"
class SSLNode
{
public:
SSLNode(Student* s,SSLNode* n);
~SSLNode();
Student* getData(){ return data; }
SSLNode* getNext(){ return next; }
void setData(Student* student){ data = student; }
void setNext(SSLNode* nextNode){ next = nextNode; }
private:
Student* data;
SSLNode* next;
};
Student.cpp
#include "Student.h"
Student::Student(string name,string userName)
{
studentName = name;
studentUserName = userName;
}
Student::~Student()
{
}
void Student::print()
{
cout << studentName + " " + studentUserName << endl;
}
SSLNode.cpp
#include "SSLNode.h"
SSLNode::SSLNode(Student* s,SSLNode* n)
{
data = s;
next = n;
}
SSLNode::~SSLNode()
{
}
SSL.h
#include "Student.h"
#include "SSLNode.h"
class SSL
{
public:
SSL();
~SSL();
SSLNode* getHead(){ return head; }
void setHead(SSLNode* h){ head = h; }
void add(Student* data);
void print();
SSL* split();
private:
SSLNode* head;
SSLNode* tail;
int size;
};
SSL.cpp
#include "SSL.h"
SSL::SSL()
{
head = nullptr;
tail = nullptr;
size = 0;
}
SSL::~SSL()
{
}
void SSL::add(Student* data)
{
if (data != nullptr)
{
SSLNode* node = new SSLNode(data, nullptr);
if (head == nullptr)
{
head = node;
tail = node;
}
else
{
tail->setNext(node);
tail = node;
}
size++;
}
}
void SSL::print()
{
SSLNode* temp = head;
//Iterating through the list
while (temp != tail)
{
Student* student = temp->getData();
student->print();
temp = temp->getNext();
}
Student* student = temp->getData();
student->print();
}
SSL* SSL::split()
{
int split_size = size / 2;
SSLNode* temp = this->head;
int i = 1;
while (i<split_size)
{
i++;
temp = temp->getNext();
}
//Creating a new list
SSL* splitList = new SSL();
//setting the head of new list to the node from where second part of list starts
splitList->head = temp->getNext();
splitList->tail = this->tail;
//Breaking the link between first and second part
temp->setNext(nullptr);
return splitList;
}
LinkedList.cpp(Driver program)
#include "SSL.h"
int main()
{
SSL* students = new SSL();
Student a("Jhon", "Jhonny@11");
Student b("Kevin", "Kevin$22");
Student c("Brad", "coolguy@007");
students->add(&a);
students->add(&b);
students->add(&c);
students->print();
SSL* second_part = students->split();
second_part->print();
delete(students);
delete(second_part);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.