Data Structure Lab 6 Name: Roll No: Section: Teacher: Task 1
Data Structure Lab 6 Name: Roll No: Section: Teacher: Task 1
Data Structure Lab 6 Name: Roll No: Section: Teacher: Task 1
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 == '^';
}
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 == '^';
}
return result;
}
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;
}
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;
}
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;
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: