Data Science Lab 2023/24 Practicals From Karan Rathod

----- Data Science Lab Practicals From 1 to 13, Best of luck-----


1

def average(l):
    sum = 0
    cnt = 0
    for i in range(len(l)):
        if l[i] != -999:
            sum += l[i]
            cnt += 1

    avg = sum / cnt
    print("Total Marks are : ", sum)
    print("Average Marks are : {:.2f}".format(avg))


# Highest score in the class

def Maximum(l):
    Max = l[0]
    for i in range(len(l)):
        if l[i] > Max:
            Max = l[i]
    return (Max)


# Lowest score in the class


def Minimum(l):
    # Assign first element in the array which corresponds to marks of first present student
    # This for loop ensures the above condition

    for i in range(len(l)):
        if l[i] != -999:
            Min = l[i]
            break

    for j in range(i + 1, len(l)):
        if l[j] != -999 and l[j] < Min:
            Min = l[j]
    return (Min)


# Count of students who were absent for the test

def absentCnt(l):
    cnt = 0
    for i in range(len(l)):
        if l[i] == -999:
            cnt += 1
    return (cnt)


# Display mark with highest frequency
# refeence link : https://www.youtube.com/watch?v=QrIXGqvvpk4&t=422s
def maxFrequency(l):
    i = 0
    Max = 0
    print(" Marks ----> frequency count ")
    for ele in l:
        if l.index(ele) == i:
            print(ele, "---->", l.count(ele))
            if l.count(ele) > Max:
                Max = l.count(ele)
                mark = ele
        i += 1
    return (mark, Max)


# Input the number of students and their corresponding marks in FDS

marksInFDS = []
noStudents = int(input("Enter total number of students : "))
for i in range(noStudents):
    marks = int(input("Enter marks of Student " + str(i + 1) + " : "))
    marksInFDS.append(marks)

flag = 1
while flag == 1:
    print("/*************MENU**************/")
    print("1. The average score of class ")
    print("2. Highest score and lowest score of class ")
    print("3. Count of students who were absent for the test ")
    print("4. Display mark with highest frequency ")
    print("5. Exit ")
    choice = int(input("Enter your choice : "))

    if choice == 1:
        average(marksInFDS)

    elif choice == 2:
        print("Highest score in the class is : ", Maximum(marksInFDS))
        print("Lowest score in the class is : ", Minimum(marksInFDS))

    elif choice == 3:
        print("Count of students who were absent for the test is : ", absentCnt(marksInFDS))

    elif choice == 4:
        mark, count = maxFrequency(marksInFDS)
        print("Highest frequency of marks {0} is {1} ".format(mark, count))

    else:
        print("Wrong choice")
        flag = 0

2

str1 = input("Enter First String:")

def long_len():
    str_list = str1.split(" ")
    e = max(str_list, key=len)
    longest = str1.index(e)
    print("Longest word:", e)
    print("Index of the longest word:", longest)

print("Enter your choice\n 1. length\n 2. palindrome\n 3. count the number and frequency\n 4. count the character\n 5. index of substring\n")
choice = int(input())

if choice == 1:
    print("The length of the string is", len(str1))
    
elif choice == 2:
    s = str1[::-1]
    if str1 == s:
        print("It is a Palindrome")
    else:
        print("It is Not a Palindrome")

elif choice == 3:
    print("The original string is", str1)
    s = str1.split()
    result = len(s)
    print("The number of words in the string:", result)
    for i in range(result):
        print(s[i], s.count(s[i]))

elif choice == 4:
    c = input("Enter the character you want to count: ")
    s = str1.count(c)
    print("The character", c, "occurs in the string", s, "times.")

elif choice == 5:
    f = input("Enter the character index you want to find: ")
    res = str1.find(f)
    if res != -1:
        print("The character", f, "is found at index:", res)
    else:
        print("The character", f, "is not found in the string.")
else:
    print("Invalid choice. Please enter a number between 1 and 5.")

3

def matrix(r,c):
    out =[]
    for i in range(r):
        row = []
        for j in range(c):
            k = int(input(f"enter [{i}][{j}]: "))
            row.append(k)
        out.append(row)
    return out

