Data Structure SHEET
Data Structure SHEET
Practical Sheet
ﻣﺤﻤﻮﺩ ﺻﻼﺡ ﺍﻟﺴﻴﺪ ﻣﺼﻄﻔﻰ ﺩﺑﺎ
Grade:
2023/2024 2023/2024 2023/2024
Signature:
١٦٨
كلية الحاسبات والمعلومات – جامعة طنطا
Faculty of Computers and Informatics – Tanta University
1. Balancing Parentheses:
Problem: Check if a given expression has balanced parentheses, brackets, and curly
braces. For example, determine if "{(A + B)}" is balanced.
Example:
Input: "{(A + B)}"
ﻣﺤﻤﻮﺩ ﺻﻼﺡ ﺍﻟﺴﻴﺪ ﻣﺼﻄﻔﻰ ﺩﺑﺎ
Output: Balanced
#include <iostream>
#include <stack>
using namespace std;
bool isBalanced(const string& expression) {
stack<char> s;
for (char c : expression) {
switch (c) {
case '(':
case '{':
case '[':
s.push(c);
break;
case ')':
if (!s.empty() && s.top() == '(') {
s.pop();
} else {
2023/2024 return false; // unbalanced
2023/2024 2023/2024
}
break;
case '}':
if (!s.empty() && s.top() == '{') {
s.pop();
} else {
return false; // unbalanced
}
break;
case ']':
if (!s.empty() && s.top() == '[') {
s.pop();
} else {
return false; // unbalanced
}
break;
}
}
return s.empty();
}
int main() {
string expression;
cout << "Enter An Expression: ";
getline(cin, expression);
if (isBalanced(expression)) {
cout << "This Expression Is Balanced" << endl;
} else {
cout << "This Expression Is Unbalanced" << endl;
}
return 0;
}
١٦٩
كلية الحاسبات والمعلومات – جامعة طنطا
Faculty of Computers and Informatics – Tanta University
2. Expression Evaluation:
Problem: Convert an infix mathematical expression to postfix notation and evaluate
the result. For example, evaluate "3 * (4 + 2)".
Example:
Input: "3 * (4 + 2)"
ﻣﺤﻤﻮﺩ ﺻﻼﺡ ﺍﻟﺴﻴﺪ ﻣﺼﻄﻔﻰ ﺩﺑﺎ
Output: 18
for (char c : postfix) {
#include <iostream> if (isdigit(c)) {
#include <stack> operands.push(c - '0');
#include <string> } else if (isOperator(c)) {
using namespace std; double operand2 = operands.top();
bool isOperator(char c) { operands.pop();
return c == '+' || c == '-' || c == '*' || c == '/'; double operand1 = operands.top();
} operands.pop();
int precedence(char c) { double result;
if (c == '+' || c == '-') { switch (c) {
return 1; case '+':
} else if (c == '*' || c == '/') { result = operand1 + operand2;
return 2; break;
} else { case '-':
return 0; result = operand1 - operand2;
} break;
} 2023/2024 2023/2024
case '*': 2023/2024
string infixToPostfix(string infix) { result = operand1 * operand2;
stack<char> operators; break;
string postfix; case '/':
for (char c : infix) { result = operand1 / operand2;
if (isalnum(c)) { break;
postfix += c; }
} else if (isOperator(c)) { operands.push(result);
while (!operators.empty() && precedence(operators.top()) }
>= precedence(c)) { }
postfix += operators.top(); if (operands.size() != 1) {
operators.pop(); throw invalid_argument("Invalid expression");
} }
operators.push(c); return operands.top();
} else if (c == '(') { }
operators.push(c); int main() {
} else if (c == ')') { string infixExpression;
while (!operators.empty() && operators.top() != '(') { cout << "Enter an infix expression: ";
postfix += operators.top(); getline(cin, infixExpression);
operators.pop(); string postfixExpression = infixToPostfix
} (infixExpression);
operators.pop(); cout << "Postfix expression: "
} else { << postfixExpression << endl;
throw invalid_argument("Invalid character in expression" double result = evaluatePostfix(postfixExpression)
); ;
} cout << "Result: " << result << endl;
} return 0;
while (!operators.empty()) { }
postfix += operators.top();
operators.pop();
}
return postfix;
}
double evaluatePostfix(string postfix) {
stack<double> operands;
١٧٠
كلية الحاسبات والمعلومات – جامعة طنطا
Faculty of Computers and Informatics – Tanta University
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* curr = head;
while (curr) {
Node* nextNode = curr->next;
2023/2024 2023/2024 2023/2024
curr->next = prev;
prev = curr;
curr = nextNode;
}
return prev;
}
void printList(Node* head) {
while (head) {
cout << head->data << " -> ";
head = head->next;
}
cout << "NULL" << endl;
}
int main() {
Node* head = new Node{1};
head->next = new Node{2};
head->next->next = new Node{3};
head->next->next->next = new Node{4};
cout << "The Original List Is: ";
printList(head);
head = reverseList(head);
cout << "The Reversed List Is: ";
printList(head);
return 0;
} ١٧١
كلية الحاسبات والمعلومات – جامعة طنطا
Faculty of Computers and Informatics – Tanta University
while (!q.empty()) {
char current = q.front();
q.pop();
2023/2024 2023/2024 2023/2024
if (current == target) {
break;
}
for (char neighbor : graph[current]) {
if (visited.count(neighbor) == 0) {
q.push(neighbor);
visited.insert(neighbor);
parent[neighbor] = current;
}
}
}
vector<char> path;
char current = target;
while (current != start) {
path.push_back(current);
current = parent[current];
}
path.push_back(start);
reverse(path.begin(), path.end());
return path;
}
int main() {
graph['A'] = {'B', 'C'};
graph['B'] = {'D', 'E'};
graph['C'] = {'F'};
graph['D'] = {};
graph['E'] = {'F'};
graph['F'] = {};
if (shortestPath.empty()) {
cout << "No path exists between " << start << " and " << target << endl;
} else {
cout << "Shortest path from " << start << " to " << target << " Is: ";
for (char node : shortestPath) {
cout << node << " -> ";
}
cout << "End" << endl;
}
return 0;
} ١٧٢
كلية الحاسبات والمعلومات – جامعة طنطا
Faculty of Computers and Informatics – Tanta University
struct Node {
int data;
Node* left;
Node* right;
Node(int val) {
data = val;
left = right = nullptr;
}
};
return root;
}
bool search(Node* root, int val) {
if (root == nullptr) {
return false;
}
if (root->data == val) {
return true;
}
if (val < root->data) {
return search(root->left, val);
} else {
return search(root->right, val);
}
}
void inorderTraversal(Node* root) {
if (root != nullptr) {
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}
}
int main() {
Node* root = nullptr;
#include <iostream>
7. Stack-Based Undo/Redo Functionality:
#include <stack>
#include <string>
using namespace std; Problem: Design a text editor that uses a stack to implement undo and redo
stack<string> undoStack;
stack<string> redoStack;functionality for user actions. For example, type, delete, and undo/redo text in a text
editor.
string currentText = "";
void displayText() {
Example:
cout << "Current text: " << currentText << endl;
}
ﻣﺤﻤﻮﺩ ﺻﻼﺡ ﺍﻟﺴﻴﺪ ﻣﺼﻄﻔﻰ ﺩﺑﺎ
}
displayText();
User redoes: "Hello, World!"
void deleteText() {
if (currentText.length() > 0) {
undoStack.push(currentText);
currentText.pop_back();
redoStack = stack<string>();
displayText();
} else {
cout << "Cannot delete: Text is empty." << endl;
}
}
void undo() {
if (!undoStack.empty()) {
redoStack.push(currentText);
currentText = undoStack.top();
undoStack.pop();
displayText();
} else {
cout << "Cannot
2023/2024undo: No more actions to undo." << endl; 2023/2024 2023/2024
}
}
void redo() {
if (!redoStack.empty()) {
undoStack.push(currentText);
currentText = redoStack.top();
redoStack.pop();
displayText();
} else {
cout << "Cannot redo: No more actions to redo." << endl;
}
}
int main() {
int choice;
do {
cout << "\nText Editor Menu:" << endl;
cout << "1. Type Text" << endl;
cout << "2. Delete Text" << endl;
cout << "3. Undo" << endl;
cout << "4. Redo" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
string text;
cout << "Enter text to type: ";
cin >> text;
getline(cin, text);
typeText(text);
break;
}
case 2:
deleteText();
break;
case 3:
undo();
break;
case 4:
redo();
break;
case 5:
cout << "Exiting..." << endl;
break;
default: ١٧٥
cout << "Invalid choice." << endl;
}
} while (choice != 5);
return 0;
}
كلية الحاسبات والمعلومات – جامعة طنطا
Faculty of Computers and Informatics – Tanta University
class TaskScheduler {
public:
void addTask(const std::string& task);
void scheduleTasks();
void executeTask();
private:
queue<string> taskQueue;
vector<string> completedTasks;
};
void TaskScheduler::scheduleTasks() {
cout << "Scheduled tasks: ";
while (!taskQueue.empty()) {
cout << taskQueue.front() << ", ";
completedTasks.push_back(taskQueue.front());
taskQueue.pop();
}
cout << "\n";
}
void TaskScheduler::executeTask() {
cout << "Execution order: ";
for (const auto& task : completedTasks) {
cout << task << " -> ";
}
cout << "\n";
}
int main() {
TaskScheduler scheduler;
scheduler.addTask("Task A");
scheduler.addTask("Task B");
scheduler.addTask("Task C");
scheduler.scheduleTasks();
scheduler.executeTask();
return 0;
}
١٧٦
كلية الحاسبات والمعلومات – جامعة طنطا
Faculty of Computers and Informatics – Tanta University
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int main() {
2023/2024 2023/2024 2023/2024
Node *head = new Node{1, new Node{2, new Node{3, new Node{4, nullptr}}}};
head->next->next->next->next = head->next;
return 0;
}
١٧٧
كلية الحاسبات والمعلومات – جامعة طنطا
Faculty of Computers and Informatics – Tanta University
Constructed BST: 3 /
25//
146
#include <iostream>
#include <queue>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int data) {
this->data = data;
left = right = nullptr;
}
};
return root;
}
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* temp = q.front();
q.pop();
if (temp->left != nullptr) {
q.push(temp->left);
}
if (temp->right != nullptr) {
q.push(temp->right);
}
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);