0% found this document useful (0 votes)
4 views18 pages

dsa notes

The document contains various C++ code snippets demonstrating data structures and algorithms, including linked lists, memory management, and pointer manipulation. It covers operations such as inserting, deleting, and displaying nodes in a linked list, as well as examples of swapping variables, reversing strings, and managing dynamic arrays. Additionally, it presents design concepts for a bank account management system and a library management system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

dsa notes

The document contains various C++ code snippets demonstrating data structures and algorithms, including linked lists, memory management, and pointer manipulation. It covers operations such as inserting, deleting, and displaying nodes in a linked list, as well as examples of swapping variables, reversing strings, and managing dynamic arrays. Additionally, it presents design concepts for a bank account management system and a library management system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 18

#include <iostream>

using namespace std;


class Node{
public:
int val;
Node* next;

Node(int data){
val=data;
next=NULL;
}
};

void insertAtHead(Node* &head,int val){


Node* new_node =new Node(val);
new_node->next = head;
head=new_node;
}

void insertAtLast(Node* &head,int val){


Node* new_node=new Node(val);
Node* temp=head;
while(temp->next != NULL){
temp=temp->next;
}
temp->next=new_node;
}

void insertAtPosition(Node* &head, int val ,int pos){


if(pos==0){
insertAtHead(head,val);
return;
}
Node* new_node=new Node(val);
Node* temp=head;
int current_pos=0;
while(current_pos!=pos-1){
temp=temp->next;
current_pos++;
}
new_node->next=temp->next;
temp->next=new_node;
}

void display(Node* head){


Node* temp=head;
while(temp!=NULL){
cout<<temp->val<<"->";
temp=temp->next;
}
cout<<endl;
}

void updateAtPos(Node* &head,int val,int pos){


Node* temp=head;
int current_pos=0;
while(current_pos!=pos){
temp=temp->next;
}
temp->val=val;
}

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;

void swap(int* a, int* b) {


int temp = *a;
*a = *b;
*b = temp;
}

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;
}

Reversing a string using pointers:


#include <iostream>
using namespace std;

void reverseString(char* str) {


if (str == nullptr)
return;
char* end = str;
while (*end != '\0')
end++;
end--;
while (str < end) {
char temp = *str;
*str++ = *end;
*end-- = temp;
}
}

int main() {
char str[] = "Hello";
reverseString(str);
cout << "Reversed string: " << str << endl;
return 0;
}

Dynamically allocating memory for an array using pointers:


#include <iostream>
using namespace std;

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;
}

Passing arrays to functions using pointers:


#include <iostream>
using namespace std;

void printArray(int* arr, int size) {


for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printArray(arr, size);
return 0;
}

Finding the length of a string using pointers:


#include <iostream>
using namespace std;

int stringLength(const char* str) {


int length = 0;
while (*str++) {
length++;
}
return length;
}

int main() {
const char* str = "Hello, World!";
cout << "Length of the string: " << stringLength(str) << endl;
return 0;
}

Copying one string to another using pointers:


#include <iostream>
using namespace std;

void stringCopy(char* dest, const char* src) {


while (*src) {
*dest++ = *src++;
}
*dest = '\0';
}

int main() {
const char* src = "Hello";
char dest[20];
stringCopy(dest, src);
cout << "Copied string: " << dest << endl;
return 0;
}

Returning a pointer from a function:


#include <iostream>
using namespace std;

int* createArray(int size) {


int* arr = new int[size];
for (int i = 0; i < size; ++i) {
arr[i] = i + 1;
}
return arr;
}

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;
}

Pointer to Pointer (Double Pointer):


#include <iostream>
using namespace std;

int main() {
int x = 10;
int* ptr = &x;
int** ptr_ptr = &ptr;

cout << "Value of x: " << x << endl;


cout << "Value at ptr: " << *ptr << endl;
cout << "Value at ptr_ptr: " << **ptr_ptr << endl;

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;

int** arr = new int*[rows];


for (int i = 0; i < rows; ++i) {
arr[i] = new int[cols];
for (int j = 0; j < cols; ++j) {
arr[i][j] = i * cols + j;
}
}

cout << "Array elements:" << endl;


for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << arr[i][j] << " ";
}
cout << endl;
}

// Free memory
for (int i = 0; i < rows; ++i) {
delete[] arr[i];
}
delete[] arr;

return 0;
}

Function pointers:
#include <iostream>
using namespace std;

void add(int a, int b) {


cout << "Sum: " << a + b << endl;
}

void subtract(int a, int b) {


cout << "Difference: " << a - b << endl;
}

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;
};

Node* createNode(int data) {


Node* newNode = new Node();
newNode->data = data;
newNode->next = nullptr;
return newNode;
}

int main() {
// Creating nodes
Node* head = createNode(1);
Node* second = createNode(2);
Node* third = createNode(3);

// Linking nodes
head->next = second;
second->next = third;

// Traversing and printing the linked list


Node* current = head;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;

return 0;
}

