Write a menu driven program to perform various list Operations such as append an element

Python Program to Append, Delete and Display Elements of a List Using Classes

by · July 10, 2019

In this example, we will write a python program to append, delete and display elements of a list usign classes. To better understand this example, make sure you have knowledge of the following tutorials:-

  • Python Class and Objects
  • Python Constructor
  • Python Functions

Python Program to Append, Delete and Display Elements of a List Using Classes

PythonServer Side ProgrammingProgramming

When it is required to append, delete, and display the elements of a list using classes, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to add elements to the list, delete elements from the list and display the elements of the list using objects.

Below is a demonstration for the same −

Menu-Driven Programs in Python

An Introduction to Menu-Driven Program

Menu-Driven Program is a program that gets input from a user by showing the options list, known as the menu, from which the user chooses their option. Systems processing the Menu-Driven programs are ordinary, starting from washing machines controlled by Microprocessors to Automated Teller Machines [ATMs]. Taking the ATM case, the user presses single keys to indicate the type of transaction [if the user wants a receipt with the cash, or if an account statement is needed]. With many, the user presses a single key to indicate the amount of cash for withdrawal.

The Menu-Driven Systems are beneficial in two ways: At first, input is taken by the single keystrokes, which reduces the chance of the system too prone to user error. Secondly, Menu-Driven Systems limits the characters range resulting in the way where the entered input becomes unambiguous. Hence, these two characteristics make the whole system pretty user-friendly.

In the following tutorial, we will discover some Menu-Driven Programs written in Python. These programs will let us understand different aspects of Menu-Driven Programs along with different libraries and modules of Python Programming Language.

So, let's get started.

Calculate the Parameter and Area of different Shapes using functions

Program:

Output:

WELCOME TO A SIMPLE MENSURATION PROGRAM MAIN MENU 1. Calculate Parameter 2. Calculate Area 3. Exit Enter the Choice:1 CALCULATE PARAMETER 1. Circle 2. Rectangle 3. Square 4. Exit Enter the Choice:2 Enter Height of Rectangle:4 Enter Width of Rectangle:5 Parameter of Rectangle: 18 MAIN MENU 1. Calculate Parameter 2. Calculate Area 3. Exit Enter the Choice:2 CALCULATE AREA 1. Circle 2. Rectangle 3. Square 4. Exit Enter the Choice:1 Enter Radius of Circle:2 Area of Circle: 12.56 MAIN MENU 1. Calculate Parameter 2. Calculate Area 3. Exit Enter the Choice:5 Oops! Incorrect Choice. MAIN MENU 1. Calculate Parameter 2. Calculate Area 3. Exit Enter the Choice:3

Explanation:

In the above example, we have defined different functions printing the estimated value after calculation. These functions include the parameters and areas of circle, rectangle, and square, respectively. We have then printed the heading of the program saying: WELCOME TO A SIMPLE MENSURATION PROGRAM. Below that, we have used the infinite while loop to print the Main Menu containing different options. The program then uses the if-elif-else statements to ask the user to input the integer choosing the options. The program will also raise an exception if the inserted integer is not present in the options list. We have then created two different submenus separating the Parameter option and the Area option. We have then added few more options within these submenus describing different shapes. These options also take different integer values indicating the radius for circle, height and width for rectangle, and side for square. As a result, the menu-driven program is successfully created and is able to calculate the parameter and areas of different shapes.

Menu-Driven Program to create a simple calculator

In the following Menu-Driven Program, we are going to build a simple calculator in Python. We will use the infinite while loop and functions same as above. We will design a menu allowing the user to interact with the calculator functions such as addition, subtract, multiplication and division.

Let us consider the following program's syntax:

Program:

Output:

