[go: up one dir, main page]

0% found this document useful (0 votes)
8 views22 pages

C - Code of Practical

Uploaded by

shravanisd2854
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views22 pages

C - Code of Practical

Uploaded by

shravanisd2854
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Practical NO 7

INPUT

#include <iostream>
#include <string>
using namespace std;

struct Node {
string PRN; // PRN number
string name; // Name of the student
Node* next; // Pointer to the next node
};

// Function to add a member to the list


void addMember(Node*& head, const string& PRN, const string& name) {
Node* newNode = new Node{PRN, name, nullptr};
if (!head) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to delete a member from the list using PRN


void deleteMember(Node*& head, const string& PRN) {
if (!head) {
cout << "List is empty!" << endl;
return;
}
if (head->PRN == PRN) {
Node* temp = head;
head = head->next;
delete temp;
cout << "Member with PRN " << PRN << " deleted." << endl;
return;
}
Node* current = head;
while (current->next && current->next->PRN != PRN) {
current = current->next;
}
if (current->next) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
cout << "Member with PRN " << PRN << " deleted." << endl;
} else {
cout << "Member with PRN " << PRN << " not found." << endl;
}
}

// Function to display all members


void displayMembers(Node* head) {
if (!head) {
cout << "No members to display!" << endl;
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << "PRN: " << temp->PRN << ", Name: " << temp->name << endl;
temp = temp->next;
}
}

// Function to compute the total number of members


int totalMembers(Node* head) {
int count = 0;
Node* temp = head;
while (temp != nullptr) {
count++;
temp = temp->next;
}
return count;
}

// Function to concatenate two lists (for two divisions)


Node* concatenateLists(Node* head1, Node* head2) {
if (!head1) return head2;
if (!head2) return head1;

Node* temp = head1;


while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = head2;
return head1;
}

int main() {
Node* division1 = nullptr;
Node* division2 = nullptr;

// Adding some members to division 1


addMember(division1, "PRN001", "Alice");
addMember(division1, "PRN002", "Bob");
addMember(division1, "PRN003", "Charlie");

// Adding some members to division 2


addMember(division2, "PRN004", "David");
addMember(division2, "PRN005", "Eve");

cout << "Members of Division 1:" << endl;


displayMembers(division1);

cout << "\nMembers of Division 2:" << endl;


displayMembers(division2);

// Concatenating two divisions


Node* concatenatedList = concatenateLists(division1, division2);
cout << "\nMembers after concatenating Division 1 and 2:" << endl;
displayMembers(concatenatedList);

// Total number of members


cout << "\nTotal number of members: " << totalMembers(concatenatedList) << endl;

// Deleting a member
deleteMember(concatenatedList, "PRN003");
cout << "\nMembers after deleting PRN003:" << endl;
displayMembers(concatenatedList);

return 0;
}

OUTPUT

/tmp/zpW0Gwklw1.o
Members of Division 1:
PRN: PRN001, Name: Alice
PRN: PRN002, Name: Bob
PRN: PRN003, Name: Charlie

Members of Division 2:
PRN: PRN004, Name: David
PRN: PRN005, Name: Eve

Members after concatenating Division 1 and 2:


PRN: PRN001, Name: Alice
PRN: PRN002, Name: Bob
PRN: PRN003, Name: Charlie
PRN: PRN004, Name: David
PRN: PRN005, Name: Eve

Total number of members: 5


Member with PRN PRN003 deleted.

Members after deleting PRN003:


PRN: PRN001, Name: Alice
PRN: PRN002, Name: Bob
PRN: PRN004, Name: David
PRN: PRN005, Name: Eve

=== Code Execution Successful ===

PRACTICAL NO 8
INPUT

#include <iostream>
using namespace std;

struct Node {
int data; // Stores binary digit (0 or 1)
Node* next;
Node* prev;
};

// Function to add a binary digit to the end of the doubly linked list
void addBinaryDigit(Node*& head, int data) {
Node* newNode = new Node{data, nullptr, nullptr};
if (!head) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}

// Function to display the binary number


void displayBinary(Node* head) {
if (!head) {
cout << "No binary number to display!" << endl;
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data;
temp = temp->next;
}
cout << endl;
}

// Function to compute the 1's complement of a binary number


void onesComplement(Node* head) {
Node* temp = head;
while (temp != nullptr) {
temp->data = (temp->data == 0) ? 1 : 0;
temp = temp->next;
}
}

// Function to compute the 2's complement of a binary number


void twosComplement(Node* head) {
onesComplement(head); // First, calculate 1's complement
Node* temp = head;

// Move to the last node (rightmost bit)


while (temp->next != nullptr) {
temp = temp->next;
}

// Add 1 to the binary number


int carry = 1;
while (temp != nullptr && carry) {
int sum = temp->data + carry;
temp->data = sum % 2;
carry = sum / 2;
temp = temp->prev;
}
}

