CS301 (P) Lab 1
CS301 (P) Lab 1
(Practical)
Page
Lab No. Lab Topic
No.
Lab 1
Lab 1 Problem Statement
Description:
Write a C++ program to create a Linked List that will store and display the data of faculty
members of a university. The user will enter the data of 5 faculty members (name and age). On
the basis of age, the post of the faculty member will be decided and then this data will be added
into the linked list. Each node of the linked list will contain object of Faculty class in data part
and next pointer.
Structure of Classes:
1. Faculty
2. Node
3. List
Faculty Class:
Data Members:
Functions:
Node Class:
Data Members:
PAGE \* MERGEFORMAT 8 | Page
2
Functions:
Faculty get()
Node * getNext()
List Class:
Data Members:
Member Functions:
List();
void add (Faculty addObject); (To Add the data of faculty member into
the linked list node)
Faculty get();
bool next();
friend void traverse(List list); (To visit each node and display the data of
a faculty members stored in that node)
Solution :
#include <iostream>
class Faculty{
string name;
string post;
int age;
public:
this->name= name;
this->post= post;
this->age=age;
string getName(){
return name;
string getPost(){
return post;
int getAge(){
return age;
};
class Node {
public:
Faculty get() {
return object;
this->object.setName(object.getName());
this->object.setPost(object.getPost());
this->object.setAge(object.getAge());
Node * getNext(){
return nextNode;
this->nextNode = nextNode;
private:
Faculty object;
Node * nextNode;
};
class List {
public:
List();
Faculty get();
bool next();
private:
int size;
Node * headNode;
Node * currentNode;
};
/* Constructor */
List::List() {
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
newNode->set(addObject);
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode );
currentNode = newNode;
else
newNode->setNext(NULL);
headNode->setNext(newNode);
currentNode = newNode;
size ++;
Faculty List::get() {
if (currentNode != NULL)
return currentNode->get();
bool List::next() {
currentNode = currentNode->getNext();
return false;
else
return true;
list.currentNode = list.headNode;
cout<<"=============Display Faculty
Information================="<<endl<<endl;
{
PAGE \* MERGEFORMAT 8 | Page
8
Faculty member;
member=list.get();
list.currentNode = savedCurrentNode;
int main() {
Faculty member;
string name,post;
List list;
int age;
cout<<"==============Enter Faculty
Information================="<<endl;
for(int a=0;a<5;a++){
cin>>age;
cin>>name;
post="Lecturer";
PAGE \* MERGEFORMAT 8 | Page
9
post="Assistant Professor";
post="Professor";
member.setName(name);
member.setAge(age);
member.setPost(post);
list.add(member);
traverse(list);