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

Sec3 Program

The document contains code examples for implementing common data structures in Java including Stack, Queue, Linked List, ArrayList, and HashSet. The Stack, Queue, and Linked List classes implement push, pop, enqueue, dequeue and add/remove methods. The ArrayList class stores different object types. The HashSet class uses the Team object's hashCode and equals methods to eliminate duplicates.

Uploaded by

paku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Sec3 Program

The document contains code examples for implementing common data structures in Java including Stack, Queue, Linked List, ArrayList, and HashSet. The Stack, Queue, and Linked List classes implement push, pop, enqueue, dequeue and add/remove methods. The ArrayList class stores different object types. The HashSet class uses the Team object's hashCode and equals methods to eliminate duplicates.

Uploaded by

paku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

*Stack:-

class StackDs

//We declared stack size & top private so that only push & pop methods can
access them.

// if they are public any other method can access them and may change its
value.

// So that will be a problem during push & pop operation.

private Object[] stack;

private int size=0;

private int top=-1;

public StackDs(int initSize)

stack=new Object[initSize];

public void push(Object ele)

if(top<stack.length-1)

stack[++top]=ele;

size++;

}
else

System.out.println("Stack is Full...");

public Object pop()

if(top!=-1)

Object val=stack[top];

top--;

size--;

return val;

else

return new IndexOutOfBoundsException();

public int size()

{
return size;

class St

public static void main(String[] args)

StackDs s1=new StackDs(5);

System.out.println("Size = "+s1.size());

s1.push(10);

s1.push(20);

s1.push(30);

s1.push(40);

s1.push(50);

System.out.println("Size = "+s1.size());

while(s1.size()>0)

System.out.println(s1.pop());

System.out.println("Size = "+s1.size());

}
*Queue:-
class QueueDs

private Object[] queue;

private int size=0;

private int front=-1;

private int end=-1;

public QueueDs(int initSize)

queue=new Object[initSize];

public void enQueue(Object ele)

if(end<queue.length-1)

queue[++end]=ele;

size++;

front=0;

System.out.println(ele+" added to Queue.");

// System.out.println("front = "+front);

// System.out.println("end = "+end);
}

else

System.out.println("Queue is Full...");

public Object deQueue()

if(end!=-1)

Object val=queue[front];

front++;

size--;

if(size==0)

front=-1;

end=-1;

return val;

else

{
throw new IndexOutOfBoundsException();

public int size()

return size;

class Q

public static void main(String[] args)

QueueDs q1=new QueueDs(4);

System.out.println("Size = "+q1.size());

q1.enQueue(10);

q1.enQueue(20);

q1.enQueue(30);

q1.enQueue(40);

System.out.println("Size = "+q1.size());

while(q1.size()>0)

{
System.out.println(q1.deQueue());

System.out.println("Size = "+q1.size());

*Linked List:-
class Node

Object data;

Node nxt;

public Node(Object data)

this.data=data;

class LinkedListDs

private Node head=null;

private Node tail=null;

private int size=0;


public boolean add(Object data)

Node nd=new Node(data);

size++;

if(head==null)

head=nd;

tail=nd;

tail.nxt=nd;

tail=nd;

return true;

public boolean add(Object data,int pos)

Node nd=new Node(data);

size++;

if(pos==0 && head==null)

head=nd;

tail=nd;
return true;

else if(pos==0 && head!=null)

nd.nxt=head;

head=nd;

tail=nd.nxt;

return true;

else

Node temp1=head.nxt;

Node temp2;

int i=1;

while(i<pos-1)

temp1=temp1.nxt;

i++;

temp2=temp1.nxt;

temp1.nxt=nd;

nd.nxt=temp2;

return true;
}

public Object getFirst()

Object val=head.data;

head=head.nxt;

size--;

return val;

public Object getLast()

Node temp1=head;

// Object val;

// while(size!=0)

// {

// for(int i=0;i<size-1;i++)

// {

// temp1=temp1.nxt;

// }

// val=temp1.data;

// System.out.println(val);
// size--;

// temp1=head;

// }

int i=0;

while(i<size-1)

temp1=temp1.nxt;

i++;

Object val=temp1.data;

size--;

return val;

public Object get(int pos)

Object val;

if(pos==0)

val=head.data;

return val;

Node temp=head.nxt;
int i=1;

while(i<pos)

temp=temp.nxt;

i++;

val=temp.data;

return val;

public boolean replace(Object val,int pos)

if(pos==0)

head.data=val;

return true;

Node temp=head.nxt;

int i=1;

while(i<pos)

temp=temp.nxt;

i++;
}

temp.data=val;

return true;

public boolean delete(int pos)

if(pos==0)

head=head.nxt;

head.nxt=null;

size--;

System.gc();

return true;

Node temp1=head.nxt;

Node temp2;

int i=1;

while(i<pos-1)

temp1=temp1.nxt;

i++;

}
temp2=temp1.nxt;

temp1.nxt=temp2.nxt;

temp2.nxt=null;

size--;

System.gc();

return true;

public int size()

return size;

class Sll

public static void main(String[] args)

