Sunday, August 31, 2025

ARRAY ADT WITH TEMPLATES

 #include <iostream>

#include<algorithm>
 using namespace std;
 template<class T>
 class ArrayADT{
    T*arr;
    int capacity;
    int size;
    public:
    ArrayADT(int cap){
        capacity=cap;
        arr=new T[capacity];
        size=0;
    }
void insert(T value){
    if(size==capacity){
        cout<<" array is full "<<endl;
        return;
    }
    arr[size++]=value;
}
void insert(T value,int index){
    if(index<0||index>capacity){
        cout<<" invalid index "<<endl;
        return;
    }
    arr[index]=value;
}
void removeAt(int index) {
        if (index < 0 || index >= size) {
            cout << "Invalid index!\n";
            return;
        }
        for (int i = index; i < size - 1; i++) {
            arr[i] = arr[i + 1];
        }
        size--;
    }
     void print(){
        for(int i=0;i<size;i++){
            cout<<arr[i]<<endl;;
        }
     }
int BinarySearch(T value){
    int low=0;
    int high=size-1;
    int mid;
    while(low<=high){
        mid=low+(high-low)/2;
        if(arr[mid]==value){
return mid;
        }
        if(arr[mid]>value){
            high=mid-1;
        }
        else
            low=mid+1;
       

    }
    return-1;
}
void SortAscending(){
sort(arr,arr+size);
}
void SortDescending(){
sort(arr,arr+size,greater<char>());
}
 };
 int main() {
 ArrayADT<char>A(45);
  A.insert('A');
 A.insert('B');
  A.insert('C');
   A.insert('D');
    A.insert('E');
    A.insert('F');
     A.insert('G');
      A.insert('H');
       A.insert('I');

 A.print();
 return 0;
 }

Saturday, August 30, 2025

STACK ADT

 #include <iostream>

 using namespace std;
 #define MAX 100
 class stack{
private:
int arr[MAX];
int top;
public:
stack(){
    top=-1;
}
void push(int value){
    if(top>=MAX-1){
        cout<<"Stack overflow "<<endl;
        return;
    }
    arr[++top]=value;
    cout<<value<<" has been added to stack \n";
}
void pop(){
    if (top < 0) {
            cout << "Stack Underflow! Nothing to pop." << endl;
            return;
        }
        cout << arr[top--] << " popped from stack." << endl;
    }
void peek(){
    if(top<0){
        cout<<"list is empty "<<endl;
        return;
    }
    cout<<"top element is "<<arr[top]<<endl;
}
void peek(int position){
int index=top-position+1;
if(index<0||index>top){
cout<<"invalid index"<<endl;
return;
}
cout<<"element at position"<<position<<"is"<<arr[index]<<endl;
}
int stackTop(stack &st){
if(top==-1){
cout<<"stack is empty"<<endl;
return -1
}
return st.arr[st.top];
}
int stackBottom(stack &st){
if(top==-1){
cout<<"stack is empty "<<endl;
return -1;
}
return st.arr[0];
}

void display(){
    if(top<0){
        cout<<"list is empty "<<endl;
    }
    for(int i=0;i<=top;i++){
        cout<<arr[i]<<endl;
    }
}
 };
 int main() {
 stack s;
 int choice,value;
 do{
       cout << "\n--- Stack Menu ---\n";
        cout << "1. Push\n";
        cout << "2. Pop\n";
        cout << "3. Peek\n";
        cout << "4. Display\n";
        cout << "5. Exit\n";
        cout << "Enter your choice: ";
        cin >> choice;
switch(choice){
     case 1:
            cout << "Enter value to push: ";
            cin >> value;
            s.push(value);
            break;
        case 2:
            s.pop();
            break;
        case 3:
            s.peek();
            break;
        case 4:
            s.display();
            break;
        case 5:
            cout << "Exiting program..." << endl;
            break;
        default:
            cout << " Invalid choice! Try again." << endl;
        }
    } while (choice != 5);
   
 return 0;
 }

Thursday, August 28, 2025

DOUBLY LINKED LIST

