cpp code

Pasted Dec 2, 2026, 10:31 AM1 file68 linesexpires in 4 days

Scan to open on your phone

Point a camera at the QR code - it opens this exact paste, no app needed.

https://codepastes.com/thaku07
cpp code
Linear Data Structures in C++
12  short, standalone programs for the given assignment questions. Each program has its own main() function, so compile and run one program at a time. Array-based queues/stacks use a fixed size of 5; enter values within that capacity.
1.	Binary Search – Book ID
#include <iostream> using namespace std;
int main() {     int a[100], n, key, low, high, mid, count = 0;     cout << "Enter number of books: ";     cin >> n;     cout << "Enter sorted book IDs: ";     for (int i = 0; i < n; i++) cin >> a[i];
    cout << "Enter book ID to search: ";     cin >> key;     low = 0; high = n - 1;
    while (low <= high) {         mid = (low + high) / 2;         count++;         if (a[mid] == key) {             cout << "Book found at position " << mid + 1 << "\\n";             cout << "Comparisons: " << count;             return 0;         }
        if (key < a[mid]) high = mid - 1;         else low = mid + 1;     }     cout << "Book not found\\nComparisons: " << count;     return 0; }
2.	Bubble Sort – Student Marks
#include <iostream> using namespace std;
int main() {     int a[100], n, temp;     cout << "Enter number of students: ";     cin >> n;     cout << "Enter marks: ";     for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n - 1; i++)         for (int j = 0; j < n - i - 1; j++)             if (a[j] > a[j + 1]) {                 temp = a[j];                 a[j] = a[j + 1];                 a[j + 1] = temp;             }
    cout << "Marks in ascending order: ";     for (int i = 0; i < n; i++) cout << a[i] << " ";     return 0; }
3.	Linear Search – Product ID
#include <iostream> using namespace std;
int main() {     int a[100], n, key, pos = -1;     cout << "Enter number of products: ";     cin >> n;
    cout << "Enter product IDs: ";     for (int i = 0; i < n; i++) cin >> a[i];
    cout << "Enter product ID to search: ";     cin >> key;
    for (int i = 0; i < n; i++) {         if (a[i] == key) {             pos = i;             break;         }     }
    if (pos == -1) cout << "Product not found";     else cout << "Product found at position " << pos + 1;     return 0; } 
4.	Queue Using Array – Railway Waiting List
#include <iostream> using namespace std;
int main() {     int q[5], front = 0, rear = -1, choice, id;     do {         cout << "\\n1.Enqueue  2.Dequeue  3.Display  0.Exit: ";         cin >> choice;         if (choice == 1) {             if (rear == 4) cout << "Queue is full";             else {                 cout << "Enter passenger ID: ";                 cin >> id;                 q[++rear] = id;             }         } else if (choice == 2) {             if (front > rear) cout << "Queue is empty";             else cout << "Passenger served: " << q[front++];         } else if (choice == 3) {             if (front > rear) cout << "Queue is empty";             else for (int i = front; i <= rear; i++) cout << q[i] << " ";         }     } while (choice != 0);     return 0; }
5.	Queue – Bank Customer Service
#include <iostream> using namespace std;
int main() {     int q[5], front = 0, rear = -1, choice, customer;     do {         cout << "\\n1.Add customer  2.Serve customer  3.Display  0.Exit: ";         cin >> choice;         if (choice == 1) {             if (rear == 4) cout << "Queue is full";             else {                 cout << "Enter customer number: ";                 cin >> customer;                 q[++rear] = customer;             }         } else if (choice == 2) {             if (front > rear) cout << "No customers waiting";             else cout << "Serving customer " << q[front++];         } else if (choice == 3) {             if (front > rear) cout << "No customers waiting";             else for (int i = front; i <= rear; i++) cout << q[i] << " ";         }     } while (choice != 0);     return 0; }
6.	Stack Using Array – Undo Operations
#include <iostream> #include <string> using namespace std;
int main() {     string s[5], action;     int top = -1, choice;     do {         cout << "\\n1.Push action  2.Undo  3.Display  0.Exit: ";         cin >> choice;         if (choice == 1) {             if (top == 4) cout << "Stack is full";             else {
                cout << "Enter action (one word): ";                 cin >> action;                 s[++top] = action;             }         } else if (choice == 2) {
            if (top == -1) cout << "Nothing to undo";             else cout << "Undone: " << s[top--];         } else if (choice == 3) {             if (top == -1) cout << "No actions";             else for (int i = top; i >= 0; i--) cout << s[i] << " ";         }     } while (choice != 0);     return 0;
}
7.	Balanced Parentheses Using Stack
#include <iostream> #include <string> using namespace std;
int main() {     string exp;     char st[100];     int top = -1;     bool balanced = true;
    cout << "Enter expression: ";     getline(cin, exp);     for (int i = 0; i < exp.length(); i++) {         char c = exp[i];         if (c == '(' || c == '[' || c == '{') st[++top] = c;         else if (c == ')' || c == ']' || c == '}') {             if (top == -1) { balanced = false; break; }             char x = st[top--];             if ((c == ')' && x != '(') ||                 (c == ']' && x != '[') ||                 (c == '}' && x != '{')) {                 balanced = false;                 break;             }
        }     }     if (top != -1) balanced = false;     if (balanced) cout << "Parentheses are balanced";     else cout << "Parentheses are not balanced";     return 0; }