WELCOME TO A SIMPLE CALCULATOR MENU 1. Sum of two Numbers 2. Difference between two Numbers 3. Product of two Numbers 4. Division of two Numbers 5. Exit Enter the Choice: 1 ADDITION First Number: 3 Second Number: 4 3 + 4 = 7 MENU 1. Sum of two Numbers 2. Difference between two Numbers 3. Product of two Numbers 4. Division of two Numbers 5. Exit Enter the Choice: 2 SUBTRACTION First Number: 6 Second Number: 3 6 - 3 = 3 MENU 1. Sum of two Numbers 2. Difference between two Numbers 3. Product of two Numbers 4. Division of two Numbers 5. Exit Enter the Choice: 3 MULTIPLICATION First Number: 8 Second Number: 2 8 x 2 = 16 MENU 1. Sum of two Numbers 2. Difference between two Numbers 3. Product of two Numbers 4. Division of two Numbers 5. Exit Enter the Choice: 4 DIVISION First Number: 10 Second Number: 4 10 / 4 = 2.5 MENU 1. Sum of two Numbers 2. Difference between two Numbers 3. Product of two Numbers 4. Division of two Numbers 5. Exit Enter the Choice: 5

Explanation:

In the above program, we have used almost similar procedure we did in the previous program. We have defined various functions such as add, subtract, multiply, and divide. We have then used the while loop to print the menu list to the users and if-elif-else statements to return the answers that the user needed. As a result, a simple calculator is successfully created and performs some basic calculations like addition, subtraction, multiplication, and division.

Menu-Driven Program to create a Phone Directory

In the following Menu-Driven Program, we are going to create a Phonebook Directory using the different functions. We will add the following features to the Phonebook Directory:

  1. Storing the Contact Numbers of People
  2. Searching for the Contact Number using the person's name

Let us implement this idea in the following program:

Program:

Output:

WELCOME TO THE PHONEBOOK DIRECTORY MAIN MENU 1. Show all existing Contacts 2. Add a new Contact 3. Search the existing Contact 4. Exit Enter your choice: 1 There is no contact in the phonebook. Press Enter to continue ... MAIN MENU 1. Show all existing Contacts 2. Add a new Contact 3. Search the existing Contact 4. Exit Enter your choice: 2 Enter your First Name: Mark Enter your Last Name: Henry Enter your Phone number: 1234567890 Enter your E-mail Address: [emailprotected] The following Contact Details: [Mark Henry, 1234567890, [emailprotected]] has been stored successfully! Press Enter to continue ... MAIN MENU 1. Show all existing Contacts 2. Add a new Contact 3. Search the existing Contact 4. Exit Enter your choice: 3 Enter First name for Searching contact record: Mark Your Required Contact Record is: [Mark Henry, 1234567890, [emailprotected]] Press Enter to continue ... MAIN MENU 1. Show all existing Contacts 2. Add a new Contact 3. Search the existing Contact 4. Exit Enter your choice: 1 [Mark Henry, 1234567890, [emailprotected]] Press Enter to continue ... MAIN MENU 1. Show all existing Contacts 2. Add a new Contact 3. Search the existing Contact 4. Exit Enter your choice: 4 Thank you for using Phonebook!

Explanation:

In the above Menu-Driven Program, we have created a Phonebook Directory that can store a new contact in a text file, display the stored contacts and allow users to search an existing number too. First of all, we have created a text file to store the contact details. We have then defined various functions in order to add, show, and search different contacts. We have also created different contact detail fields such as First Name, Last Name, Mobile Number, and E-mail Address. As a result, the program is completed successfully, and the output of the same can be seen above.

Menu driven program for all operations on singly linked list in C

A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. In this article, all the common operations of a singly linked list is discussed in one menu-driven program.

Operations to be performed:

  • createList[]: To create the list with ‘n’ number of nodes initially as defined by the user.
  • traverse[]: To see the contents of the linked list, it is necessary to traverse the given linked list. The given traverse[] function traverses and prints the content of the linked list.
  • insertAtFront[]: This function simply inserts an element at the front/beginning of the linked list.
  • insertAtEnd[]: This function inserts an element at the end of the linked list.
  • insertAtPosition[]: This function inserts an element at a specified position in the linked list.
  • deleteFirst[]: This function simply deletes an element from the front/beginning of the linked list.
  • deleteEnd[]: This function simply deletes an element from the end of the linked list.
  • deletePosition[]: This function deletes an element from a specified position in the linked list.
  • maximum[]: This function finds the maximum element in a linked list.
  • mean[]: This function finds the mean of the elements in a linked list.
  • sort[]: This function sort the given linked list in ascending order.
  • reverseLL[]: This function reverses the given linked list.