2. **Inserting a node at the beginning of a linked list:**

void insertAtBeginning(Node* &head, int data) {


Node* newNode = createNode(data);
newNode->next = head;
head = newNode;
}

3. *Inserting a node at the end of a linked list:*

void insertAtEnd(Node* &head, int data) {


Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
return;
}
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}

4. **Deleting a node from a linked list by value:**


void deleteNode(Node* &head, int key) {
Node* temp = head;
Node* prev = nullptr;

if (temp != nullptr && temp->data == key) {


head = temp->next;
delete temp;
return;
}

while (temp != nullptr && temp->data != key) {


prev = temp;
temp = temp->next;
}

if (temp == nullptr) return;

prev->next = temp->next;
delete temp;
}

5. *Finding the length of a linked list:*

int length(Node* head) {


int count = 0;
Node* current = head;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}

6. **Searching for a node in a linked list:**

bool search(Node* head, int key) {


Node* current = head;
while (current != nullptr) {
if (current->data == key) {
return true;
}
current = current->next;
}
return false;
}

7. *Reversing a linked list:*

Node* reverse(Node* head) {


Node* prev = nullptr;
Node* current = head;
Node* next = nullptr;
while (current != nullptr) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}

8. **Finding the middle element of a linked list:**

Node* findMiddle(Node* head) {


if (head == nullptr) return nullptr;
Node* slow = head;
Node* fast = head;
while (fast != nullptr && fast->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}

9. *Detecting a cycle in a linked list:*

bool hasCycle(Node* head) {


if (head == nullptr || head->next == nullptr) return false;
Node* slow = head;
Node* fast = head->next;
while (slow != fast) {
if (fast == nullptr || fast->next == nullptr) return false;
slow = slow->next;
fast = fast->next->next;
}
return true;
}

10. **Merging two sorted linked lists:**

Node* mergeSortedLists(Node* l1, Node* l2) {


if (l1 == nullptr) return l2;
if (l2 == nullptr) return l1;
Node* merged;
if (l1->data < l2->data) {
merged = l1;
merged->next = mergeSortedLists(l1->next, l2);
} else {
merged = l2;
merged->next = mergeSortedLists(l1, l2->next);
}
return merged;
}
class Node{
public:
int val;
Node* next;
Node(int data){
val=data;
next=NULL;
}
};

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);