# function for Addition of two matrices

def addition(A,B):
    result=[[0 for i in range(c)] for i in range(r)]
    for i in range(len(A)):
        for j in range(len(A[0])):
            result[i][j]=A[i][j]+B[i][j]
    for n in result:
        print(n)

            
# function for subtraction of two matrices
def subtraction(A,B):
    result=[[0 for i in range(c)] for i in range(r)]
    for i in range(len(A)):
        for j in range(len(A[0])):
            result[i][j]=A[i][j]-B[i][j]
    for n in result:
        print(n)
# function for multiplication of two matrices
def multiplication(A,B):
    result=[[0 for i in range(c)] for i in range(r)]
    for i in range(len(A)):# row of a matrix
        for j in range(len(A[0])):#coloum of B Matrix
            for k in range(len(B)):# row of B matric
                result[i][j]=result[i][j]+A[i][j]*B[i][j]
    for n in result:
        print(n)
# function for transpose of matrix
def transpose(A):
    result=[[0 for i in range(c)] for i in range(r)]
    for i in range(len(A)):
        for j in range(len(A[0])):
            result[j][i]= A[i][j]
        for n in result:
            print(n)
print("welcome to matix operation program")
while True:
    print("\n Main Menu")
    print("1. addition")
    print("2. subtraction")
    print("3. multiplication")
    print("4. Transpose")
    print("5. Exit")
    choice = int(input("Enter the choice :"))
    if choice== 1:
        r = int(input("enter the rows"))
        c = int(input("enter the coloumn"))
        print("matrix A is :")
        A= matrix(r,c)
        print("matrix B is :")
        B = matrix(r,c)
        print("\nAddition of matrix A & B is :")
        addition(A,B)
    elif choice == 2:
        r = int(input("enter the rows"))
        c = int(input("enter the coloumn"))
        print("matrix A is :")
        A= matrix(r,c)
        print("matrix B is :")
        B = matrix(r,c)
        print("\Subtraction of matrix A & B is :")
        subtraction(A,B)
    elif choice==3:
        r = int(input("enter the rows"))
        c = int(input("enter the coloumn"))
        print("matrix A is :")
        A= matrix(r,c)
        print("matrix B is :")
        B = matrix(r,c)
        print("\nMultiplication of matrix A & B is : ")
        multiplication(A,B)
    elif choice == 4:
        r = int(input("enter the rows"))
        c = int(input("enter the coloumn"))
        print("matrix A is :")
        A= matrix(r,c)
        print("\nTranspose of Matix A is :")
        transpose(A)
    elif choice ==5:
        break
    else:
        print("you have entered wrong value")

4

def linear_search(roll_no, key, n):
    for i in range(len(roll_no)):
        if roll_no[i] == key:
           return i
    return -1
def setinental_search(roll_no, key, n):
    last = roll_no[n - 1]
    roll_no[n - 1] = key
    i = 0
    while roll_no[i] != key:
          i += 1
    roll_no[n - 1] = last
    if (i < n - 1) or (roll_no[i] == key):
       print("Roll Number is Present")
    else:
       print("Roll Number is Not Present")
       
roll_no = [1, 10, 13, 2, 5, 8, 32, 22, 64, 25]
key = int(input("Enter the Roll Number to Find whether its Present or Not"))
print("________________________________________________________")
n = len(roll_no)
while True:
    print("1.Linear search\n2.Sentinel Search\n3.Exit")
    print("________________________________________________________")
    ch = int(input("Enter your choice"))
    print("________________________________________________________")
    if ch == 1:
       result = linear_search(roll_no, key, n)
       if result == -1:
          print("Roll Number is not present")
       else:
          print("Roll Number is present")
    elif ch == 2:
       setinental_search(roll_no, key, n)
    elif ch == 3:
       break
    else:
       print("Enter valid Input")
       
def binarysearch(roll, x):
   low = 0
   high = len(roll) - 1
   while high >= low:
       mid = (low + high) // 2
       if roll[mid] == x:
          return mid
          break
       elif roll[mid] > x:
          high = mid - 1
       elif roll[mid] < x:
          low = mid + 1
   return -1
   
