Name :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 06
# Write a Python program to compute following operations on
String:
# a) To display word with the longest length
# b) To determines the frequency of occurrence of particular
character in the string
# c) To check whether given string is palindrome or not
# d) To display index of first appearance of the substring
# e) To count the occurrences of each word in a given string.
# a) To display word with the longest length
sentence = input("Enter sentence: ")
longest = max(sentence.split(), key=len)
print("Longest word is: ", longest)
print("And its length is: ", len(longest))
# b) To determines the frequency of occurrence of particular
character in the string
# using naive method to get count of each element in string
all_freq = {}
for i in sentence:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
# printing result
print("Count of all characters in sentence is :\n "
+ str(all_freq))
# c) To check whether given string is palindrome or not
text="malayalam"
print("Given text is "+text)
rev=reversed(text)
if list(text)==list(rev):
print("its a palindrome")
else:
print("its not a palindrome")
# d) To display index of first appearance of the substring
sub_str1 =str(input("Enter word"))
print("index of first appearance of the substring
"+sub_str1+" is")
print(sentence.find(sub_str1))
#check if Substring found or not.
if(sentence.find(sub_str1)==-1):
print("Substring Not Found")
else:
print("Substring found")
# e) To count the occurrences of each word in a given string.
print("Following are the count the frequency of each word in
a given string")
def freq(sentence):
# break the string into list of
words sentence = sentence.split()
str2 = []
# loop till string values present in list str
for i in sentence:
# checking for the
duplicacy if i not in str2:
# insert value in str2
str2.append(i)
for i in range(0, len(str2)):
# count the frequency of each word(present in str2)
in sentence and print
print('count of frequency', str2[i],
'is :', sentence.count(str2[i]))
def main():
# call freq function
freq(sentence)
main() # call main function
Output:
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# python3
student.py
Enter sentence: Computer Engineering
Longest word is: Engineering
And its length is: 11
Count of all characters in sentence is :
{'C': 1, 'o': 1, 'm': 1, 'p': 1, 'u': 1, 't': 1, 'e': 3, 'r': 2, '
': 1, 'E': 1, 'n': 3, 'g': 2, 'i': 2}
Given text is malayalam
its a palindrome
Enter wordComputer
index of first appearance of the substring Computer is
0
Substring found
Following are the count the frequency of each word in a given string
count of frequency Computer is : 1
count of frequency Engineering is : 1
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
Name :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 06
m = int(input("Enter the number of rows:"))
n = int(input("Enter the number of columns:"))
a = []
f = 0
print("Enter the elements:")
for i in range(m):
t = []
for j in range(n):
t.append(int(input()))
a.append(t)
print("The original matrix is:")
for i in range(m):
for j in range(n):
print(a[i][j], end=" ")
print()
for i in range(m): # Fix: Change to m instead of n
row_min = a[i][0]
ci = 0
for j in range(1, n):
if row_min > a[i][j]:
row_min = a[i][j]
ci = j
k = 0
while k < m: # Fix: Change to m instead of n
if row_min < a[k][ci]:
break
k += 1
if k == m:
print("The saddle point is:", row_min)
f = 1
break # Fix: Add break to exit the loop if
saddle point is found
if f == 0:
print("Saddle point does not exist")
Output:-
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# python3
student.py
Enter the number of rows:3
Enter the number of columns:3
Enter the elements:
1
2
3
4
5
6
7
8
9
The original matrix is:
1 2 3
4 5 6
7 8 9
The saddle point is: 7
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
Name :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 06
'''
Write a python program to store first year percentage
of students in an array.
Write function for sorting array of
floating point numbers in ascending order using:
a) Selection Sort
b) Bubble Sort and display top five
scores
'''
# Function for Selection Sort of elements
def Selection_Sort(marks):
for i in range(len(marks)):
# Find the minimum element in remaining unsorted
array min_idx = i
for j in range(i + 1, len(marks)):
if marks[min_idx] > marks[j]:
min_idx = j
# Swap the minimum element with the first element
marks[i], marks[min_idx] = marks[min_idx], marks[i]
print("Marks of students after performing Selection Sort
on the list : ")
for i in range(len(marks)):
print(marks[i])
#<------------------------------------------------------------
--------------------------->
# Function for Bubble Sort of elements
def Bubble_Sort(marks):
n = len(marks)
# Traverse through all array
elements for i in range(n - 1):
# Last i elements are already in
place for j in range(0, n - i - 1):
# Traverse the array from 0 to n-i-1
# Swap if the element found is greater than the
next element
if marks[j] > marks[j + 1]:
marks[j], marks[j + 1] = marks[j + 1],
marks[j]
print("Marks of students after performing Bubble Sort on
the list :")
for i in range(len(marks)):
print(marks[i])
#<------------------------------------------------------------
--------------------------->
# Function for displaying top five marks
def top_five_marks(marks):
print("Top",len(marks),"Marks are :
") print(*marks[::-1], sep="\n")
#<------------------------------------------------------------
---------------------------->
# Main
marks=[]
n = int(input("Enter number of students whose marks are to
be displayed : "))
print("Enter marks for",n,"students (Press ENTER after every
students marks): ")
for i in range(0, n):
ele = int(input())
marks.append(ele) # adding the element
print("The marks of",n,"students are : ")
print(marks)
flag=1;
while flag==1:
print("\n---------------MENU---------------")
print("1. Selection Sort of the marks")
print("2. Bubble Sort of the marks")
print("3. Exit")
ch=int(input("\n\nEnter your choice (from 1 to 3) : "))
if ch==1:
Selection_Sort(marks)
a=input("\nDo you want to display top marks from the
list (yes/no) : ")
if a=='yes':
top_five_marks(marks)
else:
print("\nThanks for using this program!")
flag=0
elif ch==2:
Bubble_Sort(marks)
a = input("\nDo you want to display top five marks
from the list (yes/no) : ")
if a == 'yes':
top_five_marks(marks)
else:
print("\nThanks for using this program!")
flag = 0
elif ch==3:
print("\nThanks for using this program!!")
flag=0
else:
print("\nEnter a valid choice!!")
print("\nThanks for using this
program!!") flag=0
#<----------------------------------------END OF PROGRAM------
------------------------------------------->
Output:-
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# python3
student.py
Enter number of students whose marks are to be displayed : 5
Enter marks for 5 students (Press ENTER after every students marks):
25
90
84
77
63
The marks of 5 students are :
[25, 90, 84, 77, 63]
---------------MENU---------------
1. Selection Sort of the marks
2. Bubble Sort of the marks
3. Exit
Enter your choice (from 1 to 3) : 1
Marks of students after performing Selection Sort on the list :
25
63
77
84
90
Do you want to display top marks from the list (yes/no) : yes
Top 5 Marks are :
90
84
77
63
25
---------------MENU---------------
1. Selection Sort of the marks
2. Bubble Sort of the marks
3. Exit
Enter your choice (from 1 to 3) : 2
Marks of students after performing Bubble Sort on the list :
25
63
77
84
90
Do you want to display top five marks from the list (yes/no) : no
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
Name :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 06
#include<iostream>
using namespace std;
void in(float marks[20], int n)
{
int i, j;
float temp;
for (i = 1; i < n; i++)
{
temp = marks[i];
for (j = i - 1; j >= 0 && marks[j] > temp; j--)
marks[j + 1] = marks[j];
marks[j + 1] = temp;
}
cout << "\n Top five scores are:";
for (i = n - 1; i >= max(0, n - 5); i--) // Fix: Ensure the loop
doesn't go beyond the array bounds
cout << "\t" << marks[i];
}
void shell(float marks[20], int n)
{
int i, j, step;
float temp;
for (step = n / 2; step > 0; step = step / 2)
for (i = step; i < n; i++)
{
temp = marks[i];
for (j = i; j >= step && temp < marks[j - step]; j = j -
step)
marks[j] = marks[j - step];
marks[j] = temp;
}
cout << "\n Top five scores are:";
for (i = n - 1; i >= max(0, n - 5); i--) // Fix: Ensure the loop
doesn't go beyond the array bounds
cout << "\t" << marks[i];
}
int main()
{
float marks[20];
int i, n, ch;
cout << "\n Enter the total no.of students:";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "\n Enter the percentage marks of the second-
year student " << i + 1 << "::";
cin >> marks[i];
}
do // Fix: Changed while to do-while to ensure the loop runs
at least once
{
cout << "\n1. Insertion sort 2. Shell sort 3.Exit";
cout << "\n Enter your choice:"; cin >> ch;
switch (ch)
{
case 1:
in(marks, n);
break;
case 2:
shell(marks, n);
break;
case 3:
cout << "Exiting...\n";
break;
default:
cout << "Oops! Wrong choice.
\nTerminating...\nTerminated!\n";
}
} while (ch != 3);
return 0;
}
Output:-
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# python3
student.py
Enter the total no.of students:4
Enter the percentage marks of the second-year student 1::78
Enter the percentage marks of the second-year student 2::56
Enter the percentage marks of the second-year student 3::47
Enter the percentage marks of the second-year student 4::90
1. Insertion sort 2. Shell sort 3.Exit
Enter your choice:1
Top five scores are: 90 78 56 47
1. Insertion sort 2. Shell sort 3.Exit
Enter your choice:2
Top five scores are: 90 78 56 47
1. Insertion sort 2. Shell sort 3.Exit
Enter your choice:3
Exiting...
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
Name :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 06
#Write a python program to store first year percentage of students in array. Write
function for sorting array of floating point numbers in ascending order using quick sort
and display top five scores
def input_percentage():
perc = []
number_of_students = int(input("Enter the number of
Students : "))
for i in range(number_of_students):
perc.append(float(input("Enter the percentage
of Student {0} : ".format(i + 1))))
return perc
# Function for printing the percentage of the Students
def print_percentage(perc):
for i in range(len(perc)):
print(perc[i])
# Function for performing partition of the Data
def percentage_partition(perc, start, end):
pivot = perc[start]
lower_bound = start + 1
upper_bound = end
while True:
while lower_bound <= upper_bound and
perc[lower_bound] <= pivot:
lower_bound += 1
while lower_bound <= upper_bound and
perc[upper_bound] >= pivot:
upper_bound -= 1
if lower_bound <= upper_bound:
perc[lower_bound], perc[upper_bound] =
perc[upper_bound], perc[lower_bound]
else:
break
perc[start], perc[upper_bound] = perc[upper_bound],
perc[start]
return upper_bound
# Function for performing Quick Sort on the Data
def Quick_Sort(perc, start, end):
while start < end:
partition = percentage_partition(perc, start, end)
Quick_Sort(perc, start, partition - 1)
Quick_Sort(perc, partition + 1, end)
return perc
# Function for Displaying Top Five Percentages of Students
def display_top_five(perc):
print("Top Five Percentages are :
") if len(perc) < 5:
start, stop, step = len(perc) - 1, -1, -1
else:
start, stop, step = len(perc) - 1, len(perc) - 6, -1
for i in range(start, stop, step):
print(perc[i])
# Main
unsorted_percentage = []
sorted_percentage = []
flag = 1
while flag == 1:
print("\nMENU")
print("1. Accept the Percentage of Students")
print("2. Display the Percentages of Students")
print("3. Perform Quick Sort on the Data")
print("4. Exit")
ch = int(input("Enter your choice (from 1 to 4) : "))
if ch == 1:
unsorted_percentage = input_percentage()
elif ch == 2:
print_percentage(unsorted_percentage)
elif ch == 3:
print("Percentages of Students after performing
Quick Sort : ")
sorted_percentage = Quick_Sort(unsorted_percentage,
0, len(unsorted_percentage) - 1)
print_percentage(sorted_percentage)
a = input("Do you want to display the Top
5 Percentages of Students (yes/no) : ")
if a == 'yes':
display_top_five(sorted_percentage)
elif ch == 4:
print("Thanks for using this program!!")
flag = 0
else:
print("Invalid Choice!!")
Output:-
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# python3
student.py
MENU
1. Accept the Percentage of Students
2. Display the Percentages of Students
3. Perform Quick Sort on the Data
4. Exit
Enter your choice (from 1 to 4) : 1
Enter the number of Students : 3
Enter the percentage of Student 1 : 89
Enter the percentage of Student 2 : 77
Enter the percentage of Student 3 : 56
MENU
1. Accept the Percentage of Students
2. Display the Percentages of Students
3. Perform Quick Sort on the Data
4. Exit
Enter your choice (from 1 to 4) : 2
89.0
77.0
56.0
MENU
1. Accept the Percentage of Students
2. Display the Percentages of Students
3. Perform Quick Sort on the Data
4. Exit
Enter your choice (from 1 to 4) : 3
Percentages of Students after performing Quick Sort :
56.0,77.0,89.0
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
Name :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 06
#include <iostream>
#include <stdlib.h>
using namespace std;
class node
{
public:
node* next;
node* prev;
int seat;
string id;
int status;
};
class cinemax
{
public:
node* head, *tail, *temp;
cinemax()
{
head = NULL;
}
void create_list();
void display();
void book();
void cancel();
void avail();
};
void cinemax::create_list()
{
int i = 1;
temp = new node;
temp->seat = 1;
temp->status = 0;
temp->id = "null";
tail = head = temp;
for (int i = 2; i <= 70; i++)
{
node* p = new node;
p->seat = i;
p->status = 0;
p->id = "null";
tail->next = p;
p->prev = tail;
tail = p;
tail->next = head;
head->prev = tail;
}
}
void cinemax::display()
{
int r = 1;
node* temp = head;
int count = 0;
cout << "\n\n";
cout << " Screen this way \n";
cout << "\n";
while (temp->next != head)
{
if (temp->seat / 10 == 0)
cout << "S0" << temp->seat << " :";
else
cout << "S" << temp->seat << " :";
if (temp->status == 0)
cout << "| | ";
else
cout << "|_B_| ";
count++;
if (count % 7 == 0)
{
cout << endl;
r++;
}
temp = temp->next;
}
cout << "S" << temp->seat << ":";
if (temp->status == 0)
cout << "| | ";
else
cout << "|_B_| ";
}
void cinemax::book()
{
int x;
string y;
label:
cout << "\n\n\nEnter seat number to be booked\n";
cin >> x;
cout << "Enter your ID number\n";
cin >> y;
if (x < 1 || x > 70)
{
cout << "Enter correct seat number to book (1-70)\
n"; goto label;
}
node* temp = head;
while (temp->seat != x)
{
temp = temp->next;
}
if (temp->status == 1)
cout << "Seat already booked!\n";
else
{
temp->status = 1;
temp->id = y;
cout << "Seat " << x << " booked!\n";
}
}
void cinemax::cancel()
{
int x;
string y;
label1:
cout << "Enter seat number to cancel booking\n";
cin >> x;
cout << "Enter your ID\n";
cin >> y;
if (x < 1 || x > 70)
{
cout << "Enter correct seat number to cancel (1-
70)\n";
goto label1;
}
node* temp = head;
while (temp->seat != x)
{
temp = temp->next;
}
if (temp->status == 0)
{
cout << "Seat not booked yet!!\n";
}
else
{
if (temp->id == y)
{
temp->status = 0;
cout << "Seat Cancelled!\n";
}
else
cout << "Wrong User ID !!! Seat cannot be
cancelled!!!\n";
}
}
void cinemax::avail()
{
int r = 1;
node* temp = head;
int count = 0;
cout << "\n\n\n\n";
cout << "\n";
cout << " Screen this way \n";
cout << "\n";
while (temp->next != head)
{
if (temp->seat / 10 == 0)
cout << "S0" << temp->seat << " :";
else
cout << "S" << temp->seat << " :";
if (temp->status == 0)
cout << "| | ";
else if (temp->status == 1)
cout << " ";
count++;
if (count % 7 == 0)
{
cout << endl;
}
temp = temp->next;
}
if (temp->status == 0)
{
cout << "S" << temp->seat << ":";
if (temp->status == 0)
cout << "| | ";
}
}
int main()
{
cinemax obj;
obj.create_list();
int ch;
char c = 'y';
while (c == 'y')
{
obj.display();
cout <<
"\n*********************************************\n";
cout << " CINEMAX MOVIE THEATRE\n";
cout <<
"*********************************************\n";
cout << "\nEnter Choice\n1. Current SeatStatus\n2.
Book Seat\n3. Available Seat\n4. Cancel Seat\n";
cin >> ch;
switch (ch)
{
case 1:
obj.display();
break;
case 2:
obj.book();
break;
case 3:
obj.avail();
break;
case 4:
obj.cancel();
break;
default:
cout << "Wrong choice input\n";
}
cout << "\nDo you want to perform any other
operation : (y/n)\n";
cin >> c;
}
return 0;
}
Output:-
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# python3
student.py
Screen this way
S01 :| | S02 :| | S03 :| | S04 :| | S05 :| | S06 :|
| S07 :| |
S08 :| | S09 :| | S10 :| | S11 :| | S12 :| | S06 :|
| S14 :| |
S15 :| | S16 :| | S17 :| | S18 :| | S19 :| | S20 :|
| S21 :| |
S22 :| | S23 :| | S24 :| | S25 :| | S26 :| | S27 :|
| S28 :| |
S29 :| | S30 :| | S31 :| | S32 :| | S33 :| | S34 :|
| S35 :| |
S36 :| | S37 :| | S38 :| | S39 :| | S40 :| | S41 :|
| S42 :| |
S43 :| | S44 :| | S45 :| | S46 :| | S47 :| | S48 :|
| S49 :| |
S50 :| | S51 :| | S52 :| | S53 :| | S54 :| | S55 :|
| S56 :| |
S57 :| | S58 :| | S59 :| | S60 :| | S61 :| | S62 :|
| S63 :| |
S64 :| | S65 :| | S66 :| | S67 :| | S68 :| | S69 :|
| S70:| |
*********************************************
CINEMAX MOVIE THEATRE
*********************************************
Enter Choice
1. Current SeatStatus
2. Book Seat
3. Available Seat
4. Cancel Seat
1
Screen this way
S01 :| | S02 :| | S03 :| | S04 :| | S05 :| | S06 :|
| S07 :| |
S08 :| | S09 :| | S10 :| | S11 :| | S12 :| | S06 :|
| S14 :| |
S15 :| | S16 :| | S17 :| | S18 :| | S19 :| | S20 :|
| S21 :| |
S22 :| | S23 :| | S24 :| | S25 :| | S26 :| | S27 :|
| S28 :| |
S29 :| | S30 :| | S31 :| | S32 :| | S33 :| | S34 :|
| S35 :| |
S36 :| | S37 :| | S38 :| | S39 :| | S40 :| | S41 :|
| S42 :| |
S43 :| | S44 :| | S45 :| | S46 :| | S47 :| | S48 :|
| S49 :| |
S50 :| | S51 :| | S52 :| | S53 :| | S54 :| | S55 :|
| S56 :| |
S57 :| | S58 :| | S59 :| | S60 :| | S61 :| | S62 :|
| S63 :| |
S64 :| | S65 :| | S66 :| | S67 :| | S68 :| | S69 :|
| S70:| |
Do you want to perform any other operation : (y/n)
y
Screen this way
S01 :| | S02 :| | S03 :| | S04 :| | S05 :| | S06 :|
| S07 :| |
S08 :| | S09 :| | S10 :| | S11 :| | S12 :| | S06 :|
| S14 :| |
S15 :| | S16 :| | S17 :| | S18 :| | S19 :| | S20 :|
| S21 :| |
S22 :| | S23 :| | S24 :| | S25 :| | S26 :| | S27 :|
| S28 :| |
S29 :| | S30 :| | S31 :| | S32 :| | S33 :| | S34 :|
| S35 :| |
S36 :| | S37 :| | S38 :| | S39 :| | S40 :| | S41 :|
| S42 :| |
S43 :| | S44 :| | S45 :| | S46 :| | S47 :| | S48 :|
| S49 :| |
S50 :| | S51 :| | S52 :| | S53 :| | S54 :| | S55 :|
| S56 :| |
S57 :| | S58 :| | S59 :| | S60 :| | S61 :| | S62 :|
| S63 :| |
S64 :| | S65 :| | S66 :| | S67 :| | S68 :| | S69 :|
| S70:| |
*********************************************
CINEMAX MOVIE THEATRE
*********************************************
Enter Choice
1. Current SeatStatus
2. Book Seat
3. Available Seat
4. Cancel Seat
Screen this way
S01 :| | S02 :| | S03 :| | S04 :| | S05 :| | S06 :|
| S07 :| |
S08 :| | S09 :| | S10 :| | S11 :| | S12 :| | S06 :|
| S14 :| |
S15 :| | S16 :| | S17 :| | S18 :| | S19 :| | S20 :|
| S21 :| |
S22 :| | S23 :| | S24 :| | S25 :| | S26 :| | S27 :|
| S28 :| |
S29 :| | S30 :| | S31 :| | S32 :| | S33 :| | S34 :|
| S35 :| |
S36 :| | S37 :| | S38 :| | S39 :| | S40 :| | S41 :|
| S42 :| |
S43 :| | S44 :| | S45 :| | S46 :| | S47 :| | S48 :|
| S49 :| |
S50 :| | S51 :| | S52 :| | S53 :| | S54 :| | S55 :|
| S56 :| |
S57 :| | S58 :| | S59 :| | S60 :| | S61 :| | S62 :|
| S63 :| |
S64 :| | S65 :| | S66 :| | S67 :| | S68 :| | S69 :|
| S70:| |
Do you want to perform any other operation : (y/n)
y
Screen this way
S01 :| | S02 :| | S03 :| | S04 :| | S05 :| | S06 :|
| S07 :| |
S08 :| | S09 :| | S10 :| | S11 :| | S12 :| | S06 :|
| S14 :| |
S15 :| | S16 :| | S17 :| | S18 :| | S19 :| | S20 :|
| S21 :| |
S22 :| | S23 :| | S24 :| | S25 :| | S26 :| | S27 :|
| S28 :| |
S29 :| | S30 :| | S31 :| | S32 :| | S33 :| | S34 :|
| S35 :| |
S36 :| | S37 :| | S38 :| | S39 :| | S40 :| | S41 :|
| S42 :| |
S43 :| | S44 :| | S45 :| | S46 :| | S47 :| | S48 :|
| S49 :| |
S50 :| | S51 :| | S52 :| | S53 :| | S54 :| | S55 :|
| S56 :| |
S57 :| | S58 :| | S59 :| | S60 :| | S61 :| | S62 :|
| S63 :| |
S64 :| | S65 :| | S66 :| | S67 :| | S68 :| | S69 :|
| S70:| |
*********************************************
CINEMAX MOVIE THEATRE
*********************************************
Enter Choice
1. Current SeatStatus
2. Book Seat
3. Available Seat
4. Cancel Seat
2
Enter seat number to be booked
70
Enter your ID number
S70
Seat 70 booked!
Do you want to perform any other operation : (y/n)
n
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
Name :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 15
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
} node;
node *createnode()
{
int n, i;
node *p, *head, *t;
head = NULL;
printf("Enter the number of students:
"); scanf("%d", &n);
for (i = 0; i < n; i++)
{
p = (node *)malloc(sizeof(node));
printf("Enter the student's data:
"); scanf("%d", &(p->data)); p->next
= NULL;
if (head == NULL)
head = p;
else
{
t = head;
while (t->next != NULL)
t = t->next;
t->next = p;
}
}
return head;
}
void print(node *head)
{
node *p;
int cnt = 0;
p = head;
while (p != NULL)
{
printf("\t %d", p->data);
p = p->next;
cnt++;
}
printf("\nTotal number of students=%d\n", cnt);
}
int uni(node *head, node *head1)
{
node *p, *q;
int found = 0, count = 0;
p = head;
while (p != NULL)
{
printf("\t %d", p->data);
p = p->next;
}
for (q = head1; q != NULL; q = q->next)
{
found = 0;
for (p = head; p != NULL; p = p->next)
{
if (q->data == p->data)
{
found = 1;
break;
}
}
if (found != 1)
{
printf("\t %d", q->data);
}
count++;
}
return count;
}
void inter(node *head1, node *head2)
{
node *p, *q;
int found = 0;
for (q = head2; q != NULL; q = q->next)
{
found = 0;
for (p = head1; p != NULL; p = p->next)
{
if (q->data == p->data)
{
found = 1;
break;
}
}
if (found == 1)
{
printf("\t %d", q->data);
}
}
}
void sub1(node *head1, node *head2)
{
node *p, *q;
int found = 0;
for (p = head1; p != NULL; p = p->next)
{
found = 0;
for (q = head2; q != NULL; q = q->next)
{
if (p->data == q->data)
{
found = 1;
break;
}
}
if (found != 1)
{
printf("\t %d", p->data);
}
}
}
void sub2(node *head1, node *head2)
{
node *p, *q;
int found = 0;
for (q = head2; q != NULL; q = q->next)
{
found = 0;
for (p = head1; p != NULL; p = p->next)
{
if (p->data == q->data)
{
found = 1;
break;
}
}
if (found != 1)
{
printf("\t %d", q->data);
}
}
}
int main()
{
node *head, *head1;
int ch = 0, m, count;
head = head1 = NULL;
printf("Enter the number of students:
"); scanf("%d", &m);
while (ch != 8)
{
printf("\n************* Sets using singly linked
list *************************");
printf("\n1] Create\n2] Print\n3] Students like either
vanilla or butterscotch\n");
printf("4] Students like both vanilla &
butterscotch\n5] Students like vanilla only\n");
printf("6] Students like only butterscotch\n7]
Students like neither vanilla nor butterscotch\n");
printf("8] Exit\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Students like vanilla\n");
head = createnode();
printf("Students like butterscotch\n");
head1 = createnode();
break;
case 2:
printf("Students like vanilla\n");
print(head);
printf("Students like butterscotch\n");
print(head1);
break;
case 3:
printf("Students like either vanilla
or butterscotch\n");
count = uni(head, head1);
printf("\nTotal students like neither vanilla
nor butterscotch=%d\n", m - count);
break;
case 4:
printf("Students like both vanilla &
butterscotch\n");
inter(head, head1);
break;
case 5:
printf("Students like vanilla only\n");
sub1(head, head1);
break;
case 6:
printf("Students like butterscotch only\n");
sub2(head, head1);
break;
case 7:
printf("Students like neither vanilla nor
butterscotch=%d\n", m - count);
break;
case 8:
printf("Exiting the program\n");
break;
default:
printf("Invalid choice\n");
}
}
return 0;
}
Output:-
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# python3
student.py
************* Sets using singly linked list
*************************
1] Create
2] Print
3] Students like either vanilla or butterscotch
4] Students like both vanilla & butterscotch
5] Students like vanilla only
6] Students like only butterscotch
7] Students like neither vanilla nor butterscotch
8] Exit
Enter your choice: 1
Students like vanilla
Enter the number of students: 2
Enter the student's data: 1
Enter the student's data: 3
Students like butterscotch
Enter the number of students: 1
Enter the student's data: 2
************* Sets using singly linked list
*************************
1] Create
2] Print
3] Students like either vanilla or butterscotch
4] Students like both vanilla & butterscotch
5] Students like vanilla only
6] Students like only butterscotch
7] Students like neither vanilla nor butterscotch
8] Exit
Enter your choice: 2
Students like vanilla
06
Total number of students=2
Students like butterscotch
2
Total number of students=1
************* Sets using singly linked list
*************************
1] Create
2] Print
3] Students like either vanilla or butterscotch
4] Students like both vanilla & butterscotch
5] Students like vanilla only
6] Students like only butterscotch
7] Students like neither vanilla nor butterscotch
8] Exit
Enter your choice: 3
Students like either vanilla or butterscotch
06 2
Total students like neither vanilla nor butterscotch=7
************* Sets using singly linked list
*************************
1] Create
2] Print
3] Students like either vanilla or butterscotch
4] Students like both vanilla & butterscotch
5] Students like vanilla only
6] Students like only butterscotch
7] Students like neither vanilla nor butterscotch
8] Exit
Enter your choice: 4
Students like both vanilla & butterscotch
************* Sets using singly linked list
*************************
1] Create
2] Print
3] Students like either vanilla or butterscotch
4] Students like both vanilla & butterscotch
5] Students like vanilla only
6] Students like only butterscotch
7] Students like neither vanilla nor butterscotch
8] Exit
Enter your choice: 5
Students like vanilla only
06
************* Sets using singly linked list
*************************
1] Create
2] Print
3] Students like either vanilla or butterscotch
4] Students like both vanilla & butterscotch
5] Students like vanilla only
6] Students like only butterscotch
7] Students like neither vanilla nor butterscotch
8] Exit
Enter your choice: 6
Students like butterscotch only
2
************* Sets using singly linked list
*************************
1] Create
2] Print
3] Students like either vanilla or butterscotch
4] Students like both vanilla & butterscotch
5] Students like vanilla only
6] Students like only butterscotch
7] Students like neither vanilla nor butterscotch
8] Exit
Enter your choice: 7
Students like neither vanilla nor butterscotch=7
************* Sets using singly linked list
*************************
1] Create
2] Print
3] Students like either vanilla or butterscotch
4] Students like both vanilla & butterscotch
5] Students like vanilla only
6] Students like only butterscotch
7] Students like neither vanilla nor butterscotch
8] Exit
Enter your choice: 8
Exiting the program
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
Name :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 06
#include<iostream>
#include<string.h>
#define max 50
using namespace std;
class STACK {
private:
char a[max];
int top;
public:
STACK() {
top = -1;
}
void push(char);
void reverse();
void convert(char[]);
void palindrome();
};
void STACK::push(char c) {
top++;
a[top] = c;
a[top + 1] = '\0';
cout << endl << c << " is pushed on stack ...";
}
void STACK::reverse() {
char str[max];
cout << "\n\nReverse string is : ";
for (int i = top, j = 0; i >= 0; i--, j++) {
cout << a[i];
str[j] = a[i];
}
str[top + 1] = '\0';
}
void STACK::convert(char str[]) { int
j, k, len = strlen(str); cout <<
"\nConverted String : "; for (j =
0, k = 0; j < len; j++) {
if ((int)str[j] >= 65 && (int)str[j] <= 90)
{ str[k] = (char)((int)str[j] + 32);
k++;
} else if ((int)str[j] >= 97 &&
(int)str[j] <= 122) {
str[k] = str[j];
k++;
}
}
str[k] = '\0';
cout << str << "\n";
}
void STACK::palindrome() {
char str[max];
int i, j;
for (i = top, j = 0; i >= 0; i--, j++) {
str[j] = a[i];
}
str[j] = '\0';
if (strcmp(str, a) == 0)
cout << "\nString is palindrome...";
else
cout << "\nString is not palindrome...";
}
int main() {
STACK stack;
char str[max];
int i = 0;
cout << "\nEnter string to be reversed and check
if it is palindrome or not : \n\n";
cin.getline(str, 50);
stack.convert(str);
while (str[i] != '\0') {
stack.push(str[i]);
i++;
}
stack.palindrome();
stack.reverse();
return 0;
}
Output:-
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# python3
student.py
Enter string to be reversed and check if it is palindrome or
not :
Computer Engineering
Converted String : computerengineering
c is pushed on stack ...
o is pushed on stack ...
m is pushed on stack ...
p is pushed on stack ...
u is pushed on stack ...
t is pushed on stack ...
e is pushed on stack ...
r is pushed on stack ...
e is pushed on stack ...
n is pushed on stack ...
g is pushed on stack ...
i is pushed on stack ...
n is pushed on stack ...
e is pushed on stack ...
e is pushed on stack ...
r is pushed on stack ...
i is pushed on stack ...
n is pushed on stack ...
g is pushed on stack ...
String is not palindrome...
Reverse string is : gnireenigneretupmoc
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
Name :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 06
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int precedence(char op) {
if (op == '+' || op == '-') {
return 1;
} else if (op == '*' || op == '/')
{ return 2;
}
return 0; // Assuming all other characters have lower
precedence
}
string infixToPostfix(string infix)
{ string postfix = "";
stack<char> st;
for (char c : infix) {
if (isalnum(c)) {
postfix += c; // Operand, append to postfix
expression
} else if (c == '(') {
st.push(c); // Left parenthesis, push onto
stack } else if (c == ')') {
while (!st.empty() && st.top() != '(') {
postfix += st.top();
st.pop();
}
st.pop(); // Pop the left
parenthesis } else {
while (!st.empty() && precedence(st.top())
>= precedence(c)) {
postfix += st.top();
st.pop();
}
st.push(c); // Operator, push onto stack
}
}
while (!st.empty()) {
postfix += st.top();
st.pop();
}
return postfix;
}
int evaluatePostfix(string postfix)
{ stack<int> st;
for (char c : postfix) {
if (isalnum(c)) {
st.push(c - '0'); // Convert character to
integer and push onto stack
} else {
int operand2 = st.top();
st.pop();
int operand1 = st.top();
st.pop();
switch (c) {
case '+':
st.push(operand1 + operand2);
break;
case '-':
st.push(operand1 - operand2);
break;
case '*':
st.push(operand1 * operand2);
break;
case '/':
st.push(operand1 / operand2);
break;
}
}
}
return st.top();
}
int main() {
string infix_expression;
cout << "Enter an infix expression:
"; cin >> infix_expression;
string postfix_expression =
infixToPostfix(infix_expression);
cout << "Postfix expression: " << postfix_expression <<
endl;
int result = evaluatePostfix(postfix_expression);
cout << "Result: " << result << endl;
return 0;
}
Output:-
Enter an infix expression: A*b+D
Postfix expression: Ab*D+
Result: 870
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
NAME :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 06
#include <iostream>
#define MAX 10
using namespace std;
struct queue {
int data[MAX];
int front, rear;
};
class Queue {
struct queue q;
public:
Queue() {
q.front = q.rear = -1;
}
int isempty() {
return (q.front == q.rear) ? 1 : 0;
}
int isfull() {
return (q.rear == MAX - 1) ? 1 : 0;
}
void enqueue(int);
int delqueue();
void display();
};
void Queue::enqueue(int x) {
q.data[++q.rear] = x;
}
int Queue::delqueue() {
return q.data[++q.front];
}
void Queue::display() {
int i;
cout << "\n";
for (i = q.front + 1; i <= q.rear; i++)
cout << q.data[i] << " ";
}
int main() {
Queue obj;
int ch, x;
do {
cout << "\n 1.Insert Job\n 2.Delete Job\n 3.Display\
n 4.Exit\n Enter your choice : ";
cin >> ch;
switch (ch) {
case 1:
if (!obj.isfull()) {
cout << "\n Enter data : \n";
cin >> x;
obj.enqueue(x);
cout << endl;
} else {
cout << "Queue is overflow!!!\n\n";
}
break;
case 2:
if (!obj.isempty()) {
cout << "\n Deleted Element = " <<
obj.delqueue() << endl;
} else {
cout << "\n Queue is underflow!!!\n\n";
}
cout << "\nRemaining Jobs : \n";
obj.display();
break;
case 3:
if (!obj.isempty()) {
cout << "\n Queue contains : \n";
obj.display();
} else {
cout << "\n Queue is empty!!!\n\n";
}
break;
case 4:
cout << "\n Exiting Program.";
break;
}
} while (ch !=
4); return 0;
}
Output:-
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
1.Insert Job
2.Delete Job
3.Display
4.Exit
Enter your choice : 1
Enter data :
23
1.Insert Job
2.Delete Job
3.Display
4.Exit
Enter your choice : 3
Queue contains :
23
1.Insert Job
2.Delete Job
3.Display
4.Exit
Enter your choice : 2
Deleted Element = 23
Remaining Jobs :
1.Insert Job
2.Delete Job
3.Display
4.Exit
Enter your choice : 4
Exiting Program
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
Name :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 06
#include<iostream>
#include<stdio.h>
#define MAX 10
using namespace std;
struct que
{
int arr[MAX];
int front, rear;
};
void init(struct que *q)
{
q->front = -1;
q->rear = -1;
}
void print(struct que q)
{
int i = q.front;
while (i != q.rear)
{
cout << "\t" << q.arr[i];
i = (i + 1) % MAX;
}
cout << "\t" << q.arr[q.rear];
}
int isempty(struct que q)
{
return (q.rear == -1) ? 1 : 0;
}
int isfull(struct que q)
{
return (q.rear + 1) % MAX == q.front ? 1 : 0;
}
void addf(struct que *q, int data)
{
if (isempty(*q))
{
q->front = q->rear = 0;
q->arr[q->front] += data;
}
else
{
q->front = (q->front - 1 + MAX) %
MAX; q->arr[q->front] += data;
}
}
void addr(struct que *q, int data)
{
if (isempty(*q))
{
q->front = q->rear = 0;
q->arr[q->rear] += data;
}
else
{
q->rear = (q->rear + 1) % MAX;
q->arr[q->rear] += data;
}
}
int delf(struct que *q)
{
int data1;
data1 = q->arr[q->front];
if (q->front == q->rear)
init(q);
else
q->front = (q->front + 1) % MAX;
return data1;
}
int delr(struct que *q)
{
int data1;
data1 = q->arr[q->rear];
if (q->front == q->rear)
init(q);
else
q->rear = (q->rear - 1 + MAX) % MAX;
return data1;
}
int main()
{
struct que q;
int data, ch;
init(&q);
while (ch != 6)
{
cout << "\t\n1.Insert front"
< "\t\n2.Insert rear"
< "\t\n3.Delete front"
< "\t\n4.Delete rear"
< "\t\n5.Print"
< "\t\n6.Exit";
cout << "\nEnter your choice :
"; cin >> ch;
switch (ch)
{
case 1:
cout << "\nEnter data to insert front : ";
cin >> data;
addf(&q, data);
break;
case 2:
cout << "\nEnter the data to insert rear : ";
cin >> data;
addr(&q, data);
break;
case 3:
if (isempty(q))
cout << "\nDequeue is empty!!!";
else
{
data = delf(&q);
cout << "\nDeleted data is : " << data;
}
break;
case 4:
if (isempty(q))
cout << "\nDequeue is empty!!!";
else
{
data = delr(&q);
cout << "\nDeleted data is : " << data;
}
break;
case 5:
if (isempty(q))
cout << "\nDequeue is empty!!!";
else
{
cout << "\nDequeue elements are : ";
print(q);
}
break;
}
}
return 0;
}
Output:-
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# python3
student.py
1.Insert front
2.Insert rear
3.Delete front
4.Delete rear
5.Print
6.Exit
Enter your choice : 1
Enter data to insert front : 32
1.Insert front
2.Insert rear
3.Delete front
4.Delete rear
5.Print
6.Exit
Enter your choice : 2
Enter the data to insert rear : 44
1.Insert front
2.Insert rear
3.Delete front
4.Delete rear
5.Print
6.Exit
Enter your choice : 5
Dequeue elements are : 4202768 6422268
1.Insert front
2.Insert rear
3.Delete front
4.Delete rear
5.Print
6.Exit
Enter your choice : 3
Deleted data is : 4202768
1.Insert front
2.Insert rear
3.Delete front
4.Delete rear
5.Print
6.Exit
Enter your choice : 5
Dequeue elements are : 6422268
1.Insert front
2.Insert rear
3.Delete front
4.Delete rear
5.Print
6.Exit
Enter your choice : 6
root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
Name :- BHAMBARE ATHARV ACHYUT
Class :- S.E. Computer
Subject :- DSL
Roll No :- 06
#include<iostream>
#include<cstdlib>
using namespace std;
class pizza {
int front, rear, q[5];
public:
pizza() {
front = -1;
rear = -1;
}
int isfull() {
if ((front == 0 && rear == 4) || front == rear + 1)
return 1;
else
return 0;
}
int isempty() {
if (front == -1 && rear == -1)
return 1;
else
return 0;
}
void add() {
if (isfull() == 0) {
cout << "\n Enter the Pizza ID: ";
if (front == -1 && rear == -1) {
front = 0;
rear = 0;
cin >> q[rear];
}
else {
rear = (rear + 1) % 5;
cin >> q[rear];
}
char c;
cout << " Do you want to add another order ? ";
cin >> c;
if (c == 'y' || c == 'Y')
add();
else
cout << "\n Orders are full ";
}
}
void serve() {
if (isempty() == 0) {
if (front == rear) {
cout << "\n Order served is : " << q[front];
front = -1;
rear = -1;
}
else {
cout << "\n Order served is : " << q[front];
front = (front + 1) % 5;
}
}
else
cout << "\n Orders are empty ";
}
void display() {
if (isempty() == 0) {
for (int i = front; i != rear; i = (i + 1) % 5)
cout << q[i] << "<- ";
cout << q[rear];
}
else
cout << "\n Orders are empty";
}
void check() {
int ch;
cout << "\n\n * * * * PIZZA PARLOUR * * * * \n\n";
cout << "\n 1. Add a Pizza \n 2. Display the Orders \n
3. Serve a pizza \n 4. Exit \n Enter your choice : ";
cin >> ch;
switch (ch) {
case 1: add(); break;
case 2: display(); break;
case 3: serve(); break;
case 4: exit(0);
default:
cout << "Invalid choice ";
}
char ch1;
cout << "\n Do you want to continue?
"; cin >> ch1;
if (ch1 == 'y' || ch1 == 'Y')
check();
}
};
int main() {
pizza p1;
p1.check();
return 0;
}
Output:-
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# python3
student.py
* * * * PIZZA PARLOUR * * * *
1. Add a Pizza
2. Display the Orders
3. Serve a pizza
4. Exit
Enter your choice : 2
Orders are empty
Do you want to continue? y
* * * * PIZZA PARLOUR * * * *
1. Add a Pizza
2. Display the Orders
3. Serve a pizza
4. Exit
Enter your choice : 3
Orders are empty
Do you want to continue? y
* * * * PIZZA PARLOUR * * * *
1. Add a Pizza
2. Display the Orders
3. Serve a pizza
4. Exit
Enter your choice : 1
Enter the Pizza ID: 2
Do you want to add another order ? n
Orders are full
Do you want to continue? N root@root123-HP-
ProDesk-600-G3-SFF:/home/root123#