LinkedListDs l1=new LinkedListDs();

System.out.println(l1.add(10));

System.out.println(l1.add(20));

System.out.println(l1.add(30));

System.out.println(l1.add(40));

System.out.println(l1.add(222,2));
// System.out.println(l1.replace(50,3));

// l1.delete(2);

// System.out.println("-----STACK-----");

while(l1.size()!=0)

System.out.println(l1.getLast());

// System.out.println("-----QUEUE-----");

//l1.getLast();

// System.out.println("-----GetPos-----");

// System.out.println(l1.get(2));

*ArrayList:-
import java.util.ArrayList;

class Mobile

String brand;

double price;
String color;

public Mobile(String brand,double price,String color)

this.brand=brand;

this.price=price;

this.color=color;

@Override

public String toString()

return brand+" "+price+" "+color;

class Shoes

String brand;

double price;

int size;

public Shoes(String brand,double price,int size)

{
this.brand=brand;

this.price=price;

this.size=size;

@Override

public String toString()

return brand+" "+price+" "+size;

class ArList

public static void showCart(ArrayList al)

for(int i=0;i<al.size();i++)

System.out.println(al.get(i));

public static void main(String[] args)

{
ArrayList item=new ArrayList();

Mobile m1=new Mobile("Samsung",13990,"Blue");

Mobile m2=new Mobile("OnePlus",40090,"Black");

Shoes s1=new Shoes("Puma",2250,7);

Shoes s2=new Shoes("Reebok",3000,6);

item.add(m1);

item.add(m2);

item.add(s1);

item.add(s2);

showCart(item);

*HashSet:-

import java.util.HashSet;

import java.util.Iterator;

class Team

int id;
String name;

int t20;

public Team(int id,String name,int t20)

this.id=id;

this.name=name;

this.t20=t20;

@Override

public int hashCode()

return id;

@Override

public boolean equals(Object obj)

return this.hashCode()==obj.hashCode();

@Override
public String toString()

return id+" "+name+" "+t20;

class HashsetEx

public static void main(String[] args)

Team t1=new Team(1,"india",4);

Team t2=new Team(2,"Australia",5);

Team t3=new Team(3,"New Zealand",6);

Team t4=new Team(2,"Australia",7);

HashSet hs=new HashSet();

hs.add(t1);

hs.add(t2);//t2.equals(t1)

hs.add(t3);

hs.add(t4);//t4.equals(t2)

Iterator i=hs.iterator();

while(i.hasNext()==true)

{
System.out.println(i.next());

*Treeset:-
import java.util.TreeSet;

import java.util.Iterator;

class Player implements Comparable

String name;

String country;

int ratings;

public Player(String name,String country,int ratings)

this.name=name;

this.country=country;

this.ratings=ratings;

@Override

public int compareTo(Object obj)


{

Player p1=(Player)obj;

int val=p1.ratings-this.ratings;

System.out.println(p1.ratings+"-"+this.ratings+"="+val);

return val;

@Override

public String toString()

return name+" "+country+" "+ratings;

class TreesetEx

public static void main(String[] args)

TreeSet ts=new TreeSet();

ts.add(new Player("Pujara","IND",856));

ts.add(new Player("Williamson","NZ",878));

ts.add(new Player("Smith","AUS",904));

ts.add(new Player("Kohli","IND",910));
Iterator i=ts.iterator();

int rank=1;

while(i.hasNext()==true)

System.out.println(rank+" "+i.next());

rank++;

*PriorityQueue:-
import java.util.PriorityQueue;

import java.util.Comparator;

class SortPriority implements Comparator

@Override

public int compare(Object obj1,Object obj2)

Customer c1=(Customer)obj1;

Customer c2=(Customer)obj2;

int val=c2.priority-c1.priority;
return val;

class Customer

String name;

int priority;

String addr;

public Customer(String name,int priority,String addr)

this.name=name;

this.priority=priority;

this.addr=addr;

@Override

public String toString()

return name+" "+addr+" "+priority;

class PQ2

{
public static void main(String[] args)

SortPriority sp=new SortPriority();

PriorityQueue p1=new PriorityQueue(sp);

p1.add(new Customer("Foram",3,"Bilimora"));

p1.add(new Customer("Binali",2,"Valsad"));

p1.add(new Customer("Dhara",3,"Surat"));

p1.add(new Customer("Piyu",1,"Bilimora"));

while(p1.size()!=0)

System.out.println(p1.poll());

*hashTable:-
import java.util.Hashtable;

import java.util.Set;

import java.util.Iterator;

class Hashtab

public static void main(String[] args)


{

Hashtable ht=new Hashtable();

ht.put("dinga",7878982434l);

ht.put("dinga",9898724351l);

ht.put("manja",3142548);

Set keys=ht.keySet();

Iterator i=keys.iterator();

while(i.hasNext()==true)

System.out.println(ht.get(i.next()));

*SynMethod:-

class Counter

private int val=0;

synchronized public void increament()

val++;
}

synchronized public void decreament()

val--;

synchronized public void showVal()

System.out.println("value="+val);

class Syn1

public static void main(String[] args)

Counter c1=new Counter();

Runnable r1=()->

c1.increament();

c1.showVal();

};
Runnable r2=()->

c1.decreament();

c1.showVal();

};

Thread t1=new Thread(r1);

Thread t2=new Thread(r2);

t1.start();

t2.start();

*SynBlock:-
class Syn2

static String s1=new String("Hello");

public static void main(String[] args)

Runnable r1=()->

synchronized(s1)//StringBuffer is thread-
safe so if we create StringBuffer obj than don't use syn block.
{

s1=s1+"World";

System.out.println(s1);

};

Runnable r2=()->

synchronized(s1)

s1=s1+"Java";

System.out.println(s1);

};

Thread t1=new Thread(r1);

Thread t2=new Thread(r2);

t1.start();

t2.start();

}
*Deadlockhandle:-
class DeadLockHandle

static String s1=new String("Hello");

static String s2=new String("Java");

public static void main(String[] args)

Runnable r1=()->

System.out.println("t1 Starts...");

System.out.println("t1 is waiting to lock s1");

synchronized(s1)

System.out.println("t1 locked s1");

try

System.out.println("t1 going to wait


state...");

s1.wait(2000);

System.out.println("t1 resumed from


wait state...");

}
catch(InterruptedException ie)

ie.printStackTrace();

System.out.println("t1 is waiting to lock s2");

synchronized(s2)

System.out.println("t1 locked s2");

System.out.println("t1 unlocked s2");

System.out.println("t1 unlocked s1");

System.out.println("t1 ends...");

};

Runnable r2=()->

System.out.println("t2 Starts...");

System.out.println("t2 is waiting to lock s2");

synchronized(s2)

{
System.out.println("t2 locked s2");

System.out.println("t2 is waiting to lock s1");

synchronized(s1)

System.out.println("t2 locked s1");

System.out.println("t2 unlocked s1");

System.out.println("t2 unlocked s2");

System.out.println("t2 ends...");

};

Thread t1=new Thread(r1);

Thread t2=new Thread(r2);

t1.start();

t2.start();

}
*Files:-

