You are on page 1of 16

Kuwait University

College of Engineering and Petroleum.


Electrical Engineering Department.

EE207-Data Structure
Semester: spring 2022
Section No.

Assignment No. project

Student Name:

Maryam Bohamdi / Sara Abdullah

Student Id:

2171115598 / 2171119343

Instructor Name: Dr. Fawaz

TA Name: Eng. Hadeel Alqouz

Date: May30th,2022
Why do we used linked list?
A Singly Linked List consists of Nodes attached to each other where each node has two members – ‘data’ and ‘next’.’data’ is the data of the nodes and ‘next’ stores
the address of the next node.

advantages of Singly Linked List

Less memory is required for storing the members (2 members – data and next)

During execution, we can deallocate or allocate memory very easily.

Insertion and Deletion don’t require the shifting of all elements as required in the array

Sample output:
********************************** [ MAIN MENU ] ***********************************

1.Add a new record to the system.

2.Delete passenger reservation.

3.Search for a passenger using the passport number and return the record of him.

4.Print a report for a given passenger using his passport number.

5.Display the flight passengers.

6.Display the waiting list passengers.

7.Exit from the system.

*************************************************************************************

Enter your choice:1

*************** [ Add a new record to the system ] ******************

1. Add a new flight.

2. Add new Passenger reservation

3. Back to main menu

---------------------------------------------------------------------

Enter your choice:2

Flight#: Capacity Reserved# Departure date Arrival date Departure city Arrival city

*************************************************************************************************

1111 20 0 1-5-2022 1-5-2022 Kuwait Orlando

2222 50 0 3-5-2022 4-5-2022 London Kuwait

3333 35 0 4-5-2022 4-5-2022 Mecca Kuwait

Enter Flight number: 1111

Enter passport number: 99887

Enter passenger name: Sara

Enter cost of the flight: 120


Passenger is added to confirmation list

New passenger is added successful

*************** [ Add a new record to the system ] ******************

1. Add a new flight.

2. Add new Passenger reservation

3. Back to main menu

---------------------------------------------------------------------

Enter your choice:2

Flight#: Capacity Reserved# Departure date Arrival date Departure city Arrival city

*************************************************************************************************

1111 20 1 1-5-2022 1-5-2022 Kuwait Orlando

2222 50 0 3-5-2022 4-5-2022 London Kuwait

3333 35 0 4-5-2022 4-5-2022 Mecca Kuwait

Enter Flight number: 1111

Enter passport number: 66554

Enter passenger name: maryam

Enter cost of the flight: 125

Passenger is added to confirmation list

New passenger is added successful

*************** [ Add a new record to the system ] ******************

1. Add a new flight.

2. Add new Passenger reservation

3. Back to main menu

---------------------------------------------------------------------

Enter your choice:3

********************************** [ MAIN MENU ] ***********************************

1.Add a new record to the system.

2.Delete passenger reservation.

3.Search for a passenger using the passport number and return the record of him.

4.Print a report for a given passenger using his passport number.

5.Display the flight passengers.

6.Display the waiting list passengers.

7.Exit from the system.

*************************************************************************************

Enter your choice:5

Flight#: Capacity Reserved# Departure date Arrival date Departure city Arrival city
*************************************************************************************************

1111 20 2 1-5-2022 1-5-2022 Kuwait Orlando

2222 50 0 3-5-2022 4-5-2022 London Kuwait

3333 35 0 4-5-2022 4-5-2022 Mecca Kuwait

Enter Flight number: 1111

Flight number: 1111 Capacity: 20 Current Size: 2

Departure date: 1-5-2022 Arrival date: 1-5-2022

Departure city: Kuwait Arrival city: Orlando

Confirmation list

~-~-~-~-~-~-~-~-~

99887 Sara 120.0

66554 maryam 125.0

C:\Users\acer\Desktop\data\proj\Scripts\python.exe "C:/Users/acer/Desktop/Data Lab/proj/Airlines_Application.py"

********************************** [ MAIN MENU ] ***********************************

1.Add a new record to the system.

2.Delete passenger reservation.

3.Search for a passenger using the passport number and return the record of him.

4.Print a report for a given passenger using his passport number.

5.Display the flight passengers.

6.Display the waiting list passengers.

7.Exit from the system.

*************************************************************************************

Enter your choice:2