// Function to add two binary numbers stored in doubly linked lists


Node* addBinaryNumbers(Node* head1, Node* head2) {
Node* result = nullptr;
Node *temp1 = head1, *temp2 = head2;

// Move to the last nodes of both lists (rightmost bits)


while (temp1 && temp1->next != nullptr) temp1 = temp1->next;
while (temp2 && temp2->next != nullptr) temp2 = temp2->next;

int carry = 0;
// Start adding from the least significant bit
while (temp1 || temp2 || carry) {
int sum = carry;
if (temp1) {
sum += temp1->data;
temp1 = temp1->prev;
}
if (temp2) {
sum += temp2->data;
temp2 = temp2->prev;
}

carry = sum / 2;
addBinaryDigit(result, sum % 2); // Add the digit to the result list
}

return result;
}

int main() {
Node* binary1 = nullptr;
Node* binary2 = nullptr;

// Adding binary digits for first number (1011)


addBinaryDigit(binary1, 1);
addBinaryDigit(binary1, 0);
addBinaryDigit(binary1, 1);
addBinaryDigit(binary1, 1);

cout << "First Binary Number: ";


displayBinary(binary1);

// Adding binary digits for second number (1101)


addBinaryDigit(binary2, 1);
addBinaryDigit(binary2, 1);
addBinaryDigit(binary2, 0);
addBinaryDigit(binary2, 1);

cout << "Second Binary Number: ";


displayBinary(binary2);

// Compute 1's complement of the first binary number


onesComplement(binary1);
cout << "1's Complement of First Binary: ";
displayBinary(binary1);

// Reset binary1 and calculate 2's complement


binary1 = nullptr;
addBinaryDigit(binary1, 1);
addBinaryDigit(binary1, 0);
addBinaryDigit(binary1, 1);
addBinaryDigit(binary1, 1);
twosComplement(binary1);
cout << "2's Complement of First Binary: ";
displayBinary(binary1);

// Add the two binary numbers


Node* sum = addBinaryNumbers(binary1, binary2);
cout << "Sum of the two Binary Numbers: ";
displayBinary(sum);

return 0;
}

OUTPUT

/tmp/LR4NnN5Y2Z.o
First Binary Number: 1011
Second Binary Number: 1101
1's Complement of First Binary: 0100
2's Complement of First Binary: 0101
Sum of the two Binary Numbers: 01001

=== Code Execution Successful ===


PRACTICAL NO 9
INPUT

#include <iostream>
#include <stack>
#include <string>
using namespace std;

// Function to reverse a string using a stack


string reverseStringUsingStack(const string& input) {
stack<char> s;
string reversed = "";

// Push all characters of the input string onto the stack


for (char ch : input) {
s.push(ch);
}

// Pop all characters from the stack and form the reversed string
while (!s.empty()) {
reversed += s.top();
s.pop();
}

return reversed;
}

// Function to check if a string is a palindrome


bool isPalindrome(const string& input) {
int n = input.length();
for (int i = 0; i < n / 2; ++i) {
if (tolower(input[i]) != tolower(input[n - i - 1])) {
return false;
}
}
return true;
}

int main() {
string input;

// Taking input from the user


cout << "Enter a string: ";
getline(cin, input);

// Reversing the string using a stack


string reversed = reverseStringUsingStack(input);
cout << "Original String: " << input << endl;
cout << "Reversed String: " << reversed << endl;

// Checking if the string is a palindrome


if (isPalindrome(input)) {
cout << "The string is a palindrome." << endl;
} else {
cout << "The string is not a palindrome." << endl;
}

return 0;
}

SAMPLE INPUT/OUTPUT

/tmp/dk91AkFHoR.o
Enter a string: Poor Dan is in a droop
Original String: Poor Dan is in a droop
Reversed String: poord a ni si naD rooP
The string is not a palindrome.

=== Code Execution Successful ===

PRACTICAL NO 10
INPUT

#include <iostream>
#include <stack>
#include <string>
using namespace std;

// Function to determine precedence of operators


int precedence(char op) {
if (op == '+' || op == '-') {
return 1;
}
if (op == '*' || op == '/') {
return 2;
}
return 0;
}

// Function to check if the character is an operand


bool isOperand(char ch) {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
}

// Function to convert infix expression to postfix


