Lab Exam Prep
Lab Exam Prep
#include<iostream>
using namespace std;
int stack[5];
int Size = 5;
int top = -1;
void Pop() {
if (top == -1) {
cout << "Can't delete element because stack is empty." << endl;
return;
}
else {
cout << stack[top] << " element has been removed" << endl << endl;
top--;
}
}
void Display() {
if (top == -1) {
cout << "Can't display elements because stack is empty." << endl;
return;
}
else {
cout << "Elements of the stack are:" << endl;
for (int i = top; i >= 0; i--) {
cout << stack[i] << " ";
}
cout << endl << endl;
}
}
Queue
#include <iostream>
using namespace std;
int queue[5];
int Size = 5;
int rear = -1;
int front = -1;
void Dequeue() {
if ((front == -1) || (front == rear)) {
cout << "Queue is empty can't delete elements." << endl;
return;
}
else {
cout << endl;
cout << queue[front] << " is deleted from the queue" << endl;
front++;
}
}
void Display() {
if ((front == -1) || (front == rear)) {
cout << "Queue is empty can't display elements." << endl;
return;
}
else {
cout << "\nElements of the queue are: " << endl;
for (int i = front; i <= rear; i++) {
cout << queue[i] << " ";
}
cout << endl;
}
}
Circular queue
#include <iostream>
using namespace std;
int queue[5];
int Size = 5;
int front = -1;
int rear = -1;
else {
if (front == -1) {
front = 0;
}
void Dequeue() {
if (front == -1) {
cout << "Queue is empty" << endl;
return;
}
else {
cout << queue[front] << " element is removed from the queue." << endl;
front = (front + 1) % Size;
}
}
void Display() {
if (front == -1) {
cout << "Queue is empty" << endl;
return;
}
else {
int i = front;
while (true) {
cout << queue[i] << " ";
if (i == rear) {
break;
}
i = (i + 1) % Size;
}
cout << endl;
}
}
Linked list
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
if (head == NULL) {
head = newNode;
return;
}
if (position == 1) {
newNode->next = head;
head = newNode;
return;
}
if (temp == NULL) {
cout << "Position out of range!" << endl;
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
void deleteAtBeginning() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
head = head->next;
delete temp;
}
void deleteAtEnd() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
}
if (head->next == NULL) {
delete head;
head = NULL;
return;
}
Node* temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
delete temp->next;
temp->next = NULL;
}
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
int main() {
insertAtEnd(10);
insertAtEnd(20);
insertAtEnd(30);
insertAtPosition(15, 2);
cout << "Linked List after insertion: ";
display();
deleteAtBeginning();
cout << "After deleting first node: ";
display();
deleteAtEnd();
cout << "After deleting last node: ";
display();
deleteAtPosition(2);
cout << "After deleting node at position 2: ";
display();
return 0;
}
Linked list (Stack)
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void push(int x) {
Node* newNode = new Node();
newNode->data = x;
newNode->next = top;
top = newNode;
cout << x << " pushed to stack" << endl;
}
void pop() {
if (top == NULL) {
cout << "Stack is empty" << endl;
return;
}
Node* temp = top;
cout << "The popped element is: " << top->data << endl;
top = top->next;
delete temp;
}
int peek() {
if (top == NULL) {
cout << "Stack is empty" << endl;
return -1;
}
return top->data;
}
void display() {
if (top == NULL) {
cout << "Stack is empty" << endl;
return;
}
Node* temp = top;
cout << "Stack elements: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main() {
push(3);
push(2);
push(1);
display();
cout << "Top element of the stack is: " << peek() << endl;
pop();
display();
return 0;
}
Linked list (Queue)
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void Insert(int x) {
Node* newNode = new Node();
newNode->data = x;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
}
void Delete() {
if (front == NULL) {
cout << "Queue is empty" << endl;
return;
}
if (front == NULL) {
rear = NULL;
}
delete temp;
}
int Peek() {
if (front == NULL) {
return -1;
}
return front->data;
}
void Display() {
Node* temp = front;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main() {
Insert(1);
Insert(2);
Insert(3);
int y = Peek();
if (y == -1) {
cout << "Queue is empty." << endl;
} else {
cout << "Front element of the Queue is " << y << endl;
}
Display();
Delete();
Delete();
Delete();
Display();
return 0;
}
Jewellary code
#include<iostream>
#include<string>
using namespace std;
struct Jewellary {
string customerName;
string productType;
double amount;
};
Jewellary queue[10];
int Size = 10;
int front = -1;
int rear = -1;
else {
if (front == -1) {
front = 0;
}
void Dequeue() {
if ((front == -1) || (front == rear) )
{
cout << "Queue is empty (underflow), can't remove orders." << endl;
return;
}
else
{
cout << "Order deleted successfully of customer " << queue[front].customerName <<
endl;
front=(front+1)%Size;
void Display() {
if ((front == -1) || (front == rear))
{
cout << "Queue is empty (underflow), can't display orders." << endl;
return;
}
else {
int i = front;
while(true) {
cout << "Details of the customer: " << endl;
cout << "Customer name: " << queue[i].customerName << endl;
cout << "Product type: " << queue[i].productType << endl;
cout << "Amount: " << queue[i].amount << endl;
if (i== rear) {
break;
}
i = (i + 1) % Size;
}
}
}
double TotalAmountOfJewellery() {
double totalAmount = 0;
if ((front == -1) || (front == rear))
{
cout << "Queue is empty (underflow), dont have any orders." << endl;
return totalAmount;
}
else {
int i = front;
while (true) {
totalAmount = totalAmount + queue[i].amount;
if (i == rear) {
break;
}
i = (i + 1) % Size;
}
}
cout << "Total amount is: ";
return totalAmount;
double HighestAmount() {
else {
double highest=queue[front].amount;
int i = front;
while(true){
if (queue[i].amount > highest) {
highest = queue[i].amount;
}
if (i >= rear) {
break;
}
i=(i + 1) % Size;
}
cout << "Highest amount is: ";
return highest;
}
}
int main() {
int choice;
string customerName;
string productType;
double amount;
if (choice == 1) {
cout << "Enter the name of the customer: ";
cin.ignore();
getline(cin, customerName);
cout << "Enter type of the product (Necklace, Bracelet, Ring): ";
getline(cin, productType);
else if (choice == 2) {
Dequeue();
}
else if (choice == 3) {
Display();
}
else if (choice == 4) {
cout << "Total Funds Collected: $" << TotalAmountOfJewellery() << endl;
}
else if (choice == 5) {
cout << "Highest Order Amount: $" << HighestAmount() << endl;
}
else if (choice == 6) {
cout << "Exiting program..." << endl;
break;
}
else {
cout << "Invalid choice! Try again." << endl;
}
}
return 0;
}