Flight#: Capacity Reserved# Departure date Arrival date Departure city Arrival city

*************************************************************************************************

1111 20 0 1-5-2022 1-5-2022 Kuwait Orlando

2222 50 0 3-5-2022 4-5-2022 London Kuwait

3333 35 0 4-5-2022 4-5-2022 Mecca Kuwait

Enter Flight number: 1111

Enter passport number: 66665

No passenger with passport number 66665


********************************** [ MAIN MENU ] ***********************************

1.Add a new record to the system.

2.Delete passenger reservation.

3.Search for a passenger using the passport number and return the record of him.

4.Print a report for a given passenger using his passport number.

5.Display the flight passengers.

6.Display the waiting list passengers.

7.Exit from the system.

*************************************************************************************

Enter your choice:1

*************** [ Add a new record to the system ] ******************

1. Add a new flight.

2. Add new Passenger reservation

3. Back to main menu

---------------------------------------------------------------------

Enter your choice:2

Flight#: Capacity Reserved# Departure date Arrival date Departure city Arrival city

*************************************************************************************************

1111 20 0 1-5-2022 1-5-2022 Kuwait Orlando

2222 50 0 3-5-2022 4-5-2022 London Kuwait

3333 35 0 4-5-2022 4-5-2022 Mecca Kuwait

Enter Flight number: 1111

Enter passport number: 66665

Enter passenger name: Alex

Enter cost of the flight: 120

Passenger is added to confirmation list

New passenger is added successful

*************** [ Add a new record to the system ] ******************

1. Add a new flight.

2. Add new Passenger reservation

3. Back to main menu

---------------------------------------------------------------------

Enter your choice:2

Flight#: Capacity Reserved# Departure date Arrival date Departure city Arrival city

*************************************************************************************************

1111 20 1 1-5-2022 1-5-2022 Kuwait Orlando


2222 50 0 3-5-2022 4-5-2022 London Kuwait

3333 35 0 4-5-2022 4-5-2022 Mecca Kuwait

Enter Flight number: 1111

Enter passport number: 45454

Enter passenger name: Sara

Enter cost of the flight: 110

Passenger is added to confirmation list

New passenger is added successful

*************** [ Add a new record to the system ] ******************

1. Add a new flight.

2. Add new Passenger reservation

3. Back to main menu

---------------------------------------------------------------------

Enter your choice:3

********************************** [ MAIN MENU ] ***********************************

1.Add a new record to the system.

2.Delete passenger reservation.

3.Search for a passenger using the passport number and return the record of him.

4.Print a report for a given passenger using his passport number.

5.Display the flight passengers.

6.Display the waiting list passengers.

7.Exit from the system.

*************************************************************************************

Enter your choice:2

Flight#: Capacity Reserved# Departure date Arrival date Departure city Arrival city

*************************************************************************************************

1111 20 2 1-5-2022 1-5-2022 Kuwait Orlando

2222 50 0 3-5-2022 4-5-2022 London Kuwait

3333 35 0 4-5-2022 4-5-2022 Mecca Kuwait

Enter Flight number: 1111

Enter passport number: 66665

Passenger with passport number 66665 is removed from confirmation list


********************************** [ MAIN MENU ] ***********************************

1.Add a new record to the system.

2.Delete passenger reservation.

3.Search for a passenger using the passport number and return the record of him.

4.Print a report for a given passenger using his passport number.

5.Display the flight passengers.

6.Display the waiting list passengers.

7.Exit from the system.

*************************************************************************************

Enter your choice:7

Thank you for using flight reservation system

Process finished with exit code 0

The code:

# Class Node implementation for one ticket (1)


class Node:
def __init__(self, p_number=0, p_name=None, cost=0.0):
self.__p_number = p_number # passport number
self.__p_name = p_name # passenger name
self.__cost = cost # ticket price
self.__next = None

def set_passport_number(self, p_number=0):


self.__p_number = p_number

def set_passenger_name(self, p_name=None):


self.__p_name = p_name

def get_passport_number(self):
return self.__p_number

def get_passenger_name(self):
return self.__p_name

def set_next(self, n=None):


self.__next = n

def get_next(self):
return self.__next

def print_info(self): # display passenger information


print(f"{self.__p_number}\t{self.__p_name}\t{self.__cost}")

from Node import Node # (2)

class Passengers: # implementation of single linked list of passengers


