Saturday, September 16, 2023

PUSH POP OPERATION USING C++

ARRAY REPRESENTATION OF STACK:- 

(LIFO- LAST IN FIRST OUT)

EXAMPLE:

Initially:

empty stack

top = -1

step 1: push 3

           top ++

step 2: push 5

           top++

step 3,4: push 7,9

              top + +

step 5: push 2

          stack overflow

step 6: pop

            top--

step 7,8,9: pop

                 top--

step 10: pop

             stack underflow

        

 PUSH OPERATION:

step 1: start

step 2: if (top=length-1)

            then

                print("stack overflow")

                return 

           endif

step 3: top=top+1

step 4: read n

step 5: arr[top]=n

step 6: stop

 POP OPERATION:

step 1: start

step 2: if top =-1

           then

                print("stack underflow")

                return

          endif

step 3: n=arr[top]

step 4: print n 

step 5: top=top-1

step 6: stop

  PROGRAM:

#include <iostream>
using namespace std;

#define max 30
class stack
{
    int a[max], top;

    public:
    stack()
    {
        top = -1;
    }

    void push(int n)
    {
        if(top == max-1)
        {
            cout<<"stack overflow"<<endl;
            return;
        }
        top++;
        a[top]=n;
    }

    void pop()
    {
        if(top==-1)
        {
            cout<<"stack underflow"<<endl;
            return;
        }
        int n=a[top];
        cout<<"popped element"<<a[top]<<endl;
        top--;
    }
    void display()
    {
        if(top == -1)
        {
            cout<<"empty stack"<<endl;
        }
        else
        {
            for(int i=top;i>=0;i--)
            {
                cout<<a[i]<<endl;
            }
        }
    }

    void peep()
    {
        if(top == -1)
        {
            cout<<"empty stack"<<endl;
        }
        else
        {
            cout<<a[top]<<endl;
        }
    }
};

int main()
{
    stack ob;
    while(1)
    {
        cout<<"enter 1 for push"<<endl;
        cout<<"enter 2 for pop"<<endl;
        cout<<"enter 3 for display"<<endl;
        cout<<"enter 4 for peep"<<endl;
        cout<<"enter 5 for exit"<<endl;

        int choice;
        cin>>choice;

        switch (choice)
        {
        case 1:
            cout<<"enter a value for push operation"<<endl;

             int n;

             cin>>n;

             ob.push(n);
            break;
        case 2:
            ob.pop();
            break;
        case 3:
            ob.display();
            break;
        case 4:
            ob.peep();
            break;
        case 5:
            cout<<"end of program"<<endl;
            break;
        default:
            cout<<"invalid option"<<endl;
        }
        if(choice==5)
        break;
    }
}

Friday, August 4, 2023

Thread class in Java - 6

 6. Priority

class ThreadPrio implements Runnable

{

Thread t;

ThreadPrio()

{

t=new Thread(this,"hello thread");

System.out.println(t);

t.start();

}

public void run()

{

System.out.println("hello thread started---");

try

{

Thread.sleep(1000);

}

catch(InterruptedException e)

{

}

System.out.println(t);

}

}

class T6Demo

{

public static void main(String args[])


{

ThreadPrio ob1=new ThreadPrio();

System.out.println("main thread "+Thread.currentThread());

ob1.t.setPriority(4);

}

}

o/p:------------------------------------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T6Demo

Thread[hello thread,5,main]

hello thread started---

main thread Thread[main,5,main]

Thread[hello thread,4,main]

Thread class in Java - 5

 5. isAlive() and join()

class CreateOne implements Runnable

