Codes (DSA)
Codes (DSA)
Branch - DSAI
Roll No. - 231020438
STACK:
#include<iostream>
using namespace std;
int top=-1;
else{
top++;
stack[top]=n;
cout<<"Element inserted"<<endl;
}
}
else{
for(int i=0;i<=top;i++){
cout<<stack[i]<<' ';
}
cout<<endl;
}
}
int main(){
int s;
cout<<"Enter the size of stack: ";
cin>>s;
int stack[s];
char ans='y';
while(ans=='y'){
int choice;
cout<<"Enter your choice (1,2,3) ";
cin>>choice;
switch(choice){
case 1:
int x;
cout<<"Enter the number to be pushed: ";
cin>>x;
push(stack,s,x);
break;
case 2:
pop(stack,s);
break;
case 3:
display(stack,s);
break;
}
cout<<"Want to perform more operations ? ";
cin>>ans;
}
QUEUE
Queue Implementation:
#include<iostream>
using namespace std;
int rear=0,front=-1;
if(front==s-1){
cout<<"The Queue is full"<<endl;
}
else{
front++;
*(q+front)=n;
cout<<"Element Inserted"<<endl;
}
if(front<rear){
cout<<"The Queue is empty"<<endl;
}
else{
cout<<"The popped element is "<<*(q+rear)<<endl;
rear++;
}
}
if(front<rear){
cout<<"The Queue is empty"<<endl;
}
else{
for(int i=rear;i<=front;i++){
cout<<*(q+i)<<' ';
}
cout<<endl;
}
}
int main(){
int s;
cin>>s;
int queue[s];
char ans='y';
while(ans=='y'){
int choice;
cout<<"Enter your choice (1,2,3) ";
cin>>choice;
switch(choice){
case 1:
int x;
cout<<"Enter number to be pushed: ";
cin>>x;
push(queue,s,x);
break;
case 2:
pop(queue,s);
break;
case 3:
display(queue,s);
break;
}
Circular Queue:
#include <iostream>
#define SIZE 5 /* Size of Circular Queue */
class Queue {
private:
int items[SIZE], front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
// Check if the queue is full
bool isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
if (front == (rear + 1) % SIZE) {
return true;
}
return false;
}
void display() {
// Function to display status of Circular Queue
int i;
if (isEmpty()) {
cout << endl
<< "Empty Queue" << endl;
} else {
cout << "Front -> " << front;
cout << endl
<< "Items -> ";
for (i = front; i != rear; i = (i + 1) % SIZE)
cout << items[i] << "\t";
cout << items[i];
cout << endl
<< "Rear -> " << rear << endl;
}
}
};
int main() {
Queue q;
q.display();
if (elem != -1)
cout << endl
<< "Deleted Element is " << elem << endl;
q.display();
q.enQueue(7);
q.display();
return 0;
}
Deque:
#include<iostream>
using namespace std;
#define SIZE 10
class dequeue {
int a[SIZE], f, r; // SIZE instead of 20
public:
dequeue();
void insert_at_beg(int);
void insert_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void show();
};
dequeue::dequeue() {
f = -1;
r = -1;
}
void dequeue::insert_at_end(int i) {
if (r >= SIZE - 1) {
cout << "\n Insertion is not possible, overflow!!!!";
} else {
if (f == -1) {
f = 0;
r = 0;
} else {
r = r + 1;
}
a[r] = i;
cout << "\nInserted item is: " << a[r];
}
}
void dequeue::insert_at_beg(int i) {
if (f == -1) {
f = 0;
r = 0;
a[f] = i;
cout << "\nInserted element is: " << i;
} else if (f != 0) {
a[--f] = i;
cout << "\nInserted element is: " << i;
} else {
cout << "\nInsertion is not possible, overflow!!!";
}
}
void dequeue::delete_fr_front() {
if (f == -1) {
cout << "Deletion is not possible::dequeue is empty";
} else {
cout << "The deleted element is: " << a[f];
if (f == r) {
f = r = -1; // Reset to empty
} else {
f = f + 1;
}
}
}
void dequeue::delete_fr_rear() {
if (f == -1) {
cout << "Deletion is not possible::dequeue is empty";
} else {
cout << "The deleted element is: " << a[r];
if (f == r) {
f = r = -1;
} else {
r = r - 1;
}
}
}
void dequeue::show() {
if (f == -1) {
cout << "Dequeue is empty";
} else {
for (int i = f; i <= r; i++) {
cout << a[i] << " ";
}
}
}
int main() {
int c, i;
dequeue d;
do { // Corrected loop syntax
cout << "\n 1. Insert at beginning";
cout << "\n 2. Insert at end";
cout << "\n 3. Show";
cout << "\n 4. Deletion from front";
cout << "\n 5. Deletion from rear";
cout << "\n 6. Exit";
cout << "\n Enter your choice: ";
cin >> c;
switch (c) {
case 1:
cout << "Enter the element to be inserted: ";
cin >> i;
d.insert_at_beg(i);
break;
case 2:
cout << "Enter the element to be inserted: ";
cin >> i;
d.insert_at_end(i);
break;
case 3:
d.show();
break;
case 4:
d.delete_fr_front();
break;
case 5:
d.delete_fr_rear();
break;
case 6:
exit(0); // Changed to proper exit
default:
cout << "Invalid choice";
break;
}
} while (c != 6); // Exit on case 6
return 0;
}
LINKED LIST
Implementation:
#include <iostream>
using namespace std;
public:
// Constructor
LinkedList() : head(nullptr) {}
if (temp == nullptr) {
cout << "Index out of bounds!" << endl;
} else {
newNode->next = temp->next;
temp->next = newNode;
}
}
int main() {
LinkedList list;
list.append(10); // 10
list.append(20); // 10 -> 20
list.prepend(5); // 5 -> 10 -> 20
list.insertAt(1, 15); // 5 -> 15 -> 10 -> 20
list.insertAt(0, 1); // 1 -> 5 -> 15 -> 10 -> 20
list.insertAt(4, 25); // 1 -> 5 -> 15 -> 10 -> 25 -> 20
return 0;
}
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
class CircularLinkedList {
public:
Node* head;
if (position == 1) {
prepend(data);
return;
}
if (temp->next == head) {
append(data); // If position is beyond the length, append
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
prev->next = temp->next;
delete temp;
}
int main() {
CircularLinkedList list;
list.prepend(5);
cout << "After prepending: ";
list.printList();
list.insertAtNth(15, 3);
cout << "After inserting at 3rd position: ";
list.printList();
list.deleteNth(4);
cout << "After deleting 4th node: ";
list.printList();
return 0;
}
#include <iostream>
class Node {
public:
int data;
Node* next;
Node* prev;
class DoublyLinkedList {
private:
Node* head;
Node* tail;
public:
DoublyLinkedList() : head(nullptr), tail(nullptr) {}
delete current;
}
int main() {
DoublyLinkedList dll;
dll.insertAtHead(1);
dll.insertAtHead(2);
dll.insertAtTail(3);
dll.insertAtTail(4);
dll.deleteNode(2);
std::cout << "After deletion (forward): ";
dll.displayForward();
return 0;
}