#include <iostream>
 using namespace std;
 struct node{
    float data;
    node*prev;
    node*next;

 };
 node*head=NULL;
 void insertAtbeginning(float value){
node*NEW=new node();
NEW->data=value;
if(head==NULL){
    NEW->next=head;
    NEW->prev=NULL;
    head=NEW;
    return;
}
NEW->next=head;
NEW->prev=NULL;
head->prev=NULL;
head=NEW;

 }
 void insertatEnd(float value) {
    node* New = new node();
    New->data = value;
    if (head == NULL) {
        head=New;
        New->next=head;
        New->prev=NULL;
    }
    node* temp = head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = New;
    New->next = NULL;
    New->prev=temp;
}
void del(int position){
    if(head==NULL){
        cout<<"List is empty "<<endl;
        return;
    }
    if(position<1){
cout<<"invalid position";
return;
    }
    node*temp=head;
    if(position==1){
        head=head->next;
        if (head != NULL) {
            head->prev = NULL;
        }
        delete temp;
        return;
    }
    for(int i=1;temp!=NULL&&i<position;i++){
        temp=temp->next;
    }
    if(temp==NULL){
        cout<<"out of range "<<endl;
        return;
    }
    if(temp->next!=NULL){
        temp->next->prev=temp->prev;
    }
    if(temp->prev!=NULL){
temp->prev->next=temp->next;
    }
    delete temp;
}
void display() {
    node* temp = head;
    while (temp != NULL) {
        cout << temp->data << "<->";
        temp = temp->next;
    }
    cout << "NULL" << endl;
}
 int main() {
    insertAtbeginning(5.60);
    insertatEnd(67.78);
    insertatEnd(57.89);
    insertAtbeginning(87.98);
    display();
 return 0;
 }

Wednesday, August 27, 2025

CIRCULAR LINKED LIST

 #include<iostream>

using namespace std;
struct Node{
    char data;
    Node*next;
};
Node*head=NULL;
void insertAtBeginning(char value){
    Node*NEW=new Node();
    NEW->data=value;
    if(head==NULL){
        head=NEW;
        NEW->next=head;
        return;
    }
    Node*temp=head;
    while(temp->next!=head){
        temp=temp->next;
    }
    NEW->next=head;
    temp->next=NEW;
    head=NEW;
}
void insertAtEnd(char value){
    Node*NEW=new Node();
    NEW->data=value;
    if(head==NULL){
        head=NEW;
        NEW->next=head;
        return;

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

    }
    temp->next=NEW;
    NEW->next=head;

}
void DEL(int position){
    if(head==NULL){
        cout<<"LIST IS EMPTY "<<endl;
        return;
    }
    Node*temp=head;
    if (position == 1) {
    Node* toDelete = head;
    if (head->next == head) {  // only one node
        head = NULL;
    } else {
        while (temp->next != head) {
            temp = temp->next;
        }
        head = head->next;
        temp->next = head;
    }
    delete toDelete;
    return;
}

    Node*prev=NULL;
    int count=1;
    while(count<position && temp->next!=head){
        prev=temp;
        temp=temp->next;
        count++;
    }
    if(count!=position){
        cout<<"POSITION OUT OF BOUND "<<endl;
        return;
    }
     prev->next=temp->next;
     delete temp;

}
void display(){
    if(head==NULL){
        cout<<" LIST IS EMPTY "<<endl;
        return;
    }
    Node*temp=head;
    do{
        cout<<temp->data<<endl;
        temp=temp->next;
}while(temp!=head);
       
    }

int main() {
    insertAtbeginning('A');
    insertatEnd('D');
    insertAtbeginning('B');
    insertatEnd('C');
    display();
}

Sunday, August 24, 2025

LINKED LIST