def fibonacciSearch(roll, x):
   n = len(roll)
   offset = -1
   fn_2 = 0
   fn_1 = 1
   fn = fn_1 + fn_2
   while fn < n:
       temp = fn
       fn_2 = fn_1
       fn_1 = temp
       fn = fn_2 + fn_1
   while fn > 1:
       index = min((offset + fn_2), n - 1)
       if roll[index] == x:
         return index
       elif roll[index] > x:
         fn = fn_2
         fn_1 = fn_1 - fn_2
         fn_2 = fn_2 - fn_1 # Moving two down
       elif roll[index] < x:
         fn = fn_1
         fn_1 = fn_2
         fn_2 = fn - fn_1
         offset = index
   return -1
   
   
roll_no = [2, 3, 4, 5, 6, 7, 8, 9]
n = len(roll_no)
key = int(input("Enter the Roll Number to check whether he or she attended the training program"))
while True:
   print("1.Binary Search\n2.Fibonacci Search\n3.Exit")
   ch = int(input("Enter the choice"))
   if ch == 1:
      result = binarysearch(roll_no, key)
      if result == -1:
         print("Roll no : ", key, " not attended training program")
      else:
         print("Roll no : ", key, " attended training program")
   elif ch == 2:
      result = fibonacciSearch(roll_no, key)
      if result == -1:
         print("Roll no : ", key, " not attended training program")
      else:
         print("Roll no : ", key, " attended training program")
   elif ch == 3:
      break
   else:
      print("Enter valid choice")

5

def selectionSort(array, n):
    for i in range(n):
        minIndex = i
        for j in range(i+1, n):
            if array[minIndex] > array[j]:
                minIndex = j
        temp = array[i]
        array[i] = array[minIndex]
        array[minIndex] = temp
    print("Sorted array after applying Selection Sort")
    for i in array:
        print(i, end=" ")

def bubbleSort(array, n):
    for i in range(n):
        for j in range(i+1, n):
            if array[i] > array[j]:
                temp = array[i]
                array[i] = array[j]
                array[j] = temp
    print("\nSorted array after applying Bubble Sort")
    for i in array:
        print(i, end=" ")
    print("\nTop five scores are")
    for i in range(len(array)-1, len(array)-6, -1):
        print(array[i], end=" ")

print("Enter the number of students in the class")
n = int(input())
array = []
for i in range(n):
    print(f"Enter the percentage of student[{i}]")
    percentage = float(input())
    array.append(percentage)

print("You have entered the following percentages:")
for i in array:
    print(i, end=" ")

while True:
    print()
    print("---------Main Menu---------")
    print("1. Selection Sort")
    print("2. Bubble Sort")
    print("3. Exit")
    ch = int(input("Enter your choice: "))
    if ch == 1:
        selectionSort(array, n)
    elif ch == 2:
        bubbleSort(array, n)
    elif ch == 3:
        break
    else:
        print("Enter a valid choice")

6

def quicksort(alist, start, end):
    if end - start > 1:
        p = partition(alist, start, end)
        quicksort(alist, start, p)
        quicksort(alist, p + 1, end)

def partition(alist, start, end):
    pivot = alist[start]
    i = start + 1
    j = end - 1
    while True:
        while i <= j and alist[i] <= pivot:
            i = i + 1
        while i <= j and alist[j] >= pivot:
            j = j - 1
        if i <= j:
            temp = alist[j]
            alist[j] = alist[i]
            alist[i] = temp
        else:
            temp = alist[j]
            alist[j] = alist[start]
            alist[start] = temp
            return j

def display_top_five(array):
    print("Top Five Percentages are:")
    if len(array) < 5:
        start, end = len(array) - 1, -1
    else:
        start, end = len(array) - 1, len(array) - 6
    for i in range(start, end, -1):
        print(array[i])

print("Enter the number of students in class")
n = int(input())
array = []

for i in range(n):
    print(f"Enter the percentage of student[{i}]")
    percentage = float(input())
    array.append(percentage)

print("You have entered the following percentages:")
for i in array:
    print(i, end=" ")

quicksort(array, 0, n)
print('\nSorted list: ', end='')
print(array)
display_top_five(array)

