Sunday, 24 April 2022

 Linked List

#include <iostream>

#include <stdio.h>

#include <string>

using namespace std;

 

struct node {

       int info;

       node* next;

};

 

node* start = NULL;

 

void insertatBeg(int data) {

       node* tempnode;

       tempnode = new node;

       tempnode->info = data;

       tempnode->next = start;

       start = tempnode;

 

}

void DelBeg() {

       node* temp;

       temp = start;

       start = temp->next;

       free(temp);

}

void insertAtEnd(int data)

{

       node* obj = new node();

      

      obj->info=data;

       obj->next = NULL;

       if (start == NULL) {

              start = obj;

              cout << "Element Added\n";

       }

       else

       {

              node* temp = start;

              while (temp->next != NULL)

              {

                     temp = temp->next;

              }

              temp->next = obj;

              cout << "Element Added\n";

       }

}

 

 

 

void deleteFromEnd()

{

       node* temp2 = NULL;

       node* temp = start;

       if (temp == NULL)

              cout << "List is Empty!\n";

       else if (temp->next == NULL) {

              start = NULL;

              cout << "Last Element Deleted\n";

       }

       else

       {

              while (temp->next != NULL)

              {

                     temp2 = temp;

                     temp = temp->next;

              }

              temp2->next = NULL;

              delete temp;

              cout << "Last Element Deleted\n";

       }

}

void insertatPos(int data, int pos) {

       node* tempnode, * ptr;

       ptr = start;

       for (int i = 1; i <= pos - 1; i++) {

              if (ptr == NULL) {

                     cout << "create a linkedlist first";

                     return;

                     ptr = ptr->next;

              }

              if (ptr->next == NULL) {

                     insertAtEnd(data);

              }

              else {

                     tempnode = new node;

                     tempnode->next = ptr->next;

                     ptr->next = tempnode;

              }

       }

}

void DelPos(int pos) {

       node* ptr, * temp1;

       int i=1;

       ptr = start;

       while (i <= pos - 1) {

              ptr = ptr->next;

              i++;

       }

       temp1 = ptr->next;

       temp1->next = ptr->next;

       free(temp1);

       }

void traverse()

{

       struct node* ptr;

       ptr = start;

       if (ptr != NULL)

       {

              while (ptr != NULL)

              {

                     cout<<endl<<ptr->info;

                     ptr = ptr->next;

              }

       }

       else

       {

              cout<<"list is empty";

       }

}

 

 

 

int main() {

       int choice, a, b;

       cout << "----------------------Linked List implementation of LIST ADT-----------------------------------\n";

       cout << "1.Insert an element at start\n";

       cout << "2.Insert an elemt at the end of the link list\n";

       cout << "3.Insert an element at position provided by user\n";

       cout << "4.Traverse the list\n";

       cout << "5.Delete an elment from beginning\n";

       cout << "6.Delete an elment from end\n";

       cout << "7.Delete an elment from position provided by user.\n";

       cout << "8.exit\n";

       do

       {

                           cout << "Please Enter Your Choice : ";

              cin >> choice;

 

              if (choice == 1) {

                     cout << "Element:";

                     cin >> a;

                     insertatBeg(a);

                     cout << "\n";

 

                    

                     cout << "\n";

              }

              else if (choice == 2) {

                     cout << "Element:";

                     cin >> a;

                     insertAtEnd(a);

                     cout << "\n";

              }

              else if (choice == 3) {

                     cout << "Position:";

                     cin >> b;

                     cout << "Element:";

                     cin >> a;

                     insertatPos(a, b);

                     cout << "\n";

              }

              else if (choice == 4) {

                     traverse();

                     cout << "\n";

              }

              else if (choice == 5) {

                     DelBeg();

                     cout << "\n";

              }

              else if (choice == 6) {

                     deleteFromEnd();

                     cout << "\n";

              }

              else if (choice == 7) {

                     cout << "Position to delete";

                     cin >> b;

                     DelPos(b);

                     cout << "\n";

              }

              else

                     cout << "Wrong Input! Try again\n\n";

       } while (choice != 8);

       system("pause");

 

 

       cout << "------------------------------------------------------------------------------------------------\n\n";

 

}

