0% found this document useful (0 votes)
20 views20 pages

Lab Exam Prep

The document contains implementations of various data structures in C++, including Stack, Queue, Circular Queue, Linked List, and a specialized Linked List for managing jewelry orders. Each data structure has functions for insertion, deletion, and display of elements, along with specific functionalities for the jewelry queue such as calculating total funds and highest order amount. The code demonstrates basic operations and user interaction through a menu-driven interface for the jewelry store.

Uploaded by

aizasarfaraz921
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)
20 views20 pages

Lab Exam Prep

The document contains implementations of various data structures in C++, including Stack, Queue, Circular Queue, Linked List, and a specialized Linked List for managing jewelry orders. Each data structure has functions for insertion, deletion, and display of elements, along with specific functionalities for the jewelry queue such as calculating total funds and highest order amount. The code demonstrates basic operations and user interaction through a menu-driven interface for the jewelry store.

Uploaded by

aizasarfaraz921
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/ 20

Stack

#include<iostream>
using namespace std;

int stack[5];
int Size = 5;
int top = -1;

void Push(int value) {


if (top == Size - 1) {
cout << "Stack is full can't insert more items" << endl;
return;
}
else {
top++;
stack[top] = value;
}
}

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 Enqueue(int value) {


if (rear == Size - 1) {
cout << "Queue is full can't insert more elements." << endl;
return;
}
else {
if (front == -1) {
front++;
}
rear++;
queue[rear] = value;
}
}

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;

void Enqueue(int value) {


if ((rear + 1) % Size == front) {
cout << "Queue is full" << endl;
return;
}

else {
if (front == -1) {
front = 0;
}

rear = (rear + 1) % Size;


queue[rear] = value;
}
}

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

Node* head = NULL;

void insertAtEnd(int value) {


Node* newNode = new Node;
newNode->data = value;
newNode->next = NULL;

if (head == NULL) {
head = newNode;
return;
}

Node* temp = head;


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

void insertAtBeginning(int value) {


Node* newNode = new Node;
newNode->data = value;
newNode->next = head;
head = newNode;
}

void insertAtPosition(int value, int position) {


Node* newNode = new Node;
newNode->data = value;
newNode->next = NULL;

if (position == 1) {
newNode->next = head;
head = newNode;
return;
}

Node* temp = head;


for (int i = 1; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}

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 deleteAtPosition(int position) {


if (head == NULL) {
cout << "List is empty!" << endl;
return;
}
if (position == 1) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* temp = head;
for (int i = 1; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
cout << "Position out of range!" << endl;
return;
}
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
}

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

Node* top = NULL;

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

Node* front = NULL;


Node* rear = NULL;

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

Node* temp = front;


cout << "Element deleted from Queue is: " << front->data << endl;
front = front->next;

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;

void Enqueue(string customerName, string productType, double amount)


{
if ((rear + 1) % Size == front) {
cout << "Queue is full (overflow), can't insert more orders." << endl;
return;
}

else {
if (front == -1) {
front = 0;
}

rear = (rear + 1) % Size;


queue[rear].customerName = customerName;
queue[rear].productType = productType;
queue[rear].amount = amount;
cout << "Order inserted successfully of customer " << queue[rear].customerName <<
endl;
}
}

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;

if (front >= rear)


{
front = rear = -1;
}
}
}

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() {

if ((front == -1) || (front > rear))


{
cout << "Queue is empty (underflow), dont have any orders." << endl;
return 0;
}

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;

for (;;) {//while(choice!=6)


cout << "\n====== Jewellery Store Menu ======" << endl;
cout << "1. Add Order" << endl;
cout << "2. Delete Order" << endl;
cout << "3. Display Orders" << endl;
cout << "4. Total Funds" << endl;
cout << "5. Highest Order Amount" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;

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

cout << "Enter amount of the product: $";


cin >> amount;

Enqueue(customerName, productType, amount);


}

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

You might also like