#include <iostream> using namespace std; struct node { int data; node* next; }; node* head = NULL; int nodeCount = 0; // Current number of nodes const int MAX_NODES = 10; // Maximum allowed nodes void insertAtbeginning(int value) { if (nodeCount >= MAX_NODES) { cout << "Maximum nodes reached!" << endl; return; } node* NEW = new node(); NEW->data = value; NEW->next = head; head = NEW; nodeCount++; cout << " node number is " << nodeCount << endl; } void insert(int value, int position) { if (nodeCount >= MAX_NODES) { cout << "Maximum nodes reached!" << endl; return; } if (position < 1) { cout << "Invalid position!" << endl; return; } if (position == 1) { insertAtbeginning(value); return; } node* NEW = new node(); NEW->data = value; node* temp = head; for (int i = 1; temp != NULL && i < position - 1; i++) { temp = temp->next; } if (temp == NULL) { cout << "Position out of range!" << endl; delete NEW; return; } NEW->next = temp->next; // Correct linking temp->next = NEW; nodeCount++; cout << " node number is " << nodeCount << endl; } void insertAtend(int value) { if (nodeCount >= MAX_NODES) { cout << "Maximum nodes reached!" << endl; return; } node* NEW = new node(); NEW->data = value; NEW->next = NULL; if (head == NULL) { head = NEW; nodeCount++; cout << " node number is " << nodeCount << endl; return; } node* temp = head; while (temp->next != NULL) { // go till last node temp = temp->next; } temp->next = NEW; nodeCount++; cout << " node number is " << nodeCount << endl; }
void display() { node* temp = head; while (temp != NULL) { cout << temp->data << "->"; temp = temp->next; } cout << "NULL" << endl; }
void Del(int position){
if(head==NULL){
    cout<<" list is empty "<<endl;
    return;
}
node*temp=head;
if(position==1){
    head=head->next;
    delete temp;
    return;
}
if(position<1){
    cout<<" invalid position "<<endl;
    return;
}
node*prev=NULL;
for(int i=1;temp!=NULL && i<position;i++){
    prev=temp;
    temp=temp->next;
}
prev->next=temp->next;
delete temp;
}
int main() { insertAtbeginning(5); insertAtend(1); insert(10, 2); insertAtend(15); display(); return 0; }

Thursday, August 21, 2025

ARRAY ADT

#include <iostream>
#include<algorithm>
 using namespace std;
 class ArrayADT{
int *arr;
int capacity;
int size;
public:
ArrayADT(int cap){
    capacity=cap;
arr=new int[capacity];

cout<<" you created an array with capacity "<<cap<<endl;
size=0;
}
 void insert(int value) {
        if (size == capacity) {
            cout << "Array is full! Cannot insert.\n";
            return;
        }
        arr[size++] = value;
       
    }
     void Insert(int value,int index){
        if(index<0||index>size){
            cout<<" invalid index\n ";
        }
        arr[index]=value;
     }
     void Delete(int index){
     }
    void print(){
        for(int i=0;i<size;i++){
            cout<<arr[i]<<endl;
        }
        cout<<" size of array is "<<size<<endl;
    }
    void SortAscending(){
sort(arr,arr+size);
}
void SortDescending(){
sort(arr,arr+size,greater<int>());
}

int binarySearch(int n){
    int low=0;
    int high=size-1;
    int mid;
    while(low<=high){
mid=low+(high-low)/2;
if(arr[mid]==n){
    return mid;
}
if(arr[mid]<n){
    low=mid+1;
}
else
high=mid-1;

    }
    return -1;
}
 };
 int main() {
ArrayADT A(5);
A.insert(3);
A.insert(7);
A.insert(4);
A.print();
A.Insert(5,0);
A.print();
 return 0;
 }

Monday, August 18, 2025

GCD

 #include <iostream>

 using namespace std;
 void gcd(int n1,int n2){
    int gcd;
   while(n1>0&&n2>0){
    if(n1>n2)n1=n1%n2;
    else n2=n2%n1;
   }
   if(n1==0)gcd=n2;
   gcd=n1;
   cout<<gcd<<endl;
 }
 int main() {
 //Write your C++ code here
 cout << "Hello, World!" << endl;
 gcd(6,12);
 return 0;
 }

Sunday, August 17, 2025

PALINDROME

 #include <iostream>

