0% found this document useful (0 votes)
138 views

Linked List: Program: Adtstack Implementation Using Array and

The document describes an implementation of an ADT stack using both arrays and linked lists in Java, including classes for StackArray and StackLinkList that implement push, pop, and display methods. The main method creates instances of these classes and uses a menu system to allow the user to perform stack operations and choose between the array and linked list implementations.

Uploaded by

Asina Milas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views

Linked List: Program: Adtstack Implementation Using Array and

The document describes an implementation of an ADT stack using both arrays and linked lists in Java, including classes for StackArray and StackLinkList that implement push, pop, and display methods. The main method creates instances of these classes and uses a menu system to allow the user to perform stack operations and choose between the array and linked list implementations.

Uploaded by

Asina Milas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

PROGRAM: ADTStack IMPLEMENTATION USING ARRAY AND

LINKED LIST

import java.io.*;
import java.util.*;
interface Stack
{
public void push(int n);
public int pop();
public void display();
}
public class ADTStack
{
public static void main(String args[])
{
try
{

int n;
BufferedReader Buf=new BufferedReader(new
InputStreamReader(System.in));
while(true)
{
System.out.println("\n\t\t\t ADT Stack");
System.out.println("\n 1.stack using Arrray \n
2.Stack using link list \n 3.Exit");
System.out.println("\n Enter the choice:");
n=Integer.parseInt(Buf.readLine());
switch(n)
{
case 1:
new StackArray();
break;
case 2:
new StackLinkList();
break;
case 3:
System.out.println("\n Exiting..........");
System.exit(0);
default:
System.out.print("\n Choose the Corrrect
Option.");
}
}
}
catch(Exception e)
{
System.out.println("Error:"+e.getMessage());
}
}
}
class StackArray implements Stack
{
public static int top=-1;
public static int st[]=new int [10];
public StackArray()
{
try
{
int n,a;
BufferedReader Buf=new BufferedReader(new
InputStreamReader(System.in));
while(true)
{
System.out.println("\n\t\t\t 1.Stack Using Array");
System.out.println("\n 1.Push \n 2.Pop using Link List \n 3.Display
Element \n 4.No.of Elements \n 5.MainMenu \n 6.Exit");
System.out.print("\n Enter the choice:");
n=Integer.parseInt(Buf.readLine());
switch(n)
{
case 1:
System.out.print("\n Enter the element to be pushed :");
a=Integer.parseInt(Buf.readLine());
push(a);
break;
case 2:
a=pop();
if(a!=-1)
System.out.println("\n the popped element is :"+a);
break;
case 3:
display();
break;
case 4:
a=top+1;
System.out.println("\n no. of elements in the stack is:"+a);
break;
case 5:
break;
case 6:
System.out.println("\n Exiting..");
System.exit(0);
default:
System.out.print("\n Choose the correct option...");
}
if(n==5)
break;
}
}
catch (Exception e)
{
System.out.println("Error:"+e.getMessage());
}
}
public void push(int n)
{
if(top>=9)
{
System.out.println("\n Stack Over Flow.");
System.out.println("\n The Element("+n+") is not pushed.");
}
else
{
top++;
st[top]=n;
System.out.println("\n The Element("+n+") is pushed.");
}
}
public int pop()
{
if(top==-1)
{
System.out.println("\n Stack Under Flow.");
return -1;
}
else
return st[top--];
}
public void display()
{
if(top==-1)
{
System.out.println("\n Stack is empty");
return;
}
System.out.print("\n the elements of the Stack Array is:");
for(int i=0;i<=top;i++)
{
System.out.print(" "+st[i]+"->");
}
System.out.println("Top\n");
if(top==9)
System.out.println("\n Stack is Full");
}
}
class StackLinkList implements Stack
{
public LinkedList link=new LinkedList();
public StackLinkList()
{
try
{
int n,a;
BufferedReader Buf=new BufferedReader(new
InputStreamReader(System.in));
while(true)
{
System.out.println("\n\t\t\t 1.Stack Using Link List ");
System.out.println("\n 1.Push \n 2.Pop Using Link List \n 3.Display
Elements\n 4.No. of Elements \n 5.MainMenu\n 6.Exit");
System.out.println("\n Enter the choice:");
n=Integer.parseInt(Buf.readLine());
switch(n)
{
case 1:
System.out.print("\n Enter the element to be pushed:");
a=Integer.parseInt(Buf.readLine());
push(a);
break;
case 2:
a=pop();
if(a!=-1)
System.out.println("\n The popped element is:"+a);
break;
case 3:
display();
break;
case 4:
a=link.size();
System.out.println("\n No.of Elements in the Stack is:"+a);
break;
case 5:
break;
case 6:
System.out.println("\n Exiting.....");
System.exit(0);
default:
System.out.print("\n Choose the Correct Option......");
}
if(n==5)
break;
}
}
catch(Exception e)
{
System.out.println("Error:"+e.getMessage());
}
}
public void push(int n)
{
link.add(new Integer(n));
System.out.println("\n the Element("+n+")is pushed.");
}
public int pop()
{
if(link.size()==0)
{
System.out.println("\n Stack Under Flow.");
return -1;
}
else
link.removeLast();
return 1;
}
public void display()
{
if (link.size()==0)
{
System.out.println("\n Stack is Empty");
return;
}
System.out.print("\n the elements of the stack list is :->");
for(int i=0;i<link.size();i++)
{
System.out.print(" "+link.get(i)+"->");
}
System.out.println("Top\n");
}
}
OUTPUT:
PROGRAM:

import java.io.*;
import java.util.*;
class Fibonacci extends Thread
{
private PipedWriter out=new PipedWriter();
public PipedWriter getPipedWriter()
{
return out;
}
public void run()
{
Thread t=Thread.currentThread();
t.setName("Fibonacci");
System.out.println(t.getName()+"Thread started");
int fibo1=0,fibo2=1,fibo=0;
while(true)
{
try
{
fibo=fibo1+fibo2;
if(fibo>100000)
{
out.close();
break;
}
out.write(fibo);
sleep(1000);
}
catch(Exception e)
{
System.out.println("Fibonacci:"+e);
}
fibo1=fibo2;
fibo2=fibo;
}
System.out.println(t.getName()+"Thread exiting");
}
}
class Prime extends Thread
{
private PipedWriter out1=new PipedWriter();
public PipedWriter getPipedWriter()
{
return out1;
}
public void run()
{
Thread t=Thread.currentThread();
t.setName("Prime");
System.out.println(t.getName()+"Thread Started...");
int prime=1;
while(true)
{
try
{
if(prime>100000)
{
out1.close();
break;
}
if(isPrime(prime));
out1.write(prime);
prime++;
sleep(0);
}
catch(Exception e)
{
System.out.println(t.getName()+"Thread exiting...");
System.exit(0);
}
}
}
public boolean isPrime(int n)
{
int m=(int)Math.round(Math.sqrt(n));
if(n==1||n==2)
return true;
for(int i=2;i<=m;i++)
if(n%i==0)
return false;
return true;
}
}
public class PipedIo1
{
public static void main(String []args) throws Exception
{
Thread t=Thread.currentThread();
t.setName("Main");
System.out.println(t.getName()+"Thread Started...");
Fibonacci fibonacci=new Fibonacci();
Prime prime=new Prime();
PipedReader fpr=new PipedReader(fibonacci.getPipedWriter());
PipedReader ppr=new PipedReader(prime.getPipedWriter());
fibonacci.start();
prime.start();
int fib=fpr.read(),prm=ppr.read();
System.out.println("The numbers common to PRIME and FIBONACCI:");
while((fib!=-1)&&(prm!=-1))
{
while(prm<=fib)
{
if(fib==prm)
System.out.println(prm);
prm=ppr.read();
}
fib=fpr.read();
}
System.out.println(t.getName()+"thread exiting");
}
}
OUTPUT:
PROGRAM:

import java.io.*;
class Vehicle{
void test(){}
}
class Bus extends Vehicle{
void test()
{
System.out.print("This is bus");
}
}
class Car extends Vehicle{
void test()
{
System.out.println("This is car");
}
}
class Bike extends Vehicle{
void test()
{
System.out.println("This is bike");
}
}
public class VehicleHierarche{
public static void main(String[]args){
Vehicle[] v=new Vehicle[3];
v[0]=new Bus();
v[1]=new Car();
v[2]=new Bike();
for(int i=0;i<v.length;i++){
v[i].test();
}}}
OUTPUT:

You might also like