7

#include <iostream>
using namespace std;
struct node
{
    int prn;
    string name;
    node *next;
};
class linked_list
{
    node
        *s = NULL,
        *head = NULL, *temp = NULL, *head1 = NULL, *head2 = NULL, *temp1 = NULL, *temp2 = NULL;
    int ct;
    char a[20];

public:
    linked_list()
    {
        head = NULL;
    }
    node *create();
    void insertm();
    void insertp();
    void inserts();
    void delp();
    void delm();
    void dels();
    void display();
    void count();
    void concat();
};
node *linked_list::create()
{
    node *p = new node;
    cout << "Enter name of student: ";
    cin >> p->name;
    cout << "\nEnter prn no. of student:";
    cin >> p->prn;
    p->next = NULL;
    return p;
}
void linked_list::insertp()
{
    node *p = create();
    if (head == NULL)
    {
        head = p;
    }
    else
    {
        temp = head;
        head = p;
        head->next = temp->next;
    }
}
void linked_list::insertm()
{
    node *p = create();
    if (head == NULL)
    {
        head = p;
    }
    else
    {
        temp = head;
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        temp->next = p;
    }
}
void linked_list::inserts()
{
    node *p = create();
    if (head == NULL)
    {
        head = p;
    }
    else
    {
        temp = head;
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        temp->next = p;
    }
}
void linked_list::delp()
{
    temp = head;
    head = head->next;
    delete (temp);
}
void linked_list::delm()
{
    int prnno, f = 0;
    cout << "\n enter the prn no. of student whose data you want to delete";
    cin >> prnno;
    temp = head;
    while (temp->next != NULL)
    {
        if (temp->prn == prnno)
        {
            s->next = temp->next;
            delete (temp);
            f = 1;
        }
        s = temp;
        temp = temp->next;
    }
    if (f == 0)
    {
        cout << "\n sorry memeber not deleted ";
    }
}
void linked_list::dels()
{
    temp = head;
    while (temp->next != NULL)
    {
        s = temp;
        temp = temp->next;
    }
    s->next = NULL;
    delete (temp);
}
void linked_list::display()
{
    temp = head;
    while (temp != NULL)
    {
        cout << temp->prn << endl;
        cout << temp->name << endl;
        temp = temp->next;
    }
}
void linked_list::count()
{
    ct = 0;
    if (head == NULL)
    {
        cout << "List is empty" << endl;
    }
    else
    {
        temp = head;
        while (temp != NULL)
        {
            ct++;
            temp = temp->next;
        }
    }
    cout << "Total no of members :" << ct << endl;
}
void linked_list::concat()
{
    int k, j, i;
    cout << "enter no. of members in list1: ";
    cin >> k;
    head = NULL;
    for (i = 0; i < k; i++)
    {
        insertm();
        head1 = head;
    }
    head = NULL;
    cout << "enter no. of members in list2: ";
    cin >> j;
    for (i = 0; i < j; i++)
    {
        insertm();
        head2 = head;
    }
    head = NULL;
    temp1 = head1;
    while (temp1->next != NULL)
    {
        temp1 = temp1->next;
    }
    temp1->next = head2;
    temp2 = head1;
    cout << " prn Name \n";
    while (temp2->next != NULL)
    {
        cout << "\n " << temp2->prn << " " << temp2->name << "\n";
        temp2 = temp2->next;
    }
    cout << "\n " << temp2->prn << " " << temp2->name << "\n";
}
int main()
{
    linked_list a;
    int i;
    char ch;
    do
    {
        cout << "\n 1. To insert president ";
        cout << "\n 2. To insert member ";
        cout << "\n 3. To insert secretary ";
        cout << "\n 4. To delete president ";
        cout << "\n 5. To delete member ";
        cout << "\n 6. To delete secretary ";
        cout << "\n 7. To display data ";
        cout << "\n 8. Count of members";
        cout << "\n 9.To concatenate two strings ";
        cout << "\n Enter the choice: ";
        cin >> i;
        switch (i)
        {
        case 1:
            a.insertp();
            break;
        case 2:
            a.insertm();
            break;
        case 3:
            a.inserts();
            break;
        case 4:
            a.delp();
            break;
        case 5:
            a.delm();
            break;
        case 6:
            a.dels();
            break;
        case 7:
            a.display();
            break;
        case 8:
            a.count();
            break;
        case 9:
            a.concat();
            break;
        default:
            cout << "\n unknown choice";
        }
        cout << "\n do you want to continue enter y/Y: ";
        cin >> ch;
    } while (ch == 'y' || ch == 'Y');
    return 0;
}

