0% found this document useful (0 votes)
2 views

All Programs

The document contains multiple programming experiments, including implementations of stack operations, sorting algorithms (Selection Sort and Bubble Sort), and appointment scheduling using linked lists. It also includes a postfix expression evaluator and a queue implementation. Each section provides a menu-driven interface for user interaction and demonstrates various data structure manipulations.

Uploaded by

Yash Dhumal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

All Programs

The document contains multiple programming experiments, including implementations of stack operations, sorting algorithms (Selection Sort and Bubble Sort), and appointment scheduling using linked lists. It also includes a postfix expression evaluator and a queue implementation. Each section provides a menu-driven interface for user interaction and demonstrates various data structure manipulations.

Uploaded by

Yash Dhumal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Exp 2

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

cout<<endl;

void STACK::convert(char str[])

int j,k,len = strlen(str);

for(j=0, k=0; j<len; j++)

if( ( (int)str[j] >= 97 && (int)str[j] <=122 ) || ( (int)str[j] >= 65 &&

(int)str[j] <=90 ))

if( (int)str[j] <=90 )

str[k] = (char)( (int)str[j] + 32 );

}else

str[k] = str[j];

k++;

str[k]='\0';

cout<<endl<<"Converted String : "<<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<<"\n\nString is palindrome...";

else

cout<<"\n\nString is not palindrome...";

int main()

STACK stack;

char str[max];

int i=0;

cout<<"\nEnter string to be reversed and check is it 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();

}
Exp 4

# 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 = float(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

Exp 6

# Function for accepting the percentage of the Students

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],sep = "\n")

#<--------------------------------------------------------------------------------------------------------------------->

# 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 = len(perc) - 1, -1

else:

start, stop = len(perc) - 1, len(perc) - 6

for i in range(start, stop, -1):

print(perc[i],sep = "\n")

#<--------------------------------------------------------------------------------------------------------------------->

# Main

unsorted_percentage = []

sorted_percentage = []

flag = 1

while flag == 1:

print("\n--------------------MENU--------------------")

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!!")

#<-----------------------------------------------END OF PROGRAM-------------------------------------------------------->
Exp 8

#include<iostream>

using namespace std;

int size; // No of Nodes or Appointments

struct SLL_Node // Node Structure of each Appointment

int start;

int end;

int min;

int max;

int flag;

struct SLL_Node *next;

}*head;

class App_Shedule

public:

void create_Shed();

void display_Shed();

void book_App();

void cancel_App();
void sort_App();

}A1;

int main()

int ch;

char ans;

do

cout<<"\n\n *** Menu ***";

cout<<"\n 1. Create Appointment Schedule";

cout<<"\n 2. Display Free Slots";

cout<<"\n 3. Book an Appointment";

cout<<"\n 4. Cancel an Appointment";

cout<<"\n 5. Sort slots based on Time";

cout<<"\n\n\t Enter your choice: ";

cin>>ch;

switch(ch)

case 1: A1.create_Shed();

break;

case 2: A1.display_Shed();

break;
case 3: A1.book_App();

break;

case 4: A1.cancel_App();

break;

case 5: A1.sort_App();

break;

default: cout<<"\n\t Wrong choice!!!";

cout<<"\n\n\t Do you wanna continue? (y/n) : ";

cin>>ans;

}while(ans == 'y');

void App_Shedule :: create_Shed() //Function Definition to create Appointment Schedule

int i;

struct SLL_Node *temp, *last;

head = NULL;

cout<<"\n\n\t How many Appointment Slots: ";

cin>>size;

for(i=0; i<size; i++)