8.	Singly Linked List – Music Playlist
#include <iostream> #include <string> using namespace std;
struct Node {     string song;     Node* next; } ;
int main() {     Node *head = NULL, *tail = NULL, *temp;     int choice;     string name;     do {         cout << "\\n1.Insert song  2.Delete first song  3.Display  0.Exit: ";         cin >> choice;         if (choice == 1) {
            cout << "Enter song name (one word): ";             cin >> name;             temp = new Node{name, NULL};             if (head == NULL) head = tail = temp;             else { tail->next = temp; tail = temp; }         } else if (choice == 2) {             if (head == NULL) cout << "Playlist is empty";             else {                 temp = head;                 head = head->next;                 if (head == NULL) tail = NULL;                 cout << "Deleted: " << temp->song;                 delete temp;             }         } else if (choice == 3) {
            if (head == NULL) cout << "Playlist is empty";             else for (temp = head; temp != NULL; temp = temp->next)                 cout << temp->song << " -> ";         }     } while (choice != 0);     return 0; }
9.	Polynomial Addition Using Linked Lists
#include <iostream> using namespace std;
struct Node {     int coeff, power;     Node* next; } ;
void addTerm(Node*& head, int c, int p) {
    Node* n = new Node{c, p, NULL};     if (head == NULL) head = n;     else {         Node* t = head;         while (t->next != NULL) t = t->next;         t->next = n;     } }
void readPoly(Node*& head) {     int n, c, p;     cout << "Enter number of terms: ";     cin >> n;     for (int i = 0; i < n; i++) {         cout << "Enter coefficient and power: ";         cin >> c >> p;         addTerm(head, c, p);     } }
int main() {     Node *a = NULL, *b = NULL, *sum = NULL, *x, *y;     cout << "First polynomial\\n"; readPoly(a);     cout << "Second polynomial\\n"; readPoly(b);
    for (x = a; x != NULL; x = x->next) {         int c = x->coeff;         for (y = b; y != NULL; y = y->next)             if (y->power == x->power) c += y->coeff;         addTerm(sum, c, x->power);     }     for (y = b; y != NULL; y = y->next) {         bool found = false;         for (x = a; x != NULL; x = x->next)             if (x->power == y->power) found = true;         if (!found) addTerm(sum, y->coeff, y->power);     }
    cout << "Sum: ";     for (x = sum; x != NULL; x = x->next)         cout << x->coeff << "x^" << x->power << " ";     return 0;
} 
10.	Priority Queue – Printer Jobs
#include <iostream> using namespace std;
int main() {     int job[5], priority[5], n = 0, choice, id, p, best;     do {         cout << "\\n1.Add job  2.Print job  3.Display  0.Exit: ";         cin >> choice;         if (choice == 1) {             if (n == 5) cout << "Queue is full";             else {                 cout << "Enter job ID and priority (larger = urgent): ";                 cin >> id >> p;                 job[n] = id; priority[n] = p; n++;             }         } else if (choice == 2) {             if (n == 0) cout << "No print jobs";             else {                 best = 0;                 for (int i = 1; i < n; i++)                     if (priority[i] > priority[best]) best = i;                 cout << "Printing job " << job[best];                 for (int i = best; i < n - 1; i++) {                     job[i] = job[i + 1];                     priority[i] = priority[i + 1];                 }                 n--;             }         } else if (choice == 3) {             for (int i = 0; i < n; i++)
                cout << "Job " << job[i] << " Priority " << priority[i] << "\\n";         }     } while (choice != 0);     return 0; }
11.	Priority Queue – Hospital Emergency Room
#include <iostream> #include <string> using namespace std;
int main() {     string patient[5];     int priority[5], n = 0, choice, best;     do {         cout << "\\n1.Add patient  2.Serve patient  3.Display  0.Exit: ";         cin >> choice;         if (choice == 1) {             if (n == 5) cout << "Queue is full";             else {                 cout << "Enter patient name (one word) and priority (larger = critical): ";                 cin >> patient[n] >> priority[n];                 n++;             }         } else if (choice == 2) {             if (n == 0) cout << "No patients waiting";             else {                 best = 0;                 for (int i = 1; i < n; i++)
                    if (priority[i] > priority[best]) best = i;                 cout << "Treating patient: " << patient[best];                 for (int i = best; i < n - 1; i++) {                     patient[i] = patient[i + 1];                     priority[i] = priority[i + 1];                 }                 n--;             }         } else if (choice == 3) {             for (int i = 0; i < n; i++)                 cout << patient[i] << " Priority: " << priority[i] << "\\n";         }     } while (choice != 0);     return 0; }
12.	Circular Queue – Customer Support Calls
#include <iostream> using namespace std;
int main() {     int q[5], front = -1, rear = -1, choice, call;     do {         cout << "\\n1.Add call  2.Handle call  3.Display  0.Exit: ";         cin >> choice;         if (choice == 1) {             if ((rear + 1) % 5 == front) cout << "Queue is full";             else {                 cout << "Enter call/request number: ";                 cin >> call;                 if (front == -1) front = 0;                 rear = (rear + 1) % 5;                 q[rear] = call;             }         } else if (choice == 2) {             if (front == -1) cout << "No pending calls";             else {                 cout << "Handling call " << q[front];                 if (front == rear) front = rear = -1;                 else front = (front + 1) % 5;             }         } else if (choice == 3) {             if (front == -1) cout << "No pending calls";             else {                 int i = front;                 while (true) {                     cout << q[i] << " ";                     if (i == rear) break;                     i = (i + 1) % 5;                 }
            }
        }     } while (choice != 0);     return 0;
}

11,760 chars · codepastes.com