string infixToPostfix(const string& infix) {
stack<char> s;
string postfix = "";

for (char ch : infix) {


if (isOperand(ch)) {
// If character is an operand, add it to postfix expression
postfix += ch;
} else if (ch == '(') {
// If character is '(', push it to stack
s.push(ch);
} else if (ch == ')') {
// If character is ')', pop and add to postfix until '(' is found
while (!s.empty() && s.top() != '(') {
postfix += s.top();
s.pop();
}
s.pop(); // Remove '(' from the stack
} else {
// If character is an operator, pop all operators with higher or equal precedence
while (!s.empty() && precedence(s.top()) >= precedence(ch)) {
postfix += s.top();
s.pop();
}
s.push(ch);
}
}

// Pop all remaining operators in the stack


while (!s.empty()) {
postfix += s.top();
s.pop();
}

return postfix;
}

int main() {
string infix;

// Input infix expression


cout << "Enter infix expression: ";
cin >> infix;

// Convert infix to postfix


string postfix = infixToPostfix(infix);
cout << "Postfix expression: " << postfix << endl;

return 0;
}

SAMPE INPUT/OUTPUT

1.
Enter infix expression: A+B*C
Postfix expression: ABC*+

2.
Enter infix expression: (A+B)*(C-D)
Postfix expression: AB+CD-*
=== Code Execution Successful ===

PRACTICAL NO 11
INPUT

#include <iostream>
#include <queue>
#include <string>
using namespace std;

// Function to add a job to the queue


void addJob(queue<string>& jobQueue, const string& job) {
jobQueue.push(job);
cout << "Job added: " << job << endl;
}

// Function to delete a job from the queue


void deleteJob(queue<string>& jobQueue) {
if (jobQueue.empty()) {
cout << "No jobs to delete!" << endl;
} else {
cout << "Job completed and removed: " << jobQueue.front() << endl;
jobQueue.pop();
}
}

// Function to display the current queue


void displayJobs(queue<string> jobQueue) {
if (jobQueue.empty()) {
cout << "No jobs in the queue." << endl;
return;
}
cout << "Current Jobs in the Queue: ";
while (!jobQueue.empty()) {
cout << jobQueue.front() << " ";
jobQueue.pop();
}
cout << endl;
}

int main() {
queue<string> jobQueue;
int choice;
string job;

do {
cout << "\n1. Add Job\n2. Delete Job\n3. Display Jobs\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter job description: ";
cin.ignore(); // To ignore leftover newline from previous input
getline(cin, job);
addJob(jobQueue, job);
break;
case 2:
deleteJob(jobQueue);
break;
case 3:
displayJobs(jobQueue);
break;
case 4:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 4);

return 0;
}

SAMPLE INPUT/OUTPUT

1. Add Job
2. Delete Job
3. Display Jobs
4. Exit
Enter your choice: 1
Enter job description: Compile the project
Job added: Compile the project

1. Add Job
2. Delete Job
3. Display Jobs
4. Exit
Enter your choice: 1
Enter job description: Test the program
Job added: Test the program

1. Add Job
2. Delete Job
3. Display Jobs
4. Exit
Enter your choice: 3
Current Jobs in the Queue: Compile the project Test the program

1. Add Job
2. Delete Job
3. Display Jobs
4. Exit
Enter your choice: 2
Job completed and removed: Compile the project
=== Code Execution Successful ===

PRACTICAL NO 12
INPUT

#include <iostream>
using namespace std;

#define MAX 5 // Maximum number of orders (M)

class PizzaParlor {
int orders[MAX];
int front, rear;

public:
PizzaParlor() {
front = -1;
rear = -1;
}

// Function to check if the queue is full


bool isFull() {
return (front == 0 && rear == MAX - 1) || (rear == front - 1);
}

// Function to check if the queue is empty


bool isEmpty() {
return front == -1;
}

// Function to add a new order (enqueue)


void addOrder(int order) {
if (isFull()) {
cout << "Order queue is full! Cannot place more orders." << endl;
return;
}

if (front == -1) { // First order


front = rear = 0;
} else if (rear == MAX - 1 && front != 0) {
rear = 0; // Wrap around
} else {
rear++;
}

orders[rear] = order;
cout << "Order " << order << " added successfully!" << endl;
}

// Function to serve an order (dequeue)


void serveOrder() {
if (isEmpty()) {
cout << "No orders to serve!" << endl;
return;
}

cout << "Order " << orders[front] << " is served." << endl;

if (front == rear) {
// Only one order was in the queue
front = rear = -1;
} else if (front == MAX - 1) {
front = 0; // Wrap around
} else {
front++;
}
}

// Function to display the orders in the queue


void displayOrders() {
if (isEmpty()) {
cout << "No orders in the queue." << endl;
return;
}

cout << "Current orders in the queue: ";


if (rear >= front) {
for (int i = front; i <= rear; i++) {
cout << orders[i] << " ";
}
} else {
for (int i = front; i < MAX; i++) {
cout << orders[i] << " ";
}
for (int i = 0; i <= rear; i++) {
cout << orders[i] << " ";
}
}
cout << endl;
}
};

