Group Two DSA Project1
Group Two DSA Project1
NAME……………………………………………………………….ID
1. TADESSE BIRHANU……………………………1306759
2.SEwUNET BELAY……………………………….1311172
3.EPHREM ALENE………………………………….1309119
4.MISGANA SAMUEL……………………………..1308358
5.DAwIT ABATE…………………………………….1313832
6. MINYAHIL TARIkU………………………………1307378
7. EPHREM MESELLE…………………………………..1308542
#include <iostream>
#include <string> // Include the string library
using namespace std; // Use the standard namespace
struct City { // Define a struct to hold information about a city
string name; // The name of the city
float distance; // The distance of the city from Addis Ababa
float longitude; // The longitude of the city
float latitude; // The latitude of the city
City* next; // A pointer to the next city in the list
};
City* start = NULL; // Initialize a pointer to the start of the list as NULL
void insert_city() { // A function to insert a new city into the list
City* temp = new City; // Create a new City object using dynamic memory allocation
cout << "\nPlease enter the city name: "; // Display a prompt for the name of the city
cin >> temp->name; // Read in the name of the city from the user
cout << "\nPlease enter the city distance from Addis Ababa: "; // Display a prompt for the distance
of the city
cin >> temp->distance; // Read in the distance of the city from the user
cout << "\nPlease enter the city latitude: "; // Display a prompt for the latitude of the city
cin >> temp->latitude; // Read in the latitude of the city from the user
cout << "\nPlease enter the city longitude: "; // Display a prompt for the longitude of the city
cin >> temp->longitude; // Read in the longitude of the city from the user
temp->next = NULL; // Set the next pointer of the new City object to NULL
#include <iostream>
#include <cstring>
#include <ctime>
using namespace std;
struct car{
int plateNo;
int position;
string carType;
string ownerName;
string arrivalTime;
car *next;
car *prev;
};
struct car *start=NULL;
struct car *tail =NULL;
car* carStack[100];
int top= -1;
void addInfo();
void addCar();
void enqueue(car* newCar);
int setOrder();
string setTime();
void searchCar();
void dequeue();
void display();
void restructure();
void dequeueRear(car *last);
void push(car *node);
string arrival();
car * pop();
string arrival(){
time_t curr_time;
curr_time = time(NULL);
tm *tm_local = localtime(&curr_time);
string tH, tM, tS, aT;
tH=to_string(tm_local->tm_hour) ;
tM=to_string(tm_local->tm_min);
tS=to_string(tm_local->tm_sec);
aT= (tH+":" + tM +":" +tS);
return aT;
}
void enqueue(car* newCar){
if(start==NULL){
start=newCar;
start->prev=NULL;
start->next=NULL;
}
else if(start->next==NULL){
start->next=newCar;
newCar->prev=start;
newCar->next=NULL;
tail=newCar;
}
else{
//car *temp = new car;
car *temp;
temp =tail;
temp->next=newCar;
newCar->prev=temp;
tail=newCar;
tail->next=NULL;
}}
void addCar(){
int num;
cout<<" Enter the number of cars you want to add: ";
cin>>num;
for(int i=0; i<num; i++){
car *temp = new car;
cout <<endl<<"\tFill the following form with car info." << endl<<endl;
cout<<"\t Car Type: ";
cin>>temp->carType;
cout<<"\t Owner's Name: ";
cin>>temp->ownerName;
cout<<"\t Plate No: ";
cin>>temp->plateNo;
temp->position=setOrder();
temp->arrivalTime=arrival();
enqueue(temp);
}
cout<<endl<<num<< " cars added sucessfully."<<endl;
}
int setOrder(){
car*temp;
int pos = 1;
temp=start;
while(temp!=NULL){
pos++;
temp= temp->next;
}
return pos;
}
void display(){
cout<<endl<<"List Information"<<endl;
if(start==NULL)
cout<<"The list is empty!"<<endl<<endl;
else{
car *temp;
temp = start;
while(temp != NULL){
string th= "th";
int pos=temp->position;
switch(pos){
case 1:
th="st";
break;
case 2:
th="nd";
break;
case 3:
th= "rd";
break;
}
cout<<endl<<endl<<"**************************************"<<endl<<endl;
cout<<"Car order: "<<temp->position<<th<<"."<<endl;
cout<<"Owner's Name: "<< temp->ownerName<<endl;
cout<<"Car Type: "<< temp->carType<<endl;
cout<<"Plate No: "<< temp->plateNo<<endl;
cout<<"Arrival Time: "<< temp->arrivalTime<<endl;
cout<<endl<<endl<<"**************************************"<<endl<<endl;
temp= temp->next;
}}}
void dequeue(){
car *temp;
temp= start;
start= start->next;
delete temp;
car *temp2;
temp2 =start;
while(temp2!=NULL){
temp2->position-=1;
temp2= temp2->next;
}
cout<<"One car was deleted!"<<endl;
}
void searchCar(){
int pno, position=0;
car *temp;
temp = start;
cout<<"\t Enter plate number of a car to search from the list: ";
cin>>pno;
while(temp!=NULL){
if(pno== temp->plateNo){
position = temp->position;
break;
}
temp= temp->next;
}
if(position == 0)
cout<<endl<<"\t The car was not found in the list."<<endl;
else
cout<<endl<<"\t The car is found at position number: "<<position<<endl;
}
void restructure(){
int pno;
bool isFound = false;
car *temp;
temp = start;
cout<<"\t Enter plate number of the car needed in urgency before service: ";
cin>>pno;
while(temp!=NULL){
if(pno== temp->plateNo){
isFound= true;
break;
}
temp= temp->next;
}
if(isFound==false)
cout<<endl<<"\t The car was not found in the list."<<endl;
else{
car *last;
last =temp;
dequeueRear(last);
tail = last->prev;
delete last;
last=NULL;
car*c;
while(top!=-1){
c = pop();
c->position-=1;
enqueue(c);
}}}
void dequeueRear(car *last){
car *temp;
while(tail!=last){
push(tail);
temp = tail;
tail = tail->prev;
}
}
void push(car *node){
if(top==-1){
top=0;
carStack[top]= node;
}
else{
top +=1;
carStack[top]=node;
}}
car* pop(){
car *popCar;
popCar= carStack[top];
carStack[top]=NULL;
top-=1;
return popCar;
}
int main()
{
int x;
do{
cout<<endl<<"\t\t WELCOME TO CHISS ABAY GARAGE"<<endl<<endl;
cout<<endl<<"\t\t\t--------MENU--------"<<endl<<endl;
cout<<"\t\tEnter 1 to add a car in the queue."<<endl;
cout<<"\t\tEnter 2 to give service to a car."<<endl;
cout<<"\t\tEnter 3 to display the queue info."<<endl;
cout<<"\t\tEnter 4 to search for a car in the list."<<endl;
cout<<"\t\tEnter 5 to take a car out before being served."<<endl;
cout<<"\t\tEnter 0 to exit."<<endl;
cout<<endl<<">>:";
cin>>x;
switch(x){
case 1:
addCar();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
searchCar();
break;
case 5:
restructure();
break;
case 0:
break;
}
}while(x!=0);
return 0;
}
3.Write a C++ linked list program that implements student ADT with properties id, name, father name,
sex, CGPA, etc. The ADT has also the following operation
A) Add operation that add a student wherever required
B) Delete operation that deletes an academic dismissal student ie. If his/her CGPA is less than 1.75;
C) Search operation that accept student name then search and display his/here information.
D) Display operation that displays student information in the linked list.
Sort operation that sort students linked list by their name in ascending order (use either of searching
technique.
#include <iostream>
#include <string>
using namespace std;
struct Student {
int id;
string name;
string fatherName;
string sex;
double cgpa;
Student* next;
};
Student* head = nullptr;
// Function to add a student to the linked list
void addStudent() {
int id;
string name, fatherName, sex;
double cgpa;
cout << "Enter student ID: ";
cin >> id;
cout << "Enter student name: ";
cin.ignore(); // Ignore the newline character in the input stream
getline(cin, name);
cout << "Enter father's name: ";
getline(cin, fatherName);
cout << "Enter sex: ";
cin >> sex;
cout << "Enter CGPA: ";
cin >> cgpa;
Student* newStudent = new Student;
newStudent->id = id;
newStudent->name = name;
newStudent->fatherName = fatherName;
newStudent->sex = sex;
newStudent->cgpa = cgpa;
newStudent->next = nullptr;
if (head == nullptr) {
// If the list is empty
head = newStudent;
} else {
// Add the student at the beginning of the list
newStudent->next = head;
head = newStudent;
}
cout << "Student added successfully.\n";
}
// Function to delete an academically dismissed student
void deleteStudent() {
if (head == nullptr) {
cout << "No students in the list.\n";
return;
}
// Check if the first student in the list should be deleted
if (head->cgpa < 1.75) {
Student* temp = head;
head = head->next;
delete temp;
cout << "Academically dismissed student deleted.\n";
return;
}
Student* prev = head;
Student* curr = head->next;
while (curr != nullptr) {
if (curr->cgpa < 1.75) {
prev->next = curr->next;
delete curr;
cout << "Academically dismissed student deleted.\n";
return;
}
prev = curr;
curr = curr->next;
}
cout << "No academically dismissed students found.\n";
}
// Function to search for a student using their name and display their information
void searchStudent() {
string name;cout << "Enter the name of the student to search: ";
cin.ignore(); // Ignore the newline character in the input stream
getline(cin, name);
Student* curr = head;
bool found = false;
while (curr != nullptr) {
if (curr->name == name) {
cout << "Student found.\n";
cout << "ID: " << curr->id <<endl;
cout << "Name: " << curr->name << endl;
cout << "Father's Name: " << curr->fatherName << endl;
cout << "Sex: " << curr->sex << endl;
cout << "CGPA: " << curr->cgpa << endl;
found = true;
break;
}
curr = curr->next;
}
if (!found) {
cout << "Student with name '" << name << "' not found.\n";
}
}
// Function to display all students in the linked list
void displayStudents() {
if (head == nullptr) {
cout << "No students in the list.\n";
return;
}
Student* curr = head;
while (curr != nullptr) {
cout << "ID: " << curr->id <<endl;
cout << "Name: " << curr->name <<endl;
cout << "Father's Name: " << curr->fatherName <<endl;
cout << "Sex: " << curr->sex << endl;
cout << "CGPA: " << curr->cgpa << endl;
cout << endl;
curr = curr->next;
}
}
// Function to swap the data of two students
void swapStudents(Student* a, Student* b) {
int id = a->id;
string name = a->name;
string fatherName = a->fatherName;
string sex = a->sex;
double cgpa = a->cgpa;
a->id = b->id;
a->name = b->name;
a->fatherName = b->fatherName;
a->sex = b->sex;
a->cgpa = b->cgpa;
b->id = id;
b->name = name;
b->fatherName = fatherName;
b->sex = sex;
b->cgpa = cgpa;
}
// Function to sort the students linked list by their name in ascending order
void sortStudents() {
if (head == nullptr || head->next == nullptr) {
// If the list is empty or contains only one student, it is already sorted
return;
}
bool swapped;
Student* curr;
Student* last = nullptr;
do {
swapped = false;
curr = head;
while (curr->next != last) {
if (curr->name > curr->next->name) {
swapStudents(curr, curr->next);
swapped = true;
}
curr = curr->next;
}
last = curr;
} while (swapped);
}
int main() {
int choice;
while (true) {
cout << "---------------------" <<endl;
cout << "1. Add a student" <<endl;
cout << "2. Delete academically dismissed student" <<endl;
cout << "3. Search for a student" <<endl;
cout << "4. Display all students" <<endl;
cout << "5. Sort students by name" <<endl;
cout << "6. Exit" <<endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
searchStudent();
break;
case 4:
displayStudents();
break;
case 5:
sortStudents();
cout << "Students sorted by name.\n";
break;
case 6:
exit(0);
default:
cout << "Invalid choice. Please try again.\n";
}}
return 0;
}