import java.util.*; public class Apriori { public static void main(String[] args) { Scanner sc=new Scanner(System.in); Apriori x=new Apriori(); Vector<String> v=new Vector<String>(), temp=new Vector<String>(), prev=new Vector<String>(); System.out.println("Enter no. of items:"); int p=1, n=sc.nextInt(); System.out.println("Enter no. of transactions:"); int t=sc.nextInt(); String s[]=new String[t]; for(int i=0;i<t;i++) { System.out.print("Transaction "+(i+1)+": "); s[i]=sc.next(); } System.out.println("Enter value for minimum support: "); int minSupp=sc.nextInt(); System.out.println("\nInitial count:\nItem\tCount"); for(int i=0;i<n;i++) { System.out.println((i+1)+"\t"+x.count(s, (i+1)+"")); if(x.count(s, (i+1)+"")>=minSupp) v.add((i+1)+""); temp.add(i+""); } System.out.println("\nFrequent "+(p++)+" itemsets: "+v); do{ temp.removeAllElements(); prev.removeAllElements(); for(int i=0;i<v.size();i++) prev.add(v.elementAt(i)); for(int i=0;i<v.size();i++) for(int j=i+1;j<v.size();j++) { String str=v.elementAt(i)+v.elementAt(j); if(x.count(s, str)>=minSupp) temp.add(x.removeDuplicates(str)); } v.removeAllElements(); for(int i=0;i<temp.size();i++) v.add(temp.elementAt(i)); x.removeRepeated(v); v = new Vector(new LinkedHashSet(v)); if(!v.isEmpty()) System.out.println("Frequent "+(p++)+" itemsets: "+v); }while(!v.isEmpty()); } int count(String[] a, String s) { int count=0; for(int i=0;i<a.length;i++) if(intersect(s, a[i])) count++; return count; } boolean intersect(String s1, String s2) { boolean b=true; for(int i=0;i<s1.length();i++) if(s2.indexOf(s1.charAt(i)+"")==-1) { b=false; break; } return b; } String removeDuplicates(String s) { StringBuilder noDupes = new StringBuilder(); for (int i = 0; i < s.length(); i++) { String si = s.substring(i, i+1); if (!si.equals(",") && noDupes.indexOf(si) == -1) noDupes.append(si); } return noDupes.toString(); } Vector<String> removeRepeated(Vector<String> vect) { for(int i=0;i<vect.size();i++) { char[] chars = vect.elementAt(i).toCharArray(); Arrays.sort(chars); vect.removeElementAt(i); vect.add(i, new String(chars)); } return vect; } }
Monday, October 27, 2014
Apriori in JAVA
Wednesday, April 16, 2014
multithreading in JAVA
public class ProducerConsumerTest {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}
class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
}
catch (InterruptedException e) {
}
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
}
catch (InterruptedException e) {
}
}
contents = value;
available = true;
notifyAll();
}
}
class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
System.out.println("Consumer #"
+ this.number
+ " got: " + value);
}
}
}
class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number
+ " put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}
class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
}
catch (InterruptedException e) {
}
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
}
catch (InterruptedException e) {
}
}
contents = value;
available = true;
notifyAll();
}
}
class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
System.out.println("Consumer #"
+ this.number
+ " got: " + value);
}
}
}
class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number
+ " put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
Tuesday, April 15, 2014
File operation in Assembly language 8086
;random access from file
.model small
.stack 64
.data
name1 label byte
maxlen db 5
actlen db ?
record db 5 dup(' '),'$'
nl db 13,10,'$'
filename db 'c:\rec.asm',00h
filehandle1 dw ?
recno dw ?
error db 10,13,"error $"
msg db 10,13,"recno:- $"
.code
main proc near
mov ax,@data
mov ds,ax
mov ah,3ch
mov cx,00
lea dx,filename
int 21h
jc end1
mov filehandle1,ax
a10:
mov ah,0ah
lea dx,name1
int 21h
cmp actlen,0
jbe a20
mov ah,40h
mov bx,filehandle1
mov cx,5
lea dx,record
int 21h
mov ah,09h
lea dx,nl
int 21h
jmp a10
a20:
mov ah,3eh
mov bx,filehandle1
int 21h
mov ah,3dh
mov al,00
lea dx,filename
int 21h
jc end1
mov filehandle1,ax
mov ah,09h
lea dx,msg
int 21h
mov ah,01h
int 21h
sub al,31h
mov ah,00
mul maxlen
mov recno,ax
mov ah,42h
mov al,00
mov bx,filehandle1
mov cx,00
mov dx,recno
int 21h
mov ah,3fh
mov bx,filehandle1
mov cx,5
lea dx,record
int 21h
mov ah,09h
lea dx,record
int 21h
mov ah,3eh
mov bx,filehandle1
int 21h
jmp end2
end1:
mov ah,09h
lea dx,error
int 21h
end2:
mov ax,4c00h
int 21h
main endp
end main
.model small
.stack 64
.data
name1 label byte
maxlen db 5
actlen db ?
record db 5 dup(' '),'$'
nl db 13,10,'$'
filename db 'c:\rec.asm',00h
filehandle1 dw ?
recno dw ?
error db 10,13,"error $"
msg db 10,13,"recno:- $"
.code
main proc near
mov ax,@data
mov ds,ax
mov ah,3ch
mov cx,00
lea dx,filename
int 21h
jc end1
mov filehandle1,ax
a10:
mov ah,0ah
lea dx,name1
int 21h
cmp actlen,0
jbe a20
mov ah,40h
mov bx,filehandle1
mov cx,5
lea dx,record
int 21h
mov ah,09h
lea dx,nl
int 21h
jmp a10
a20:
mov ah,3eh
mov bx,filehandle1
int 21h
mov ah,3dh
mov al,00
lea dx,filename
int 21h
jc end1
mov filehandle1,ax
mov ah,09h
lea dx,msg
int 21h
mov ah,01h
int 21h
sub al,31h
mov ah,00
mul maxlen
mov recno,ax
mov ah,42h
mov al,00
mov bx,filehandle1
mov cx,00
mov dx,recno
int 21h
mov ah,3fh
mov bx,filehandle1
mov cx,5
lea dx,record
int 21h
mov ah,09h
lea dx,record
int 21h
mov ah,3eh
mov bx,filehandle1
int 21h
jmp end2
end1:
mov ah,09h
lea dx,error
int 21h
end2:
mov ax,4c00h
int 21h
main endp
end main
Monday, April 14, 2014
string copying in assembly language 8086
.model small
.stack 64
.data
str1 db 'pawan samarwal$'
str2 db 20 dup(' '),'$'
.code
main proc far
mov ax,@data
mov ds,ax
mov es,ax
cld
mov cx,20
lea di,str1
lea si,str2
rep movsb
mov ah,09h
lea dx,str1
int 21h
mov ah,4ch
int 21h
main endp
end main
.stack 64
.data
str1 db 'pawan samarwal$'
str2 db 20 dup(' '),'$'
.code
main proc far
mov ax,@data
mov ds,ax
mov es,ax
cld
mov cx,20
lea di,str1
lea si,str2
rep movsb
mov ah,09h
lea dx,str1
int 21h
mov ah,4ch
int 21h
main endp
end main
doubly linked list in C++
a doubly-linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders.
#include<iostream> #include<iomanip> template<class s> ///templats classclass stack { private: int top; s item; s array[5]; public: stack() { top=-1; } void getdata() { cout<<"enter item to push in stack"<<endl; cin>>item; } void push() { if(top==5) cout<<"overflow"<<endl; else { top++; array[top]=item; } } void pop() { if(top==-1) cout<<"underflow"<<endl; else { array[top]=NULL; top--; } } void disp() { for(int i=0;i<=top;i++) { cout<<array[i]<<'\t'; } cout<<endl; } }; int main() { stack<int>stacki; stack<char>stackc; int r; char ch; do { cout<<"integer array"<<endl; cout<<"press 1 to push element in stack & 2 to pop it"<<endl; cout<<" 3 to display stack"<<endl; cin>>r; switch (r) { case 1: stacki.getdata(); stacki.push(); break; case 2: stacki.pop(); break; case 3: stacki.disp(); break; default: cout<<"bad input"<<endl; break; } cout<<"do you want to process more y/n"<<endl; cin>>ch; } while(ch!='n'); do { cout<<"character array"<<endl; cout<<"press 1 to push element in stack & 2 to pop it"<<endl; cout<<" 3 to display stack"<<endl; cin>>r; switch (r) { case 1: stackc.getdata(); stackc.push(); break; case 2: stackc.pop(); break; case 3: stackc.disp(); break; default: cout<<"bad input"<<endl; break; } cout<<"do you want to process more y/n"<<endl; cin>>ch; } while(ch!='n'); return 0; }
String comparison in assembly language 8086
DATA SEGMENT STR1 DB "ENTER FIRST STRING HERE ->$" STR2 DB "ENTER SECOND STRING HERE ->$" STR11 DB "FIRST STRING : ->$" STR22 DB "SECOND STRING: ->$" INSTR1 DB 20 DUP("$") INSTR2 DB 20 DUP("$") NEWLINE DB 10,13,"$" N DB ? S DB ? MSG1 DB "BOTH STRING ARE SAME$" MSG2 DB "BOTH STRING ARE DIFFERENT$" DATA ENDS CODE SEGMENT ASSUME DS:DATA,CS:CODE START: MOV AX,DATA MOV DS,AX LEA SI,INSTR1 LEA DI,INSTR2 ;GET STRING MOV AH,09H LEA DX,STR1 INT 21H MOV AH,0AH MOV DX,SI INT 21H MOV AH,09H LEA DX,NEWLINE INT 21H MOV AH,09H LEA DX,STR2 INT 21H MOV AH,0AH MOV DX,DI INT 21H MOV AH,09H LEA DX,NEWLINE INT 21H ;PRINT THE STRING MOV AH,09H LEA DX,STR11 INT 21H MOV AH,09H LEA DX,INSTR1+2 INT 21H MOV AH,09H LEA DX,NEWLINE INT 21H MOV AH,09H LEA DX,STR22 INT 21H MOV AH,09H LEA DX,INSTR2+2 INT 21H MOV AH,09H LEA DX,NEWLINE INT 21H ;STRING COMPARISION MOV BX,00 MOV BL,INSTR1+1 MOV BH,INSTR2+1 CMP BL,BH JNE L1 ADD SI,2 ADD DI,2 L2:MOV BL,BYTE PTR[SI] CMP BYTE PTR[DI],BL JNE L1 INC SI INC DI CMP BYTE PTR[DI],"$" JNE L2 MOV AH,09H LEA DX,MSG1 INT 21H JMP L5 L1:MOV AH,09H LEA DX,MSG2 INT 21H L5: MOV AH,09H LEA DX,NEWLINE INT 21H MOV AH,4CH INT 21H CODE ENDS END START
TSR DIGITAL CLOCK in ASSEMBLY LANGUAGE 8086
I've been trying to write TSR (Terminate-Stay-Resident) programs (in general) in Assembly (16-bit) for MS-DOS. I've read through a Wikipedia page on TSR and also a page on using it specifically in DOS (but it seems to be teaching it in C and not Assembly directly). I've looked at a site with tons of DOS interrupt documentation and find this one, this one, and another most relevant to TSR programs. I can't post all of the links because as a new user I can have up to 2 hyperlinks on a post.
So, I've tried writing a (seemingly) very simple TSR program in real mode flat model (.COM file format) in NASM. Here's the code:
creating digital clock
.model small
.stack 64h
code segment
assume cs:code
begin :
jmp tsr
address dd ?
str db ':','$'
hrs db ?
min db ?
sec db ?
num db 10
h db 2 dup(0)
m db 2 dup(0)
s db 2 dup(0)
clrscrn proc near
mov ax,0600h
mov bh,34h
mov cx,0000h
mov dx,2060h
int 10h
ret
clrscrn endp
set proc near
mov ah,02h
mov bh,00h
mov dh,0ah
mov dl,0ah
int 10h
ret
set endp
getv proc near
mov ah,2ch
int 21h
mov al,ch
mov ah,00
div num
mov bl,ah
mov dl,al
add dl,30h
mov ah,02h
int 21h
mov dl,bl
add dl,30h
mov ah,02h
int 21h
mov ah,09h
lea dx,str
int 21h
mov ah,2ch
int 21h
mov al,cl
mov ah,00h
div num
mov bl,ah
mov dl,al
add dl,30h
mov ah,02h
int 21h
mov dl,bl
add dl,30h
mov ah,02h
int 21h
mov ah,09h
lea dx,str
int 21h
mov ah,2ch
int 21h
mov al,dh
mov ah,00h
div num
mov bl,ah
mov dl,al
add dl,30h
mov ah,02h
int 21h
mov dl,bl
add dl,30h
mov ah,02h
int 21h
ret
getv endp
clock proc near
mov cl,100
b11:
call clrscrn
call set
call getv
call clrscrn
loop b11
ret
clock endp
l1:
mov ax,cs
mov ds,ax
mov es,ax
mov ah,00h
mov al,03h
int 10h
in al,60h
cmp al,01
jne e1
call clock
e1:
jmp dword ptr cs:address
tsr:
mov ax,cs
mov ds,ax
mov es,ax
mov ah,35h
mov al,09h
int 21h
mov word ptr address,bx
mov word ptr address+2,es
mov ah,25h
mov al,09h
mov dx,offset l1
int 21h
mov ah,31h
mov dx,offset tsr
int 21h
code ends
end begin
Labels:
8086 assembly,
8086 assembly language program,
assembly language graphics programming,
assembly language programming,
bouncing ball assembly program in java,
Data structure,
digital clock in assembly language,
geeksforgeeks,
moving object in assembly language,
system programming code
large number adition using linked list C++
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
int a2[10],m=0,n=0,a1[10],y=0,r1[10],r2[10],i=0,j=0;
char c1[10],c2[10],b;
node *l1,*l2,*s1,*s2;
cout<<"enter 1st number";
cin.getline(c1,10);
while(c1[i]!='\0')
{
a1[i]=c1[i]-48;
i++;m++;
}
cout<<"\n enter 2nd number";
cin.getline(c2,10);
while(c2[j]!='\0')
{
a2[j]=c2[j]-48;
j++;n++; }
for(i=0;i<m;i++)
cout<<a1[i];
s1=l1;
for(j=m-1;j>0;j--)
{
cout<<"hi";
l1=new node;
l1->data=a1[m-j-1];
l1=l1->next;
}
l1->next=NULL;
l1=s1;
s2=l2;
for(i=n;i>0;i--)
{
l2=new node;
l2->data=a2[n-i];
l2=l2->next;
}
l2->next=NULL;
l2=s2;
cout<<"________+++++++";
while(l1!=NULL)
{
cout<<l1->data;
l1=l1->next;
}}
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
int a2[10],m=0,n=0,a1[10],y=0,r1[10],r2[10],i=0,j=0;
char c1[10],c2[10],b;
node *l1,*l2,*s1,*s2;
cout<<"enter 1st number";
cin.getline(c1,10);
while(c1[i]!='\0')
{
a1[i]=c1[i]-48;
i++;m++;
}
cout<<"\n enter 2nd number";
cin.getline(c2,10);
while(c2[j]!='\0')
{
a2[j]=c2[j]-48;
j++;n++; }
for(i=0;i<m;i++)
cout<<a1[i];
s1=l1;
for(j=m-1;j>0;j--)
{
cout<<"hi";
l1=new node;
l1->data=a1[m-j-1];
l1=l1->next;
}
l1->next=NULL;
l1=s1;
s2=l2;
for(i=n;i>0;i--)
{
l2=new node;
l2->data=a2[n-i];
l2=l2->next;
}
l2->next=NULL;
l2=s2;
cout<<"________+++++++";
while(l1!=NULL)
{
cout<<l1->data;
l1=l1->next;
}}
key identification in assembly language 8086
.model small
.stack 64h
.data
st1 db 'home $'
st2 db 'other $'
CTR DB 00H
.code
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
A20:
MOV AH,10H
INT 16H
CMP Ah,47h
JE B10
MOV AH,09H
LEA DX,ST2
INT 21H
CMP CTR,09H
JNE A50
jmp a20
B10:
MOV AH,09H
LEA DX,ST1
INT 21H
JMP A20
a50:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
.stack 64h
.data
st1 db 'home $'
st2 db 'other $'
CTR DB 00H
.code
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
A20:
MOV AH,10H
INT 16H
CMP Ah,47h
JE B10
MOV AH,09H
LEA DX,ST2
INT 21H
CMP CTR,09H
JNE A50
jmp a20
B10:
MOV AH,09H
LEA DX,ST1
INT 21H
JMP A20
a50:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
screensaver in SP using keyboard input 8086
pawan macro
mov ax,0600h
mov cx,0000h
mov dx,184fh
mov bh,a
int 10h
endm
.model small
.stack 64
.data
ctr db 10h
tik db 'pawan$'
a db 00h
.code
main proc far
mov ax,@data
mov ds,ax
a20:
inc ctr
mov cx,0fh
pawa:
inc a
loop pawa
mov ah,10h
int 16h
cmp al,0e0h
je a10
jmp a20
a10:
pawan
jmp a20
mov ah,4ch
int 21h
main endp
end main
mov ax,0600h
mov cx,0000h
mov dx,184fh
mov bh,a
int 10h
endm
.model small
.stack 64
.data
ctr db 10h
tik db 'pawan$'
a db 00h
.code
main proc far
mov ax,@data
mov ds,ax
a20:
inc ctr
mov cx,0fh
pawa:
inc a
loop pawa
mov ah,10h
int 16h
cmp al,0e0h
je a10
jmp a20
a10:
pawan
jmp a20
mov ah,4ch
int 21h
main endp
end main
Saturday, April 5, 2014
BINARY TREE Level Printing in c++
The binary tree is a fundamental data structure used in computer science. The binary tree is a useful data structure for rapidly storing sorted data and rapidly retrieving stored data. A binary tree is composed of parent nodes, or leaves, each of which stores data and also links to up to two other child nodes (leaves) which can be visualized spatially as below the first node with one placed to the left and with one placed to the right. It is the relationship between the leaves linked to and the linking leaf, also known as the parent node, which makes the binary tree such an efficient data structure. It is the leaf on the left which has a lesser key value (i.e., the value used to search for a leaf in the tree), and it is the leaf on the right which has an equal or greater key value. As a result, the leaves on the farthest left of the tree have the lowest values, whereas the leaves on the right of the tree have the greatest values. More importantly, as each leaf connects to two other leaves, it is the beginning of a new, smaller, binary tree. Due to this nature, it is possible to easily access and insert data in a binary tree using search and insert functions recursively called on successive leaves.
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
struct btree
{
char data;
btree* lchild;
btree* rchild;
};
btree* bcreate(btree* t,char k);
void show(btree *t,int n);
int hgt(btree* t);
int main()
{
btree *p=NULL;
char c,d; int l,k=0;
cout<<"enter data\n";
cin>>c;
p=bcreate(p,c);
cout<<endl;
l=hgt(p);
while(k<=l)
{
show(p,k++);
cout<<endl;
}
return 0;
}
btree* bcreate(btree* t,char k)
{
btree *s=NULL,*r=NULL;char c1,c2;
if(t==NULL && k!='.')
{
t=new(btree);
t->data=k;
t->rchild=NULL; t->lchild=NULL;
cout<<"enter left and right child of "<<k<<endl;
cin>>c1>>c2;
t->lchild=bcreate(s,c1);
t->rchild=bcreate(r,c2);
return t;
}
return t;
}
int hgt(btree* t)
{
if(t==NULL)
return -1;
else
{
int a=hgt(t->lchild);
int b=hgt(t->rchild);
if(a>b)
return a+1;
else
return b+1;
}
}
void show(btree *t,int n)
{
if(n==0 && t!=NULL)
cout<<t->data<<" ";
else
{
show(t->lchild,n-1);
show(t->rchild,n-1);
}
}
Moving CAR in ASSEMBLY LANGUAGE
int 10h
is a video BIOS interrupt that is older than dirt and cannot be accessed from user mode in current operating systems.model small
.stack 64
.data
tik dw ?
a db 05h
b db 05h
c db 08h
d db 0fh
e db 06h
f db 12h
str db ' 0 0 ','$'
ctr db 00
.code
main proc far
mov ax,@data
mov ds,ax
pawan:
mov ah,00h
mov al,03h
int 10h
inc ctr
mov ah,02h
mov bh,00
mov dh,09h
mov dl,b
int 10h
mov ah,09h
lea dx,str
int 21h
mov ax,600h
mov bh,61h
mov cx,0a00h
mov dx,0aaah
int 10h
mov ax,600h
mov bh,30h
mov ch,a
mov cl,b
mov dh,c
mov dl,d
int 10h
mov ch,07h
mov cl,e
mov dl,f
int 10h
inc b
inc d
inc e
inc f
call delay
cmp ctr,60
jne pawan
mov ah,4ch
int 21h
main endp
delay proc near
mov ah,00h
int 1ah
mov tik,dx
add tik,1h
b10:
mov ah,00h
int 1ah
cmp tik,dx
jge b10
ret
delay endp
end main
Labels:
8086 assembly,
8086 assembly language program,
assembly language graphics programming,
assembly language programming,
bouncing ball assembly language,
Data structure,
geeksforgeeks,
moving ball inassembly language,
moving object in assembly language,
system programming code
Wednesday, March 26, 2014
ternory search tree in C++
TERNORY SEARCH TREE
having two data field and three pointers to the childs.
#include<iostream>
using namespace std;
struct tnode
{
int data1,data2;
tnode *left,*right,*mid;
};
void add(tnode *&t,int k1,int k2)
{
if(t==NULL)
{
t=new tnode;
t->data1=k1;
t->data2=k2;
t->right=NULL;
t->left=NULL;
t->mid=NULL;
}
else
{
if(k1<t->data1)
add(t->left,k1,k2);
if(k1>t->data2)
add(t->right,k1,k2);
else
add(t->mid,k1,k2);
}}
void search(tnode *t,int k)
{
if(t->data1==k||t->data2==k)
cout<<"found";
else
{
if(k<t->data1)
search(t->left,k);
else if(k>t->data2)
search(t->right,k);
else if(k>t->data1&&k<t->data2)
search(t->mid,k);
}
}
void output(tnode *t)
{
if(t!=NULL)
{
cout<<t->data1<<" "<<t->data2<<" ";
output(t->left);
output(t->mid);
output(t->right);
}
}
int main()
{
tnode *t;
t=new tnode;
t=NULL;
cout<<"enter the key values";
int k1,k2;
cin>>k1>>k2;
do
{
add(t,k1,k2);
cin>>k1>>k2;
}while(k1!=-1&&k2!=-1);
output(t);
cout<<"\nenter the element to be searched";
int a;
cin>>a;
search(t,a);
return 0;
}
having two data field and three pointers to the childs.
#include<iostream>
using namespace std;
struct tnode
{
int data1,data2;
tnode *left,*right,*mid;
};
void add(tnode *&t,int k1,int k2)
{
if(t==NULL)
{
t=new tnode;
t->data1=k1;
t->data2=k2;
t->right=NULL;
t->left=NULL;
t->mid=NULL;
}
else
{
if(k1<t->data1)
add(t->left,k1,k2);
if(k1>t->data2)
add(t->right,k1,k2);
else
add(t->mid,k1,k2);
}}
void search(tnode *t,int k)
{
if(t->data1==k||t->data2==k)
cout<<"found";
else
{
if(k<t->data1)
search(t->left,k);
else if(k>t->data2)
search(t->right,k);
else if(k>t->data1&&k<t->data2)
search(t->mid,k);
}
}
void output(tnode *t)
{
if(t!=NULL)
{
cout<<t->data1<<" "<<t->data2<<" ";
output(t->left);
output(t->mid);
output(t->right);
}
}
int main()
{
tnode *t;
t=new tnode;
t=NULL;
cout<<"enter the key values";
int k1,k2;
cin>>k1>>k2;
do
{
add(t,k1,k2);
cin>>k1>>k2;
}while(k1!=-1&&k2!=-1);
output(t);
cout<<"\nenter the element to be searched";
int a;
cin>>a;
search(t,a);
return 0;
}
Sunday, March 23, 2014
HEAP sort in C++
#include<iostream>
using namespace std;
int n,g[20],ctr=0;;
void heapsort(int a[],int n);
void heapify(int a[],int n);
void delheap(int a[])
{
int i,t,p;
g[ctr++]=a[0];
a[0]=a[n];
n--;
p=(n%2==1)?(n-1)/2:(n-2)/2;
if(a[p]>a[0])
{
swap(a[p],a[0]);
delheap(a);
}
}
int main()
{
int a[20];
int i=0,j=0;
cout<<"enter the elements";
cin>>n;
while(n!=-1)
{
a[i++]=n;
cin>>n;
}
n=i;
heapsort(a,n);
cout<<"\nsorted numbers are ";
delheap(a);
for(i=0;i<n;i++)
cout<<g[i]<<" ";
return 0;
}
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void heapsort(int a[],int n)
{
int i,t;
for(i=n-1;i>0;i--)
{
int p;
p=(i%2==1)?(i-1)/2:(i-2)/2;
if(a[p]>a[i])
{
swap(a[p],a[i]);
heapsort(a,p);
}
}
}
using namespace std;
int n,g[20],ctr=0;;
void heapsort(int a[],int n);
void heapify(int a[],int n);
void delheap(int a[])
{
int i,t,p;
g[ctr++]=a[0];
a[0]=a[n];
n--;
p=(n%2==1)?(n-1)/2:(n-2)/2;
if(a[p]>a[0])
{
swap(a[p],a[0]);
delheap(a);
}
}
int main()
{
int a[20];
int i=0,j=0;
cout<<"enter the elements";
cin>>n;
while(n!=-1)
{
a[i++]=n;
cin>>n;
}
n=i;
heapsort(a,n);
cout<<"\nsorted numbers are ";
delheap(a);
for(i=0;i<n;i++)
cout<<g[i]<<" ";
return 0;
}
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void heapsort(int a[],int n)
{
int i,t;
for(i=n-1;i>0;i--)
{
int p;
p=(i%2==1)?(i-1)/2:(i-2)/2;
if(a[p]>a[i])
{
swap(a[p],a[i]);
heapsort(a,p);
}
}
}
Saturday, March 15, 2014
color change of screen in 8086
color change of screen if press 1
.model small
.stack 64
.data
clor db 10h
.code
main proc near
mov ax,@data
mov ds,ax
mov ax,3h ; clear screen
int 10h
a10:
mov ax,0600h ;set disply
mov bh,clor
mov cx,0000h ; x1,y1
mov dx,184fh ;x2,y2
int 10h
add clor,10h ;change background color
mov ah,8h ; ikeyboard nput
int 21h
cmp al,'1' ; if 1 press then color change otherwise exit
je a10
mov ax,4c00h
int 21h
main endp
end main
.model small
.stack 64
.data
clor db 10h
.code
main proc near
mov ax,@data
mov ds,ax
mov ax,3h ; clear screen
int 10h
a10:
mov ax,0600h ;set disply
mov bh,clor
mov cx,0000h ; x1,y1
mov dx,184fh ;x2,y2
int 10h
add clor,10h ;change background color
mov ah,8h ; ikeyboard nput
int 21h
cmp al,'1' ; if 1 press then color change otherwise exit
je a10
mov ax,4c00h
int 21h
main endp
end main
moving screen box in 8086
.model small
.stack 64
.data
point1a dw 510h
point1b dw 1520h ;coordinate of box
tik dw ?
.386
.code
main proc near
mov ax,@data
mov ds,ax
a10:
mov ax,0600h ;set disply
mov bh,52h ; 5 background color ,2 forehead color
mov cx,point1a ; x1, y1
mov dx,point1b ;x2, y2
int 10h
call delay ;delay of 1 sec
inc point1a ; move point forward for next disply
inc point1b
mov dl,cl
mov ax,0600h ; erase starting row of box
mov bh,00h
int 10h
cmp cl,63
jb a10
mov ax,4c00h
int 21h
main endp
delay proc near
pusha
mov ah,00h ;give system time
int 1ah
mov tik,dx ;in dx
add tik,12h ; store in tik (1 sec= 12h value) ,system time+1sec
b10:
mov ah,00h
int 1ah
cmp tik,dx ;execute loop until 1sec over
jge b10
popa
ret
delay endp
end main
.stack 64
.data
point1a dw 510h
point1b dw 1520h ;coordinate of box
tik dw ?
.386
.code
main proc near
mov ax,@data
mov ds,ax
a10:
mov ax,0600h ;set disply
mov bh,52h ; 5 background color ,2 forehead color
mov cx,point1a ; x1, y1
mov dx,point1b ;x2, y2
int 10h
call delay ;delay of 1 sec
inc point1a ; move point forward for next disply
inc point1b
mov dl,cl
mov ax,0600h ; erase starting row of box
mov bh,00h
int 10h
cmp cl,63
jb a10
mov ax,4c00h
int 21h
main endp
delay proc near
pusha
mov ah,00h ;give system time
int 1ah
mov tik,dx ;in dx
add tik,12h ; store in tik (1 sec= 12h value) ,system time+1sec
b10:
mov ah,00h
int 1ah
cmp tik,dx ;execute loop until 1sec over
jge b10
popa
ret
delay endp
end main
Labels:
8086 assembly,
8086 assembly language program,
assembly language graphics programming,
assembly language programming,
bouncing ball assembly language,
Data structure,
geeksforgeeks,
moving ball inassembly language,
moving object in assembly language,
system programming code
quicksort in c++
#include<iostream>
using namespace std;
void swap(int& a,int& b)
{
int c=a;
a=b;
b=c;
}
int partion(int a[10],int low,int hi)
{
int i,j,ce,lo,h1,b[3];
ce=(low+hi)/2;
if((a[ce]>=a[low] && a[ce]<=a[hi])||(a[ce]<=a[low] && a[ce]>=a[hi]))
swap(a[low],a[ce]);
else if((a[hi]>=a[low] && a[hi]<=a[ce])||(a[hi]<=a[low] && a[hi]>=a[ce]))
swap(a[low],a[hi]);
lo=low+1;
h1=hi;
while(lo<=hi)
{
while(a[lo]<a[low])
lo++;
while(a[hi]>a[low] && hi>=lo)
hi--;
if(lo<hi)
{
swap(a[lo],a[hi]);
hi--;lo++;
}
}
swap(a[low],a[hi]);
return hi;
}
void quick(int a[10],int low,int hi)
{
int ce;
if(low<hi)
{
ce=partion(a,low,hi);
quick(a,low,ce-1);
quick(a,ce+1,hi);
}
}
int main()
{
int data[20],n,co=0;
cout<<"enter data\n";
cin>>n;
while(n!=-1) //-1 for end inputs
{
data[co++]=n;
cin>>n;
}
quick(data,0,co-1);
for(int i=0;i<co;i++) //output in sorted order
cout<<data[i]<<" ";
return 0;
}
using namespace std;
void swap(int& a,int& b)
{
int c=a;
a=b;
b=c;
}
int partion(int a[10],int low,int hi)
{
int i,j,ce,lo,h1,b[3];
ce=(low+hi)/2;
if((a[ce]>=a[low] && a[ce]<=a[hi])||(a[ce]<=a[low] && a[ce]>=a[hi]))
swap(a[low],a[ce]);
else if((a[hi]>=a[low] && a[hi]<=a[ce])||(a[hi]<=a[low] && a[hi]>=a[ce]))
swap(a[low],a[hi]);
lo=low+1;
h1=hi;
while(lo<=hi)
{
while(a[lo]<a[low])
lo++;
while(a[hi]>a[low] && hi>=lo)
hi--;
if(lo<hi)
{
swap(a[lo],a[hi]);
hi--;lo++;
}
}
swap(a[low],a[hi]);
return hi;
}
void quick(int a[10],int low,int hi)
{
int ce;
if(low<hi)
{
ce=partion(a,low,hi);
quick(a,low,ce-1);
quick(a,ce+1,hi);
}
}
int main()
{
int data[20],n,co=0;
cout<<"enter data\n";
cin>>n;
while(n!=-1) //-1 for end inputs
{
data[co++]=n;
cin>>n;
}
quick(data,0,co-1);
for(int i=0;i<co;i++) //output in sorted order
cout<<data[i]<<" ";
return 0;
}
queue in c++
#include<iostream>
using namespace std;
class queue
{
public :
int front,rear,size,a[10];
void enque(int c);
int delque();
queue()
{
front=-1;
rear=-1;
size=4;
}
};
void queue::enque(int c)
{
if((rear+1)%size==front)
cout<<"full";
else if(front==-1)
{
rear++;
a[++front]=c;
}
else
{
rear=(rear+1)%size;
a[rear]=c;
}
}
int queue::delque()
{int d;
if(front<=-1)
{
cout<<"empty";
exit(1);
}
else if(front==rear)
{ d=a[front];
front=-1;rear=-1;
return d;
}
else
{d=a[front];
front=(front+1)%size;
return d;
}
}
int main()
{queue q;
int e;
cout<<"enter 4 no.s";
for(int i=0;i<4;i++)
{cin>>e;
q.enque(e);
}
cout<<"after insertion store data in queue retrive";
for(int i=0;i<4;i++)
{
e=q.delque();
cout<<e<<" ";
}
return 0;
}
moving ball in java
import java.applet.*;
import java.awt.*;
import java.util.Random;
public class Bouncing_ball extends Applet implements Runnable {
int x1=100,xrate=1;
int y1=200,yrate=3;
int radius1=20;
Random re;
Thread th;
public void start(){
setBackground(Color.red);
setSize(400,400);
re=new Random();
th=new Thread(this);
th.start();
}
int t=200;
public void run(){
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while( true){
if(x1<=0|| x1>=360){
xrate=-xrate;
if(yrate<0)
yrate=-re.nextInt(4);
else
yrate=re.nextInt(4);
}
if(y1<=0|| y1>=360){
yrate=-yrate;
if(xrate<0)
xrate=-re.nextInt(4);
else
xrate=re.nextInt(4);
}
x1+=xrate;
y1+=yrate;
repaint();
try{
Thread.sleep(20);
}
catch(InterruptedException ex){
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint(Graphics g){
g.setColor(Color.green);
g.fillOval(x1,y1,radius1*2,radius1*2);
}
}
Spiral motion code in java
import java.applet.*;
import java.awt.*;
public class Sprial extends Applet{
public void paint(Graphics g){
int x=0,y=10,xrate=240,yrate=210;
setBackground(Color.green);
g.setColor(Color.black);
for(int i=1;i<=15;i++){
int m=x;
int n=y;
if(i%4==1){
x+=xrate;
xrate-=30;
}
else if(i%4==2){
y+=yrate;
yrate-=30;
}
else if(i%4==3){
x-=xrate;
xrate-=30;
}
else
{
y-=yrate;
yrate-=30;
}
g.drawLine(m, n, x, y);
}
}
}
stack using link list
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class stack
{
public:
node *p=NULL,*q,*t;
void push(int d);
int pop();
};
void stack::push(int d)
{
if(p==NULL)
{
p=new(node);
p->data=d; p->next=NULL;
}
else
{
t=new(node);
t->data=d;
t->next=p;
p=t;
}
}
int stack:: pop()
{
int a;
if(p!=NULL)
{
a=p->data;
p=p->next;
return a;
}
}
int main()
{ stack s;
int i,j,n;
cout<<"enter 4 no";
for(i=0;i<4;i++)
{
cin>>n;
s.push(n);
}
cout<<"in reverse ";
for(i=0;i<4;i++)
{
cout<<s.pop();
}
return 0;
}
using namespace std;
struct node
{
int data;
node *next;
};
class stack
{
public:
node *p=NULL,*q,*t;
void push(int d);
int pop();
};
void stack::push(int d)
{
if(p==NULL)
{
p=new(node);
p->data=d; p->next=NULL;
}
else
{
t=new(node);
t->data=d;
t->next=p;
p=t;
}
}
int stack:: pop()
{
int a;
if(p!=NULL)
{
a=p->data;
p=p->next;
return a;
}
}
int main()
{ stack s;
int i,j,n;
cout<<"enter 4 no";
for(i=0;i<4;i++)
{
cin>>n;
s.push(n);
}
cout<<"in reverse ";
for(i=0;i<4;i++)
{
cout<<s.pop();
}
return 0;
}
sort using stack
#include<iostream>
using namespace std;
class stack
{public:
int top,s,a[10];
void push(int d);
int pop();
stack(){
top=-1,s=4;
}
};
void stack::push(int d)
{if(top>=s-1)
cout<<"stack is full\n";
else
a[++top]=d;
}
int stack::pop()
{int c;
if(top<=-1)
{
cout<<"stack is empty\n";
return 0;
}
else
{ c =a[top--];
return c;
}}
int main()
{stack s,s1; int i,j,n,a,b;
cout<<"enter ";
for(i=0;i<4;i++) //enter 4 numbers
{cin>>n;
s.push(n);
}
for(i=3;i>0;i--)
{
for(j=i;j>0;j--)
{a=s.pop(); // pop 2 numbers in stack
b=s.pop();
if(a>=b)
{s1.push(a); //store minimum into 2nd stack s1
s.push(b);
}
else
{s1.push(b);
s.push(a);
}
}
while(s1.top>-1)
{n=s1.pop();
s.push(n);
}
}
while(s.top>-1)
cout<<s.pop();
return 0;
}
using namespace std;
class stack
{public:
int top,s,a[10];
void push(int d);
int pop();
stack(){
top=-1,s=4;
}
};
void stack::push(int d)
{if(top>=s-1)
cout<<"stack is full\n";
else
a[++top]=d;
}
int stack::pop()
{int c;
if(top<=-1)
{
cout<<"stack is empty\n";
return 0;
}
else
{ c =a[top--];
return c;
}}
int main()
{stack s,s1; int i,j,n,a,b;
cout<<"enter ";
for(i=0;i<4;i++) //enter 4 numbers
{cin>>n;
s.push(n);
}
for(i=3;i>0;i--)
{
for(j=i;j>0;j--)
{a=s.pop(); // pop 2 numbers in stack
b=s.pop();
if(a>=b)
{s1.push(a); //store minimum into 2nd stack s1
s.push(b);
}
else
{s1.push(b);
s.push(a);
}
}
while(s1.top>-1)
{n=s1.pop();
s.push(n);
}
}
while(s.top>-1)
cout<<s.pop();
return 0;
}
BUBBLE SORT USING QUEUE
#include<iostream>
#include<cstdlib>
using namespace std;
class queue
{
public :
int front,rear,size,a[10];
void enque(int c);
int delque();
queue()
{
front=-1;
rear=-1;
size=4;
}
};
void queue::enque(int c)
{
if((rear+1)%size==front)
cout<<"full";
else if(front==-1)
{
rear++;
a[++front]=c;
}
else
{
rear=(rear+1)%size;
a[rear]=c;
}
}
int queue::delque()
{int d;
if(front<=-1)
{
cout<<"empty";
exit(1);
}
else if(front==rear)
{ d=a[front];
front=-1;rear=-1;
return d;
}
else
{d=a[front];
front=(front+1)%size;
return d;
}
}
int main()
{queue q;
int e,f,i,j,k,l,m,n,p;
cout<<"enter 4 no.s";
for(i=0;i<4;i++)
{cin>>e;
q.enque(e); //INSERT INTO QUEUE
}
for(i=4;i>0;i--)
{
m=0;k=0;
for(j=0;j<i;j++)
{
n=q.delque();
if(m<n) // IF MAXIMUM THEN BEFORE THEN STORE POSITION
{ //AND CHANGE M VALUE
k=j;
m=n;
}
q.enque(n);
}
for(l=0;l<k;l++)
{
p=q.delque(); // GO TO THAT POSITION WHICH HAVE TO SHOW AND AGAIN
q.enque(p); //STORE IN QUEUE
}
cout<<q.delque()<<" ";
}
return 0;
}
AVL tree in C++
AVL tree is a balanced tree.
DEFINITION: An AVL tree is a binary search tree with the additional
balance property that, for any node in the tree, the height of the left and
right subtrees can differ by at most 1. As usual, the height of an empty
subtree is -1.
#include<iostream>
using namespace std;
struct avlnode //node structure of node
{ int data;
avlnode *lchild;
avlnode *rchild;
};
int height(avlnode *t) //height of node
{
if(t==NULL)
return -1;
else
{
int a=height(t->lchild);
int b=height(t->rchild);
if(a>b)
return a+1;
else return b+1;
}
}
avlnode *rotatelchild(avlnode *t) //rotate left
{
char k1,k2;
k1=t->data;
k2=t->rchild->data;
t->data=k2;
t->lchild->data=k1;
t->rchild=t->rchild->rchild;
return t;
}
avlnode *rotaterchild(avlnode *t) //rotate right
{
char k1,k2;
k1=t->data;
k2=t->lchild->data;
t->data=k2;
t->rchild->data=k1;
t->lchild=t->lchild->lchild;
return t;
}
void doublerotatelchild(avlnode *&k3) //double rotation right to left
{
rotaterchild(k3->lchild);
rotatelchild(k3);
}
void doublerotaterchild(avlnode *&k3) //double rotation left to right
{
rotatelchild(k3->rchild);
rotaterchild(k3);
}
void creatavltree(avlnode *&t,int k)
{
if(t==NULL)
{
t=new(avlnode);
t->data=k;
t->lchild=NULL;
t->rchild=NULL;
}
else if(k<t->data)
{
creatavltree(t->lchild,k);
if((height(t->lchild)-height(t->rchild))==2||height(t->lchild)-height(t->rchild)==-2)
{
if(k<t->lchild->data)
rotatelchild(t);
else doublerotatelchild(t);
}
}
else if(k>t->data)
{
creatavltree(t->rchild,k);
if((height(t->rchild)-height(t->lchild))==2||height(t->rchild)-height(t->lchild)==-2)
{
if(t->rchild->data<k)
rotaterchild(t);
else doublerotaterchild(t);
}
}
}
void printpreorder(avlnode *t)
{
if(t!=NULL)
{
cout<<t->data<<" ";
printpreorder(t->lchild);
printpreorder(t->rchild);
}
}
int main()
{
avlnode *t;
t=NULL;
int d,h;
cout<<"enter the elements of avl tree and -1 to terminate"<<endl;
cin>>d;
while(d!=-1)
{
creatavltree(t,d);
cin>>d;
}
cout<<"\nthe height of avl tree is\n";
h=height(t);
cout<<h<<endl;
cout<<"\nthe elements of avl tree is\n";
printpreorder(t);
return 0;
}
DEFINITION: An AVL tree is a binary search tree with the additional
balance property that, for any node in the tree, the height of the left and
right subtrees can differ by at most 1. As usual, the height of an empty
subtree is -1.
#include<iostream>
using namespace std;
struct avlnode //node structure of node
{ int data;
avlnode *lchild;
avlnode *rchild;
};
int height(avlnode *t) //height of node
{
if(t==NULL)
return -1;
else
{
int a=height(t->lchild);
int b=height(t->rchild);
if(a>b)
return a+1;
else return b+1;
}
}
avlnode *rotatelchild(avlnode *t) //rotate left
{
char k1,k2;
k1=t->data;
k2=t->rchild->data;
t->data=k2;
t->lchild->data=k1;
t->rchild=t->rchild->rchild;
return t;
}
avlnode *rotaterchild(avlnode *t) //rotate right
{
char k1,k2;
k1=t->data;
k2=t->lchild->data;
t->data=k2;
t->rchild->data=k1;
t->lchild=t->lchild->lchild;
return t;
}
void doublerotatelchild(avlnode *&k3) //double rotation right to left
{
rotaterchild(k3->lchild);
rotatelchild(k3);
}
void doublerotaterchild(avlnode *&k3) //double rotation left to right
{
rotatelchild(k3->rchild);
rotaterchild(k3);
}
void creatavltree(avlnode *&t,int k)
{
if(t==NULL)
{
t=new(avlnode);
t->data=k;
t->lchild=NULL;
t->rchild=NULL;
}
else if(k<t->data)
{
creatavltree(t->lchild,k);
if((height(t->lchild)-height(t->rchild))==2||height(t->lchild)-height(t->rchild)==-2)
{
if(k<t->lchild->data)
rotatelchild(t);
else doublerotatelchild(t);
}
}
else if(k>t->data)
{
creatavltree(t->rchild,k);
if((height(t->rchild)-height(t->lchild))==2||height(t->rchild)-height(t->lchild)==-2)
{
if(t->rchild->data<k)
rotaterchild(t);
else doublerotaterchild(t);
}
}
}
void printpreorder(avlnode *t)
{
if(t!=NULL)
{
cout<<t->data<<" ";
printpreorder(t->lchild);
printpreorder(t->rchild);
}
}
int main()
{
avlnode *t;
t=NULL;
int d,h;
cout<<"enter the elements of avl tree and -1 to terminate"<<endl;
cin>>d;
while(d!=-1)
{
creatavltree(t,d);
cin>>d;
}
cout<<"\nthe height of avl tree is\n";
h=height(t);
cout<<h<<endl;
cout<<"\nthe elements of avl tree is\n";
printpreorder(t);
return 0;
}
HEAPSORT in C++
Heap is an array which stores elements in unsorted order. When an element is poped out it gives the minimum or maximum element (eg. 5,8,2,9,1) it will give 1,2,5,8,9.
#include<iostream>
using namespace std;
int n,g[20],ctr=0;;
void heapsort(int a[],int n);
void heapify(int a[],int n);
void delheap(int a[])
{
int i,t,p;
g[ctr++]=a[0];
a[0]=a[n];
n--;
p=(n%2==1)?(n-1)/2:(n-2)/2;
if(a[p]>a[0])
{
swap(a[p],a[0]);
delheap(a);
}
}
int main()
{
int a[20];
int i=0,j=0;
cout<<"enter the elements";
cin>>n;
while(n!=-1)
{
a[i++]=n;
cin>>n;
}
n=i;
heapsort(a,n);
cout<<"\nsorted numbers are ";
delheap(a);
for(i=0;i<n;i++)
cout<<g[i]<<" ";
return 0;
}
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void heapsort(int a[],int n)
{
int i,t;
for(i=n-1;i>0;i--)
{
int p;
p=(i%2==1)?(i-1)/2:(i-2)/2;
if(a[p]>a[i])
{
swap(a[p],a[i]);
heapsort(a,p);
}
}
}
#include<iostream>
using namespace std;
int n,g[20],ctr=0;;
void heapsort(int a[],int n);
void heapify(int a[],int n);
void delheap(int a[])
{
int i,t,p;
g[ctr++]=a[0];
a[0]=a[n];
n--;
p=(n%2==1)?(n-1)/2:(n-2)/2;
if(a[p]>a[0])
{
swap(a[p],a[0]);
delheap(a);
}
}
int main()
{
int a[20];
int i=0,j=0;
cout<<"enter the elements";
cin>>n;
while(n!=-1)
{
a[i++]=n;
cin>>n;
}
n=i;
heapsort(a,n);
cout<<"\nsorted numbers are ";
delheap(a);
for(i=0;i<n;i++)
cout<<g[i]<<" ";
return 0;
}
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void heapsort(int a[],int n)
{
int i,t;
for(i=n-1;i>0;i--)
{
int p;
p=(i%2==1)?(i-1)/2:(i-2)/2;
if(a[p]>a[i])
{
swap(a[p],a[i]);
heapsort(a,p);
}
}
}
Friday, March 14, 2014
COMPARE 2 TREES in C++
Comparing two tree means check each and every node of tree on each level that it is equal or not.
for two similar trees parent as well as both child node should be equal.
the code for comparing two trees is given below.
first tree and second tree
we copare node 'a' with 'x' and its child subtrees with 'others child subtree.
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct btree //defined the node structure
{
btree *lchild;
char data;
btree *rchild;
};
btree* bcreate(btree *t,char k);
void printsort(btree *t,char b[15]);
int l=0,m=0;
int main()
{
btree *p=NULL,*q=NULL;
char d,a[15],b[15],c[15];
int n,i;
cout<<"tree data\n";
cin>>d;
p=bcreate(p,d);
printsort(p,a);
a[l]='\0';
n=l;
l=0;
cout<<"tree data\n";
cin>>d;
q=bcreate(q,d);
printsort(q,b);
b[l]='\0';
for(i=0;i<l; i++)
{
c[i]=b[l-1-i];
}
c[l]='\0';
if(!strcmp(a,b))
cout<<"similar";
else if(!strcmp(a,c))
cout<<"mirror";
else
cout<<"nonsimilar";
return 0;
}
btree* bcreate(btree* t,char k) //to get input from user
{
btree *s=NULL,*r=NULL;char c1,c2;
if(t==NULL && k!='.') //k='.' used here for stop making nodes in tree
{
t=new(btree);
t->data=k;
t->rchild=NULL; t->lchild=NULL;
cout<<"enter left and right child of "<<k<<endl;
cin>>c1>>c2;
t->lchild=bcreate(s,c1); //recursive call for create child
t->rchild=bcreate(r,c2);
}
return t;
}
void printsort(btree *t,char b[15]) // f\print the tree content in sorted order nodes // (left child,parent ,right child) and store in character array
{
if(t!=NULL)
if(t->lchild==NULL && t->rchild==NULL)
b[l++]=t->data;
else
{
printsort(t->lchild,b);
b[l++]=t->data;
printsort(t->rchild,b);
}
}
for two similar trees parent as well as both child node should be equal.
the code for comparing two trees is given below.
first tree and second tree
we copare node 'a' with 'x' and its child subtrees with 'others child subtree.
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct btree //defined the node structure
{
btree *lchild;
char data;
btree *rchild;
};
btree* bcreate(btree *t,char k);
void printsort(btree *t,char b[15]);
int l=0,m=0;
int main()
{
btree *p=NULL,*q=NULL;
char d,a[15],b[15],c[15];
int n,i;
cout<<"tree data\n";
cin>>d;
p=bcreate(p,d);
printsort(p,a);
a[l]='\0';
n=l;
l=0;
cout<<"tree data\n";
cin>>d;
q=bcreate(q,d);
printsort(q,b);
b[l]='\0';
for(i=0;i<l; i++)
{
c[i]=b[l-1-i];
}
c[l]='\0';
if(!strcmp(a,b))
cout<<"similar";
else if(!strcmp(a,c))
cout<<"mirror";
else
cout<<"nonsimilar";
return 0;
}
btree* bcreate(btree* t,char k) //to get input from user
{
btree *s=NULL,*r=NULL;char c1,c2;
if(t==NULL && k!='.') //k='.' used here for stop making nodes in tree
{
t=new(btree);
t->data=k;
t->rchild=NULL; t->lchild=NULL;
cout<<"enter left and right child of "<<k<<endl;
cin>>c1>>c2;
t->lchild=bcreate(s,c1); //recursive call for create child
t->rchild=bcreate(r,c2);
}
return t;
}
void printsort(btree *t,char b[15]) // f\print the tree content in sorted order nodes // (left child,parent ,right child) and store in character array
{
if(t!=NULL)
if(t->lchild==NULL && t->rchild==NULL)
b[l++]=t->data;
else
{
printsort(t->lchild,b);
b[l++]=t->data;
printsort(t->rchild,b);
}
}
POTATO (circular queue) using LINKEDLIST using C++
#include<iostream>
using namespace std;
class circular
{
public:
struct node
{
int data;
node *next;
}*front,*rear;
void add();
void out();
void delete();
void initial();
};
void circular::initial()
{
front=NULL;
rear=NULL;
}
void circular:: add()
{
node *k;
k=new node;
int d;
cout<<"enter";
cin>>d;
k->data=d;
if(front==NULL)
rear=front=k;
else
{
front->next=k;
front=k;
front->next=rear;
}
}
void circular::delete()
{
node *k;
k=new node;
rear=rear->next;rear=rear->next;
k=rear;
rear=rear->next;rear=rear->next;
k->next=rear;
rear=k;
}
void circular::out()
{
cout<<rear->data;
}
int main()
{
circular c;
c.initial();
int a,b=0;
do
{
c.add();
b++;
cout<<"enter 1 to add more and 0 to terminate";
cin>>a;
}while(a==1);
for(int i=0;i<b;i++)
{
c.delete();
}
c.out();
}
using namespace std;
class circular
{
public:
struct node
{
int data;
node *next;
}*front,*rear;
void add();
void out();
void delete();
void initial();
};
void circular::initial()
{
front=NULL;
rear=NULL;
}
void circular:: add()
{
node *k;
k=new node;
int d;
cout<<"enter";
cin>>d;
k->data=d;
if(front==NULL)
rear=front=k;
else
{
front->next=k;
front=k;
front->next=rear;
}
}
void circular::delete()
{
node *k;
k=new node;
rear=rear->next;rear=rear->next;
k=rear;
rear=rear->next;rear=rear->next;
k->next=rear;
rear=k;
}
void circular::out()
{
cout<<rear->data;
}
int main()
{
circular c;
c.initial();
int a,b=0;
do
{
c.add();
b++;
cout<<"enter 1 to add more and 0 to terminate";
cin>>a;
}while(a==1);
for(int i=0;i<b;i++)
{
c.delete();
}
c.out();
}
LARGE NUMBER ADDITION using LINKEDLIST in C++
Suppose we have to add two very large number like for eg. (43653465365365+74365365436765).
we store both the numbers in two linked lists we start adding the content of each unit from right hand side and store in third linked list.
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
int a2[10],m=0,n=0,a1[10],x=0,y=0,r1[10],r2[10],i=0,j=0;
char c1[10],c2[10],b;
cout<<"enter 1st number";
cin.getline(c1,10);
while(c1[i]!='\0')
{
a1[i]=c1[i]-48;
i++;m++;
}
cout<<"\n enter 2nd number";
cin.getline(c2,10);
while(c2[j]!='\0')
{
a2[j]=c2[j]-48;
j++;n++; }
for(i=0;i<m;i++)
r1[m-i-1]=a1[i];
for(j=0;j<n;j++)
r2[n-j-1]=a2[j];
node *l1,*l2,*l3,*p,*q,*t3,*t1,*t2;
l1=new node;
l1->data=r1[0];
l1->next=NULL;
p=l1;
do
{
t1=new node;
t1->data=r1[++x];
t1->next=NULL;
l1->next=t1;
l1=t1;
}while(x<m-1);
l1=p;
l2=new node;
l2->data=r2[0];
l2->next=NULL;
q=l2;
do
{
t2=new node;
t2->data=r2[++y];
t2->next=NULL;
l2->next=t2;
l2=t2;
}while(y<n-1);
l2=q;
node *z;
int c,a=0;
int aa;
int carry=0;
l3=new node;
aa=(l1->data)+(l2->data);
if(aa<10)
l3->data=aa;
else{
l3->data=(aa%10);
carry=1;}
l3->next=NULL;
z=l3;
l1=l1->next;
l2=l2->next;
while((l1!=NULL)&&(l2!=NULL))
{
aa= (l1->data)+(l2->data)+carry;
t3=new node;
if(aa<10)
{
t3->data=aa;
carry=0;}
else
{
t3->data=aa%10;
carry=1;
}
t3->next=NULL;
l3->next=t3;
l1=l1->next;
l2=l2->next;
l3=t3;
}
while((l1!=NULL&&l2==NULL))
{
aa=((l1->data)+carry);
carry=0;
t3=new node;
if(aa<10)
t3->data=aa;
t3->next=NULL;
l3->next=t3;
l3=t3;
l1=l1->next;
}
while((l2!=NULL&&l1==NULL))
{
aa=((l2->data)+carry);
carry=0;
t3=new node;
if(aa<10)
t3->data=aa;
t3->next=NULL;
l3->next=t3;
l3=t3;
l2=l2->next;
}
l3=z;
int k=0,d[11];
while(l3!=NULL)
{
d[k++]=l3->data;
l3=l3->next;
}
cout<<"\n the addition of two large number is\n";
for(int s=0;s<k;s++)
{
cout<<d[k-s-1]<<" ";
}
return 0;
}
we store both the numbers in two linked lists we start adding the content of each unit from right hand side and store in third linked list.
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
int a2[10],m=0,n=0,a1[10],x=0,y=0,r1[10],r2[10],i=0,j=0;
char c1[10],c2[10],b;
cout<<"enter 1st number";
cin.getline(c1,10);
while(c1[i]!='\0')
{
a1[i]=c1[i]-48;
i++;m++;
}
cout<<"\n enter 2nd number";
cin.getline(c2,10);
while(c2[j]!='\0')
{
a2[j]=c2[j]-48;
j++;n++; }
for(i=0;i<m;i++)
r1[m-i-1]=a1[i];
for(j=0;j<n;j++)
r2[n-j-1]=a2[j];
node *l1,*l2,*l3,*p,*q,*t3,*t1,*t2;
l1=new node;
l1->data=r1[0];
l1->next=NULL;
p=l1;
do
{
t1=new node;
t1->data=r1[++x];
t1->next=NULL;
l1->next=t1;
l1=t1;
}while(x<m-1);
l1=p;
l2=new node;
l2->data=r2[0];
l2->next=NULL;
q=l2;
do
{
t2=new node;
t2->data=r2[++y];
t2->next=NULL;
l2->next=t2;
l2=t2;
}while(y<n-1);
l2=q;
node *z;
int c,a=0;
int aa;
int carry=0;
l3=new node;
aa=(l1->data)+(l2->data);
if(aa<10)
l3->data=aa;
else{
l3->data=(aa%10);
carry=1;}
l3->next=NULL;
z=l3;
l1=l1->next;
l2=l2->next;
while((l1!=NULL)&&(l2!=NULL))
{
aa= (l1->data)+(l2->data)+carry;
t3=new node;
if(aa<10)
{
t3->data=aa;
carry=0;}
else
{
t3->data=aa%10;
carry=1;
}
t3->next=NULL;
l3->next=t3;
l1=l1->next;
l2=l2->next;
l3=t3;
}
while((l1!=NULL&&l2==NULL))
{
aa=((l1->data)+carry);
carry=0;
t3=new node;
if(aa<10)
t3->data=aa;
t3->next=NULL;
l3->next=t3;
l3=t3;
l1=l1->next;
}
while((l2!=NULL&&l1==NULL))
{
aa=((l2->data)+carry);
carry=0;
t3=new node;
if(aa<10)
t3->data=aa;
t3->next=NULL;
l3->next=t3;
l3=t3;
l2=l2->next;
}
l3=z;
int k=0,d[11];
while(l3!=NULL)
{
d[k++]=l3->data;
l3=l3->next;
}
cout<<"\n the addition of two large number is\n";
for(int s=0;s<k;s++)
{
cout<<d[k-s-1]<<" ";
}
return 0;
}
Subscribe to:
Posts (Atom)
Blog Archive
-
▼
2014
(54)
-
►
April
(11)
- multithreading in JAVA
- File operation in Assembly language 8086
- string copying in assembly language 8086
- doubly linked list in C++
- String comparison in assembly language 8086
- TSR DIGITAL CLOCK in ASSEMBLY LANGUAGE 8086
- large number adition using linked list C++
- key identification in assembly language 8086
- screensaver in SP using keyboard input 8086
- BINARY TREE Level Printing in c++
- Moving CAR in ASSEMBLY LANGUAGE
-
►
March
(42)
- ternory search tree in C++
- HEAP sort in C++
- color change of screen in 8086
- moving screen box in 8086
- quicksort in c++
- queue in c++
- moving ball in java
- Spiral motion code in java
- stack using link list
- sort using stack
- BUBBLE SORT USING QUEUE
- AVL tree in C++
- HEAPSORT in C++
- COMPARE 2 TREES in C++
- POTATO (circular queue) using LINKEDLIST using C++
- LARGE NUMBER ADDITION using LINKEDLIST in C++
-
►
April
(11)