8

#include <iostream>
using namespace std;
struct node
{
    int rollno;
    node *next;
};
class information
{
public:
    node
        *head1 = NULL,
        *temp1 = NULL, *head2 = NULL, *temp2 = NULL, *head = NULL, *temp = NULL, *h1 = NULL, *head3 = NULL, *temp3 = NULL, *p = NULL;
    int roll_no, i, f, bs_student, totalstudent, v_student;
    node *create();
    void insert();
    void allstud();
    void vanila();
    void butterscotch();
    void union_vanilla_butterscotch();
    void intersection_vanila_butterscotch();
    void notice();
    void onlyvanila();
    void onlybutterscotch();
    void display();
};
node *information::create()
{
    node *p = new node;
    cout << "Enter student roll no";
    cin >> roll_no;
    p->rollno = roll_no;
    p->next = NULL;
    return p;
}
void information::insert()
{
    node *p = create();
    if (head == NULL)
    {
        head = p;
    }
    else
    {
        temp = head;
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        temp->next =
            p;
    }
}
void information::display()
{
    temp = head;
    cout << "\nRoll no of student is : ";
    while (temp != NULL)
    {
        cout << " " << temp->rollno;
        temp = temp->next;
    }
}
void information::allstud()
{
    cout << "Enter total no of student in class:";
    cin >> totalstudent;
    for (i = 0; i < totalstudent; i++)
    {
        insert();
        h1 = head;
    }
    cout << "\n------------------------------------------------";
    display();
    head = NULL;
}
void information::vanila()
{
    cout << "\n------------------------------------------------";
    cout << "\nEnter total no of student who likes only vanila:";
    cin >> v_student;
    for (i = 0; i < v_student; i++)
    {
        insert();
        head1 = head;
    }
    cout << "\n------------------------------------------------";
    display();
    head = NULL;
}
void information::butterscotch()
{
    cout << "\n------------------------------------------------";
    cout << "\nEnter total no of student who likes only butterscotch:";
    cin >> bs_student;
    for (i = 0; i < bs_student; i++)
    {
        insert();
        head2 = head;
    }
    cout << "\n------------------------------------------------";
    display();
    head = NULL;
}
void information::union_vanilla_butterscotch()
{
    cout << "\n------------------------------------------------";
    cout << "\nstudents who like vanila or butterscotch: ";
    temp1 = head1;
    while (temp1 != NULL)
    {
        node *p = new node;
        p->rollno = temp1->rollno;
        p->next = NULL;
        if (head3 == NULL)
        {
            head3 = p;
        }
        else
        {
            temp3 = head3;
            while (temp3->next != NULL)
            {
                temp3 = temp3->next;
            }
            temp3->next = p;
        }
        temp1 = temp1->next;
    }
    temp2 = head2;
    while (temp2 != NULL)
    {
        f = 0;
        temp1 = head1;
        while (temp1 != NULL)
        {
            if (temp2->rollno == temp1->rollno)
            {
                f = 1;
                break;
            }
            temp1 = temp1->next;
        }
        if (f == 0)
        {
            node *p = new node;
            p->rollno = temp2->rollno;
            p->next = NULL;
            if (head3 == NULL)
            {
                head3 = p;
            }
            else
            {
                temp3 = head3;
                while (temp3->next != NULL)
                {
                    temp3 = temp3->next;
                }
                temp3->next = p;
            }
        }
        temp2 = temp2->next;
    }
    temp3 = head3;
    while (temp3 != NULL)
    {
        cout << " " << temp3->rollno;
        temp3 = temp3->next;
    }
    cout << "\n------------------------------------------------";
}
void information::onlyvanila()
{
    cout << "\nRoll no of student who like only vanila:";
    temp1 = head1;
    while (temp1 != NULL)
    {
        temp2 = head2;
        f = 0;
        while (temp2 != NULL)
        {
            if (temp2->rollno == temp1->rollno)
            {
                f = 1;
                break;
            }
            temp2 = temp2->next;
        }
        if (f == 0)
        {
            cout << " " << temp1->rollno;
        }
        temp1 = temp1->next;
    }
    cout << "\n------------------------------------------------";
}
void information::onlybutterscotch()
{
    cout << "\nRoll no of student who like only butterscotch:";
    temp2 = head2;
    while (temp2 != NULL)
    {
        temp1 = head1;
        f = 0;
        while (temp1 != NULL)
        {
            if (temp1->rollno == temp2->rollno)
            {
                f = 1;
            }
            temp1 = temp1->next;
        }
        if (f == 0)
        {
            cout << " " << temp2->rollno;
        }
        temp2 = temp2->next;
    }
    cout << "\n------------------------------------------------";
}
void information::intersection_vanila_butterscotch()
{
    cout << "\nstudents who like both vanila and butterscotch: ";
    temp1 = head1;
    while (temp1 != NULL)
    {
        temp2 = head2;
        while (temp2 != NULL)
        {
            if (temp1->rollno == temp2->rollno)
            {
                cout << " " << temp1->rollno;
            }
            temp2 = temp2->next;
        }
        temp1 = temp1->next;
    }
    cout << "\n------------------------------------------------";
}
void information ::notice()
{
    cout << "\nstudents who like neither vanila nor butterscotch\n";
    temp = h1;
    while (temp != NULL)
    {
        temp3 = head3;
        f = 0;
        while (temp3 != NULL)
        {
            if (temp3->rollno == temp->rollno)
            {
                f = 1;
            }
            temp3 = temp3->next;
        }
        if (f == 0)
        {
            cout << " " << temp->rollno;
        }
        temp = temp->next;
    }
}
int main()
{
    information info;
    info.allstud();
    info.vanila();
    info.butterscotch();
    info.union_vanilla_butterscotch();
    info.onlyvanila();
    info.onlybutterscotch();
    info.intersection_vanila_butterscotch();
    info.notice();
    return 0;
}