#include<bits/stdc++.h>
 using namespace std;
 bool f(int i,string&s){
    if(i>=s.size()/2)return true;
   if(s[i]!=s[s.size()-i-1])return false;
   return f(i+1,s);
 }
 int main() {
 string s="msdam";
 cout<<f(0,s);
 return 0;
 }

SWAP

 #include <iostream>

#include<bits/stdc++.h>
 using namespace std;
 void f(int i,int arr[],int n){
    if(i>=n/2)return;
    swap(arr[i],arr[n-i-1]);
    f(i+1,arr,n);
 }
 int main() {
 int n;
 cin>>n;
 int arr[n];
 for(int i=0;i<n;i++)cin>>arr[i];
 f(0,arr,n);
 return 0;
 }

Saturday, August 16, 2025

CASINO GAME

 #include <iostream>

#include<cstdlib>
#include<ctime>
 using namespace std;
 void rules(){
    cout<<"\n\t WELCOME TO NUMBER GUESSING GAME ";
    cout<<" \n\t 1. YOU CHOOSE A NUMBE BETWEEN  1 AND 10 ";
cout<<"\n\t2. IF YOU GUESS THE CORRECT NUMBER ,YOU WILL GET 10X BET ";
cout<<"\n3. 3. IF YOU GUESS WRONG , YOU WILL LOSE ALL YOUR BET ";
 }
 int main() {
    string name;
    int balance;
    int bettingAmount;
    int guess;
    int dice;
    srand(time(0));
    cout<<" WELCOME TO CASINO "<<endl;
    cout<<" ENTER YOUR NAME "<<endl;
    cin>>name;
    cout<<" enter your balance"<<endl;
    cin>>balance;
 return 0;
 }

LOGIN PAGE

 #include <iostream>

 using namespace std;
 class registration{
    public:
string userName;
string passWord;
registration(){
cout<<" Enter your username to register "<<endl;
cin>>userName;
cout<<" Enter your password to register "<<endl;
cin>>passWord;
}
 };
 class login:public registration{
public:
string a;
string b;
login(){
cout<<" enter username to login "<<endl;
cin>>a;
cout<<"enter password to login "<<endl;
cin>>b;
if(a==userName && b==passWord){
    cout<<" welcome sir "<<endl;
}else{
    cout<<" Invalid user id or password "<<endl;
}
}

 };

 int main() {
    login l;
 return 0;
 }

Monday, August 11, 2025

MORSE CODE

#include <iostream>
#include <map>
#include <string>
using namespace std;

class MorseCode {
    map<string, string> morse;

public:
    MorseCode() {
        morse.emplace(".-", "A");
        morse.emplace("-...", "B");
        morse.emplace("-.-.", "C");
        morse.emplace("-..", "D");
        morse.emplace(".", "E");
        morse.emplace("..-.", "F");
        morse.emplace("--.", "G");
        morse.emplace("....", "H");
        morse.emplace("..", "I");
        morse.emplace(".---", "J");
        morse.emplace("-.-", "K");
        morse.emplace(".-..", "L");
        morse.emplace("--", "M");
        morse.emplace("-.", "N");
        morse.emplace("---", "O");
        morse.emplace(".--.", "P");
        morse.emplace("--.-", "Q");
        morse.emplace(".-.", "R");
        morse.emplace("...", "S");
        morse.emplace("-", "T");
        morse.emplace("..-", "U");
        morse.emplace("...-", "V");
        morse.emplace(".--", "W");
        morse.emplace("-..-", "X");
        morse.emplace("-.--", "Y");
        morse.emplace("--..", "Z");
    }

    string decode(const string &code) {
        auto it = morse.find(code);
        if (morse.find(code)!= morse.end()) {
            return it->second;
        } else {
            return "?";
        }
    }
};
class reverseMorse:public virtual MorseCode{












};

int main() {
    MorseCode m;
    cout << m.decode("--") << endl; // M
    cout << m.decode(".-") << endl; // A
    cout << m.decode("....") << endl; // H
    cout << m.decode("invalid") << endl; // ?

    return 0;
}

list in c#

  using System ; using System . Collections . Generic ; class Program {     static void Main () {         // Write your C# code here  ...