Stack

int stack[7] = {}, top = -1, item;

void push(){

        if (top >= 7)

        {

                cout << "Stack OVERFLOW!\n";

        }

        else

        {

                top++;

                cout << "Enter Element to Push : ";

                cin >> stack[top];

                cout << "Element PUSHED!\n";

        }

 

}

void pop(){

        if (top < 0)

        {

                cout << "Stack UNDERFLOW!\n";

        }

        else

        {

                item = stack[top];

                cout << "Element POPPED!\n";

                top--;

        }

}

void display(){

        if (top < 0){

                cout << "Empty Stack\n";

        }

        else{

                for (int i = 0; i <= top; i++)

                {

                        cout << "Element " << i + 1 << " is " << stack[i] << endl;

                }

        }

}

ADD SUBRACT

#include<iostream>

#include<string>

#include<cmath>

#include<stack>

using namespace std;

float scanNum(char ch) {

                int value;

                value = ch;

                return float(value - '0');

}

 

int isOperator(char ch) {

                if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^')

                                return 1;

                return -1;

}

 

int isOperand(char ch) {

                if (ch >= '0' && ch <= '9')

                                return 1;

                return -1;

}

 

int operation(int a, int b, char op) {

                if (op == '+')

                                return b + a;

                else if (op == '-')

                                return b - a;

                else if (op == '*')

                                return b*a;

                else if (op == '/')

                                return b / a;

                else if (op == '^')

                                return pow(b, a);

                else

                                return INT_MIN;

}

 

float postfixEval(string postfix) {

                int a, b;

                stack<float> st;

                string::iterator it;

                for (it = postfix.begin(); it != postfix.end(); it++) {

                                if (isOperator(*it) != -1) {

                                                a = st.top();

                                                st.pop();

                                                b = st.top();

                                                st.pop();

                                                st.push(operation(a, b, *it));

                                }

                                else if (isOperand(*it) > 0) {

                                                st.push(scanNum(*it));

                                }

                }

                return st.top();

}

 

int main() {

                string post = "78+2/5*7+";

                cout << "Postfix Notation : " << post;

                cout << "\n\nValue = " << postfixEval(post) << "\n\n";

                system("pause");

}

 

Clock

#include<math.h>

#include<chrono>

using namespace std;

void main()

{

       int n = 0;

       clock_t c_s, c_e;

       c_s = clock();

       int a[] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40,

              42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 100, 102, 104, 106, 108, 110 };

 

       int l = -2;

       int item;

       cout << "ENTER THE NUMBER YOU WANT TO SEARCH :";

       cout << endl;

       cin >> item;

       for (int i = 0; i < 52; i++)

       {

              if (a[i] == item)

              {

                     l = i;

                     cout << "the index of the searched value is:";

                     cout << endl;

                     cout << i<<endl;

              }

       }

       if (l == -2)

       {

              cout << "item not found";

              cout << endl;

 

       }

       c_e = clock();

       double time;

       time = (double)(c_e - c_s) / (double)CLOCKS_PER_SEC;

       cout << "The time is " << time <<" "<< " Secs"<<endl<<" No of steps "<<n<<endl;

       system("pause");

}

Queue

#include <iostream>

#include <string>

using namespace std;

int Queue[6], num = 6, front = -1, rear = -1;

 

void enqueue(int x)

{

       num = x;

       if (rear == num - 1)

              cout << "Queue is OVERFLOWN!\n\n";

       else

       {

              if (front == -1) {

                     front = 0;

              }

              rear++;

              Queue[rear] = num;

              cout << num << " has been added!\n\n";

       }

}

 

void dequeue()

{

       int num;

       if (rear == -1 || front > rear)

       {

              cout << "Queue is UNDERFLOWN!\n\n";

       }

       else

       {

              num = Queue[front];

              cout << num << " has been removed!\n\n";

              front++;

       }

}

 

void display()

{

       if (front == -1)

              cout << "Queue is EMPTY!\n";

       else {

              cout << "Queue Elements :\n";

              for (int i = front; i <= rear; i++)

              {

                     cout << Queue[i] << "\n";

              }

       }

       cout << "\n";

}