0% found this document useful (0 votes)
6 views

Assignment 1

Great assignment for DSA to go

Uploaded by

Aleena Altaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment 1

Great assignment for DSA to go

Uploaded by

Aleena Altaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

University of the Punjab, Gujranwala Campus

Department of Information Technology, Spring 2022

Course: CC-213 + Data Structures and Algorithms Instructor: Dr. Saad Ahmad Khan
Assignment: 1st Date: 18th July, 2024 Submission Date: 24th July, 2024
Class: BSCS + BSIT (M) Semester: 4th Total Marks: 15
Name: Roll Number:

Submission Guidelines:
1. Make a .cpp file and name it according to your roll number, i.e., BXX22YYY.cpp. Here, XX is defined
as CS or IT and YYY is defined as your roll number. For example: BCS22001.cpp
2. Create an email and attach the cpp file with it. The subject of the email must be your roll number.
3. Now, BCS students will send it to [email protected] and BIT students will send to
[email protected].

Question:
Today, you will implement the Stack class as a template class and verify it using a user-defined class
Student.

Suppose, we have the following Stack class.


template <class S>
class Stack
{
private:
int size;
int noOfElements;
S * data;

public:
Stack() {…} //constructor to initialize all the required data members
Stack(int size) {…} //constructor to initialize all the required data members
bool push (S element) {…} //add the element S in the stack and if stack is full,
then add 5 new locations to it
S top () {…} //return the top of the stack without removing it from stack
S pop () {…} //return the top of the stack and also remove it from stack.
If stack is empty return default object
int totalElements () {…} //return the total number of elements from the stack
void display() {…} //display all the elements of the stack from bottom to top
~Stack() {…} //delete the allocated memory from heap
};
class Student {
private:
string name;
string rollNo;
int semester;
double cgpa;
public:
Student(){
name = "-";
rollNo = "-";
semester = -1;
cgpa = -1.0;
}
Student(string name, string rollNo, int semester, double cgpa) {
this->name = name;
this->rollNo = rollNo;
University of the Punjab, Gujranwala Campus
Department of Information Technology, Spring 2022

this->semester = semester;
this->cgpa = cgpa;
}
void display() {
cout << "Name: " << name << ", Roll Number: " << rollNo << ", CGPA: " << cgpa
<< ", Current Semester: " << semester << endl;
}
};

1. You have to implement all its functions.


2. Now, you write the following main function and verify the output.
Main function:
int main()
{
Student s1("S1", "BCSF19-001", 4, 3.91);
Student s2("S2", "BCSF19-201", 4, 3.92);
Student s3("S3", "BITF19-001", 4, 3.93);
Student s4("S4", "BITF19-201", 4, 3.94);

Stack<Student> s(2);

cout << s.push(s1) << endl;


cout << s.push(s2) << endl;
cout << s.push(s3) << endl;
s.display();
cout << s.total_Elements() << endl;
cout << s.push(s4) << endl;
cout << s.total_Elements() << endl;
Student s5 = s.pop();
s5.display();
s.display(); return 0;
}
Output :
1
1
1
Stack:
Name: S1, Roll Number: BCSF19-001, CGPA: 3.91, Current Semester: 4
Name: S2, Roll Number: BCSF19-201, CGPA: 3.92, Current Semester: 4
Name: S3, Roll Number: BITF19-001, CGPA: 3.93, Current Semester: 4
3
1
4
Name: S4, Roll Number: BITF19-201, CGPA: 3.94, Current Semester: 4
Stack:
Name: S1, Roll Number: BCSF19-001, CGPA: 3.91, Current Semester: 4
Name: S2, Roll Number: BCSF19-201, CGPA: 3.92, Current Semester: 4
Name: S3, Roll Number: BITF19-001, CGPA: 3.93, Current Semester: 4

You might also like