Department of Electrical Engineering (Computer
Engineering Program)
BS Computer Engineering
Assignment of Data Structures and
Algorithms
Submitted To: Dr. Haris Masood.
Student Name: Abdullah Rashid.
Semester:3rd.
Reg No: UW-23-CPE-BS-009th.
Question 01:
Write a program from decimal to binary coversion by stacks in C++.
#include <iostream>
using namespace std;
class Stack {
private:
int* arr;
int top;
int capacity;
public:
Stack(int size) {
arr = new int[size];
top = -1;
capacity = size;
}
void push(int value) {
if (top == capacity - 1) {
cout << "Stack overflow" << endl;
return;
}
arr[++top] = value;
}
int pop() {
if (isEmpty()) {
cout << "Stack underflow" << endl;
return -1;
}
return arr[top--];
}
int peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return -1;
}
return arr[top];
}
bool isEmpty() {
return top == -1;
}
};
void decimalToBinary(int number) {
Stack binaryStack(32);
if (number == 0) {
cout << "Binary: 0" << endl;
return;
}
while (number > 0) {
binaryStack.push(number % 2);
number /= 2;
}
cout << "Binary: ";
while (!binaryStack.isEmpty()) {
cout << binaryStack.pop();
}
cout << endl;
}
int main() {
int decimalNumber;
cout << "Enter a decimal number: ";
cin >> decimalNumber;
decimalToBinary(decimalNumber);
return 0;
}
Output/Screen Shots:
Question 02:
Implement the append insert and delelte functiion of lists.
#include <iostream>
using namespace std;
class CustomList {
private:
int* data;
int capacity;
int size;
void resize(int newCapacity) {
int* newData = new int[newCapacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
delete[] data;
data = newData;
capacity = newCapacity;
}
public:
CustomList() {
capacity = 10;
size = 0;
data = new int[capacity];
}
~CustomList() {
delete[] data;
}
void append(int value) {
if (size == capacity) {
resize(capacity * 2);
}
data[size++] = value;
}
void insert(int index, int value) {
if (index < 0 || index > size) {
cout << "Index out of bounds" << endl;
return;
}
if (size == capacity) {
resize(capacity * 2);
}
for (int i = size; i > index; i--) {
data[i] = data[i - 1];
}
data[index] = value;
size++;
}
void remove(int index) {
if (index < 0 || index >= size) {
cout << "Index out of bounds" << endl;
return;
}
for (int i = index; i < size - 1; i++) {
data[i] = data[i + 1];
}
size--;
if (size <= capacity / 4) {
resize(capacity / 2);
}
}
void printList() const {
for (int i = 0; i < size; i++) {
cout << data[i] << " ";
}
cout << endl;
}
};
int main() {
CustomList list;
list.append(10);
list.append(20);
list.append(30);
cout << "List after appending: ";
list.printList();
list.insert(1, 15);
cout << "List after inserting 15 at index 1: ";
list.printList();
list.remove(2);
cout << "List after removing element at index 2: ";
list.printList();
return 0;
Output/Screen Shots:
Question 03:
Implement the (RPN)Reverse Polish Notation or Postfix Notation in Flow Chart.