dsa notes
dsa notes
Node(int data){
val=data;
next=NULL;
}
};
int main()
{
Node* head = NULL;
insertAtHead(head,2);
display(head);
insertAtHead(head,3);
insertAtLast(head,4);
display(head);
insertAtPosition(head,5,2);
display(head);
return 0;
}
#pointer questions
Swapping two numbers using pointers:
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 10;
cout << "Before swapping: x = " << x << ", y = " << y << endl;
swap(&x, &y);
cout << "After swapping: x = " << x << ", y = " << y << endl;
return 0;
}
int main() {
char str[] = "Hello";
reverseString(str);
cout << "Reversed string: " << str << endl;
return 0;
}
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
int* arr = new int[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
cout << "Entered elements are: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
delete[] arr; // Freeing up the allocated memory
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printArray(arr, size);
return 0;
}
int main() {
const char* str = "Hello, World!";
cout << "Length of the string: " << stringLength(str) << endl;
return 0;
}
int main() {
const char* src = "Hello";
char dest[20];
stringCopy(dest, src);
cout << "Copied string: " << dest << endl;
return 0;
}
int main() {
int size = 5;
int* arr = createArray(size);
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
delete[] arr;
return 0;
}
int main() {
int x = 10;
int* ptr = &x;
int** ptr_ptr = &ptr;
return 0;
}
Using pointers to access elements of a dynamically allocated 2D array:
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter number of rows and columns: ";
cin >> rows >> cols;
// Free memory
for (int i = 0; i < rows; ++i) {
delete[] arr[i];
}
delete[] arr;
return 0;
}
Function pointers:
#include <iostream>
using namespace std;
int main() {
void (*ptr)(int, int);
ptr = &add;
ptr(5, 3);
ptr = &subtract;
ptr(5, 3);
return 0;
}
Linkedlist
1. *Creating a singly linked list:*
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int main() {
// Creating nodes
Node* head = createNode(1);
Node* second = createNode(2);
Node* third = createNode(3);
// Linking nodes
head->next = second;
second->next = third;
return 0;
}
prev->next = temp->next;
delete temp;
}
class LinkedList{
public:
Node* head;
LinkedList(){
head=NULL;
}
void insertAtTail(int value){
Node* new_node=new Node(value);
if(head==NULL){
head=new_node;
return;
}
Node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=new_node;
}
void display(){
Node* temp=head;
while(temp!=NULL){
cout<<temp->val<<"->";
temp=temp->next;
}cout<<"NULL"<<endl;
}
};
void deleteAlternateNodes(Node* & head){
Node* curr_node=head;
while(curr_node!=NULL && curr_node->next!=NULL){
Node* temp=curr_node->next;
free(temp);
curr_node=curr_node->next;
}
}
LinkedList ll;
ll.insertAtTail(1);
ll.display();
deleteAlternateNodes(ll.head);
public:
BankAccount(int accNum, string accHolder, double initialBalance) {
accountNumber = accNum;
accountHolderName = accHolder;
balance = initialBalance;
}
void displayBalance() {
cout << "Account Number: " << accountNumber << endl;
cout << "Account Holder: " << accountHolderName << endl;
cout << "Balance: " << balance << endl;
}
};
int main() {
BankAccount account(12345, "John Doe", 1000.0);
account.deposit(500.0);
account.withdraw(200.0);
account.displayBalance();
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Book {
private:
string title;
string author;
bool isAvailable;
public:
Book(string t, string a) : title(t), author(a), isAvailable(true) {}
class Library {
private:
vector<Book> books;
public:
void addBook(const Book& book) {
books.push_back(book);
}
void displayAvailableBooks() {
cout << "Available Books:" << endl;
for (const auto& book : books) {
if (book.getAvailability()) {
cout << book.getTitle() << " by " << book.getAuthor() << endl;
}
}
}
};
int main() {
Library library;
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
library.displayAvailableBooks();
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Employee {
protected:
string name;
double salary;
public:
Employee(string n, double s) : name(n), salary(s) {}
public:
HourlyEmployee(string n, double h, double r) : Employee(n, 0), hoursWorked(h),
hourlyRate(r) {}
int main() {
vector<Employee*> employees;
employees.push_back(new HourlyEmployee("John Doe", 40, 25));
employees.push_back(new MonthlyEmployee("Jane Smith", 3000));
// Cleanup
for (auto emp : employees) {
delete emp;
}
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Item {
private:
string name;
double price;
public:
Item(string n, double p) : name(n), price(p) {}
class ShoppingCart {
private:
vector<Item> items;
public:
void addItem(const Item& item) {
items.push_back(item);
}
void displayCart() {
cout << "Shopping Cart:" << endl;
for (const auto& item : items) {
cout << item.getName() << ": $" << item.getPrice() << endl;
}
}
double calculateTotal() {
double total = 0;
for (const auto& item : items) {
total += item.getPrice();
}
return total;
}
};
int main() {
Item item1("Laptop", 999.99);
Item item2("Phone", 599.99);
ShoppingCart cart;
cart.addItem(item1);
cart.addItem(item2);
cart.displayCart();
cout << "Total: $" << cart.calculateTotal() << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student {
private:
string name;
int grade;
public:
Student(string n, int g) : name(n), grade(g) {}
class School {
private:
vector<Student> students;
public:
void addStudent(const Student& student) {
students.push_back(student);
}
void displayStudents() {
cout << "Students:" << endl;
for (const auto& student : students) {
cout << student.getName() << " - Grade: " << student.getGrade() <<
endl;
}
}
};
int main() {
Student student1("John Doe", 10);
Student student2("Jane Smith", 11);
School school;
school.addStudent(student1);
school.addStudent(student2);
school.displayStudents();
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
class ParkingLot {
private:
vector<bool> parkingSpaces;
public:
ParkingLot(int size) : parkingSpaces(size, false) {}
int findAvailableSpace() {
for (int i = 0; i < parkingSpaces.size(); ++i) {
if (!parkingSpaces[i]) {
return i;
}
}
return -1; // No available space
}
bool parkVehicle() {
int space = findAvailableSpace();
if (space != -1) {
parkingSpaces[space] = true;
cout << "Vehicle parked in space " << space << endl;
return true;
} else {
cout << "No available space" << endl;
return false;
}
}
int main() {
ParkingLot parkingLot(10);
parkingLot.parkVehicle();
parkingLot.parkVehicle();
parkingLot.removeVehicle(1);
parkingLot.parkVehicle();
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Piece {
protected:
int x, y;
public:
Piece(int x, int y) : x(x), y(y) {}
int main() {
Rook rook(1, 1);
Knight knight(2, 1);
return 0;
}