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

2379

Uploaded by

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

2379

Uploaded by

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

7

public class Node


{
String data;
int prn;
Node next;
Node(String str,int p)
{
data=str;
prn=p;
}
}
class LinkedPriorityQueue
{
Node head;
public void insert (String item,int pkey)
{
Node newNode=new Node(item,pkey);
int k;
if(head==null)
k=1;
else if(newNode.prn<head.prn)
k=2;
else
k=3;
switch(k)
{
case 1: head=newNode;
head.next=null;
break;
case 2: Node oldHead=head;
head=newNode;
newNode.next=oldHead;
break;
case 3: Node p=head;
Node prev=p;
Node nodeBefore=null;
while(p!=null)
{
if(newNode.prn<p.prn)
{
nodeBefore=p;
break;
}
else
{
prev=p;
p=p.next;
}
}
newNode.next=nodeBefore;
prev.next=newNode;
}
}

public Node delete()


{
if( isEmpty() )
{
System.out.println("Queue is empty");
return null;
}
else
{
Node tmp = head;
head = head.next;
return tmp;
}
}

public void displayList()


{
Node p = head;
System.out.print("\nQueue: ");
while( p != null )
{
System.out.print(p.data+"(" +p.prn+ ")" + " ");
p = p.next;
}
System.out.println();
}
public boolean isEmpty()
{
return (head == null);
}
public Node peek()
{
return head;
}
}
class LinkedPriorityQueueDemo
{
public static void main(String[] args)
{
LinkedPriorityQueue pq = new LinkedPriorityQueue();
Node item;
pq.insert("Babu", 3);
pq.insert("Nitin", 2);
pq.insert("Laxmi", 2);
pq.insert("Kim", 1);
pq.insert("Jimmy", 3);
pq.displayList();
item = pq.delete();
if( item != null )
System.out.println("delete():" + item.data+"("+item.prn+")");
pq.displayList();
pq.insert("scott",2);
pq.insert("Anu", 1);
pq.insert("Lehar", 4);
pq.displayList();
}
}

2
class LinkedList
{
class Node
{
Object data; //data item
Node next; //refers to next node in the list

Node(Object d) //constructor
{
data=d;
} // ,,next is automatically set to null
}

Node head; // head referes to first node


Node p; // p refers to current node
int count; //current number of nodes

public void createList(int n) //create 'n' nodes


{
p=new Node(11); //create first node
head=p; //assign mem.address of 'p' to head
for(int i=1;i<n;i++) //create 'n-1' nodes
p=p.next=new Node(11+11*i);
count=n;
}
public void insertFirst(Object item)//insert at the begining of
list
{
p=new Node(item);//create new node
p.next=head;//new node refers to old head
head=p; //new head refers to nea node
count++;
}
public void insertAfter(Object item ,Object key)
{
p=find(key);//get location of the key item"
if(p==null)
System.out.println(key+"key is not found");
else
{
Node q=new Node(item); // create new node
q.next=p.next; // new node next refers to p.next
p.next=q;//p.next refers to new node
count++;
}
}
public Node find(Object key)
{
p=head;
while(p!=null) //start begining of list until end of list
{
if(p.data==key)
return p;
p=p.next;//move to next node
}
return null; // of key search is unsuccessful, return null
}
public Object deleteFirst() //delete first node
{
if(isEmpty())
{
System.out.println("list is empty: no deletion");
return null;
}
Node tmp=head; //tmp saves reference to head
head=tmp.next;
count--;
return tmp.data;
}
public Object deleteAfter(Object key) // delete node after key item
{
p=find(key); // p="location of key node"
if(p==null)
{
System.out.println(key+"key is not found");
return null;
}
if(p.next==null) //if(there is no node after key node)
{
System.out.println("No deletion");
return null;
}
else
{
Node tmp=p.next; //save node after key node
p.next=tmp.next; //point to next of node deleted
count--;
return tmp.data; // return deleted node
}
}
public void displayList()
{
p=head; // assign mem.addressn of 'heap' to 'p'
System.out.println("\n Linke List:");
while(p!=null) //start at begining of list until end of
list
{
System.out.println(p.data+"->"); //print data
p=p.next;//move to next node
}
System.out.println(p); //print 'null'
}
public boolean isEmpty() //true if list is empty
{
return (head==null);
}
public int size()
{
return count;
}

}// end of linked list

class LinkedlistDemo
{
public static void main(String[] args)
{
LinkedList list=new LinkedList(); //create list

list.createList(4); // create 4 nodes


list.displayList();
list.insertFirst(55); //insert 55 as node
list.displayList();

list.insertAfter(66,33); //insert 66 after 33


list.displayList();

Object item=list.deleteFirst(); //delete first node


if(item!=null)
{
System.out.println("deleteFirst():"+item);
list.displayList();
}
item=list.deleteAfter(22);//delete a node after node(22)
if(item!=null)
{
System.out.println("deleteAfter(22):"+item);
list.displayList();
}
System.out.println("size():"+list.size());
}
}

class Node

{
Object data;
Node left;
Node right;
Node( Object d ) // constructor
{ data = d; }
}
class BinaryTree
{ Object tree[];
int maxSize;
java.util.LinkedList<Node> que =
new java.util.LinkedList<Node>();
BinaryTree( Object a[], int n ) // constructor
{ maxSize = n;
tree = new Object[maxSize];
for( int i=0; i<maxSize; i++ )
tree[i] = a[i];
}
public Node buildTree( int index )
{ Node p = null;
if( tree[index] != null )
{ p = new Node(tree[index]);
p.left = buildTree(2*index+1);
p.right = buildTree(2*index+2);
}
return p;
}
public void levelorder(Node p)
{
que.addLast(p);
while( !que.isEmpty() )
{
p = que.removeFirst();
System.out.print(p.data + " ");
if(p.left != null)
que.addLast(p.left);
if(p.right != null)
que.addLast(p.right);
}
}
} // end of BinaryTree class
////////////////////////
LevelOrderTraversal.java //////////////////////
class LevelOrderTraversal
{
public static void main(String args[])
{
Object arr[] = {'E', 'C', 'G', 'A', 'D', 'F', 'H',
null,'B', null, null, null, null,
null, null, null, null, null, null};
BinaryTree t = new BinaryTree( arr, arr.length );
Node root = t.buildTree(0); // buildTree() returns reference to root
System.out.print("\n Level Order Tree Traversal: ");

t.levelorder(root);
}
}

12
public class BubbleSort{
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}

}
}

}
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};

System.out.println("Array Before Bubble Sort");


for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();

bubbleSort(arr);//sorting array elements using bubble


sort

System.out.println("Array After Bubble Sort");


for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}

}
}

You might also like