Below is the implementation of the above operations:

C




// C program for the all operations in
// the Singly Linked List
#include
#include
// Linked List Node
struct node {
int info;
struct node* link;
};
struct node* start = NULL;
// Function to create list with n nodes initially
void createList[]
{
if [start == NULL] {
int n;
printf["\nEnter the number of nodes: "];
scanf["%d", &n];
if [n != 0] {
int data;
struct node* newnode;
struct node* temp;
newnode = malloc[sizeof[struct node]];
start = newnode;
temp = start;
printf["\nEnter number to"
" be inserted : "];
scanf["%d", &data];
start->info = data;
for [int i = 2; i link = newnode;
printf["\nEnter number to"
" be inserted : "];
scanf["%d", &data];
newnode->info = data;
temp = temp->link;
}
}
printf["\nThe list is created\n"];
}
else
printf["\nThe list is already created\n"];
}
// Function to traverse the linked list
void traverse[]
{
struct node* temp;
// List is empty
if [start == NULL]
printf["\nList is empty\n"];
// Else print the LL
else {
temp = start;
while [temp != NULL] {
printf["Data = %d\n", temp->info];
temp = temp->link;
}
}
}
// Function to insert at the front
// of the linked list
void insertAtFront[]
{
int data;
struct node* temp;
temp = malloc[sizeof[struct node]];
printf["\nEnter number to"
" be inserted : "];
scanf["%d", &data];
temp->info = data;
// Pointer of temp will be
// assigned to start
temp->link = start;
start = temp;
}
// Function to insert at the end of
// the linked list
void insertAtEnd[]
{
int data;
struct node *temp, *head;
temp = malloc[sizeof[struct node]];
// Enter the number
printf["\nEnter number to"
" be inserted : "];
scanf["%d", &data];
// Changes links
temp->link = 0;
temp->info = data;
head = start;
while [head->link != NULL] {
head = head->link;
}
head->link = temp;
}
// Function to insert at any specified
// position in the linked list
void insertAtPosition[]
{
struct node *temp, *newnode;
int pos, data, i = 1;
newnode = malloc[sizeof[struct node]];
// Enter the position and data
printf["\nEnter position and data :"];
scanf["%d %d", &pos, &data];
// Change Links
temp = start;
newnode->info = data;
newnode->link = 0;
while [i < pos - 1] {
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}
// Function to delete from the front
// of the linked list
void deleteFirst[]
{
struct node* temp;
if [start == NULL]
printf["\nList is empty\n"];
else {
temp = start;
start = start->link;
free[temp];
}
}
// Function to delete from the end
// of the linked list
void deleteEnd[]
{
struct node *temp, *prevnode;
if [start == NULL]
printf["\nList is Empty\n"];
else {
temp = start;
while [temp->link != 0] {
prevnode = temp;
temp = temp->link;
}
free[temp];
prevnode->link = 0;
}
}
// Function to delete from any specified
// position from the linked list
void deletePosition[]
{
struct node *temp, *position;
int i = 1, pos;
// If LL is empty
if [start == NULL]
printf["\nList is empty\n"];
// Otherwise
else {
printf["\nEnter index : "];
// Position to be deleted
scanf["%d", &pos];
position = malloc[sizeof[struct node]];
temp = start;
// Traverse till position
while [i < pos - 1] {
temp = temp->link;
i++;
}
// Change Links
position = temp->link;
temp->link = position->link;
// Free memory
free[position];
}
}
// Function to find the maximum element
// in the linked list
void maximum[]
{
int a[10];
int i;
struct node* temp;
// If LL is empty
if [start == NULL]
printf["\nList is empty\n"];
// Otherwise
else {
temp = start;
int max = temp->info;
// Traverse LL and update the
// maximum element
while [temp != NULL] {
// Update the maximum
// element
if [max < temp->info]
max = temp->info;
temp = temp->link;
}
printf["\nMaximum number "
"is : %d ",
max];
}
}
// Function to find the mean of the
// elements in the linked list
void mean[]
{
int a[10];
int i;
struct node* temp;
// If LL is empty
if [start == NULL]
printf["\nList is empty\n"];
// Otherwise
else {
temp = start;
// Stores the sum and count of
// element in the LL
int sum = 0, count = 0;
float m;
// Traverse the LL
while [temp != NULL] {
// Update the sum
sum = sum + temp->info;
temp = temp->link;
count++;
}
// Find the mean
m = sum / count;
// Print the mean value
printf["\nMean is %f ", m];
}
}
// Function to sort the linked list
// in ascending order
void sort[]
{
struct node* current = start;
struct node* index = NULL;
int temp;
// If LL is empty
if [start == NULL] {
return;
}
// Else
else {
// Traverse the LL
while [current != NULL] {
index = current->link;
// Traverse the LL nestedly
// and find the minimum
// element
while [index != NULL] {
// Swap with it the value
// at current
if [current->info > index->info] {
temp = current->info;
current->info = index->info;
index->info = temp;
}
index = index->link;
}
// Update the current
current = current->link;
}
}
}
// Function to reverse the linked list
void reverseLL[]
{
struct node *t1, *t2, *temp;
t1 = t2 = NULL;
// If LL is empty
if [start == NULL]
printf["List is empty\n"];
// Else
else {
// Traverse the LL
while [start != NULL] {
// reversing of points
t2 = start->link;
start->link = t1;
t1 = start;
start = t2;
}
start = t1;
// New head Node
temp = start;
printf["Reversed linked "
"list is : "];
// Print the LL
while [temp != NULL] {
printf["%d ", temp->info];
temp = temp->link;
}
}
}
// Driver Code
int main[]
{
int choice;
while [1] {
printf["\n\t1 To see list\n"];
printf["\t2 For insertion at"
" starting\n"];
printf["\t3 For insertion at"
" end\n"];
printf["\t4 For insertion at "
"any position\n"];
printf["\t5 For deletion of "
"first element\n"];
printf["\t6 For deletion of "
"last element\n"];
printf["\t7 For deletion of "
"element at any position\n"];
printf["\t8 To find maximum among"
" the elements\n"];
printf["\t9 To find mean of "
"the elements\n"];
printf["\t10 To sort element\n"];
printf["\t11 To reverse the "
"linked list\n"];
printf["\t12 To exit\n"];
printf["\nEnter Choice :\n"];
scanf["%d", &choice];
switch [choice] {
case 1:
traverse[];
break;
case 2:
insertAtFront[];
break;
case 3:
insertAtEnd[];
break;
case 4:
insertAtPosition[];
break;
case 5:
deleteFirst[];
break;
case 6:
deleteEnd[];
break;
case 7:
deletePosition[];
break;
case 8:
maximum[];
break;
case 9:
mean[];
break;
case 10:
sort[];
break;
case 11:
reverseLL[];
break;
case 12:
exit[1];
break;
default:
printf["Incorrect Choice\n"];
}
}
return 0;
}




Output:

Menu:

Insertion at the starting:

Insertion at the end:

Insertion at specific position:

Print the Linked List:

Maximum among Linked List:

Sorting the Linked List:

Reverse the Linked List:

Delete the first and last element with choice 5 and 6:




Article Tags :
Data Structures
Linked List
Technical Scripter
Delete a Linked List
Linked Lists
Linked-List-Sorting
Menu driven programs
Technical Scripter 2020
Practice Tags :
Data Structures
Linked List
Read Full Article

Video liên quan

Bài Viết Liên Quan

Bài mới nhất

Chủ Đề