You are not allowed to use any build in libraries - you have to build them from
ID: 3663169 • 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 allocated 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 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
/*
* C++ Program to Implement Singly Linked List for student node
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct Studentnode
{
int info;
struct Studentnode *next;
}*start;
/*
* Class Declaration
*/
class Studentlist
{
public:
Studentnode* create_node(int);
void addnode(Studentnoe *);
void display();
Studentlist()
{
start = NULL;
}
};
/*
* Main :contains menu
*/
main()
{
int choice, nodes, element, position, i;
Studentlist sl;
start = NULL;
// youc can add any number of student objects using this loop
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations Student object singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Add Node <<endl;
cout<<"2.Display Linked List"<<endl;
cout<<"3.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.addnode(StudentNode *);
cout<<endl;
break;
case :
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
case 3:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
/*
* Creating Node
*/
node *Studentlist::create_node(int value)
{
struct Studentnode *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
/*
* add the new node
*/
void Studentlist::add(StudentNode *)
{
int value;
cout<<"Enter the student node to be inserted: ";
cin>>value;
struct Studentnode *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"nodes are inserted"<<endl;
}
/*
* Display nodesof a Studentlist
*/
void Studentlist::display()
{
struct Studentnode *temp;
if (start == NULL)
{
cout<<"The StudentList is Empty"<<endl;
return;
}
temp = start;
cout<<"Students of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.