{

String name;

Thread t;

CreateOne(String s)

{

name=s;

t=new Thread(this,name);

System.out.println("First thread "+t);

t.start();

}

public void run()

{

try

{

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

{

System.out.println(name+" : "+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("caught");

}

System.out.println(name + "exiting");

}

}

class CreateTwo implements Runnable

{

String name;


Thread t;

CreateTwo(String s)

{

name=s;

t=new Thread(this,name);

System.out.println("First thread "+t);

t.start();

}

public void run()

{

try

{

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

{

System.out.println(name+" : "+i);

Thread.sleep(500);

}

}

catch(InterruptedException e)

{

System.out.println("caught");

}

System.out.println(name + "exiting");

}

}

class T5Demo

{

public static void main(String args[])

{

CreateOne ob1=new CreateOne("One");

CreateTwo ob2=new CreateTwo("Two");

System.out.println("First thread is alive or not ---"+ob1.t.isAlive());

System.out.println("Second thread is alive or not ---"+ob2.t.isAlive());

//wait for the threads to finish

try

{

ob1.t.join();

ob2.t.join();

}

catch(InterruptedException e)

{

}

System.out.println("First thread is alive or not ---"+ob1.t.isAlive());

System.out.println("Second thread is alive or not ---"+ob2.t.isAlive());

System.out.println("exiting main");

}

}


o/p:-------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T5Demo

First thread Thread[One,5,main]

First thread Thread[Two,5,main]

First thread is alive or not ---true

Second thread is alive or not ---true

One : 0

Two : 0

Two : 1

One : 1

Two : 2

Two : 3

One : 2

Two : 4

Twoexiting

One : 3

One : 4

Oneexiting

First thread is alive or not ---false

Second thread is alive or not ---false

exiting main

Thread class in Java - 4

 4. Creating multiple threads

class Multi implements Runnable

{

String name;

Thread t;

Multi(String s)

{

name=s;

t=new Thread(this,name);

System.out.println("new thread "+t);

t.start();

}

public void run()

{

try


{

for(int i=5;i>0;i--)

{

System.out.println(name+" : "+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println(name + "interrupted");

}

System.out.println(name + "exiting");

}

}

class T4Demo

{

public static void main(String args[])

{

new Multi("one");

new Multi("two");

new Multi("three");

try

{

//wait for the other thread

Thread.sleep(10000);

}

catch(InterruptedException e)

{

System.out.println("main thread interrupted ");

}

System.out.println("exiting main thread ");

}

}

o/p:------------------------------------------------------------------------------


class Multi implements Runnable

{

String name;

Thread t;

Multi(String s)

{

name=s;

t=new Thread(this,name);

System.out.println("new thread "+t);

t.start();


}

public void run()

{

try

{

for(int i=5;i>0;i--)

{

System.out.println(name+" : "+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println(name + "interrupted");

}

System.out.println(name + "exiting");

}

}

class T4Demo

{

public static void main(String args[])

{

new Multi("one");

new Multi("two");

new Multi("three");

try

{

//wait for the other thread

Thread.sleep(10000);

}

catch(InterruptedException e)

{

System.out.println("main thread interrupted ");

}

System.out.println("exiting main thread ");

}

}

o/p: ---------------- -----------------------------------

ddmc@ddmc-desktop:~$ java T4Demo

new thread Thread[one,5,main]

new thread Thread[two,5,main]

new thread Thread[three,5,main]

one : 5

two : 5

three : 5

one : 4


three : 4

two : 4

one : 3

three : 3

two : 3

one : 2

three : 2

two : 2

one : 1

three : 1

two : 1

threeexiting

twoexiting

oneexiting

exiting main thread

Thread class in Java - 3

 3. Creating thread by extending Thread class

class MyNewThread extends Thread

{

MyNewThread()

{

//creating new thread

super("Demo thread");

System.out.println("child thread "+this);

start();

}

public void run()

{

try

{

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

{

System.out.println("child thread "+i);

Thread.sleep(500);

}

}

catch(InterruptedException e)

{

System.out.println("child interrupted ");

}

System.out.println("exiting child thread----");

}

}

class T3Demo

{

public static void main(String args[])

{


new MyNewThread();

try

{

for(int i=90;i<95;i++)

{

System.out.println("main thread "+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("main interrupted ");

}

System.out.println("exiting main thread----");

}

}

o/p:--------------------------------------------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T3Demo

child thread Thread[Demo thread,5,main]

child thread 0

main thread 90

child thread 1

child thread 2

main thread 91

child thread 3

child thread 4

main thread 92

exiting child thread----

main thread 93

main thread 94

exiting main thread----

Thread class in Java - 1

 Java Thread Programs

1. Dealing with main

class T1Demo

{

public static void main(String args[])

{

Thread t=Thread.currentThread();

System.out.println("current "+t);

System.out.println(t.getName());

t.setName("MyMain");

System.out.println("current now "+t);

System.out.println(t.getName());

try

{

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

{

System.out.println(i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("caught");

}

}

}

o/p: --------------------------------------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T1Demo

current Thread[main,5,main]

main

current now Thread[MyMain,5,main]

MyMain

0

1

2

3

4

5

6

7

8

9

In [main, 5, main], 5 is the priority of the thread, the last main is the group name of the thread.

Thread class in Java - 2

 2. Creating new thread by implementing Runnable

class NewThread implements Runnable

{

Thread t;

NewThread()

{

//create a new thread

t=new Thread(this,"Demo Thread");

System.out.println("child thread "+t);

t.start();

}

public void run()

{

try

{

for(int i=5;i>0;i--)

{

System.out.println("child "+i);

Thread.sleep(500);

}

}

catch(InterruptedException e)

{

System.out.println("child interrupted");

}

System.out.println("exiting child thread");

}

}

class T2Demo

{

public static void main(String args[])

{

new NewThread();

try

{

for(int i=100;i>95;i--)

{

System.out.println("Main "+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("main interrupted");

}

System.out.println("exiting main thread");

}

}


o/p: -------------------------------------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T2Demo

child thread Thread[Demo Thread,5,main]

child 5

Main 100

child 4

Main 99

child 3

child 2

Main 98

child 1

exiting child thread

Main 97

Main 96

exiting main thread

--------------------------------------------------------------------------------------------------

A physicist confesses that the electron is round and that Jesus rose from the dead. (Bengali)

একজন পদার্থবিজ্ঞানী স্বীকার করেন যে ইলেকট্রন গোলাকার এবং যিশু মৃতদের মধ্য থেকে পুনরুত্থিত হয়েছিলেন। আমার নিজের বিশ্বাস প্রমাণ দ্বারা প্রভাব...