void deleteduplicatenodes(Node* & head){


Node* curr_node=head;
while(curr_node){
while(curr_node->next && curr_node->val==curr_node->next->val){
Node* temp=curr_node->next;
curr_node->next=curr_node->next->next;
free(temp);
}
curr_node=curr_node->next;
}
}
#Traversing in the reverse order
void reversePrint(Node* head){
if(head==NULL){
return;
}
reversePrint(head->next);
cout<<head->val<<" ";

#delete at kth position in node


void deleteAtPosition(Node* &head,int pos){
if(pos==0){
deleteAtHead(head);
return;
}
int curr_pos=0;
Node* prev=head;
while(curr_pos!=pos-1){
prev=prev->next;
curr_pos++;
}
Node* temp=prev->next;
prev->next=prev->next->next;
free(temp);
}

void deleteAtHead(Node* &head){


Node* temp=head;
head = head->next;
free(temp);
}

void deleteAtTail(Node* &head){


Node* second_last = head;
while(second_last->next->next != NULL){
second_last= second_last->next;
}

Node* temp =second_last->next;


second_last->next=NULL;
free(temp);
}

some important questions

1. *Bank Account Management System:*


- *Question:* Design a system to manage bank accounts with
functionalities like deposit, withdrawal, balance inquiry, and account creation.
- *Solution:*
```cpp
#include <iostream>
using namespace std;
class BankAccount {
private:
int accountNumber;
string accountHolderName;
double balance;

public:
BankAccount(int accNum, string accHolder, double initialBalance) {
accountNumber = accNum;
accountHolderName = accHolder;
balance = initialBalance;
}

void deposit(double amount) {


balance += amount;
}

void withdraw(double amount) {


if (amount <= balance) {
balance -= amount;
} else {
cout << "Insufficient balance" << endl;
}
}

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;
}

2. *Library Management System:*


- *Question:* Design a system to manage a library with features like
adding books, issuing books, returning books, and viewing available books.
- *Solution:*

#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) {}

string getTitle() const {


return title;
}

string getAuthor() const {


return author;
}

bool getAvailability() const {


return isAvailable;
}

void setAvailability(bool available) {


isAvailable = available;
}
};

class Library {
private:
vector<Book> books;

public:
void addBook(const Book& book) {
books.push_back(book);
}

void issueBook(const string& title) {


for (auto& book : books) {
if (book.getTitle() == title && book.getAvailability()) {
book.setAvailability(false);
cout << "Book \"" << title << "\" issued successfully." << endl;
return;
}
}
cout << "Book \"" << title << "\" not available for issue." << endl;
}

void returnBook(const string& title) {


for (auto& book : books) {
if (book.getTitle() == title && !book.getAvailability()) {
book.setAvailability(true);
cout << "Book \"" << title << "\" returned successfully." << endl;
return;
}
}
cout << "Invalid book return." << endl;
}

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;

Book book1("The Great Gatsby", "F. Scott Fitzgerald");


Book book2("To Kill a Mockingbird", "Harper Lee");
Book book3("1984", "George Orwell");

library.addBook(book1);
library.addBook(book2);
library.addBook(book3);

library.displayAvailableBooks();

library.issueBook("The Great Gatsby");


library.returnBook("The Great Gatsby");

return 0;
}

3. *Employee Payroll System:*


- *Question:* Develop a system to manage employee payroll, including
features like adding employees, calculating salaries, and generating payslips.
- *Solution:*

#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) {}

virtual double calculatePay() = 0;

virtual void generatePayslip() {


cout << "Name: " << name << endl;
cout << "Salary: " << salary << endl;
}
};

class HourlyEmployee : public Employee {


private:
double hoursWorked;
double hourlyRate;

public:
HourlyEmployee(string n, double h, double r) : Employee(n, 0), hoursWorked(h),
hourlyRate(r) {}

double calculatePay() override {


salary = hoursWorked * hourlyRate;
return salary;
}

void generatePayslip() override {


cout << "Hourly Employee Payslip" << endl;
Employee::generatePayslip();
}
};

class MonthlyEmployee : public Employee {


public:
MonthlyEmployee(string n, double s) : Employee(n, s) {}

double calculatePay() override {


return salary;
}

void generatePayslip() override {


cout << "Monthly Employee Payslip" << endl;
Employee::generatePayslip();
}
};

int main() {
vector<Employee*> employees;
employees.push_back(new HourlyEmployee("John Doe", 40, 25));
employees.push_back(new MonthlyEmployee("Jane Smith", 3000));

for (auto emp : employees) {


emp->calculatePay();
emp->generatePayslip();
cout << endl;
}

// Cleanup
for (auto emp : employees) {
delete emp;
}

return 0;
}

Apologies for the oversight. Here are the remaining 7 medium-level


Object-Oriented Programming (OOP) questions along with their
code solutions in C++:

4. *Online Shopping System:*


- *Question:* Design an online shopping system with functionalities
like adding items to cart, checkout, and payment processing.
- *Solution:*

#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) {}

string getName() const {


return name;
}

double getPrice() const {


return price;
}
};

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;
}

5. *School Management System:*


- *Question:* Create a system to manage a school with features like
adding students, assigning courses, managing grades, and generating reports.
- *Solution:* (Assuming simplified version with basic functionalities)

#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) {}

string getName() const {


return name;
}

int getGrade() const {


return grade;
}
};

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;
}

6. *Parking Lot Management System:*


- *Question:* Develop a system to manage a parking lot with
functionalities like vehicle entry, vehicle exit, and parking space availability.
- *Solution:* (Assuming simplified version with basic functionalities)

#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;
}
}

bool removeVehicle(int space) {


if (space >= 0 && space < parkingSpaces.size() && parkingSpaces[space]) {
parkingSpaces[space] = false;
cout << "Vehicle removed from space " << space << endl;
return true;
} else {
cout << "Invalid space or no vehicle parked" << endl;
return false;
}
}
};

int main() {
ParkingLot parkingLot(10);
parkingLot.parkVehicle();
parkingLot.parkVehicle();
parkingLot.removeVehicle(1);
parkingLot.parkVehicle();

return 0;
}

7. *Chess Game Implementation:*


- *Question:* Implement a basic version of a chess game with
features like moving pieces, capturing pieces, and determining checkmate.
- *Solution:* (Assuming simplified version with basic functionalities)

#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) {}

virtual bool isValidMove(int newX, int newY) = 0;

virtual void move(int newX, int newY) {


if (isValidMove(newX, newY)) {
x = newX;
y = newY;
} else {
cout << "Invalid move" << endl;
}
}
};

class Rook : public Piece {


public:
Rook(int x, int y) : Piece(x, y) {}

bool isValidMove(int newX, int newY) override {


return (newX == x || newY == y);
}
};

class Knight : public Piece {


public:
Knight(int x, int y) : Piece(x, y) {}

bool isValidMove(int newX, int newY) override {


int dx = abs(newX - x);
int dy = abs(newY - y);
return (dx == 1 && dy == 2) || (dx == 2 && dy == 1);
}
};

int main() {
Rook rook(1, 1);
Knight knight(2, 1);

rook.move(5, 1); // Invalid move


knight.move(4, 3); // Valid move

return 0;
}

You might also like