def __init__(self): # O(1)
self.head = None

# to check if the list is empty or not true if it is empty otherwise return false if
not empty
# O(1)
def isEmpty(self):
return self.head is None

# return how many passenger in the list


# O(n)
def size(self):
count = 0 # variable to count how many passenger
n = self.head # start from head
while n: # while there is data keep inside the loop
n = n.get_next() # go to next
count += 1 # increment the count each iteration
return count

# add new passenger to the list


# O(n)
def insert(self, p_number=0, p_name=None, cost=0.0):
if not self.head: # first check if list is empty
self.head = Node(p_number, p_name, cost) # add to the first (head)

else:
n = self.head # otherwise, move to the end of the list
while n.get_next():
n = n.get_next()
n.set_next(Node(p_number, p_name, cost)) # add to the last of the list

# search by passport number


# O(n)
def search(self, p_number=0):
n = self.head
while n:
if n.get_passport_number() == p_number: # check if the current passenger
object passport number = p_number
return True # found stop search
n = n.get_next()
return False # if not found return false and stop

# Cancel passenger reservation


# O(n)
def remove(self, p_number=0):
if self.head is None: # list is empty >> nothing to remove
return False

if self.head.get_passport_number() == p_number: # check if the requested


passport number is the first
self.head = self.head.get_next() # remove the first element
return True

else: # otherwise, search in the list for the required passport


number
n = self.head
while n: # each time check if the current object passport number =
p_number
if n.get_next().get_passport_number() == p_number:
n.set_next(n.get_next.get_next())
return True
n = n.get_next()
return False # not found

# show all list passenger information


# O(n)
def print_all(self):
n = self.head
if not n:
print(None)
while n:
n.print_info() # call print_info from Node class
n = n.get_next()

# print passenger information by passport number


# O(n)
def print_passenger_by_passport_number(self, p_number):
n = self.head
if not n:
print(None)
while n:
if n.get_passport_number() == p_number:
n.print_info()
break
n = n.get_next()

from Passengers import Passengers # (3)

class Flight: # class implementation of one flight


def __init__(self, nos=0, cap=0, dpt_date=None, arr_date=None, dpt_city=None,
arr_city=None):
self.__flight_number = nos
self.__flight_capacity = cap # total number of seats
self.__flight_confirmation_list = Passengers() # Object from single linked list
of passenger
self.__flight_waiting_list = Passengers() # Object from single linked list of
passenger
self.__flight_departure_date = dpt_date
self.__flight_arrival_date = arr_date
self.__flight_departure_city = dpt_city
self.__flight_arrival_city = arr_city
self.__next = None

# O(1)
def get_next(self):
return self.__next

# O(1)
def set_next(self, nxt):
self.__next = nxt

# O(1)
def get_flight_number(self):
return self.__flight_number

# check if the current flight is full or not


# O(1)
def isFull(self):
return self.__flight_confirmation_list.size() == self.__flight_capacity

# insert new passenger to the current flight


# O(n)
def insert(self, p_number=0, p_name=None, cost=0.0): # if current flight seats is
full >> insert to waiting list
if self.isFull():
self.__flight_waiting_list.insert(p_number, p_name, cost)
print("Passenger is added to waiting list")
else: # otherwise, insert into confirmation list
self.__flight_confirmation_list.insert(p_number, p_name, cost)
print("Passenger is added to confirmation list")

# Cancel reservation from the current flight


