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

Source Code2

This document discusses data structures stacks and queues implemented using arrays. It provides code to perform push, pop, insert, delete operations on stacks and queues. It also includes code for tree operations like insertion, deletion, traversal and merging two sorted arrays without duplicates.

Uploaded by

arunsmile
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Source Code2

This document discusses data structures stacks and queues implemented using arrays. It provides code to perform push, pop, insert, delete operations on stacks and queues. It also includes code for tree operations like insertion, deletion, traversal and merging two sorted arrays without duplicates.

Uploaded by

arunsmile
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 18

STACK AND QUEUE AS ARRAYS

#include<conio.h>
#include<stdio.h>
#include<dos.h>
#define size 10
int top=-1,bottom=-1,array[size]
!oid main"#
$
int ch
%hile"ch&='#
$
clrscr"#
printf"()o %hat do * %ant to ma+e,(#
printf"(-n1. )tac+(#
printf"(-n.. /ueue(#
printf"(-n'. 01it(#
printf"(-n0nter *2er choice , (#
scanf"(3d(,4ch#
s%itch"ch#
$
case 1, stac+"#
brea+
case ., 5ueue"#
brea+
case ', 1it"#
brea+
default, printf"(-n6ot an allo%ed type(#
7etch"#
brea+
8
8
8
1it"#
$ int i
printf"(-n-t9ait %hile : 5uit(#
for"i=0i<;i<<#
$ delay";00#
printf"(.(#
8
printf"(-n-t=ress any +ey to 5uit(#
7etch"#
8
stac+"#
$ int ch
%hile"ch&='#
$
clrscr"#
printf"()o %hat * %ant to perform,(#
printf"(-n1. =*)>(#
printf"(-n.. =?=(#
printf"(-n'. 01it(#
printf"(-n0nter *2er choice , (#
scanf"(3d(,4ch#
s%itch"ch#
$
case 1, push"#
brea+
case ., pop"#
brea+
case ', 1it"#
brea+
default, printf"(-n6ot an allo%ed type(#
7etch"#
brea+
8
8
8
push"#
$ if"top==-1#
$ top=top<1
bottom=top
printf"(-n0nter 1st element ,(#
scanf"(3d(,4array[top]#
8
else if"top&=size#
$ top=top<1
printf"(-n0nter element ,(#
scanf"(3d(,4array[top]#
8
else
$ printf"(-n6? more @0@ a!ilible(#
7etch"#
8
8
pop"#
$ if"top&=-1#
$ printf"(3d =?=ed(,array[top]#
top=top-1
8
else
printf"(6o more item left(#
7etch"#
8
5ueue"#
$ int ch
%hile"ch&='#
$
clrscr"#
printf"()o %hat * %ant to perform,(#
printf"(-n1. :nsert(#
printf"(-n.. Ael(#
printf"(-n'. 01it(#
printf"(-n0nter *2er choice , (#
scanf"(3d(,4ch#
s%itch"ch#
$
case 1, ins"#
brea+
case ., del"#
brea+
case ', 1it"#
brea+
default, printf"(-n6ot an allo%ed type(#
7etch"#
brea+
8
8
8
ins"#
$ if"top==-1#
$ top=top<1
bottom=top
printf"(0nter data ,(#
scanf"(3d(,4array[bottom]#
8
else if"bottom&=size#
$ bottom=bottom<1
printf"(0nter data ,(#
scanf"(3d(,4array[bottom]#
8
else
$ printf"(6o @0@ letf(#
7etch"#
8
8
del"#
$ if"bottom>0#
$ printf"(3d is no% deleted(,array[bottom]#
bottom=bottom-1
8
else if"bottom==0#
$ printf"(Bhe last item is no% been deleted i.e. 3d(,array[bottom]#
top=bottom=-1
8
else if"top==-1#
printf"(6o item 01ists(#
7etch"#
8
MERGING WITHOUT DUPLICATE ELEMENTS
#include <stdio.h>
main"#
$
int i,C,+,l,m,n,!1[;0],!.[;0],!'[;0]
!oid sortD!ect"#
printf"(=ro7ram for mer7in7 %ithout duplicate elements-n-n(#
printf"(0nter size of the !ector 1 , (#
scanf"(3d(,4n#
printf"(-n0nter 3d elements-n(,n#
for "i = 0 i < n i<<#
scanf"(3d(,4!1[i]#
sortD!ect"!1,n-1#
printf"(0nter size of the !ector . , (#
scanf"(3d(,4m#
printf"(-n0nter 3d elements-n(,m#
for "i = 0 i < m i<<#
scanf"(3d(,4!.[i]#
sortD!ect"!.,m-1#
printf"(-n-t)orted !ector 1-n(#
for "i = 0 i < n i<<#
printf"(3;d(,!1[i]#
printf"(-n(#
printf"(-n-t)orted !ector .-n(#
for "i = 0 i < m i<<#
printf"(3;d(,!.[i]#
printf"(-n(#
i = 0 C = 0 + = 0
%hile ""i<n# 44 "C<m##
$
if "!1[i] < !.[C]#
$
!'[+] = !1[i]
i<<
+<<
8
else
if "!1[i] > !.[C]#
$
!'[+] = !.[C]
C<<
+<<
8
else
$
!'[+] = !1[i]
i<<
C<<
+<<
8
8
if "i < n#
$
for "l = i l < n l<<#
$
!'[+] = !1[i]
i<<
+<<
8
8
else
if "C < m#
$
for "l = C l < m l<<#
$
!'[+] = !.[C]
C<<
+<<
8
8
printf"(-n-t@er7ed !ector "%ithout 3d dupilicates#-n(,"m<n#-+#
for "i = 0 i < + i<<#
printf"(3;d(,!'[i]#
printf"(-n(#
return"0#
8
!oid sortD!ect"s!,1#
int s![;0],1
$
int i,fla7,temp
fla7 = 0
%hile "fla7 == 0#
$
fla7 = 1
for "i = 0 i < 1 i<<#
$
if "s![i] > s![i<1]#
$
temp = s![i]
s![i] = s![i<1]
s![i<1] = temp
fla7 = 0
8
8
8
8
TREE OPERATIONS
#include <stdio.h>
#include <malloc.h>
typedef struct tree
$
int item
struct tree Eleft, Eri7ht
8tree
tree Einsert"item,p#
int item
tree Ep
$
if "&p#
$
p = "treeE#malloc"sizeof"tree##
p->item = item
p->left = 6*FF
p->ri7ht = 6*FF
return"p#
8
if "item < p->item#
p->left = insert"item,p->left#
else
if "item > p->item#
p->ri7ht = insert"item,p->ri7ht#
return"p#
8
!oid output"temp,le!el#
tree Etemp
int le!el
$
int i
if"temp#
$
output"temp->ri7ht,le!el<1#
printf"(-n(#
for "i = 0 i < le!el i<<#
printf"( (#
printf"(3d(,temp->item#
output"temp->left,le!el<1#
8
8
!oid depthbr"temp,depth,le!el#
tree Etemp
int Edepth, le!el
$
if"temp#
$
if "le!el > Edepth#
Edepth = le!el
depthbr"temp->left,depth,le!el<1#
depthbr"temp->ri7ht,depth,le!el<1#
8
8
tree Erepl"s,p#
tree Es,Ep
$
tree Edata
if"s->ri7ht &= 6*FF#
s->ri7ht = repl"s->ri7ht,p#
else
$
data = s
p->item = s->item
s = s->left
free"data#
8
return"s#
8
tree Edel"p,item#
tree Ep
int item
$
tree Es
if "&p#
$
printf"(Aata not a!ailable-n(#
return"p#
8
else
$
if "item < p->item#
p->left = del"p->left,item#
else
if "item > p->item#
p->ri7ht = del"p->ri7ht,item#
else
$
s = p
if"s->ri7ht == 6*FF#
$
p = s->left
free"s#
8
else
if"s->left == 6*FF#
$
p = s->ri7ht
free"s#
8
else
s->left = repl"s->left,s#
8
8
return"p#
8
!oid main"#
$
int item,depth,5uit=0
char c
tree Etemp = 6*FF
printf"(-n=ro7ram for operations of tree-n(#
do
$
printf"(-n-t?ptions-t-t-tGhoice(#
printf"(-n-t:nsert-t-t:(#
printf"(-n-tAelete-t-tA(#
printf"(-n-t01it-t-t0(#
printf"(-n-t0nter choice , (#
do
c = 7etchar"#
%hile "strchr"(:iAd0e(,c# == 6*FF#
s%itch"c#
$
case 2:2 ,
case 2i2 ,
printf"(0nter element , (#
scanf"(3d(,4item#
temp = insert"item,temp#
printf"(-n-tEEEEE Bree EEEEE-n(#
output"temp,1#
depth = 0
depthbr"temp,4depth,0#
printf"(-nAepth = 3d-n(,depth#
brea+
case 2A2 ,
case 2d2 ,
printf"(0nter element , (#
scanf"(3d(,4item#
temp = del"temp,item#
printf"(-n-tEEEEE Bree EEEEE-n(#
output"temp,1#
depth = 0
depthbr"temp,4depth,0#
printf"(-nAepth = 3d-n(,depth#
brea+
case 202 ,
case 2e2 ,
5uit = 1
8
8
%hile "&5uit#
printf"(-n(#
8
TREE CREATION
#include <stdio.h>
#include <malloc.h>
typedef struct tree
$
int item
struct tree Eleft, Eri7ht
8tree
tree Einsert"item,s#
int item
tree Es
$
if "&s#
$
s = "treeE#malloc"sizeof"tree##
s->item = item
s->left = 6*FF
s->ri7ht = 6*FF
return"s#
8
if "item < s->item#
s->left = insert"item,s->left#
else
if "item > s->item#
s->ri7ht = insert"item, s->ri7ht#
return"s#
8
!oid display"ped,le!el#
tree Eped
int le!el
$
int i
if "ped#
$
display"ped->ri7ht, le!el<1#
printf"(-n(#
for "i = 0 i < le!el i<<#
printf"( (#
printf"(3d(,ped->item#
display"ped->left, le!el<1#
8
8
!oid main"#
$
int item
tree Eped = 6*FF
printf"(=ro7ram for tree creation-n(#
%hile"1#
$
printf"(-n0nter item "-HHHH to e1it# , (#
scanf"(3d(,4item#
if "item == -HHHH#
brea+
ped = insert"item,ped#
printf"(-n-tEEEEE Bree EEEEE-n(#
display"ped,1#
8
8
TREE TRAVERSAL
#include <stdio.h>
#include <malloc.h>
struct tree
$
int item
struct tree Eleft, Eri7ht
8
struct tree Enode
int ele, tele
!oid depth"node#
struct tree Enode
$
if "node->left == 6*FF#
if "ele < tele#
ele = tele
else
else
$
tele<<
depth"node->left#
8
if "node->ri7ht == 6*FF#
if "ele < tele#
ele = tele
else
else
$
tele<<
depth"node->ri7ht#
8
tele--
8
!oid preorder"node#
struct tree Enode
$
if "node &= 6*FF#
$
printf"(3i (,node->item#
preorder"node->left#
preorder"node->ri7ht#
8
8
!oid inorder"node#
struct tree Enode
$
if "node &= 6*FF#
$
inorder"node->left#
printf"(3i (,node->item#
inorder"node->ri7ht#
8
8
!oid postorder"node#
struct tree Enode
$
if "node &= 6*FF#
$
postorder"node->left#
postorder"node->ri7ht#
printf"(3i (,node->item#
8
8
!oid disp"#
$
printf"(-n=reorder tra!ersal , (#
preorder"node#
printf"(-n:norder tra!ersal , (#
inorder"node#
printf"(-n=ostorder tra!ersal , (#
postorder"node#
printf"(-n(#
8
!oid ins"#
$
struct tree Etemp, Epre!
int n, c, chec+
do
$
printf"(0nter number of elements , (#
scanf"(3i(,4n#
fflush"stdin#
8
%hile"n < 1#
do
$
printf"(0nter element , (#
scanf"(3i(,4c#
fflush"stdin#
temp = node
chec+ = 0
if "temp == 6*FF#
$
node = "struct treeE#malloc"sizeof"struct tree##
temp = node
8
else
$
%hile "temp &= 6*FF 44 &chec+#
$
pre! = temp
if "c == temp->item#
$
printf"(3i is in the tree(,c#
printf"(-n(#
chec+ = 1
8
else
$
if "c < temp->item#
temp = temp->left
else
temp = temp->ri7ht
8
8
if "&chec+#
$
temp = "struct treeE#malloc"sizeof"struct tree##
if "c < pre!->item#
pre!->left = temp
else
pre!->ri7ht = temp
8
8
if "&chec+#
$
temp->item = c
temp->left = 6*FF
temp->ri7ht = 6*FF
n--
8
8
%hile "n > 0#
8
!oid search"c,srs,prs#
int c
struct tree EEsrs,EEprs
$
Esrs = 6*FF
Eprs = node
%hile "Eprs &= 6*FF 44 "Eprs#->item &= c#
$
Esrs = Eprs
if "c < "Eprs#->item#
"Eprs# = "Eprs#->left
else
"Eprs# = "Eprs#->ri7ht
8
8
!oid delet0"prs,srs#
struct tree Eprs,Esrs
$
struct tree Etemp
if "prs->left == 6*FF#
temp = prs->ri7ht
else
temp = prs->left
if "srs &= 6*FF#
if "prs == srs->left#
srs->left = temp
else
srs->ri7ht = temp
else
node = temp
8
!oid delet1"prs,srs#
struct tree Eprs,Esrs
$
struct tree Esuc,Esrssuc
suc = prs->ri7ht
srssuc = prs
%hile "suc->left &= 6*FF#
$
srssuc = suc
suc = suc->left
8
delet0"suc,srssuc#
if "srs &= 6*FF#
if "prs == srs->left#
srs->left = suc
else
srs->ri7ht = suc
else
node = suc
suc->left = prs->left
suc->ri7ht = prs->ri7ht
8
!oid del"#
$
struct tree Eprs,Esrs
int n,c
do
$
printf"(6umber of element "for deletion# , (#
scanf"(3i(,4n#
fflush"stdin#
8
%hile "n < 1#
do
$
printf"(0nter the element , (#
scanf"(3i(,4c#
fflush"stdin#
search"c,4srs,4prs#
if "prs == 6*FF#
printf"(0lement not found -n(#
else
if "prs->left &= 6*FF 44 prs->ri7ht &= 6*FF#
delet1"prs,srs#
else
delet0"prs,srs#
n--
8
%hile "n > 0#
8
!oid main"#
$
int 5uit=0
char c
node = 6*FF
printf"(-n=ro7ram for operations of binary tree-n(#
do
$
printf"(-n-t?ptions-t-t-tGhoice(#
printf"(-n-t:nsert-t-t:(#
printf"(-n-tAelete-t-tA(#
printf"(-n-tAisplay-t-t?(#
printf"(-n-t01it-t-t0(#
printf"(-n-t0nter choice , (#
do
c = 7etchar"#
%hile "strchr"(:iAd?o0e(,c# == 6*FF#
s%itch"c#
$
case 2:2 ,
case 2i2 ,
ins"#
brea+
case 2A2 ,
case 2d2 ,
del"#
brea+
case 2?2 ,
case 2o2 ,
disp"#
if "node == 6*FF#
printf"(Bree empty-n(#
else
$
ele = 0
tele = 0
depth"node#
printf"(Aepth of tree = 3i-n(,ele#
8
brea+
case 202 ,
case 2e2 ,
5uit = 1
8
8
%hile "&5uit#
printf"(-n(#
8

You might also like