CS301 Assignment 1 Solution Fall 2018
#1
CS301 - Assignment 1 Solution

Semester: Fall 2018



Solution Code 
Code:
#include<iostream>
#include<conio.h>
#include<string>
#include<cstring>
using namespace std;
struct Node{
int empId;
char* empName;
double empSalary;
Node* left;
Node* right;
};
Node* buildBST(Node* root, int i,int id, char* name, double salary){
if(root== NULL){
Node* newNode = new Node();
newNode->empId = id;
newNode->empName = name;
newNode->empSalary = salary;
newNode->left = newNode->right = NULL; 
return newNode;
}
if(root->empId == id){
return root;
}
else if(id <= root->empId){
root->left = buildBST(root->left, i, id, name, salary);
}
else{
root->right = buildBST(root->right, i, id, name, salary);
}
}
void ascendingOrder(Node* root){
if(root==NULL){
return;
}
ascendingOrder(root->left);
cout<<endl<<root->empId<<"\t"<<root->empName<<"\t"<<root->empSalary<<endl;
ascendingOrder(root->right);
}
void postOrder(Node* root){
if(root==NULL){
return;
}
postOrder(root->left);
postOrder(root->right);
cout<<root->empId<<"\t"<<root->empName<<"\t"<<root->empSalary<<endl; 
}
main(){
int id[]={32,56,93,5,10};
char* name[]={"Raza","Sajjad","Rabia","Sehar","Ali"};
double salary[]={3000, 25000, 19230, 24000, 22200};
int c;
int x = (sizeof(id)/sizeof(*id)); 
Node* root = NULL;
char string[20];


for(int i=0; i<x; i++){
root = buildBST(root, i, id[i], name[i], salary[i]); 
}


s: cout<<endl<<"Press 1 to Enter Data \n"; 
cout<<"Press 2 to see the Records in Ascending Order \n";
cout<<"Press 3 to see the Records in Post Order \n";
cout<<"Press 4 to Exit\n ";
cin>>c;
switch(c){
case 1:{
r: cout<<endl<<"Enter Employee ID : ";
cin>>id[x];
cout<<endl<<"Enter Employee Salary : ";
cin>>salary[x];
cout<<endl<<"Enter Employee Name : ";
cin>>string;

for(int i=0;i<x;i++){
if(id[x] == id[i]){
cout<<endl<<"Record Already Exists...";
goto r;
}
}

name[x] = new char[strlen(string)+1];
strcpy(name[x], string);

for(int i=0; i<(x+1); i++){
root = buildBST(root, i, id[i], name[i], salary[i]); 
}
break;
}
case 2: 
cout<<"Ascending Order: \n";
ascendingOrder(root);
break;
case 3:
cout<<"Post Order: \n";
postOrder(root);
break;
case 4:
break;
default:
cout<<"\nWrong Input.";
break; 
}
char ch;
y: cout<<endl<<"DO YOU WANT TO DO SOMETHING ELSE...\nYES = y \t NO = n";
ch = getch();
if(ch=='y'){
goto s;
}
else if(ch=='n'){
cout<<"\nBye!";
}
else{
cout<<endl<<"\nWrong input.";
goto y;
}
}
#2
Thank you for sharing the solution.
  


Possibly Related Threads...
Thread Author Replies Views Last Post
  CS301 GDB Solution Mishi Khan 0 204 02-08-2019, 01:42 AM
Last Post: Mishi Khan
  CS301 Assignment 2019 Mishi Khan 0 184 01-30-2019, 01:06 AM
Last Post: Mishi Khan
  CS301 MidTerm Past Papers Mishi Khan 0 322 12-14-2018, 09:52 PM
Last Post: Mishi Khan
  need GDB CS301 solution raja kamran 3 1,625 08-10-2018, 10:35 AM
Last Post: Pakistani
  CS301 Assignment 3 Solution Spring 2018 Admin 6 3,294 07-27-2018, 09:49 AM
Last Post: Pakistani
Thumbs Up required CS301 solution fahadmukhtar 3 1,898 07-25-2018, 01:49 PM
Last Post: Admin
  CS301 Assignment 2 Solution Spring 2018 Admin 1 3,182 05-28-2018, 10:10 PM
Last Post: rabia nawaz
  cs 301 assignment 2 solution needed rabia nawaz 0 409 05-26-2018, 10:18 PM
Last Post: rabia nawaz
  Required CS301 - Data Structures assignment solution please Javed Latif 0 1,382 05-08-2018, 12:47 PM
Last Post: Javed Latif
  CS301 Assignment 3 Solution Pakistani 2 5,282 07-09-2017, 07:13 PM
Last Post: Admin



Users browsing this thread:
1 Guest(s)