int main() {
PizzaParlor p;
int choice, order;

do {
cout << "\n1. Add Order\n2. Serve Order\n3. Display Orders\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter order number: ";
cin >> order;
p.addOrder(order);
break;
case 2:
p.serveOrder();
break;
case 3:
p.displayOrders();
break;
case 4:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 4);

return 0;
}

SAMPLE INPUT/OUTPUT

1. Add Order
2. Serve Order
3. Display Orders
4. Exit
Enter your choice: 1
Enter order number: 101
Order 101 added successfully!
1. Add Order
2. Serve Order
3. Display Orders
4. Exit
Enter your choice: 3
Current orders in the queue: 101

1. Add Order
2. Serve Order
3. Display Orders
4. Exit
Enter your choice: 2
Order 101 is served.

1. Add Order
2. Serve Order
3. Display Orders
4. Exit
Enter your choice: 3
No orders in the queue.

PRACTICAL NO 13

INPUT

#include <iostream>
using namespace std;

#define MAX 5 // Maximum size of the deque

class Deque {
int deque[MAX];
int front, rear;

public:
Deque() {
front = -1;
rear = -1;
}

// Function to check if the deque is full


bool isFull() {
return (front == 0 && rear == MAX - 1) || (rear == front - 1);
}

// Function to check if the deque is empty


bool isEmpty() {
return front == -1;
}

// Function to insert an element at the front


void insertFront(int value) {
if (isFull()) {
cout << "Deque is full! Cannot insert at front." << endl;
return;
}

if (front == -1) { // First element


front = rear = 0;
} else if (front == 0) {
front = MAX - 1; // Wrap around to the end
} else {
front--;
}

deque[front] = value;
cout << "Inserted " << value << " at front." << endl;
}

// Function to insert an element at the rear


void insertRear(int value) {
if (isFull()) {
cout << "Deque is full! Cannot insert at rear." << endl;
return;
}

if (front == -1) { // First element


front = rear = 0;
} else if (rear == MAX - 1) {
rear = 0; // Wrap around to the front
} else {
rear++;
}

deque[rear] = value;
cout << "Inserted " << value << " at rear." << endl;
}
// Function to delete an element from the front
void deleteFront() {
if (isEmpty()) {
cout << "Deque is empty! Cannot delete from front." << endl;
return;
}

cout << "Deleted " << deque[front] << " from front." << endl;

if (front == rear) { // Only one element was present


front = rear = -1;
} else if (front == MAX - 1) {
front = 0; // Wrap around to the start
} else {
front++;
}
}

// Function to delete an element from the rear


void deleteRear() {
if (isEmpty()) {
cout << "Deque is empty! Cannot delete from rear." << endl;
return;
}

cout << "Deleted " << deque[rear] << " from rear." << endl;

if (front == rear) { // Only one element was present


front = rear = -1;
} else if (rear == 0) {
rear = MAX - 1; // Wrap around to the end
} else {
rear--;
}
}

// Function to display the elements in the deque


void displayDeque() {
if (isEmpty()) {
cout << "Deque is empty!" << endl;
return;
}

cout << "Deque elements: ";


if (rear >= front) {
for (int i = front; i <= rear; i++) {
cout << deque[i] << " ";
}
} else {
for (int i = front; i < MAX; i++) {
cout << deque[i] << " ";
}
for (int i = 0; i <= rear; i++) {
cout << deque[i] << " ";
}
}
cout << endl;
}
};

int main() {
Deque dq;
int choice, value;

do {
cout << "\n1. Insert at Front\n2. Insert at Rear\n3. Delete from Front\n4. Delete from Rear\n5. Display
Deque\n6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to insert at front: ";
cin >> value;
dq.insertFront(value);
break;
case 2:
cout << "Enter value to insert at rear: ";
cin >> value;
dq.insertRear(value);
break;
case 3:
dq.deleteFront();
break;
case 4:
dq.deleteRear();
break;
case 5:
dq.displayDeque();
break;
case 6:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 6);

return 0;
}

Sample Input/Output:

1. Insert at Front
2. Insert at Rear
3. Delete from Front
4. Delete from Rear
5. Display Deque
6. Exit
Enter your choice: 1
Enter value to insert at front: 10
Inserted 10 at front.

1. Insert at Front
2. Insert at Rear
3. Delete from Front
4. Delete from Rear
5. Display Deque
6. Exit
Enter your choice: 2
Enter value to insert at rear: 20
Inserted 20 at rear.

1. Insert at Front
2. Insert at Rear
3. Delete from Front
4. Delete from Rear
5. Display Deque
6. Exit
Enter your choice: 5
Deque elements: 10 20

1. Insert at Front
2. Insert at Rear
3. Delete from Front
4. Delete from Rear
5. Display Deque
6. Exit
Enter your choice: 3
Deleted 10 from front.

You might also like