CS301 Assignment 1 Solution and Discussion
-
Re: CS301 Assignment 1 Solution and Discussion
Assignment No. 1
Semester: Spring 2021
CS301 – Data Structures Total Marks: 20Due Date: 18/05/2021
Instructions
Please read the following instructions carefully before submitting the assignment solution:
It should be clear that your assignment will not get any credit/marks if:o Assignment is submitted after due date.
o Submitted assignment does not open or file is corrupt.
o Assignment is copied (From internet/students).Recommended Tools
• Dev C++Objectives:
To enable students to understand and practice the concepts of:
• Linked list implementation
• Efficient memory management
• Efficient use of pointerAssignment Submission Instructions
You have to submit only .zip file which will have code (.cpp) and screenshot image files on the assignments interface from your LMS account.
Assignment submitted in any other format will not be accepted and will be scaled with zero marks. No excuse will be accepted on submitting solution file in any other format.For any query related to assignment, please contact [email protected].
Problem Statement:
In the lesson videos and handouts you learned to save only one integer element as data in a node. Please note, it is not compulsory to have only one value in a node. There can be more than one and different types of elements as data of a node but pointer will remain one for singly linked list.
Write a C++ program to implement linked list data structure. In this problem every node should have two data elements called name and course of a student and a pointer to link the current node to the next node. Your program should prompt the user to enter name and course code of a student and save it into a Node (Student). User can enter the records of multiple students. So you need to create a linked list of students. One node of list will represent one student.
Instead of saving the name of course you should use code of course into Student node. This will help you save lots of memory. “short int” type variable can be used to save code of course which will take two bytes while name of course will take many bytes. This technique will help you save lots of memory.
Use the following course names and codes while entering the record of a student.
Course Name Course Code
Introduction to Computing 1
Introduction to Programming 2
Data Structures 3
Object Oriented Programming 4After saving the required information of all students, you need to perform following operations operated through menu showing in sample output video.
• Show the names of students enrolled in a specific or all courses (user will enter a choice of operation).
• Show the count of students enrolled in a specific or all courses (user will enter a choice of operation).
• Option to close the program by selection from menu as showing in sample output video.
• While testing your application and entering the record of first student, use your VU ID for the name of student. Take screenshot of your input which should be showing your VU ID entered as first student name. Zip your code (.cpp) and screenshot image files and submit zip file from your LMS account.Sample Output:
For sample output watch the video file “A1 Sample Output.mp4” attached with this assignment file.Lectures Covered: (Lecture # 1- 8) and Due date to submit solution: (Tuesday, May 18, 2021).
Solution:
//vu id XXXXXXXXXXXXXXXXXX #include <iostream> #include <conio.h> using namespace std; class student{ public: string name; int code; student* next_add; }; class linked_list{ public: student *head=NULL; void menu(); int get_code(); string set_name(); string get_name(); string set_name(string n); void insert(); void show_all(); void show_one(); void show_two(); void show_three(); void show_four(); }; string linked_list::get_name(){ string name; cout<<"enter the name of the student: "; cin>>name; return name; } int linked_list::get_code(){ int code; cout<<"\n\n1. introduction to computing: " ; cout<<"\n2. introduction to programming "; cout<<"\n3. data structures "; cout<<"\n4. object oriented programming "; cout<<"\nEnter the course name "; cin>>code; return code; } string linked_list::set_name(string n){ return n; } void linked_list::insert(){ student *new_node=new student; new_node->name=get_name(); new_node->code=get_code(); new_node->next_add=NULL; if(head==NULL){ head=new_node; } else{ student*ptr=head; while(ptr ->next_add!=NULL){ ptr=ptr->next_add; } ptr->next_add=new_node; } cout<<" students information saved successfully."; } void linked_list::menu(){ p: int choice; cout<<"\n\n0. display all students "; cout<<"\n1. display all students enrolled in introduction to computing "; cout<<"\n2. display all students enrolled in introduction to programming "; cout<<"\n3. display all students enrolled in data structures "; cout<<"\n4. display all students enrolled in object oriented programming "; cout<<"\n5. close the program "; cout<<"\n\n select an option for required operation: "; cin>>choice; switch(choice){ case 0: show_all(); break; case 1: show_one(); break; case 2: show_two(); break; case 3: show_three(); break; case 4: show_four(); break; case 5: exit(0); } goto p; } void linked_list::show_all(){ int count=0; cout<<"\n\nfollowing students are enrolled in all courses: "; student *ptr=head; while(ptr!=NULL){ cout<<"\nStudent name: " <<set_name(ptr->name); count ++; ptr=ptr->next_add; } cout<<"\nenrollemnet count " <<count ; } void linked_list::show_one(){ int count=0; cout<<"\n\nfollowing students are enrolled in intoduction to computing: "; student *ptr=head; while(ptr!=NULL){ if(ptr->code==1){ cout<<"\nStudent name: "<<set_name(ptr->name); count ++; } ptr=ptr->next_add; } cout<<"\nenrollement cout:"<<count; } void linked_list::show_two(){ int count=0; cout<<"\n\nfollowing students are enrolled in intoduction to computing: "; student *ptr=head; while(ptr!=NULL){ if(ptr->code==2){ cout<<"\nStudent name: "<<set_name(ptr->name); count ++; } ptr=ptr->next_add; } cout<<"\nenrollement cout:"<<count; } void linked_list::show_three(){ int count=0; cout<<"\n\nfollowing students are enrolled in intoduction to computing: "; student *ptr=head; while(ptr!=NULL){ if(ptr->code==3){ cout<<"\nStudent name: "<<set_name(ptr->name); count ++; } ptr=ptr->next_add; } cout<<"\nenrollement cout:"<<count; } void linked_list::show_four(){ int count=0; cout<<"\n\nfollowing students are enrolled in intoduction to computing: "; student *ptr=head; while(ptr!=NULL){ if(ptr->code==4){ cout<<"\nStudent name: "<<set_name(ptr->name); count ++; } ptr=ptr->next_add; } cout<<"\nenrollement cout:"<<count; } main(){ linked_list obj; char x; do { obj.insert(); cout<<"\ndo you want to add another student?" ; cin>>x; } while(x=='Y'||x=='y'); obj.menu(); }
-
Sample Output
A1 Sample Output.mp4