import java.io.File;

class Folder

public static void main(String[] args)

System.out.println("Main starts...");

String path="D:/FILEDEMO";

File f1=new File(path);

if(f1.exists()==false)

f1.mkdir();

System.out.println("Folder created...");

else

System.out.println("Folder already exists...");

System.out.println("Main ends...");

}
import java.io.File;

import java.io.IOException;

class File1

public static void main(String[] args)

System.out.println("Main starts...");

String path="D:/FILEDEMO/TestOne.txt";

File f1=new File(path);

if(f1.exists()==false)

try

f1.createNewFile();

System.out.println("File created...");

catch(IOException ie)

ie.printStackTrace();

else
{

System.out.println("File already exists...");

System.out.println("Main ends...");

import java.io.File;

import java.io.IOException;

import java.io.FileWriter;

class WFile

public static void main(String[] args)

System.out.println("Main starts...");

String path="D:/FILEDEMO/TestOne.txt";

File f1=new File(path);

try

FileWriter fw=new FileWriter(f1);

fw.write("Hello World...");

fw.flush();

System.out.println("Writing into file is completed...");


}

catch(IOException ie)

ie.printStackTrace();

System.out.println("Main ends...");

import java.io.File;

import java.io.IOException;

import java.io.FileReader;

class RFile

public static void main(String[] args)

System.out.println("Main starts...");

String path="D:/FILEDEMO/TestOne.txt";

File f1=new File(path);

try

FileReader fr=new FileReader(f1);

int len=(int)f1.length();
char c1[]=new char[len];

fr.read(c1);

System.out.println(c1);

catch(IOException e)

e.printStackTrace();

System.out.println("Main ends...");

*Serialization:-
import java.io.FileOutputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

class Email implements Serializable

String subject;

String msg;

public void compose(String subject,String msg)


{

this.subject=subject;

this.msg=msg;

class Main1

public static void main(String[] args)

Email e1=new Email();

e1.compose("greetings","Good Morning");

String path="D:/FILEDEMO/Email.ser";

try

FileOutputStream fos=new FileOutputStream(path);

ObjectOutputStream os=new ObjectOutputStream(fos);

os.writeObject(e1);

System.out.println("Object Serialized....");

catch(Exception e)

e.printStackTrace();

}
}

import java.io.FileInputStream;

import java.io.ObjectInputStream;

class Main2

public static void main(String[] args)

String path="D:/FILEDEMO/Email.ser";

try

FileInputStream fis=new FileInputStream(path);

ObjectInputStream ois=new ObjectInputStream(fis);

Email e=(Email)ois.readObject();

System.out.println(e.subject);

System.out.println(e.msg);

catch(Exception e)

e.printStackTrace();

}
}

*Garbage Collector:-
class Sample //extends Object

public void display()

System.out.println("display() of Sample class...");

@Override

public void finalize()

System.out.println("Object deleted....");

class Main3

public static void main(String[] args)

Sample s=new Sample();

s.display();

s=null;
System.gc();

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

System.out.println(i);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy