Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Structure Lab 6 Name: Roll No: Section: Teacher: Task 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

DATA STRUCTURE LAB 6

Name : Muhammad Asaad


Roll No : 22F-3879
Section : 3D
Teacher: Mam Rabia Anwar

Task 1:
#include<iostream>
#include<string>
using namespace std;
class Stack {
int top;
public:
int a[10]; // Maximum size of Stack
Stack()
{
top = -1;
}
bool push(int x);
int pop();
int peek();
bool isEmpty();
};
bool Stack::push(int x)
{
if (top>=9) {
cout << "Stack Overflow"<<endl;
return false;
}
else {
a[++top] = x;
cout << x << " pushed into stack"<<endl;
return true;
}
}

int Stack::pop()
{
if (top<0) {
cout << "Stack Underflow"<<endl;
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::peek()
{
if (top < 0) {
cout <<"Stack is Empty"<<endl;
return 0;
}
else {
int x = a[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
int main()
{
Stack stack;
int numbers[10];
int num=0,numcount=0;
cout << "Enter numbers For pushing into stack (-1 for
stopping)"<<endl;
while(num!=-1)
{
cin >> num;
if (num == -1)
break;
stack.push(num);
numcount++;
}
for (int i = 0; i < numcount; i++)
{
if (i >= 10) break;
stack.pop();
}
stack.peek();
system("pause");
return 0;
}

Output:
Task 2:
#include <iostream>
#include <string>
using namespace std;

bool isOperator(char c)
{
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^';
}

int precedence(char op)


{
if (op == '^')
return 3;
else if (op == '*' || op == '/')
return 2;
else if (op == '+' || op == '-')
return 1;
else
return 0;
}

string infixToPostfix(const string& infix)


{
string postfix;
char stack[100];
int top = -1;

for (char c : infix) {


if (isdigit(c)) {
postfix += c;
}
else if (c == ' ') {
continue;
}
else if (isOperator(c)) {
while (top >= 0 && precedence(stack[top]) >= precedence(c)) {
postfix += stack[top--];
}
stack[++top] = c;
}
}
while (top >= 0) {
postfix += stack[top--];
}
return postfix;
}
int main() {
string infixExpression = "3 + 4 * 2 / 1 - 5^2";
string postfixExpression = infixToPostfix(infixExpression);
cout << "Input: " << infixExpression << endl;
cout << "Output: " << postfixExpression << endl;
string infixExpression2 = "2^6*3+2-5";
string postfixExpression2 = infixToPostfix(infixExpression2);
cout << "Input: " << infixExpression2 << endl;
cout << "Output: " << postfixExpression2 << endl;
return 0;
}

Output:

Task 3:
#include <iostream>
#include <string>
using namespace std;

bool isDigit(char c)
{
return c >= '0' && c <= '9';
}

bool isOperator(char c)
{
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^';
}

int performOperation(char operation, int operand1, int operand2)


{
int result = 0;
switch (operation) {
case '+':
return operand1 + operand2;
case '-':
return operand1 - operand2;
case '*':
return operand1 * operand2;
case '/':
return operand1 / operand2;
case '^':
result = 1;
for (int i = 0; i < operand2; ++i) {
result *= operand1;
}
break;
default:
break;
}

return result;
}

int evaluatePostfix(const string& postfix)


{
int operands[100];
int top = -1;

for (char c : postfix) {


if (isDigit(c)) {
operands[++top] = c - '0';
}
else if (isOperator(c)) {
int operand2 = operands[top--];
int operand1 = operands[top--];
int result = performOperation(c, operand1, operand2);
operands[++top] = result;
}
}
return operands[top];
}

int main() {
string postfixExpression = "3 4 2 * 1 / + 5 2 ^ -";
int result = evaluatePostfix(postfixExpression);
cout << "Output: " << result << endl;
string postfixExpression2;
cout << "Enter Expression :";
cin >> postfixExpression2;
int result2 = evaluatePostfix(postfixExpression2);
cout << "Output: " << result2 << endl;
cout << "Thanks for using! " << endl;
return 0;
}

Output:

Task 4:
#include <iostream>
#include <string>
using namespace std;

struct Node {
int data;
Node* next;
Node(int data) {
this->data = data;
next = nullptr;
}
};

class Stack {
int pop;
};

struct LibraryMain {
int bookno;
float price;
string book_name, edition, category;
bool is_available;
int issueDate;
LibraryMain() {
is_available = true;
}

void setAvailability(bool status) {


is_available = status;
}

bool getAvailability() {
return is_available;
}
};

struct Student
{
string name, rollno, department;
};

struct Faculty
{
string name, course, department;
};

class StudentList
{
Node* head;
public:
StudentList() {
head = nullptr;
}
void insert(int data) {
Node* newNode = new Node(data);
if (head == nullptr) {
head = newNode;
return;
}
else {
newNode->next = head;
head = newNode;
}
}

void display() {
Node* current = head;
while (current != nullptr) {
cout << current->data << ' ';
current = current->next;
}
cout << endl;
}
};
class FacultyList {
Node* head;
public:
FacultyList() {
head = nullptr;
}
void insert(int data) {
Node* newNode = new Node(data);
if (head == nullptr) {
head = newNode;
return;
}
else {
newNode->next = head;
head = newNode;
}
}
void display() {
Node* current = head;
while (current != nullptr) {
cout << current->data << ' ';
current = current->next;
}
cout << endl;
}
};
class FacultyStack {
int top;
public:
LibraryMain array[10];
FacultyStack() {
top = -1;
}
void Push(LibraryMain val) {
if (top >= 10 - 1) {
cout << "Stack Overflow\n";
}
else {
top++;
array[top] = val;
}
}
};

class Library {
string department;
int totalbookscategorywise[3];
StudentList studentRecords;
FacultyList facultyRecords;
public:
LibraryMain lib[10];

Library(string dept) {
department = dept;
for (int i = 0; i < 10; ++i)
lib[i].is_available = true;
for (int i = 0; i < 3; i++)
totalbookscategorywise[i] = 0;
}

void issueBook(LibraryMain book, Student student) {


bool available = checkAvailability(book);

if (available) {
studentRecords.insert(book.bookno);
totalbookscategorywise[book.category[0] - 'A']++;
cout << "Book issued to student " << student.name << " with roll no. " <<
student.rollno << endl;
for (int i = 0; i < 10; ++i) {
if (lib[i].bookno == book.bookno) {
lib[i].setAvailability(false);
lib[i].issueDate = 100;
break;
}
}
}
else {
cout << "Book not available. It will be available from next week" << endl;
}
}
bool checkAvailability(LibraryMain book) {
for (int i = 0; i < 10; ++i) {
if (lib[i].bookno == book.bookno) {
if (lib[i].is_available) {
return true;
}
else {
return false;
}
}
}
cout << "Book not found." << endl;
return false;
}
void issueBook(LibraryMain book, Faculty faculty) {
bool available = checkAvailability(book);

if (available) {
facultyRecords.insert(book.bookno);
cout << "Book issued to faculty " << faculty.name << " teaching " <<
faculty.course << " in department " << faculty.department << endl;
for (int i = 0; i < 10; ++i) {
if (lib[i].bookno == book.bookno) {
lib[i].setAvailability(false);
lib[i].issueDate = 100;
break;
}
}
}
else {
cout << "Book not available. It will be available from next week" << endl;
}
}
void checkOverdue() {
int borrowingDuration = 7;
int currentDate = 2;

for (int i = 0; i < 10; ++i) {


if (!lib[i].getAvailability()) {
int dueDate = lib[i].issueDate + borrowingDuration;
if (currentDate > dueDate) {
int daysOverdue = currentDate - dueDate;
float fineAmount = 0.5;
float totalFine = fineAmount * daysOverdue;
cout << "Book with book number " << lib[i].bookno << " is overdue by
" << daysOverdue << " days. Fine amount: $" << totalFine << endl;
}
}
}
}
};

int main() {
Library library("Computer Science");
Student student1 = { "Asaad", "22F-3879", "Computer Science" };
Faculty faculty1 = { "Hassaan Ch", "Data Structures", "Computer Science" };

library.lib[0].bookno = 1;
library.lib[0].price = 50.0;
library.lib[0].book_name = "Introduction to Algorithms";
library.lib[0].edition = "2nd Edition";
library.lib[0].category = "Education";
library.lib[0].is_available = true;
library.lib[1].bookno = 2;
library.lib[1].price = 60.0;
library.lib[1].book_name = "Titanic";
library.lib[1].edition = "3rd Edition";
library.lib[1].category = "Novel";
library.lib[1].is_available = true;
LibraryMain book1;
book1.bookno = 1;
book1.price = 50.0;
book1.book_name = "Introduction to Algorithms";
book1.edition = "2nd Edition";
book1.category = "A";

library.issueBook(book1, student1);
library.issueBook(book1, faculty1);
library.checkOverdue();

return 0;
}

Output:

You might also like