9

#include <iostream>
#include <string.h>
#define max 50
using namespace std;
class STACK
{
private:
    char stackstring[max];
    int top;

public:
    STACK()
    {
        top = -1;
    }
    void push(char);
    void reverse();
    void convert(char[]);
    void palindrome();
};
void STACK::push(char c)
{
    top++;
    stackstring[top] = c;
    stackstring[top + 1] = '\0';
    cout << endl
         << c << " is pushed on stack ...";
}
void STACK::reverse()
{
    cout << "\nReverse String is :";
    for (int i = strlen(stackstring) - 1; i >= 0; i--)
        cout << stackstring[i];
    cout << "\n"
         << endl;
}
void STACK::convert(char str[])
{
    int j, k, len = strlen(str);
    for (j = 0, k = 0; j < len; j++)
    {
        if (isalpha(str[j]))
        {
            str[k] = tolower(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] = stackstring[i];
    }
    str[j] = '\0';
    if (strcmp(str, stackstring) == 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 is it palindrome or not : \n\n";
    cin.getline(str, 50);
    stack.convert(str);
    while (str[i] != '\0')
    {
        stack.push(str[i]);
        i++;
    }
    stack.reverse();
    stack.palindrome();
}

10

#include <iostream>
#include <string.h>
using namespace std;
class paranthesis
{
    char st[20];
    int top;

public:
    void push(char a);
    void pop();
    void input();
};
void paranthesis::push(char a)
{
    top++;
    st[top] = a;
}
void paranthesis::pop()
{
    top--;
}
void paranthesis::input()
{
    char ch[20];
    int i = 0;
    top = -1;
    cout << "\nenter the expression";
    cin >> ch;
    while (i < strlen(ch))
    {
        if ((ch[i] == '{') || (ch[i] == '[') || (ch[i] == '('))
        {
            push(ch[i]);
        }
        if (ch[i] == '}')
        {
            if (st[top] == '{')
                pop();
            else
            {
                cout << "\n matching opening brace '{' is not found";
            }
        }
        if (ch[i] == ']')
        {
            if (st[top] == '[')
                pop();
            else
            {
                cout << "\n matching brace '[' is not found";
            }
        }
        if (ch[i] == ')')
        {
            if (st[top] == '(')
                pop();
            else
            {
                cout << "\n matching opening brace '(' is not found";
            }
        }
        i++;
    }
    if (top == -1)
    {
        cout << "\nstack is empty";
        cout << "\n EXPRESSION IS WELL PARENTHESIZED";
    }
    else
    {
        while (top != -1)
        {
            if (st[top] == '[')
            {
                pop();
                cout << "\n matching closing brace ']' is not found";
            }
            if (st[top] == '{')
            {
                pop();
                cout << "\n matching closing brace '}' is not found";
            }
            if (st[top] == '(')
            {
                pop();
                cout << "\n matching closing brace ')' is not found";
            }
        }
        cout << "\n EXPRESSION IS NOT WELL PARENTHESIZED";
    }
}
int main()
{
    paranthesis p;
    p.input();
    return 0;
}

11


#include <iostream>
using namespace std;
#define max 5
class queue
{
private:
    int front, rear;
    int data[max];

public:
    queue()
    {
        front = -1;
        rear = -1;
    }
    void enqueue(int x);
    void dequeue();
    void display();
};
void queue::enqueue(int x)
{
    if (rear == max - 1)
    {
        cout << "Queue is overflow";
    }
    else if (front == -1 && rear == -1)
    {
        front = 0;
        rear = 0;
        data[rear] = x;
    }
    else
    {
        rear++;
        data[rear] = x;
    }
}
void queue::dequeue()
{
    if (front == -1 && rear == -1)
    {
        cout << "Queue is empty";
    }
    else if (front == rear)
    {
        front = rear = -1;
    }
    else
    {
        cout << "Deleted elemnt is :" << data[front];
        front++;
    }
}
void queue::display()
{
    int i;
    if (front == -1 && rear == -1)
    {
        cout << "Queue is empty";
    }
    else
    {
        for (i = front; i <= rear; i++)
        {
            cout << " " << data[i];
        }
    }
}
int main()
{
    queue q;
    int ch, i, data;
    do
    {
        cout << "\n1.To insert element in queue.";
        cout << "\n2.To delete element from queue.";
        cout << "\n3.To display element of queue.";
        cout << "\n4.To exit";
        cout << "\nEnter your choice";
        cin >> ch;
        switch (ch)
        {
        case 1:
            cout << "Enter the elemtnt";
            cin >> data;
            q.enqueue(data);
            break;
        case 2:
            q.dequeue();
            break;
        case 3:
            q.display();
            break;
        default:
            cout << "Invalid Choice";
            break;
        }
    } while (i != 4);
}

12

#include <iostream>
using namespace std;
#define max 5
class pizza
{
private:
    int pizzaparlar[max];
    int front, rear;

public:
    pizza()
    {
        front = rear = -1;
    }
    void acceptOrder(int); // enqueue operation
    void makePayment(int); // dequeue operation
    void display();
};
void pizza::acceptOrder(int item)
{
    if ((rear + 1) % max == front)
    {
        cout << "Can't take order.Please waite for some time";
    }
    else if (front == -1 && rear == -1)
    {
        front = rear = 0;
        pizzaparlar[rear] = item;
    }
    else
    {
        rear = (rear + 1) % max;
        pizzaparlar[rear] = item;
    }
}
void pizza::makePayment(int n)
{
    if (front == -1 && rear == -1)
    {
        cout << "No Pizza order yet placed";
    }
    else if (front == rear)
    {
        front = rear = -1;
    }
    else
    {
        cout << "Payment received for order no :" << front;
        front = (front + 1) % max;
    }
}
void pizza::display()
{
    int i = front;
    cout << "Pizza oreders are :";
    while (i != rear)
    {
        cout << pizzaparlar[i];
        i = (i + 1) % max;
    }
    cout << pizzaparlar[i];
}
int main()
{
    pizza p;
    int ch, k, n;
    do
    {
        cout << "\n**********WELCOME TO PIZZA PARLOR************";
        cout << "\n 1.Place pizza order";
        cout << "\n 2.Make payment";
        cout << "\n 3.Pending orders";
        cout << "\n 4.To exit";
        cout << "\n Enter your choice";
        cin >> ch;
        switch (ch)
        {
        case 1:
            cout << "\nWhich Pizza do u like most....\n";
            cout << "\n1.Veg Soya Pizza\n2.Veg butter Pizza\n3.Egg_Pizza";
            cout << "\nPlease enter u r order: ";
            cin >> k;
            p.acceptOrder(k);
            break;
        case 2:
            p.makePayment(n);
            break;
        case 3:
            p.display();
            break;
        default:
            cout << "Invalid choice";
            break;
        }
    } while (ch != 4);
    return 0;
}

13

#include <iostream>

// #include

// #include

using namespace std;

#define SIZE 5

// ERROR HANDLINH NOT DOne

// program is not working correct.

//

class dequeue

{

    int a[10], front, rear, count;

public:
    dequeue();

    void add_at_beg(int);

    void add_at_end(int);

    void delete_fr_front();

    void delete_fr_rear();

    void display();
};

dequeue::dequeue()

{

    front = -1;

    rear = -1;

    count = 0;
}

void dequeue::add_at_beg(int item)

{

    int i;

    if (front == -1)

    {

        front++;

        rear++;

        a[rear] = item;

        count++;
    }

    else if (rear >= SIZE - 1)

    {

        cout << "\nInsertion is not possible,overflow!!!!";
    }

    else

    {

        for (i = count; i >= 0; i--)

        {

            a[i] = a[i - 1];
        }

        a[i] = item;

        count++;

        rear++;
    }
}

void dequeue::add_at_end(int item)

{

    if (front == -1)

    {

        front++;

        rear++;

        a[rear] = item;

        count++;
    }

    else if (rear >= SIZE - 1)

    {

        cout << "\nInsertion is not possible,overflow!!!";

        return;
    }

    else

    {

        a[++rear] = item;
    }
}

void dequeue::display()

{

    for (int i = front; i <= rear; i++)

    {

        cout << a[i] << " ";
    }
}

void dequeue::delete_fr_front()

{

    if (front == -1)

    {

        cout << "Deletion is not possible:: Dequeue is empty";

        return;
    }

    else

    {

        if (front == rear)

        {

            front = rear = -1;

            return;
        }

        cout << "The deleted element is " << a[front];

        front = front + 1;
    }
}

void dequeue::delete_fr_rear()

{

    if (front == -1)

    {

        cout << "Deletion is not possible:Dequeue is empty";

        return;
    }

    else

    {

        if (front == rear)

        {

            front = rear = -1;
        }

        cout << "The deleted element is " << a[rear];

        rear = rear - 1;
    }
}

int main()

{

    int c, item;

    dequeue d1;

    do

    {

        cout << "\n\n****DEQUEUE OPERATION****\n";

        cout << "\n1-Insert at beginning";

        cout << "\n2-Insert at end";

        cout << "\n3_Display";

        cout << "\n4_Deletion from front";

        cout << "\n5-Deletion from rear";

        cout << "\n6_Exit";

        cout << "\nEnter your choice<1-4>:";

        cin >> c;

        switch (c)

        {

        case 1:

            cout << "Enter the element to be inserted:";

            cin >> item;

            d1.add_at_beg(item);

            break;

        case 2:

            cout << "Enter the element to be inserted:";

            cin >> item;

            d1.add_at_end(item);

            break;

        case 3:

            d1.display();

            break;

        case 4:

            d1.delete_fr_front();

            break;

        case 5:

            d1.delete_fr_rear();

            break;

        case 6:

            exit(1);

            break;

        default:

            cout << "Invalid choice";

            break;
        }

    } while (c != 7);

    return 0;
}







                                  -------    Thank you  ---------

Comments

Popular posts from this blog

CG And OOPs Practicals for 2023-24 For Computer Science Student By Karan Rathod

25 Scientifically Proven Tips for More Effective Studying by KKR