# O(n^2)
def flight_reservation_cancel(self, p_number=0):
# search by passport number in confirmation list
if self.__flight_confirmation_list.search(p_number): # if found --> remove it
O(n)
self.__flight_confirmation_list.remove(p_number) # O(n)
print(f"Passenger with passport number {p_number} is removed from
confirmation list")

# search in the waiting list, if found >> remove it


elif self.__flight_waiting_list.search(p_number): # O(n)
self.__flight_waiting_list.remove(p_number) # O(n)
print(f"Passenger with passport number {p_number} is removed from waiting
list")

else:
print(f"No passenger with passport number {p_number}")

# Search for a passenger by passport number


# O(N^2)
def search_by_passport_number(self, p_number=0):
# search in the confirmation list
if self.__flight_confirmation_list.search(p_number): # O(9)n)
print("Passenger found in the confirmation list and the following his
information: ")
self.__flight_confirmation_list.print_passenger_by_passport_number(p_number)
# (n)

# search in the waiting list


elif self.__flight_waiting_list.search(p_number):
print("Passenger found in the waiting list and the following his information:
")
self.__flight_waiting_list.print_passenger_by_passport_number(p_number)

# not found in both confirmation and waiting list


else:
print(f"There is no information with passport number {p_number}")

# show all fight information (Main Information) - without passengers


# (1)
def flights_information_by_passenger(self): # for choice 5
print("Flight number: ", self.__flight_number, "\tCapacity: ",
self.__flight_capacity,
"Current Size: ", self.__flight_confirmation_list.size())
print("Departure date: ", self.__flight_departure_date, "\tArrival date: ",
self.__flight_arrival_date)
print("Departure city: ", self.__flight_departure_city, "\tArrival city: ",
self.__flight_arrival_city)

# show all confirmation list passenger


# O(n)
def flights_information_by_passenger_confirmation_list(self):
print("Confirmation list")
print("~-~-~-~-~-~-~-~-~")
self.__flight_confirmation_list.print_all() # prints the whole list

# show all waiting list passenger


# O(n)
def flights_information_by_passenger_waiting_list(self):
print("Waiting list")
print("~-~-~-~-~-~-~")
self.__flight_waiting_list.print_all() # prints the whole list

# header of table showing


# O(n)
def headers(self):
print("Flight#:\tCapacity\tReserved#\tDeparture date\tArrival date\tDeparture
city\tArrival city")
# for i in range(47):
# print("--", end="")

print("**********************************************************************************
***************")

# show data of flight as tabulate


# O(1)
def flights_information_main(self):
print(self.__flight_number, "\t\t", self.__flight_capacity, "\t\t",
self.__flight_confirmation_list.size(),
"\t\t\t", self.__flight_departure_date, "\t\t", self.__flight_arrival_date,
"\t\t",
self.__flight_departure_city, "\t\t", self.__flight_arrival_city)

# Class implantation of list of flights (4)


class Sll_Flights:

def __init__(self): # O(1)


self.head = None

def isEmpty(self):
return self.head is None

def size(self):
count = 0
n = self.head
while n:
n = n.get_next()
count += 1
return count

def insert(self, new_flight=None):


if not self.head:
self.head = new_flight
else:
n = self.head
while n.get_next():
n = n.get_next()
n.set_next(new_flight)

def search(self, f_number=0):


n = self.head
while n:
if n.get_flight_number() == f_number:
return True # return true and stop search
n = n.get_next()
return False # if not found return false and stop

def get_flight(self, f_number=0):


n = self.head # move from first to the last
while n: # each iteration check if the current flight object flight
number (n) = f_number
if n.get_flight_number() == f_number:
return n # return flight object
n = n.get_next() # if not equals >> move to the next
return False # not found return false

def remove(self, f_number=0):


if self.head is None:
return False
if self.head.get_flight_number() == f_number:
self.head = self.head.get_next()
return True
else:
n = self.head
while n:
if n.get_next().get_flight_number() == f_number:
n.set_next(n.get_next.get_next())
return True
n = n.get_next()
return False

def print_all(self):
if self.head is not None:
self.head.headers()
n = self.head
while n:
n.flights_information_main() # call flights_information_main from flight
class
n = n.get_next()
print()

# print flight information by flight number


# O(n)
def print_flight_by_number(self, f_number, key=0): # used in app for choice 5
change the key from there
if self.head is not None:
n = self.head
while n:
if n.get_flight_number() == f_number:
if key == 1: # show flight information with confirmation list
and waiting list
n.flights_information_by_passenger()
n.flights_information_by_passenger_confirmation_list()
n.flights_information_by_passenger_waiting_list()
elif key == 2: # show flight information with confirmation list
only
n.flights_information_by_passenger()
n.flights_information_by_passenger_confirmation_list()
elif key == 3: # show flight information with waiting list only
n.flights_information_by_passenger()
n.flights_information_by_passenger_waiting_list()
break
n = n.get_next()

# insert new passenger to specific flight


# O(n)
def add_passenger(self, f_number=0, p_number=0, p_name=None, cost=0.0):
if self.head is not None:
n = self.head
while n:
if n.get_flight_number() == f_number:
n.insert(p_number, p_name, cost)
break
n = n.get_next()
# cancel reservation of one passenger for specific flight
# O(n)
def reservation_cancel(self, f_number=0, p_number=0):
if self.head is not None:
n = self.head
while n:
if n.get_flight_number() == f_number:
n.flight_reservation_cancel(p_number)
break
n = n.get_next()

# search for specific passenger in a specific flight number


# O(n)
def search_by_passport_number_in_all_flights(self, p_number=0):
if self.head is not None:
n = self.head
while n:
print("Flight number: ", n.get_flight_number())
n.search_by_passport_number(p_number)
n = n.get_next()

from Flight import Flight # (5)


from Sll_Flights import Sll_Flights

air_line = Sll_Flights() # object from SLL-Flights class (list)

# read flights information from text file


input_file = open("flights_info.txt", 'r')
line = input_file.readline()

while line != '': # read to the end of the file line by line
ls = line.split(',')
flight_obj = Flight(ls[0], int(ls[1]), ls[2], ls[3], ls[4], ls[5]) # declare an
object from the flight
air_line.insert(flight_obj) # insert new flight into the ssl_flights
line = input_file.readline() # read next line
input_file.close() # close file after finish

# add new record to one of the two choice (passenger or flights)


def add_new_record_to_the_system():
while True:
print("*************** [ Add a new record to the system ] ******************")
print("1. Add a new flight.")
print("2. Add new Passenger reservation ")
print("3. Back to main menu ")
print("---------------------------------------------------------------------")
ch = int(input("Enter your choice:"))

if ch == 1:
flight_number = input("Enter flight number: ")
capacity = int(input("Enter capacity of the flight: "))
departure_date = input("Enter departure date: ")
arrival_date = input("Enter arrival date: ")
departure_city = input("Enter departure city: ")
arrival_city = input("Enter arrival city: ")
anEntry = Flight(flight_number, capacity, departure_date, arrival_date,
departure_city, arrival_city)
air_line.insert(anEntry)

elif ch == 2:
air_line.print_all()
f_num = input("Enter Flight number: ")
if air_line.search(f_num):
p_num = int(input("Enter passport number: "))
p_name = input("Enter passenger name: ")
cost = float(input("Enter cost of the flight: "))
air_line.add_passenger(f_num, p_num, p_name, cost)
print("New passenger is added successful")

elif ch == 3:
break
else:
print("invalid choice")

# main menu of the system


while True:
print("\n********************************** [ MAIN MENU ]
***********************************")
print("1.Add a new record to the system.")
print("2.Delete passenger reservation.")
print("3.Search for a passenger using the passport number and return the record of
him.")
print("4.Print a report for a given passenger using his passport number.")
print("5.Display the flight passengers.")
print("6.Display the waiting list passengers.")
print("7.Exit from the system.")

print("**********************************************************************************
***")
choice = int(input("Enter your choice:"))

if choice == 1:
add_new_record_to_the_system()

elif choice == 2:
air_line.print_all()
f_number = input("Enter Flight number: ")
if air_line.search(f_number):
p_number = int(input("Enter passport number: "))
air_line.reservation_cancel(f_number, p_number)
else:
print("Invalid flight number")

elif choice == 3:
air_line.print_all()
f_number = input("Enter Flight number: ")
if air_line.search(f_number):
flight = air_line.get_flight(f_number)
passport_number = int(input("Enter passport number: "))
flight.search_by_passport_number(p_number)
else:
print("Invalid flight number")

elif choice == 4:
p_number = int(input("Enter passport number: "))
air_line.search_by_passport_number_in_all_flights(p_number)

elif choice == 5:
air_line.print_all()
f_number = input("Enter Flight number: ")
if air_line.search(f_number):
air_line.print_flight_by_number(f_number, 2) # key: (1. c&w || 2.C only ||
3.W only)
else:
print("Invalid flight number")

elif choice == 6:
air_line.print_all()
f_number = input("Enter Flight number: ")
if air_line.search(f_number):
air_line.print_flight_by_number(f_number, 3)
else:
print("Invalid flight number")

elif choice == 7:
print("Thank you for using flight reservation system")
break
else:
print("Invalid menu choice")

1111, 20,1-5-2022, 1-5-2022,Kuwait,Orlando


2222, 50,3-5-2022, 4-5-2022,London,Kuwait
3333, 35,4-5-2022, 4-5-2022,Mecca,Kuwait

You might also like