CMP2006 LabWorksheet 6 LinkedList
CMP2006 LabWorksheet 6 LinkedList
Scenario
Utech has asked you to create a program that will track all its students’ details. Each student has
an Id, Name and GPA.
C++ Implementation
//Student Class
#ifndef Student_H
#define Student_H
#include <iostream>
using namespace std;
//Data Portion
class Student
{
//Class attributes
private:
int Id;
string Name;
float GPA;
public:
// Default Constructor
Student()
{
Id = 0;
Name = "";
GPA = 0.0f;
}
// Primary Constructor
Student(int id, string name, float gpa)
{
Id = id;
Name = name;
GPA = gpa;
}
// Copy Constructor
Student(Student &s)
//Accessors
int GetId()
{
return Id;
}
string GetName()
{
return Name;
}
float GetGPA()
{
return GPA;
}
//Mutators
void SetId(int id)
{
Id = id;
}
void SetName(string name)
{
Name = name;
}
void SetGPA(float gpa)
{
GPA = gpa;
}
void Display()
{
cout<<"Id: "<< Id << ", Name: " << Name << ", GPA: " << GPA;
}
};
#endif
//Node Class
#ifndef Node_H
#define Node_H
#include "Student.h"
//Node object
class Node
{
//Class Attributes
private:
Student Data; //Data portion - Composition
Node * NextNode; //Link portion (dynamically declared)
public:
//default constructor
//primary constructor 1
Node(Student data, Node * nextNode):Data(data) //initialize (composition) student using its copy
constructor
{
NextNode = nextNode;
}
//primary constructor 3
Node(int id, string name, float gpa):Data(id, name, gpa) //initialize (composition) student using
its primary constructor
{
NextNode = NULL; //set the next node to null
}
//copy constructor
Node(Node *node)
{
Data = node->Data;
NextNode = node->NextNode;
}
//Accessors
Student GetData()
{
return Data;
}
Node * GetNextNode()
{
return NextNode;
}
//Mutators
void SetData(Student data)
{
Data = data;
}
};
#endif
#ifndef LinkedList_H
#define LinkedList_H
#include "Node.h"
//Node object
class SinglyLinkedList
{
//Class attribues
private:
Node * Head; //Head should point to the first element in list
public:
//default constructor
SinglyLinkedList() //Creates an empty list || 99% used
{
Head = NULL;
}
//primary
SinglyLinkedList(Node * h)//Creates a list with one element in there
{
Head = h;
}
//Accessors
Node * GetHead()
{
return Head;
}
//Mutator
void SetHead(Node * head)
{
Head = head;
}
}
else //if memory was not allocated successfully
{
cerr<<"Error! List is full (Out of Memory), can NOT add a new node"<<endl;
}
}
Node * curr = Head; //point curr to the first element in the list.
while (curr != NULL) //while curr is pointing to a valid node
{
if(curr->GetData().GetId() == studId) //if the curr node has the data we are searching for
{
isFound = true; //set the bool to true (element found)
break; //jump out of loop
}
curr = curr->GetNextNode(); //point curr to IT'S next node
}
return isFound;
}
void DisplayList()
{
Node * curr = Head; //point curr to the first element in the list.
while (curr != NULL) //while curr is pointing to a valid node
{
cout<<"[";
curr->GetData().Display(); //display the data for that node
cout<<"]->";
curr = curr->GetNextNode(); //point curr to IT'S next node
}
cout<<"NULL";
}
bool IsEmpty()
{
if(Head == NULL) //if the list is empty
{
return true; //return TRUE - list is empty
}
return false; //List is not empty
}
bool IsFull()
{
Node *temp = new Node; //attempt to reserve space for a new node by calling default
constructor
if(temp != NULL) //if memory was allocated successfully
{
delete temp; //deallocate the memory for temp
Student dataToReturn;
if(!IsEmpty())
{
Node *curr = Head, *prev=NULL; //point curr to the first element in the list.
}
else
{
cerr<<"The list is empty; there is nothing to delete!"<<endl;
}
return dataToReturn;
}
};
#endif
//Main/Driver Class
#include "SinglyLinkedList.h"
int main()
/*
list->InsertAtFront(stud1);
list->InsertAtFront(stud2);
list->InsertAtFront(stud3);
*/
list->InsertAtBack(stud1);
list->InsertAtBack(stud2);
list->InsertAtBack(stud3);
list->DisplayList();
list->DeleteANode(333).Display();
cout<<"\n\n";
return 0;
//Class attributes
// Default Constructor
public Student()
Id = 0;
Name = "";
GPA = 0.0f;
// Primary Constructor
Id = id;
Name = name;
GPA = gpa;
// Copy Constructor
public Student(Student s)
Id = s.Id;
Name = s.Name;
GPA = s.GPA;
//Accessors
return Id;
return Name;
return GPA;
//Mutators
Id = id;
Name = name;
GPA = gpa;
System.out.print("Id: " + Id + ", Name: " + Name + ", GPA: " + GPA);
@Override
//Node Class
//Node object
class Node
{
//Class Attributes
//default constructor
public Node()
{
Data = new Student();//initialize (composition) student using its default constructor
NextNode = null;
}
//primary constructor 1
public Node(Student data, Node nextNode)
{
Data = new Student(data); //initialize (composition) student using its copy constructor
NextNode = nextNode;
}
//primary constructor 3
public Node(int id, String name, float gpa)
{
Data = new Student(id, name, gpa); //initialize (composition) student using its primary
constructor
NextNode = null; //set the next node to null
}
//copy constructor
public Node(Node node)
{
Data = node.Data;
NextNode = node.NextNode;
}
//Accessors
public Student GetData()
{
//Mutators
public void SetData(Student data)
{
Data = data;
}
//LinkedList Class
//List object
class SinglyLinkedList
{
//Class attribues
private Node Head; //Head should point to the first element in list
//default constructor
public SinglyLinkedList() //Creates an empty list || 99% used
{
Head = null;
}
//primary
public SinglyLinkedList(Node h)//Creates a list with one element in there
{
Head = h;
}
//Accessors
public Node GetHead()
{
return Head;
}
//Mutator
public void SetHead(Node head)
{
Head = head;
}
}
else //if memory was not allocated successfully
{
System.err.println("Error! List is full (Out of Memory), can NOT add a new node");
}
}
Node curr = Head; //point curr to the first element in the list.
while (curr != null) //while curr is pointing to a valid node
{
if(curr.GetData().GetId() == studId) //if the curr node has the data we are searching for
{
isFound = true; //set the bool to true (element found)
break; //jump out of loop
}
curr = curr.GetNextNode(); //point curr to IT'S next node
}
return isFound;
}
if(!IsEmpty())
{
Node curr = Head, prev=null; //point curr to the first element in the list.
}
else
{
System.err.println("The list is empty; there is nothing to delete!");
}
return dataToReturn;
}
list.InsertAtBack(stud1);
list.InsertAtBack(stud2);
list.InsertAtBack(stud3);
list.DisplayList();
1. Utech has engaged you as a software developer to manage scholarship opportunities that
the students may apply for. Each scholarship has a unique numeric identifier, a sponsor
and a cash value. Create a program that would demonstrate the use of a singly linear
linked list that may prompt the user with the following menu options:
a. Add Scholarship (Adds element to the middle of the list)
b. Search Scholarship (prompts the user for an Id, then searches and displays all the
details of the scholarship if found; an appropriate message if not found)
c. Display All Scholarships (shows the details for all the scholarships in list format)
d. Count Scholarships (returns the number of scholarships in list)
2. Create a Doubly Linked List and a Circular Linked List that is able to manage a series of
numbers. Users should be able to:
a. Add new numbers to the back
b. Add new numbers to the front
c. Count and display the number of elements in list
d. Search the list for a particular number and indicate if it was found in the list or not