0% found this document useful (0 votes)
31 views12 pages

Group Two DSA Project1

data structure

Uploaded by

asmeradagne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views12 pages

Group Two DSA Project1

data structure

Uploaded by

asmeradagne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

BAHIR UNIVERSITY

BAHIR INSTITUTE OF TECHNOLOGY


FACULTY OF COMPUTING
ITBED DEPARTEMENT
DATA STRUCTURE AND ALGORITHM GROUP PROjECT

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

SUBMITTED TO: LEC. MELkAMU.


SUBMISSION DATE:11/09/2015 E.C
1.Implement town linked list from BahirDar to Addis Ababa. Each town record contain the name of
the town, distance from its neighbor, longitude and latitudes. Your program should implement
operations of insertion, deletion ,searching by name and displaying all town information’s. Write
another operation to accept the starting and destination town and display the distance between
towns(use linked list).

#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

if (start == NULL) { // If the list is empty


start = temp; // Set the start pointer to point to the new City object
}
else { // If the list is not empty
City* cur = start; // Create a pointer to the start of the list
while (cur->next != NULL) { // Traverse the list until the last element is reached
cur = cur->next; // Move the pointer to the next element
}
cur->next = temp; // Set the next pointer of the last element to point to the new City object
}
}
void display() { // A function to display the list of cities
int range; // Declare a variable to hold the range between neighboring cities
if (start == NULL) { // If the list is empty
cout << "\nThere is no data to display.\n"; // Display a message indicating that there is no data
to display
}
else { // If the list is not empty
City* temp = start; // Create a pointer to the start of the list
while (temp != NULL) { // Traverse the list until the end is reached
cout << "\nName: " << temp->name; // Display the name of the city
cout << "\nDistance from Addis Ababa: " << temp->distance; // Display the distance of the city
from Addis Ababa
cout << "\nLatitude: " << temp->latitude; // Display the latitude of the city
cout << "\nLongitude: " << temp->longitude; // Display the longitude of the city
if (temp->next != NULL) { // If the current element is not the last element in the list
range = temp->distance - temp->next->distance; // Calculate the range between the current
element and the next element
cout << "\nDistance from its neighbor city: " << range << endl; // Display the range between
the current element and the next element
}
temp = temp->next; // Move the pointer to the next element
}
}
}
void search_city(string name) { // A function to search for a city by name
City* temp = start; // Create a pointer to the start of the list
int i = 0; // Initialize a counter for the number of results found to 0
while (temp != NULL) { // Traverse the list until the end is reached
i++; // Increment the counter
if (temp->name == name) { // If the name of the current element matches the search name
cout << "\nFound result no: " << i; // Display the result number
cout << "\nName: " << temp->name; // Display the name of the city
cout << "\nDistance from Addis Ababa: " << temp->distance; // Display the distance of the city
from Addis Ababa
cout << "\nLatitude: " << temp->latitude; // Display the latitude of the city
cout << "\nLongitude: " << temp->longitude; // Display the longitude of the city
}
temp = temp->next; // Move the pointer to the next element
}
if (i == 0) { // If no results were found
cout << "\nSorry, we could not find any match.\n"; // Display a message indicating that no
match was found
}
}
void delete_city() { // A function to delete a city from the list
if (start == NULL) { // If the list is empty
cout << "\nThe list is already empty.\n"; // Display a message indicating that the list is empty
}
else { // If the list is not empty
cout << "\nYou are about to delete " << start->name << " from the list. Enter any numbers to
delete...\n"; // Display a message indicating which city is about to be deleted
int i; // Declare a variable to hold the input from the user
cin >> i; // Read in the input from the user
if (i) { // If the input is non-zero
City* temp = start->next; // Create a pointer to the second element in the list
delete start; // Deallocate the memory for the first element
start = temp; // Set the start pointer to point to the second element
cout<<"data deleted succesfully"<<endl; // Display a message indicating that the data was
deleted successfully
}
}
}

int main() { // The main function


int i; // Declare a variable to hold the input from the user
string name; // Declare a variable to hold the input city name
do { // Start a do-while loop to display a menu and prompt the user for input
cout<<"=======MENU==========="<<endl; // Display the menu
cout << "\nEnter 1 to insert data.";
cout << "\nEnter 2 to display data.";
cout << "\nEnter 3 to search city by its name.";
cout << "\nEnter 4 to delete data.";
cout << "\nEnter 0 to exit.";
cin >> i; // Read in the input from the user
switch (i) { // Use a switch statement to perform different actions based on the input
from the user
case 1:
insert_city(); // Call the insert_city function
break;
case 2:
display(); // Call the display function
break;
case 3:
cout << "\nPlease enter the city name to search: ";
cin >> name;
search_city(name); // Call the search_city function with the input city name
break;
case 4:
delete_city(); // Call the delete_city function
break;
case 0:
i = 0; // Set the input variable to 0 to exit the loop
break;
default:
cout << "\nInvalid input.\n"; // Display an error message for invalid input
}
} while (i != 0); // Continue the loop until the user enters 0 to exit
return 0; // Return 0 to indicate successful program execution
}
2.Chiss Abay Garage is found at Bahirdar city . This garage has one entrance and one exit gates. Its
parking which used to hold the repaired cars as a queue serves only a single car(does not allow two
cars parked parallel). The garage uses first come first served data structure principle (FIFO). write a
c++ program that stores the car information like plate number, type of cars, owner of the car, arrival
time. Your program should implement the following operations.
a) To add the car in the queue when new car is arrived.
b) Display all cars in the queue.
c) To search the care using his plate number and display ordered number from the first car
in the list.
d) To restructure the list. If the car wanted urgently before served or repaired, all cars
behind it will be out of the garage then returned back to the queue as previous
order(the car behind it will be take its turn)ie. Delete operation

#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;
}

You might also like