├── README.md ├── 01.Single Inheritance.cpp ├── 07.Print upto Nth Element.cpp ├── 06.Print Nth Element.cpp ├── 14.Insertion sort.cpp ├── 12.Bubble sort.cpp ├── 13.Selection sort.cpp ├── 02.Multilevel Inheritance.cpp ├── 04.Hierarchical Inheritance.cpp ├── 03.Multiple Inheritance.cpp ├── 11.Deletion in Array.cpp ├── 08.Linear Search.cpp ├── 09.Binary Search.cpp ├── 10.Insertion in Array.cpp ├── 05.Hybrid_inheritance.cpp ├── 15.Insertion From Beg .cpp ├── 17.Insertion From end .cpp ├── 21.Stack.cpp ├── 22.Queue.cpp ├── 18.Deletion from beginning.cpp ├── 16.Insertion From Position .cpp ├── 19. Deletion from pos.cpp └── 20. Deletion form end.cpp /README.md: -------------------------------------------------------------------------------- 1 | # CU-2nd-sem-Cpp-codes -------------------------------------------------------------------------------- /01.Single Inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class vehicle{ 5 | public: 6 | void display(){ 7 | cout << "This is a vehicle" << endl; 8 | } 9 | }; 10 | 11 | class car : public vehicle{ 12 | 13 | }; 14 | 15 | int main(){ 16 | car obj; 17 | obj.display(); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /07.Print upto Nth Element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fib(int n){ 5 | if(n == 0) return 0; 6 | if(n == 1) return 1; 7 | return fib(n-1) + fib(n-2); 8 | } 9 | 10 | int main(){ 11 | int n; 12 | cout << "Enter the Limit: "; 13 | cin >> n; 14 | 15 | for(int i = 0; i < n; i++) { 16 | cout << fib(i) << " "; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /06.Print Nth Element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fib(int n){ 5 | if(n == 0) return 0; 6 | if(n == 1) return 1; 7 | return fib(n-1) + fib(n-2); 8 | } 9 | 10 | int main(){ 11 | int n; 12 | cout << "Enter the Nth number: "; 13 | cin >> n; 14 | 15 | cout << n << "th number of fibonacci series: "; 16 | cout << fib(n-1) << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /14.Insertion sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | 8 | int arr[n]; 9 | for(int i = 0; i < n; i++){ 10 | cin >> arr[i]; 11 | } 12 | 13 | for(int i = 1; i < n; i++){ 14 | int j = i; 15 | while(arr[j-1] > arr[j] && j > 0){ 16 | swap(arr[j-1],arr[j]); 17 | j--; 18 | } 19 | } 20 | 21 | for(int i = 0; i < n; i++){ 22 | cout << arr[i] << " "; 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /12.Bubble sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | 8 | int arr[n]; 9 | for(int i = 0; i < n; i++){ 10 | cin >> arr[i]; 11 | } 12 | 13 | for(int i = 0; i < n; i++){ 14 | for(int j = 0; j < n-i-1; j++){ 15 | if(arr[j] > arr[j+1]){ 16 | swap(arr[j],arr[j+1]); 17 | } 18 | } 19 | } 20 | 21 | for(int i = 0; i < n; i++){ 22 | cout << arr[i] << " "; 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /13.Selection sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | 8 | int arr[n]; 9 | for(int i = 0; i < n; i++){ 10 | cin >> arr[i]; 11 | } 12 | 13 | for(int i = 0; i < n-1; i++){ 14 | int min = i; 15 | for(int j = i+1; j < n; j++){ 16 | if(arr[j] < arr[min]){ 17 | min = j; 18 | } 19 | } 20 | swap(arr[i],arr[min]); 21 | } 22 | 23 | for(int i = 0; i < n; i++){ 24 | cout << arr[i] << " "; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /02.Multilevel Inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Vehicle { 5 | public: 6 | Vehicle() { 7 | cout << "This is a vehicle" << endl; 8 | } 9 | }; 10 | 11 | class Car : public Vehicle { 12 | public: 13 | Car() { 14 | cout << "This is a car" << endl; 15 | } 16 | }; 17 | 18 | class SportsCar : public Car { 19 | public: 20 | SportsCar() { 21 | cout << "This is a sports car" << endl; 22 | } 23 | }; 24 | 25 | int main() { 26 | SportsCar obj; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /04.Hierarchical Inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Vehicle { 5 | public: 6 | Vehicle() { 7 | cout << "This is a vehicle" << endl; 8 | } 9 | }; 10 | 11 | class Car : public Vehicle { 12 | public: 13 | Car() { 14 | cout << "This is a car" << endl; 15 | } 16 | }; 17 | 18 | class Bike : public Vehicle { 19 | public: 20 | Bike() { 21 | cout << "This is a bike" << endl; 22 | } 23 | }; 24 | 25 | int main() { 26 | Car obj1; 27 | Bike obj2; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /03.Multiple Inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Vehicle { 5 | public: 6 | void show() { 7 | cout << "This is a vehicle" << endl; 8 | } 9 | }; 10 | 11 | class FourWheeler { 12 | public: 13 | void disp() { 14 | cout << "This is a 4-wheeler vehicle" << endl; 15 | } 16 | }; 17 | 18 | class Car : public Vehicle, public FourWheeler { 19 | // Inherits methods from both base classes 20 | }; 21 | 22 | int main() { 23 | Car obj; 24 | obj.show(); 25 | obj.disp(); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /11.Deletion in Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cout << "Enter size of array: "; 7 | cin >> n; 8 | 9 | int ar[n]; 10 | cout << "Enter Elements of array: "; 11 | for(int i = 0; i < n; i++){ 12 | cin >> ar[i]; 13 | } 14 | int pos; 15 | cout << "Enter Position to delete: "; 16 | cin >> pos; 17 | 18 | for(int i = pos - 1 ; i < n; i++){ 19 | ar[i] = ar[i+1]; 20 | } 21 | cout << "Elements of array after Deletion: " << endl; 22 | for(int i = 0; i < n - 1; i++){ 23 | cout << ar[i] << endl; 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /08.Linear Search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cout << "Enter size of array: "; 7 | cin >> n; 8 | 9 | int ar[n], num; 10 | cout << "Enter Elements of array: "; 11 | for(int i = 0; i < n; i++){ 12 | cin >> ar[i]; 13 | } 14 | 15 | cout << "Enter Number to Search: "; 16 | cin >> num; 17 | 18 | for(int i = 0; i < n; i++){ 19 | if(num == ar[i]){ 20 | cout << "Number found at Position: " << i + 1 << endl; 21 | return 0; 22 | } 23 | } 24 | cout << "Number Doesn't Exist in the list." << endl; 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /09.Binary Search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | 8 | int arr[n]; 9 | for(int i = 0; i < n; i++){ 10 | cin >> arr[i]; 11 | } 12 | 13 | sort(arr, arr + n); 14 | 15 | int input; 16 | cin >> input; 17 | 18 | int l = 0, r = n-1, mid; 19 | while(l <= r){ 20 | mid = (l + r) / 2; 21 | if(arr[mid] == input){ 22 | cout << arr[mid] << endl; 23 | return 0; 24 | } 25 | else if(arr[mid] < input){ 26 | l = mid + 1; 27 | } 28 | else{ 29 | r = mid - 1; 30 | } 31 | } 32 | cout << input << " not found" << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /10.Insertion in Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cout << "Enter size of array: "; 7 | cin >> n; 8 | 9 | int arr[n+1]; 10 | cout << "Enter Elements of array: "; 11 | for(int i = 0; i < n; i++){ 12 | cin >> arr[i]; 13 | } 14 | 15 | int pos, x; 16 | cout << "Enter Position : "; 17 | cin >> pos; 18 | cout << "Enter Element : "; 19 | cin >> x; 20 | 21 | for(int i = n; i > pos - 2; i--){ 22 | arr[i] = arr[i-1]; 23 | } 24 | arr[pos-1] = x; 25 | cout << "Elements of arrray after Insertion: " << endl; 26 | for(int i = 0; i < n + 1; i++){ 27 | cout << arr[i] << endl; 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /05.Hybrid_inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Vehicle { 5 | public: 6 | Vehicle() { 7 | cout << "This is a vehicle" << endl; 8 | } 9 | }; 10 | 11 | class FourWheeler : public Vehicle { 12 | public: 13 | FourWheeler() { 14 | cout << "This is a four-wheeler vehicle" << endl; 15 | } 16 | }; 17 | 18 | class Electric { 19 | public: 20 | Electric() { 21 | cout << "This is an electric vehicle" << endl; 22 | } 23 | }; 24 | 25 | class Car : public FourWheeler, public Electric { 26 | public: 27 | Car() { 28 | cout << "This is a car" << endl; 29 | } 30 | }; 31 | 32 | int main() { 33 | Car obj; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /15.Insertion From Beg .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node *next; 8 | node(int value){ 9 | data = value; 10 | next = nullptr; 11 | } 12 | }; 13 | 14 | class singlyll{ 15 | public: 16 | node *head; 17 | 18 | singlyll(){ 19 | head = nullptr; 20 | } 21 | 22 | void insert(int val){ 23 | node *newNode = new node(val); 24 | newNode->next = head; 25 | head = newNode; 26 | } 27 | 28 | void print(){ 29 | node* temp = head; 30 | while (temp != nullptr) { 31 | cout << temp->data << " -> "; 32 | temp = temp->next; 33 | } 34 | cout << "NULL\n"; 35 | } 36 | }; 37 | 38 | int main(){ 39 | singlyll ll; 40 | ll.insert(100); 41 | ll.insert(200); 42 | ll.insert(300); 43 | ll.print(); 44 | } -------------------------------------------------------------------------------- /17.Insertion From end .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node *next; 8 | node(int value){ 9 | data = value; 10 | next = nullptr; 11 | } 12 | }; 13 | 14 | class singlyll{ 15 | public: 16 | node *head; 17 | 18 | singlyll(){ 19 | head = nullptr; 20 | } 21 | 22 | void insert(int val){ 23 | node *newnode = new node(val); 24 | // cout << "Inserted: " << val << endl; 25 | if(head == nullptr){ 26 | head = newnode; 27 | } 28 | else{ 29 | node *temp = head; 30 | while(temp->next != nullptr){ 31 | temp = temp->next; 32 | } 33 | temp->next = newnode; 34 | } 35 | } 36 | 37 | void print(){ 38 | node* temp = head; 39 | while (temp != nullptr) { 40 | cout << temp->data << " -> "; 41 | temp = temp->next; 42 | } 43 | cout << "NULL\n"; 44 | } 45 | }; 46 | 47 | 48 | int main(){ 49 | singlyll ll; 50 | ll.insert(1); 51 | ll.insert(10); 52 | ll.insert(100); 53 | ll.insert(1000); 54 | ll.print(); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /21.Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Stack { 5 | private: 6 | int arr[100]; 7 | int top; 8 | 9 | public: 10 | Stack() { 11 | top = -1; 12 | } 13 | 14 | void push(int val) { 15 | if (top >= 99) { 16 | cout << "Stack Overflow\n"; 17 | return; 18 | } 19 | arr[++top] = val; 20 | } 21 | 22 | void pop() { 23 | if (top < 0) { 24 | cout << "Stack Underflow\n"; 25 | return; 26 | } 27 | top--; 28 | } 29 | 30 | int peek() { 31 | if (top < 0) { 32 | cout << "Stack is Empty\n"; 33 | return -1; 34 | } 35 | return arr[top]; 36 | } 37 | 38 | bool isEmpty() { 39 | return top == -1; 40 | } 41 | 42 | void print() { 43 | for (int i = top; i >= 0; i--) { 44 | cout << arr[i] << " "; 45 | } 46 | cout << "\n"; 47 | } 48 | }; 49 | 50 | int main() { 51 | Stack s; 52 | s.push(10); 53 | s.push(20); 54 | s.push(30); 55 | s.print(); 56 | s.pop(); 57 | s.print(); 58 | 59 | return 0; 60 | } -------------------------------------------------------------------------------- /22.Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Queue { 5 | private: 6 | int arr[100]; 7 | int front, rear; 8 | 9 | public: 10 | Queue() { 11 | front = rear = -1; 12 | } 13 | 14 | void enqueue(int val) { 15 | if (rear >= 99) { 16 | cout << "Queue Overflow\n"; 17 | return; 18 | } 19 | if (front == -1) front = 0; 20 | arr[++rear] = val; 21 | } 22 | 23 | void dequeue() { 24 | if (front == -1 || front > rear) { 25 | cout << "Queue Underflow\n"; 26 | return; 27 | } 28 | front++; 29 | } 30 | 31 | int peek() { 32 | if (front == -1 || front > rear) { 33 | cout << "Queue is Empty\n"; 34 | return -1; 35 | } 36 | return arr[front]; 37 | } 38 | 39 | bool isEmpty() { 40 | return front == -1 || front > rear; 41 | } 42 | 43 | void print() { 44 | for (int i = front; i <= rear; i++) { 45 | cout << arr[i] << " "; 46 | } 47 | cout << "\n"; 48 | } 49 | }; 50 | int main() { 51 | 52 | Queue q; 53 | q.enqueue(1); 54 | q.enqueue(2); 55 | q.enqueue(3); 56 | q.print(); 57 | q.dequeue(); 58 | q.print(); 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /18.Deletion from beginning.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node *next; 8 | node(int value){ 9 | data = value; 10 | next = nullptr; 11 | } 12 | }; 13 | 14 | class singlyll{ 15 | public: 16 | node *head; 17 | 18 | singlyll(){ 19 | head = nullptr; 20 | } 21 | 22 | void insert(int val){ 23 | node *newNode = new node(val); 24 | newNode->next = head; 25 | head = newNode; 26 | } 27 | void deleteFromBeginning() { 28 | if (head == nullptr) return; 29 | node* temp = head; 30 | head = head->next; 31 | delete temp; 32 | } 33 | 34 | void print(){ 35 | node* temp = head; 36 | while (temp != nullptr) { 37 | cout << temp->data << " -> "; 38 | temp = temp->next; 39 | } 40 | cout << "NULL\n"; 41 | } 42 | }; 43 | 44 | int main(){ 45 | singlyll ll; 46 | 47 | ll.insert(100); 48 | ll.insert(200); 49 | ll.insert(300); 50 | ll.deleteFromBeginning(); 51 | ll.insert(400); 52 | ll.deleteFromBeginning(); 53 | ll.insert(500); 54 | ll.print(); 55 | } -------------------------------------------------------------------------------- /16.Insertion From Position .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node *next; 8 | node(int value){ 9 | data = value; 10 | next = nullptr; 11 | } 12 | }; 13 | 14 | class singlyll{ 15 | public: 16 | node *head; 17 | 18 | singlyll(){ 19 | head = nullptr; 20 | } 21 | void insert(int value, int pos){ 22 | if(pos <= 0){ 23 | cout << "Invalid, Position must be 1 or higher." << endl; 24 | return; 25 | } 26 | if(pos == 1){ 27 | node *newnode = new node(value); 28 | newnode->next = head; 29 | head = newnode; 30 | return; 31 | } 32 | 33 | node *newnode = new node(value); 34 | node *temp = head; 35 | 36 | for(int i = 1; i < pos - 1 && temp != nullptr; i++){ 37 | temp = temp->next; 38 | } 39 | newnode->next = temp->next; 40 | temp->next = newnode; 41 | } 42 | 43 | void print(){ 44 | if(head == nullptr){ 45 | cout << "List is Empty" << endl; 46 | } 47 | else{ 48 | node *temp = head; 49 | while(temp != nullptr){ 50 | cout << temp->data << "->"; 51 | temp = temp->next; 52 | } 53 | cout << "NULL" << endl; 54 | } 55 | } 56 | }; 57 | 58 | int main(){ 59 | singlyll ll; 60 | ll.insert(10,1); 61 | ll.insert(20,2); 62 | ll.insert(30,3); 63 | ll.insert(40,1); 64 | ll.insert(50,3); 65 | 66 | ll.print(); 67 | } -------------------------------------------------------------------------------- /19. Deletion from pos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node *next; 8 | node(int value) { 9 | data = value; 10 | next = nullptr; 11 | } 12 | }; 13 | 14 | class singlyll { 15 | public: 16 | node *head; 17 | 18 | singlyll() { 19 | head = nullptr; 20 | } 21 | 22 | void insert(int val) { 23 | node *newNode = new node(val); 24 | newNode->next = head; 25 | head = newNode; 26 | } 27 | 28 | void deleteFromPosition(int pos) { 29 | if (pos <= 0 || head == nullptr) return; 30 | if (pos == 1) { 31 | node* temp = head; 32 | head = head->next; 33 | delete temp; 34 | return; 35 | } 36 | node* temp = head; 37 | for (int i = 1; i < pos - 1 && temp->next; i++) { 38 | temp = temp->next; 39 | } 40 | if (temp->next == nullptr) return; 41 | node* toDelete = temp->next; 42 | temp->next = toDelete->next; 43 | delete toDelete; 44 | } 45 | 46 | void print() { 47 | node* temp = head; 48 | while (temp != nullptr) { 49 | cout << temp->data << " -> "; 50 | temp = temp->next; 51 | } 52 | cout << "NULL\n"; 53 | } 54 | }; 55 | 56 | int main() { 57 | singlyll ll; 58 | 59 | ll.insert(10); 60 | ll.insert(20); 61 | ll.insert(30); 62 | ll.insert(40); 63 | ll.insert(50); 64 | 65 | ll.print(); 66 | 67 | ll.deleteFromPosition(3); 68 | ll.print(); 69 | 70 | ll.deleteFromPosition(1); 71 | ll.print(); 72 | 73 | ll.deleteFromPosition(5); 74 | ll.print(); 75 | 76 | return 0; 77 | } -------------------------------------------------------------------------------- /20. Deletion form end.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node *next; 8 | node(int value) { 9 | data = value; 10 | next = nullptr; 11 | } 12 | }; 13 | 14 | class singlyll { 15 | public: 16 | node *head; 17 | 18 | singlyll() { 19 | head = nullptr; 20 | } 21 | 22 | void insert(int val) { 23 | node *newNode = new node(val); 24 | newNode->next = head; 25 | head = newNode; 26 | } 27 | 28 | void deleteFromPosition(int pos) { 29 | if (pos <= 0 || head == nullptr) return; 30 | if (pos == 1) { 31 | node* temp = head; 32 | head = head->next; 33 | delete temp; 34 | return; 35 | } 36 | node* temp = head; 37 | for (int i = 1; i < pos - 1 && temp->next; i++) { 38 | temp = temp->next; 39 | } 40 | if (temp->next == nullptr) return; 41 | node* toDelete = temp->next; 42 | temp->next = toDelete->next; 43 | delete toDelete; 44 | } 45 | 46 | void deleteFromEnd() { 47 | if (head == nullptr) return; 48 | if (head->next == nullptr) { 49 | delete head; 50 | head = nullptr; 51 | return; 52 | } 53 | node* temp = head; 54 | while (temp->next->next != nullptr) 55 | temp = temp->next; 56 | delete temp->next; 57 | temp->next = nullptr; 58 | } 59 | 60 | void print() { 61 | node* temp = head; 62 | while (temp != nullptr) { 63 | cout << temp->data << " -> "; 64 | temp = temp->next; 65 | } 66 | cout << "NULL\n"; 67 | } 68 | }; 69 | 70 | int main() { 71 | singlyll ll; 72 | 73 | ll.insert(10); 74 | ll.insert(20); 75 | ll.insert(30); 76 | ll.insert(40); 77 | ll.insert(50); 78 | 79 | ll.print(); 80 | 81 | ll.deleteFromPosition(3); 82 | ll.print(); 83 | 84 | ll.deleteFromPosition(1); 85 | ll.print(); 86 | 87 | ll.deleteFromEnd(); 88 | ll.print(); 89 | 90 | return 0; 91 | } --------------------------------------------------------------------------------