{

temp = new(struct SLL_Node); // Step 1: Dynamic Memory Allocation

cout<<"\n\n\t Enter Start Time: "; // Step 2: Assign Data & Address

cin>>temp->start;

cout<<"\n\t Enter End Time: ";

cin>>temp->end;

cout<<"\n\n\t Enter Minimum Duration: ";

cin>>temp->min;

cout<<"\n\t Enter Maximum Duration: ";

cin>>temp->max;

temp->flag = 0;

temp->next = NULL;

if(head == NULL)

head = temp;

last = head;

else

last->next = temp;

last = last->next;

void App_Shedule :: display_Shed() //Function Definition to Display Appointment Schedule

{
int cnt = 1;

struct SLL_Node *temp;

cout<<"\n\n\t ****Appointment Schdule****";

cout<<"\n\n\t Srno.\tStart\tEnd\tMin_Dur\tMax_Dur\tStatus";

temp = head;

while(temp != NULL)

cout<<"\n\n\t "<<cnt;

cout<<"\t "<<temp->start;

cout<<"\t "<<temp->end;

cout<<"\t "<<temp->min;

cout<<"\t "<<temp->max;

if(temp->flag)

cout<<"\t-Booked-";

else

cout<<"\t--Free--";

temp = temp->next;

cnt++;

void App_Shedule :: book_App() //Function Definition to Book Appointment

int start;

struct SLL_Node *temp;


cout<<"\n\n\t Please enter Appointment time: ";

cin>>start;

temp = head;

while(temp != NULL)

if(start == temp->start)

if(temp->flag == 0)

cout<<"\n\n\t Appointment Slot is Booked!!!";

temp->flag = 1;

else

cout<<"\n\n\t Appointment Slot is not Available!!!";

temp = temp->next;

void App_Shedule :: cancel_App() //Function Defination to Cancel Appointment

int start;

struct SLL_Node *temp;

cout<<"\n\n\t Please enter Appointment time to Cancel: ";


cin>>start;

temp = head;

while(temp != NULL)

if(start == temp->start)

if(temp->flag == 1)

cout<<"\n\n\t Your Appointment Slot is Canceled!!!";

temp->flag = 0;

else

cout<<"\n\n\t Your Appointment was not Booked!!!";

temp = temp->next;

void App_Shedule :: sort_App() //Function Definition to Sort Appointments

int i,j,val;

struct SLL_Node *temp;

for(i=0; i < size-1; i++)

temp = head;
while(temp->next != NULL)

if(temp->start > temp->next->start)

val = temp->start;

temp->start = temp->next->start;

temp->next->start = val;

val = temp->end;

temp->end = temp->next->end;

temp->next->end = val;

val = temp->min;

temp->min = temp->next->min;

temp->next->min = val;

val = temp->max;

temp->max = temp->next->max;

temp->next->max = val;

temp = temp->next;

cout<<"\n\n\t The Appointments got Sorted!!!";

}
Exp 10

#include<iostream>

#include<cctype>

#include<stack>

using namespace std;

// returns the value when a specific operator

// operates on two operands

int eval(int op1, int op2, char operate) {

switch (operate) {

case '*': return op2 * op1;

case '/': return op2 / op1;

case '+': return op2 + op1;

case '-': return op2 - op1;

default : return 0;

int getWeight(char ch) {

switch (ch) {

case '/':

case '*': return 2;

case '+':

case '-': return 1;

default : return 0;

int evalPostfix(char postfix[], int size) {


stack<int> s;

int i = 0;

char ch;

int val;

while (i < size) {

ch = postfix[i];

if (isdigit(ch)) {

// we saw an operand

// push the digit onto stack

s.push(ch-'0');

else {

// we saw an operator

// pop off the top two operands from the

// stack and evalute them using the current

// operator

int op1 = s.top();

s.pop();

int op2 = s.top();

s.pop();

val = eval(op1, op2, ch);

// push the value obtained after evaluating

// onto the stack

s.push(val);

i++;

return val;

// main
int main() {

char postfix[] = {'a','b','c','+','*','d','/'};

int i=0;

// cout<<"Postfix 0"<<postfix[i];

while(postfix[i]!='\0')

if(getWeight(postfix[i])==0)

cout<<" Enter value for "<<postfix[i]<<" = ";

cin>>postfix[i];

i++;

int size = sizeof(postfix);

int val = evalPostfix(postfix, size);

cout<<"\nExpression evaluates to "<<val;

cout<<endl;

return 0;

}
Exp 12

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

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

You might also like