Skip to content

CS301 – Data Structures

2 Topics 35 Posts
  • CS301 Assignment 1 Solution and Discussion

    Solved cs301 assignment 1 solution discussion spring 2020
    4
    0 Votes
    4 Posts
    2k Views
    zareenZ
    @zareen 100% Solution Code #include <iostream> using namespace std; /* The Student class */ class Student { private: string firstname, lastname, VUID; int marks; Student *nextStudent; public: // constructor of Student class to initialize data members of class Student(){ VUID = ""; marks = 0; firstname = ""; lastname = ""; nextStudent = NULL; } // Student class method to set VU ID of Student void setVUID(string val){ VUID = val; }; //Student class method to get VU ID of Student string getVUID(){ return VUID; }; // Student class method to set first name of Student void setFirstName(string val){ firstname = val; }; //Student class method to get first name of Student string getFirstName(){ return firstname; }; // Student class method to set last name of Student void setLastName(string val){ lastname = val; }; //Student class method to get last name of Student string getLastName(){ return lastname; }; //Student class method to set the Marks of Student void setMarks(int val) { marks = val; }; // Student class method to get the Marks of Student int getMarks() { return marks; } //Student class method to point current Student to next Student void setNext(Student *nextStudent) { this->nextStudent = nextStudent; } // Student class method to get memory address where pointer is pointing Student *getNext() { return nextStudent; } }; /* The List class */ class List { private: Student * head; Student * current; public: // constructor of list class to initialize data members of class List() { head = new Student(); head->setNext(NULL); current = NULL; } // list class method to add Students into list void add() { Student *newStudent = new Student(); int loc_marks = 0; string loc_vuid = "", loc_fname = "", loc_lname = ""; cout<<"\nEnter VU ID: "; cin>>loc_vuid; newStudent->setVUID(loc_vuid); cout<<"Enter Marks: "; cin>>loc_marks; newStudent->setMarks(loc_marks); cout<<"Enter First Name: "; cin>>loc_fname; newStudent->setFirstName(loc_fname); cout<<"Enter Last Name: "; cin>>loc_lname; newStudent->setLastName(loc_lname); if(head->getNext() == NULL){ newStudent->setNext(NULL); head->setNext(newStudent); current = newStudent; } else{ Student *temp = head; while(temp->getNext() != NULL && temp->getNext()->getMarks() >= loc_marks){ temp = temp->getNext(); } current = temp; newStudent->setNext(current->getNext()); current->setNext( newStudent ); current = newStudent; } }; // list class method to get the information of Student void getInfo() { if (current != NULL){ cout<<"VU ID: "<<current->getVUID()<<endl; cout<<"Marks: "<<current->getMarks()<<endl; cout<<"First Name: "<<current->getFirstName()<<endl; cout<<"Last Name: "<<current->getLastName()<<endl<<endl; } }; // list class method to move current to next Student bool next() { if (current == NULL){ return false; } current = current->getNext(); }; // frient function to list class to show all students in the list friend void showStudents(List list){ Student* savedCurrent = list.current; list.current = list.head; for(int i = 1; list.next(); i++){ list.getInfo(); } list.current = savedCurrent; }; }; main() { int input = 0; List lst; while(input != -1) { cout<<"1. To Add New Student in Ranking"<<endl; cout<<"2. To Display Ranking"<<endl; cout<<"3. To Close"<<endl<<endl; cout<<"Enter Your Choice: (1, 2 or 3) "; cin>> input; if(input == 1) { lst.add(); cout<<"Student's information saved successfully.\n"; } else if(input == 2) { cout<<"\nRanking Chart"<<endl; showStudents(lst); return 0; } else { return 0; } } }
  • 0 Votes
    2 Posts
    935 Views
    zareenZ
    #include<iostream> using namespace std; class TNode{ private: node* root; public: TNode(); int isEmpty(); void buildTree(int item); void insert(int item,node *,node *); void minNode(node*); void maxNode(node*); int countNodes(node*); int treeHeight(node*); void displayBinTree(); }; TNode::TNode(){ root = NULL; } int TNode::isEmpty() { return (root == NULL); } void TNode::buildTree(int item){ node *p = new node; node *parent; cout <<"Insert node in BST :" << item <<endl; insert(item,p,parent); } void TNode::insert(int item,node * p,node * parent){ p->data=item; p->left=NULL; p->right=NULL; parent=NULL; if(isEmpty()){ root = p; } else{ node *ptr; ptr = root; while(ptr != NULL){ parent = ptr; if(item > ptr->data){ ptr = ptr->right; } else{ ptr = ptr->left; } } if(item < parent->data){ parent->left = p; } else{ parent->right = p; } } } void TNode::minNode(node* p){ while (p->left != NULL){ p = p->left; } cout << "Minimum value : " << p->data <<endl; } void TNode::maxNode(node* p){ while (p->right != NULL){ p = p->right; } cout << "Maximum value : " << p->data <<endl; } int TNode::countNodes(node* p){ int node = 1; //Node itself should be counted if (p == NULL){ return 0; } else{ node += countNodes(p->left); node += countNodes(p->right); return node; } } int TNode::treeHeight(node* p){ if (p == NULL) { return 0; } else { int left_side = treeHeight(p->left); int right_side = treeHeight(p->right); if (left_side > right_side) { return(left_side + 1); } else { return(right_side + 1); } } } void TNode::displayBinTree(){ cout <<endl<<endl; minNode(root); maxNode(root); cout <<"Height of BST : "<<treeHeight(root); cout <<"\nTotal nodes : "<<countNodes(root); } int main(){ TNode b; int data[] = {7,2,9,1,5,14}; cout <<"Constructing Binary Search Tree by Inserting Nodes One by One "<<endl; cout <<"------------------------------------------------------------- "<<endl; int arrSize = sizeof(data)/sizeof(data[0]); for(int i = 0; i < arrSize; i++) { b.buildTree(data[i]); } }
How to Build a $1,000/Month PAK VS BAN Live Live Cricket Streaming
File Sharing

2

Online

3.0k

Users

2.8k

Topics

8.2k

Posts
| |