[go: up one dir, main page]

0% found this document useful (0 votes)
76 views10 pages

CS301 (P) Lab 1

Uploaded by

bc220414927aas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views10 pages

CS301 (P) Lab 1

Uploaded by

bc220414927aas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

0

Lab Manual CS301P – Data Structures (Practical)

(Practical)

Page
Lab No. Lab Topic
No.

1 Lab 1: Learn to implement linked list data structure 2


1

Lab 1
Lab 1 Problem Statement

Lab Title: Learn to implement linked list data structure

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.

Age Criteria for Faculty member’s post:

· Lecturer (if age>=25 and age<=35)

· Assistant Professor (if age>=36 && age<=45)

· Professor (if age>=46 && age<=60)

Structure of Classes:

The program should contain the following classes:

1. Faculty
2. Node
3. List

Faculty Class:

Data Members:

Private variables (string name, int age, string post)

Functions:

Setter and Getter functions

Node Class:

Data Members:
PAGE \* MERGEFORMAT 8 | Page
2

Private variables (Faculty object, Node * nextNode)

Functions:

Faculty get()

void set(Faculty object)

Node * getNext()

void setNext(Node * nextNode)

List Class:

Data Members:

Private variables (int size, Node * headNode, Node * currentNode)

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>

using namespace std;

class Faculty{

string name;

PAGE \* MERGEFORMAT 8 | Page


3

string post;

int age;

public:

void setName(string name){

this->name= name;

void setPost(string post){

this->post= post;

void setAge(int age){

this->age=age;

string getName(){

return name;

string getPost(){

return post;

int getAge(){

return age;

PAGE \* MERGEFORMAT 8 | Page


4

};

class Node {

public:

Faculty get() {

return object;

void set(Faculty object){

this->object.setName(object.getName());

this->object.setPost(object.getPost());

this->object.setAge(object.getAge());

Node * getNext(){

return nextNode;

void setNext(Node * nextNode){

this->nextNode = nextNode;

private:

Faculty object;

Node * nextNode;

};

PAGE \* MERGEFORMAT 8 | Page


5

/* The List class */

class List {

public:

List();

void add (Faculty addObject);

Faculty get();

bool next();

friend void traverse(List list);

private:

int size;

Node * headNode;

Node * currentNode;

};

/* Constructor */

List::List() {

headNode = new Node();

headNode->setNext(NULL);

currentNode = NULL;

size = 0;

PAGE \* MERGEFORMAT 8 | Page


6

/* add() class method */

void List::add (Faculty addObject) {

Node * newNode = new Node();

newNode->set(addObject);

if( currentNode != NULL )

newNode->setNext(currentNode->getNext());

currentNode->setNext( newNode );

currentNode = newNode;

else

newNode->setNext(NULL);

headNode->setNext(newNode);

currentNode = newNode;

size ++;

/* get() class method */

Faculty List::get() {

PAGE \* MERGEFORMAT 8 | Page


7

if (currentNode != NULL)

return currentNode->get();

/* next() class method */

bool List::next() {

if (currentNode == NULL) return false;

currentNode = currentNode->getNext();

if (currentNode == NULL || size == 0)

return false;

else

return true;

/* Friend function to traverse linked list */

void traverse(List list) {

Node* savedCurrentNode = list.currentNode;

list.currentNode = list.headNode;

cout<<"=============Display Faculty
Information================="<<endl<<endl;

for(int i = 1; list.next(); i++)

{
PAGE \* MERGEFORMAT 8 | Page
8

Faculty member;

member=list.get();

cout << "Name : " << member.getName()<<endl;

cout << "Age : " << member.getAge()<<endl;

cout << "Post : " << member.getPost()<<endl;

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++){

cout<<"Enter Faculty Member Age: "<<endl;

cin>>age;

cout<<"Enter Faculty Member Name: "<<endl;

cin>>name;

if(age>=25 && age<=35){

post="Lecturer";
PAGE \* MERGEFORMAT 8 | Page
9

else if(age>=36 && age<=45){

post="Assistant Professor";

else if(age>=46 && age<=60){

post="Professor";

member.setName(name);

member.setAge(age);

member.setPost(post);

list.add(member);

traverse(list);

PAGE \* MERGEFORMAT 8 | Page

You might also like