Code:
echo -n "Enter the number : "
read x
i=1
sum=0
while test $i -le $x
do
fact=1
a=1
while test $a -le $i
do
let fact=$fact*$a
let a=$a+1
done
let sum=$sum+$fact
let i=$i+1
done
let i=2
echo -n "1!"
while test $i -le $x
do
echo -n " + $i !"
let i=$i+1
done
echo " = $sum"
Output:
[ty2011330@localhost ~]$ sh j08.sh
Enter the number : 4
1! + 2 ! + 3 ! + 4 ! = 33
Tuesday, 11 June 2013
Shell Script to accept a number n from user and display the squares of all numbers from 1 to n
Code:
echo -n "Enter the number : "
read n
c=1
a=1
while test $c -le $n
do
let a=$c*$c
echo "$c square = $a"
let c=c+1
done
Output:
[ty2011330@localhost ~]$ sh j09.sh
Enter the number : 5
1 square = 1
2 square = 4
3 square = 9
4 square = 16
5 square = 25
echo -n "Enter the number : "
read n
c=1
a=1
while test $c -le $n
do
let a=$c*$c
echo "$c square = $a"
let c=c+1
done
Output:
[ty2011330@localhost ~]$ sh j09.sh
Enter the number : 5
1 square = 1
2 square = 4
3 square = 9
4 square = 16
5 square = 25
Labels:
Shell Scripts
Shell Script to accept 5 subject marks of a student and display whether the student got first class, second class, third class or Student failed .
Code:
echo "Enter the marks of five subjects : "
read m1 m2 m3 m4 m5
let per=$m1+$m2+$m3+$m4+$m5
let per=per/5
echo "Percentage of students is $per %"
if [ $per -lt 35 ]
then
echo "Student Failed."
elif [ $per -ge 35 -a $per -lt 45 ]
then
echo "Student passed with third class."
elif [ $per -ge 45 -a $per -lt 60 ]
then
echo "Student passed with second class."
elif [ $per -ge 60 -a $per -lt 75 ]
then
echo "Student passed with first class."
fi
Output:
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
20 15 32 14 34
Percentage of students is 23 %
Student Failed.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
35 45 35 36 40
Percentage of students is 38 %
Student passed with third class.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
50 55 45 56 60
Percentage of students is 53 %
Student passed with second class.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
60 78 89 96 47
Percentage of students is 74 %
Student passed with first class
echo "Enter the marks of five subjects : "
read m1 m2 m3 m4 m5
let per=$m1+$m2+$m3+$m4+$m5
let per=per/5
echo "Percentage of students is $per %"
if [ $per -lt 35 ]
then
echo "Student Failed."
elif [ $per -ge 35 -a $per -lt 45 ]
then
echo "Student passed with third class."
elif [ $per -ge 45 -a $per -lt 60 ]
then
echo "Student passed with second class."
elif [ $per -ge 60 -a $per -lt 75 ]
then
echo "Student passed with first class."
fi
Output:
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
20 15 32 14 34
Percentage of students is 23 %
Student Failed.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
35 45 35 36 40
Percentage of students is 38 %
Student passed with third class.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
50 55 45 56 60
Percentage of students is 53 %
Student passed with second class.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
60 78 89 96 47
Percentage of students is 74 %
Student passed with first class
Shell Script to accept a number from user and print the day of the week using case … in
Code:
echo -n "Enter the day number : "
read num
case $num in
1) echo "Sunday";;
2) echo "Monday";;
3) echo "Tuesday";;
4) echo "Wednesday";;
5) echo "Thursday";;
6) echo "Friday";;
7) echo "Saturday";;
*) echo "Enter any number between 1 to 7."
esac
Output:
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 1
Sunday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 3
Tuesday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 2
Monday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 4
Wednesday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 5
Thursday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 6
Friday
echo -n "Enter the day number : "
read num
case $num in
1) echo "Sunday";;
2) echo "Monday";;
3) echo "Tuesday";;
4) echo "Wednesday";;
5) echo "Thursday";;
6) echo "Friday";;
7) echo "Saturday";;
*) echo "Enter any number between 1 to 7."
esac
Output:
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 1
Sunday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 3
Tuesday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 2
Monday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 4
Wednesday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 5
Thursday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 6
Friday
shell script to accept a number from the user and check the number entered is prime or not.
Code:
echo "Enter the number"
read num
i=2
flag=0
while [ $i -lt $num ]
do
k=`expr $num % $i`
if test $k -eq 0
then
flag=1
fi
i=`expr $i + 1`
done
if test $flag -eq 0
then
echo "Number is prime"
else
echo "Number is not prime"
fi
Output:
Enter the number
2
Number is prime
Enter the number
4
Number is not prime
echo "Enter the number"
read num
i=2
flag=0
while [ $i -lt $num ]
do
k=`expr $num % $i`
if test $k -eq 0
then
flag=1
fi
i=`expr $i + 1`
done
if test $flag -eq 0
then
echo "Number is prime"
else
echo "Number is not prime"
fi
Output:
Enter the number
2
Number is prime
Enter the number
4
Number is not prime
Shell script to accept a number from the user and display the number of days in that particular month.
Code:
echo Enter month
month=(30 31 29)
read mon
case "$mon" in
[jJ][aA][nN]*) echo "No.of days : ${month[1]} ";;
[fF][eE][bB]*) echo enter year
read year
let rem="$year % 4"
if [ $rem -eq 0 ] ; then
echo "No. of days : ${month[2]}"
else
echo "No. of days : 28 "
fi;;
[mM][aA][rR]*) echo "No. of days : ${month[1]}";;
[aA][pP][rR]*) echo "No. of days: ${month[0]}";;
[mM][aA][yY]*) echo "No. of days: ${month[1]}";;
[jJ[uU][nN]*) echo "No. of days: ${month[0]}";;
[jJ][uU][lL]*) echo "No. of days: ${month[1]}";;
[aA][uU][gG]*) echo " No. of days: ${month[1]}";;
[sS][eE][pP]*) echo " No. of days: ${month[0]}";;
[aA][cC][tT]*) echo "No. of days: ${month[1]}";;
[nN][oO][vV]*) echo "No. of days: ${month[0]}";;
[dD][eE][cC]*) echo "No. of days: ${month[1]}";;
esac
Output:
[ty2011330@localhost ~]$ vi month.sh
[ty2011330@localhost ~]$ sh month.sh
Enter month
feb
enter year
2005
No. of days : 28
echo Enter month
month=(30 31 29)
read mon
case "$mon" in
[jJ][aA][nN]*) echo "No.of days : ${month[1]} ";;
[fF][eE][bB]*) echo enter year
read year
let rem="$year % 4"
if [ $rem -eq 0 ] ; then
echo "No. of days : ${month[2]}"
else
echo "No. of days : 28 "
fi;;
[mM][aA][rR]*) echo "No. of days : ${month[1]}";;
[aA][pP][rR]*) echo "No. of days: ${month[0]}";;
[mM][aA][yY]*) echo "No. of days: ${month[1]}";;
[jJ[uU][nN]*) echo "No. of days: ${month[0]}";;
[jJ][uU][lL]*) echo "No. of days: ${month[1]}";;
[aA][uU][gG]*) echo " No. of days: ${month[1]}";;
[sS][eE][pP]*) echo " No. of days: ${month[0]}";;
[aA][cC][tT]*) echo "No. of days: ${month[1]}";;
[nN][oO][vV]*) echo "No. of days: ${month[0]}";;
[dD][eE][cC]*) echo "No. of days: ${month[1]}";;
esac
Output:
[ty2011330@localhost ~]$ vi month.sh
[ty2011330@localhost ~]$ sh month.sh
Enter month
feb
enter year
2005
No. of days : 28
shell script to accept two numbers from user and display n^y using while loop.
Code:
echo Enter value for n:
read n
echo Enter value for y:
read y
iter=1
sum=1
while [ $iter -le $y ]
do
let sum=sum*n
let iter=$iter+1
done
echo $sum
Output:
[ty2011330@localhost ~]$ sh file21.sh
Enter value for n:
2
Enter value for y:
3
8
echo Enter value for n:
read n
echo Enter value for y:
read y
iter=1
sum=1
while [ $iter -le $y ]
do
let sum=sum*n
let iter=$iter+1
done
echo $sum
Output:
[ty2011330@localhost ~]$ sh file21.sh
Enter value for n:
2
Enter value for y:
3
8
shell script to accept a number from user and display multiplication table of given number.
Code:
echo "Multiplication Table:"
tab=1
i=1
a=10
echo "Input Number :"
read x
echo
echo "Table of $x"
for i in 1 2 3 4 5 6 7 8 9 10
do
i=`expr $x \* $i`
echo $i
done
Output:
[pritam1@localhost ~]$ sh demofor.sh
Multiplication Table:
Input Number :
15
Table of 15
15
30
45
60
75
90
105
120
135
150
echo "Multiplication Table:"
tab=1
i=1
a=10
echo "Input Number :"
read x
echo
echo "Table of $x"
for i in 1 2 3 4 5 6 7 8 9 10
do
i=`expr $x \* $i`
echo $i
done
Output:
[pritam1@localhost ~]$ sh demofor.sh
Multiplication Table:
Input Number :
15
Table of 15
15
30
45
60
75
90
105
120
135
150
shell script for menu driven program to see the contents of /etc/passwd, to see the list of users, to see present working directory or to exit.
Code:
echo enter
echo 1 to see the contents of /etc/passwd
echo 2 to see list of users
echo 3 to see present working directory
echo 4 exit
echo enter your choice
read n
case $n in
1) ls /etc ;;
2) ls /home;;
3) pwd;;
4) exit;;
*) echo enter the choice as 1,2,3or4;;
esac
Output:
[ty2011330@localhost ~]$ sh file13.sh
enter
1 to see the contents of /etc/passwd
2 to see list of users
3 to see present working directory
4 exit
enter your choice
2
administrator pritam ty11305 ty11313 ty11321 ty11329 ty11337 ty11345
amita pritam1 ty11306 ty11314 ty11322 ty11330 ty11338 ty11346
dipti shital ty11307 ty11315 ty11323 ty11331 ty11339 ty11347
exam5154 shital1 ty11308 ty11316 ty11324 ty11332 ty11340 ty11348
enter your choice
1
a2ps.cfg inittab quotatab
a2ps-site.cfg inputrc racoon
acpi iproute2 raddb
enter your choice
3
/home/tybsc
enter your choice
4
2.
ans=Y
while [ $ans = "Y" ]
do
echo " MENU"
echo "1 . Query on product"
echo "2 . Query on customer"
echo "0 . Exit"
echo "enter ur choice : "
read ch
case "$ch" in
1) echo "Enter product number :"
read pno
grep $pno masterdata
echo "Query on other product : "
read ans
if(test $ans = "Y")
then
continue
fi;;
2) echo "Enter customer number :"
read cno
grep $cno transdata;;
0) exit;;
*) echo "Invalid choice. Try again..."
esac
done
echo enter
echo 1 to see the contents of /etc/passwd
echo 2 to see list of users
echo 3 to see present working directory
echo 4 exit
echo enter your choice
read n
case $n in
1) ls /etc ;;
2) ls /home;;
3) pwd;;
4) exit;;
*) echo enter the choice as 1,2,3or4;;
esac
Output:
[ty2011330@localhost ~]$ sh file13.sh
enter
1 to see the contents of /etc/passwd
2 to see list of users
3 to see present working directory
4 exit
enter your choice
2
administrator pritam ty11305 ty11313 ty11321 ty11329 ty11337 ty11345
amita pritam1 ty11306 ty11314 ty11322 ty11330 ty11338 ty11346
dipti shital ty11307 ty11315 ty11323 ty11331 ty11339 ty11347
exam5154 shital1 ty11308 ty11316 ty11324 ty11332 ty11340 ty11348
enter your choice
1
a2ps.cfg inittab quotatab
a2ps-site.cfg inputrc racoon
acpi iproute2 raddb
enter your choice
3
/home/tybsc
enter your choice
4
2.
ans=Y
while [ $ans = "Y" ]
do
echo " MENU"
echo "1 . Query on product"
echo "2 . Query on customer"
echo "0 . Exit"
echo "enter ur choice : "
read ch
case "$ch" in
1) echo "Enter product number :"
read pno
grep $pno masterdata
echo "Query on other product : "
read ans
if(test $ans = "Y")
then
continue
fi;;
2) echo "Enter customer number :"
read cno
grep $cno transdata;;
0) exit;;
*) echo "Invalid choice. Try again..."
esac
done
Shell Program to find the type of the character entered
echo Enter any character
read c
case $c in
[a-z]) echo Small Case letter;;
[A-Z]) echo Capital letter;;
[0-9]) echo digit;;
?) echo special symbol;;
*) echo more than one character, reenter;;
esac
11)#Program to find the pattern
echo Enter the word
read str
case $str in
[aeiou]*) echo The word begins with a vowel;;
[0-9]*) echo The word starts with a digit;;
*[0-9]) echo The word ends with a digit;;
????) echo The word entered is 4 lettered word;;
*) echo The word entered is either starts with a
consonant or incorrect input;;
esac
read c
case $c in
[a-z]) echo Small Case letter;;
[A-Z]) echo Capital letter;;
[0-9]) echo digit;;
?) echo special symbol;;
*) echo more than one character, reenter;;
esac
11)#Program to find the pattern
echo Enter the word
read str
case $str in
[aeiou]*) echo The word begins with a vowel;;
[0-9]*) echo The word starts with a digit;;
*[0-9]) echo The word ends with a digit;;
????) echo The word entered is 4 lettered word;;
*) echo The word entered is either starts with a
consonant or incorrect input;;
esac
Shell Script to accept a file name and check whether the file name exist or not in current directory .
Code:
echo -n "Enter the Filename : "
read fname
if [ -f $fname ]
then
echo "File exists in the current directory. "
else
echo "File with this name does not exist in the current directory."
fi
Output:
[ty2011330@localhost ~]$ sh j03.sh
Enter the Filename : file5
File exists in the current directory.
[ty2011330@localhost ~]$ sh j03.sh
Enter the Filename : file12
File with this name doesnot exist in the current directory.
echo -n "Enter the Filename : "
read fname
if [ -f $fname ]
then
echo "File exists in the current directory. "
else
echo "File with this name does not exist in the current directory."
fi
Output:
[ty2011330@localhost ~]$ sh j03.sh
Enter the Filename : file5
File exists in the current directory.
[ty2011330@localhost ~]$ sh j03.sh
Enter the Filename : file12
File with this name doesnot exist in the current directory.
Shell Script to accept a number from user and check whether the number is even or odd .
Code:
echo -n "Enter the number : "
read n
let rem=n%2
if test $rem -eq 0
then
echo "$n is a even number ."
else
echo "$n is a odd number ."
fi
Output:
[ty2011330@localhost ~]$ sh j11.sh
Enter the number : 7
7 is a odd number .
[ty2011330@localhost ~]$ sh j11.sh
Enter the number : 10
10 is a even number .
echo -n "Enter the number : "
read n
let rem=n%2
if test $rem -eq 0
then
echo "$n is a even number ."
else
echo "$n is a odd number ."
fi
Output:
[ty2011330@localhost ~]$ sh j11.sh
Enter the number : 7
7 is a odd number .
[ty2011330@localhost ~]$ sh j11.sh
Enter the number : 10
10 is a even number .
Shell Script to accept a character from user and display whether it is alphabet, digit, special character or user has entered more than one character.
Code:
echo -n "Enter the Character : "
read c
case $c in
[ABCDEFGHIJKLMNOPQRSTUVWXYZZ]) echo "Alphabet";;
[a-z]) echo "Alphabet";;
[0-9]) echo "Digit";;
?) echo "special Character";;
*) echo "More than two Characters";;
esac
let i=2
echo -n "1!"
while test $i -le $x
do
echo -n " + $i !"
let i=$i+1
done
echo " = $sum"
Output:
[ty2011330@localhost ~]$ sh j12.sh
Enter the Character : A
Alphabet
[ty2011330@localhost ~]$ sh j12.sh
Enter the Character : f
Alphabet
[ty2011330@localhost ~]$ sh j12.sh
Enter the Character : 90
More than two Characters
echo -n "Enter the Character : "
read c
case $c in
[ABCDEFGHIJKLMNOPQRSTUVWXYZZ]) echo "Alphabet";;
[a-z]) echo "Alphabet";;
[0-9]) echo "Digit";;
?) echo "special Character";;
*) echo "More than two Characters";;
esac
let i=2
echo -n "1!"
while test $i -le $x
do
echo -n " + $i !"
let i=$i+1
done
echo " = $sum"
Output:
[ty2011330@localhost ~]$ sh j12.sh
Enter the Character : A
Alphabet
[ty2011330@localhost ~]$ sh j12.sh
Enter the Character : f
Alphabet
[ty2011330@localhost ~]$ sh j12.sh
Enter the Character : 90
More than two Characters
shell script to accept a number from the user and display all prime numbers till the number user enters.
Code:
echo Enter limit
read limit
j=1
flag=1
echo "Prime Number Series"
while [ $j -le $limit ]
do
i=2
while [ $i -lt $j ]
do
temp=`expr $j % $i`
if [ $temp -eq 0 ]
then
flag=0
break;
else
flag=1
fi
i=`expr $i + 1`
done
if test $flag -eq 1
then
echo $j
fi
j=`expr $j + 1`
done
Output:
Enter limit
15
Prime Number Series
1
2
3
5
7
11
13
echo Enter limit
read limit
j=1
flag=1
echo "Prime Number Series"
while [ $j -le $limit ]
do
i=2
while [ $i -lt $j ]
do
temp=`expr $j % $i`
if [ $temp -eq 0 ]
then
flag=0
break;
else
flag=1
fi
i=`expr $i + 1`
done
if test $flag -eq 1
then
echo $j
fi
j=`expr $j + 1`
done
Output:
Enter limit
15
Prime Number Series
1
2
3
5
7
11
13
shell script to accept a character from the user and check whether the word begins with vowel or begins with digit or ends with digit or is a four lettered word or a consonant.
Code:
echo Enter the word
read str
case $str in
[aeiou]*) echo The word begins with a vowel;;
[0-9]*) echo The word starts with a digit;;
*[0-9]) echo The word ends with a digit;;
????) echo The word entered is 4 lettered word;;
*) echo The word entered is either starts with a
consonant or incorrect input;;
esac
Output:
[ty2011330@localhost ~]$ sh file12.sh
Enter the word
a
The word begins with a vowel
Enter the word
hello
The word entered is either starts with a consonant or incorrect input
Enter the word
film
The word entered is 4 lettered word
Enter the word
*9
The word ends with a digit
echo Enter the word
read str
case $str in
[aeiou]*) echo The word begins with a vowel;;
[0-9]*) echo The word starts with a digit;;
*[0-9]) echo The word ends with a digit;;
????) echo The word entered is 4 lettered word;;
*) echo The word entered is either starts with a
consonant or incorrect input;;
esac
Output:
[ty2011330@localhost ~]$ sh file12.sh
Enter the word
a
The word begins with a vowel
Enter the word
hello
The word entered is either starts with a consonant or incorrect input
Enter the word
film
The word entered is 4 lettered word
Enter the word
*9
The word ends with a digit
shell script to accept a file name from user and check whether file has permission to write. If write permission is there then append to the file
Code:
echo Enter the file name
read fname
if [ -w $fname ]
then
echo Type text to append. To stop press Ctrl D.
cat >> $fname
else
echo The file has no write permission
fi
Output:
[ty2011330@localhost ~]$ sh per.sh
Enter the file name
b2
The file has no write permission
[ty2011330@localhost ~]$ sh per.sh
Enter the file name
prime.sh
Type text to append. To stop press Ctrl D.
echo Enter the file name
read fname
if [ -w $fname ]
then
echo Type text to append. To stop press Ctrl D.
cat >> $fname
else
echo The file has no write permission
fi
Output:
[ty2011330@localhost ~]$ sh per.sh
Enter the file name
b2
The file has no write permission
[ty2011330@localhost ~]$ sh per.sh
Enter the file name
prime.sh
Type text to append. To stop press Ctrl D.
Shell Script to convert number in binary
echo Enter the number
read no
a=$no
s=0
x=1
while(test $a -ne 0)
do
temp=`expr $a % 2`
a=`expr $a / 2`
y=`expr $temp "*" $x`
s=`expr $s + $y`
x=`expr $x "*" 10`
done
read no
a=$no
s=0
x=1
while(test $a -ne 0)
do
temp=`expr $a % 2`
a=`expr $a / 2`
y=`expr $temp "*" $x`
s=`expr $s + $y`
x=`expr $x "*" 10`
done
Shell Script to accept a number and display whether the number is positive, negative or zero.
Code:
echo -n "Enter the Number : "
read x
if [ $x -lt 0 ]
then
echo "$x is Negative"
elif [ $x -eq 0 ]
then
echo "Number entered is Zero"
else
echo "$x is Positive"
fi
Output:
[ty2011330@localhost ~]$ sh j02.sh
Enter the Number : 7
7 is Positive
[ty2011330@localhost ~]$ sh j02.sh
Enter the Number : -9
-9 is Negative
[ty2011330@localhost ~]$ sh j02.sh
Enter the Number : 0
Number entered is Zero
echo -n "Enter the Number : "
read x
if [ $x -lt 0 ]
then
echo "$x is Negative"
elif [ $x -eq 0 ]
then
echo "Number entered is Zero"
else
echo "$x is Positive"
fi
Output:
[ty2011330@localhost ~]$ sh j02.sh
Enter the Number : 7
7 is Positive
[ty2011330@localhost ~]$ sh j02.sh
Enter the Number : -9
-9 is Negative
[ty2011330@localhost ~]$ sh j02.sh
Enter the Number : 0
Number entered is Zero
shell script to accept a number from the user and display even number series till that number
Code:
no=0
echo "Enter limit"
read limit
echo "Even number series"
while [ $no -le $limit ]
do
echo $no
let no= $no + 2
done
Output:
[ty2011330@localhost ~]$ sh even.sh
Enter limit
10
Even number series
0
2
4
6
8
10
no=0
echo "Enter limit"
read limit
echo "Even number series"
while [ $no -le $limit ]
do
echo $no
let no= $no + 2
done
Output:
[ty2011330@localhost ~]$ sh even.sh
Enter limit
10
Even number series
0
2
4
6
8
10
Shell program to display summation of 1+2+3+4………+n
Code:
echo Enter the value for n:
read num
sum=0
iter=1
while [ $iter -le $num ]
do
let sum=$sum+$iter
let iter=$iter+1
done
echo $sum
Output:
[ty2011330@localhost ~]$ sh summation.sh
Enter the value for n:
10
55
echo Enter the value for n:
read num
sum=0
iter=1
while [ $iter -le $num ]
do
let sum=$sum+$iter
let iter=$iter+1
done
echo $sum
Output:
[ty2011330@localhost ~]$ sh summation.sh
Enter the value for n:
10
55
Labels:
Shell Scripts
Sunday, 9 June 2013
PL/SQL cursors
PL/SQL block:
DECLARE
cursor aa is
select * from emp1;
aa1 aa%rowtype;
BEGIN
open aa;
if aa%isopen then
loop
fetch aa into aa1;
EXIT when aa%NOTFOUND;
if aa%FOUND then
dbms_output.put_line(aa1.eno || aa1.name);
end if;
end loop;
else
dbms_output.put_line('not open');
end if;
END;
DECLARE
cursor aa is
select * from emp1;
aa1 aa%rowtype;
BEGIN
open aa;
if aa%isopen then
loop
fetch aa into aa1;
EXIT when aa%NOTFOUND;
if aa%FOUND then
dbms_output.put_line(aa1.eno || aa1.name);
end if;
end loop;
else
dbms_output.put_line('not open');
end if;
END;
PL/SQL to fetch data using cursors
Creating a table:
Create table emp_g(eno number,e_name varchar2(20))
Inserting values in the table:
Insert into emp_g(eno,e_name) values(1,'steve');
PL/SQL block:
DECLARE
cursor emp_detail (eno1 number) is select e_name from emp_g where eno=eno1;
eno2 emp_g.eno%type:=2;
name2 emp_g.e_name%type;
BEGIN
Open emp_detail(eno2);
Fetch emp_detail into name2;
dbms_output.put_line(name2);
Close emp_detail;
END;
Create table emp_g(eno number,e_name varchar2(20))
Inserting values in the table:
Insert into emp_g(eno,e_name) values(1,'steve');
PL/SQL block:
DECLARE
cursor emp_detail (eno1 number) is select e_name from emp_g where eno=eno1;
eno2 emp_g.eno%type:=2;
name2 emp_g.e_name%type;
BEGIN
Open emp_detail(eno2);
Fetch emp_detail into name2;
dbms_output.put_line(name2);
Close emp_detail;
END;
PL/SQL program for explicit cursors
Creating a table:
Create table emp_29(e_no number, e_name varchar2(20),e_sal number)
Inserting values in the table:
Insert into emp3 (e_no,e_name,e_sal) values(1,'Mak',20000);
Insert into emp3 (e_no,e_name,e_sal) values(2,'Monish',25000);
Insert into emp3 (e_no,e_name,e_sal) values(3,'Murthy',25000);
PL/SQL block:
DECLARE
cursor emp_disp is
select e_name from emp3 ;
name1 emp3.e_name%type;
BEGIN
open emp_disp;
if emp_disp%ISOPEN then
loop
fetch emp_disp into name1;
exit when emp_disp%NOTFOUND;
if emp_disp%FOUND then
dbms_output.put_line(name1);
end if;
end loop;
commit;
else
dbms_output.put_line('unable to open the cursor');
end if;
close emp_disp;
END;
Create table emp_29(e_no number, e_name varchar2(20),e_sal number)
Inserting values in the table:
Insert into emp3 (e_no,e_name,e_sal) values(1,'Mak',20000);
Insert into emp3 (e_no,e_name,e_sal) values(2,'Monish',25000);
Insert into emp3 (e_no,e_name,e_sal) values(3,'Murthy',25000);
PL/SQL block:
DECLARE
cursor emp_disp is
select e_name from emp3 ;
name1 emp3.e_name%type;
BEGIN
open emp_disp;
if emp_disp%ISOPEN then
loop
fetch emp_disp into name1;
exit when emp_disp%NOTFOUND;
if emp_disp%FOUND then
dbms_output.put_line(name1);
end if;
end loop;
commit;
else
dbms_output.put_line('unable to open the cursor');
end if;
close emp_disp;
END;
PL/SQL program for simple cursors
Create table employee29(eno varchar2(30),salary number(10));
Inserting values in the table:
Insert into employee29 values('e1001',20000);
Insert into employee29 values('e1002',15000);
Insert into employee29 values('e1003',17000);
Insert into employee29 values('e1004',10000);
Insert into employee29 values('e1005',21000);
PL/SQL block:
BEGIN
update employee29 set salary=salary+1000 where eno='&eno';
if SQL%FOUND then
dbms_output.put_line('updated successfully');
end if;
if SQL%NOTFOUND then
dbms_output.put_line('not existing');
end if;
END;
Output:
Inserting values in the table:
Insert into employee29 values('e1001',20000);
Insert into employee29 values('e1002',15000);
Insert into employee29 values('e1003',17000);
Insert into employee29 values('e1004',10000);
Insert into employee29 values('e1005',21000);
PL/SQL block:
BEGIN
update employee29 set salary=salary+1000 where eno='&eno';
if SQL%FOUND then
dbms_output.put_line('updated successfully');
end if;
if SQL%NOTFOUND then
dbms_output.put_line('not existing');
end if;
END;
Output:
PL/SQL program to show GOTO statement
CREATE TABLE MyTable29( num_col NUMBER,char_col VARCHAR2(60));
PL\SQL Block:
DECLARE
v_Counter BINARY_INTEGER := 1;
BEGIN
LOOP
INSERT INTO MyTable29
VALUES (v_Counter, 'Loop count');
v_Counter := v_Counter + 1;
IF v_Counter > 50 THEN
GOTO l_EndOfLoop;
END IF;
END LOOP;
<<l_EndOfLoop>>
INSERT INTO MyTable29 (char_col)
VALUES ('Done!');
END;
PL\SQL Block:
DECLARE
v_Counter BINARY_INTEGER := 1;
BEGIN
LOOP
INSERT INTO MyTable29
VALUES (v_Counter, 'Loop count');
v_Counter := v_Counter + 1;
IF v_Counter > 50 THEN
GOTO l_EndOfLoop;
END IF;
END LOOP;
<<l_EndOfLoop>>
INSERT INTO MyTable29 (char_col)
VALUES ('Done!');
END;
PL/SQL program to show IF statement
PL\SQL Block:
DECLARE
counter NUMBER := 1;
BEGIN
WHILE (counter < 5) LOOP
IF counter = 2 THEN
GOTO loopindex;
ELSE
dbms_output.put_line('Index ['||counter||'].');
END IF;
<< loopindex >>
IF counter >= 1 THEN
counter := counter + 1;
END IF;
END LOOP;
END;
DECLARE
counter NUMBER := 1;
BEGIN
WHILE (counter < 5) LOOP
IF counter = 2 THEN
GOTO loopindex;
ELSE
dbms_output.put_line('Index ['||counter||'].');
END IF;
<< loopindex >>
IF counter >= 1 THEN
counter := counter + 1;
END IF;
END LOOP;
END;
PL/SQL program to show Case statement
Creating the table:
Create table lecturer29(lid varchar2(10),major_sub varchar2(10));
Inserting values in the table:
Insert into lecturer29(lid,major_sub) values('l001','computer');
Code Block:
select lid,major_sub,
case major_sub
when 'computer' then 'BSC(cs)'
when 'accounts' then 'BCom'
when 'photography' then 'BMM'
when NULL then 'BMS'
else 'no course mentioned'
end as course
from lecturer29;
Create table lecturer29(lid varchar2(10),major_sub varchar2(10));
Inserting values in the table:
Insert into lecturer29(lid,major_sub) values('l001','computer');
Code Block:
select lid,major_sub,
case major_sub
when 'computer' then 'BSC(cs)'
when 'accounts' then 'BCom'
when 'photography' then 'BMM'
when NULL then 'BMS'
else 'no course mentioned'
end as course
from lecturer29;
Loop in PL/SQL
Create table suppliers29 (sup_id varchar2(10), sup_name varchar2(20),sup_address varchar2(20));
PL\SQL Block:
DECLARE
contin varchar2(10);
BEGIN
contin:='&contin';
while(contin='y')
loop
Insert into suppliers29 values ('&sup_id','&sup_name','&sup_address');
dbms_output.put_line('Want another (y/n)');
contin:='&contin';
if contin='n' then
exit;
end if;
end loop;
END;
PL\SQL Block:
DECLARE
contin varchar2(10);
BEGIN
contin:='&contin';
while(contin='y')
loop
Insert into suppliers29 values ('&sup_id','&sup_name','&sup_address');
dbms_output.put_line('Want another (y/n)');
contin:='&contin';
if contin='n' then
exit;
end if;
end loop;
END;
PL/SQL program to update Table with select query
Create table place_jack (room_id varchar2(5), building varchar2(20), room_no number(5), no_of_seats number(5),description varchar2(50)) ;
Inserting values in the table:
Insert into place_jack (room_id, building, room_no, no_of_seats) values(‘p001’,’A’,101,30);
Insert into place_jack (room_id, building, room_no, no_of_seats) values(‘p002’,’B’,202,15);
Insert into place_jack (room_id, building, room_no, no_of_seats) values(‘p003’,’C’,204,35);
PL\SQL Block:
DECLARE
nos number(5);
rid varchar2(5);
BEGIN
rid:='&rid';
SELECT no_of_seats INTO nos from place_jack where room_id=rid;
if nos<20 then
update place_jack set description='fairly small' where room_id=rid;
else
if nos between 20 and 40 then
update place_jack set description='a little bigger' where room_id=rid;
else
if nos between 20 and 40 then
update place_jack set description='lots of room' where room_id=rid;
end if;
end if;
end if;
END;
Inserting values in the table:
Insert into place_jack (room_id, building, room_no, no_of_seats) values(‘p001’,’A’,101,30);
Insert into place_jack (room_id, building, room_no, no_of_seats) values(‘p002’,’B’,202,15);
Insert into place_jack (room_id, building, room_no, no_of_seats) values(‘p003’,’C’,204,35);
PL\SQL Block:
DECLARE
nos number(5);
rid varchar2(5);
BEGIN
rid:='&rid';
SELECT no_of_seats INTO nos from place_jack where room_id=rid;
if nos<20 then
update place_jack set description='fairly small' where room_id=rid;
else
if nos between 20 and 40 then
update place_jack set description='a little bigger' where room_id=rid;
else
if nos between 20 and 40 then
update place_jack set description='lots of room' where room_id=rid;
end if;
end if;
end if;
END;
PL/SQL program to update a table
Create table emp29(name varchar2(22),startdate date,salary number(33),flag number(22));
Inserting values in the table:
Insert into emp29 values('abc','1-mar-99',4000,1);
Insert into emp29 values('ac','1-mar-99',40040,0);
Insert into emp29 values('bc','1-mar-99',1000,1);
PL/SQL block:
DECLARE
date_compare date;
date_ofjoin date;
BEGIN
date_compare:='&date_compare';
if(to_char(date_ofjoin) < to_char(date_compare)) then
update emp29
set salary=salary+(salary*0.15);
else
update emp29 set salary=salary+(salary*0.5) ;
end if;
END;
Inserting values in the table:
Insert into emp29 values('abc','1-mar-99',4000,1);
Insert into emp29 values('ac','1-mar-99',40040,0);
Insert into emp29 values('bc','1-mar-99',1000,1);
PL/SQL block:
DECLARE
date_compare date;
date_ofjoin date;
BEGIN
date_compare:='&date_compare';
if(to_char(date_ofjoin) < to_char(date_compare)) then
update emp29
set salary=salary+(salary*0.15);
else
update emp29 set salary=salary+(salary*0.5) ;
end if;
END;
PL/SQL program for Inserting Values in a table.
Creating the table:
Create table suppliers29 (s_id varchar2(20), name varchar2(20), salary number(10));
PL\SQL Block:
DECLARE
status varchar2(20);
BEGIN
status:='&status';
loop
if(status='y')then
insert into supplier29 values('s1001','abc',8765);
end if;
status:='&status';
dbms_output.put_line('want another(y/n)');
if(status='n')then
exit;
end if;
end loop;
END;
Create table suppliers29 (s_id varchar2(20), name varchar2(20), salary number(10));
PL\SQL Block:
DECLARE
status varchar2(20);
BEGIN
status:='&status';
loop
if(status='y')then
insert into supplier29 values('s1001','abc',8765);
end if;
status:='&status';
dbms_output.put_line('want another(y/n)');
if(status='n')then
exit;
end if;
end loop;
END;
PL/SQL program for defining and calling simple function
create or replace function Addition(x number,y number)
return number is
z number;
begin
z:=x+y;
return z;
end;
---------------------------------------
declare
z number;
begin
z:=Addition(5,2);
dbms_output.put_line('The Addition:-'||z);
end;
Output:
The Addition:-7
PL/SQL procedure successfully completed.
return number is
z number;
begin
z:=x+y;
return z;
end;
---------------------------------------
declare
z number;
begin
z:=Addition(5,2);
dbms_output.put_line('The Addition:-'||z);
end;
Output:
The Addition:-7
PL/SQL procedure successfully completed.
Basic PL/SQL Loop Program
DECLARE
counts number(2);
BEGIN
counts:=1;
dbms_output.put_line('before my loop');
loop
if(counts>4)then
exit;
end if;
dbms_output.put_line('iteration no'||counts);
counts:=counts+1;
end loop;
END;
counts number(2);
BEGIN
counts:=1;
dbms_output.put_line('before my loop');
loop
if(counts>4)then
exit;
end if;
dbms_output.put_line('iteration no'||counts);
counts:=counts+1;
end loop;
END;
Wednesday, 5 June 2013
Queue using Array
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class queue
{
int array[15];
int front,rear;
public:
queue()
{
front=-1;
rear=-1;
}
void enqueue(int);
void dequeue();
void print();
};
void queue::print()
{
if(front <=rear)
{
for(int i=front;i<=rear;i++)
{
cout<<endl<<array[i];
}
}
else
cout<<"\n queue is empty ......";
}
void queue::dequeue()
{
if(rear < front)
{
rear=-1;
front=-1;
cout<<"\n queue is empty ......";
}
else
{
cout<<array[front]<<" is dequeue out form the queue";
front++;
}
}
void queue::enqueue(int data)
{
if(rear<15)
{
rear=rear+1;
array[rear]=data;
if(front<0) front=0;
}
else
cout<<"\n\t queue Full";
}
void main()
{
clrscr();
queue s;
int count,data;
char choice,option;
do
{
clrscr();
cout<<"\n\t Select Operation \n";
cout<<"\n\t1. To Create the queue";
cout<<"\n\t2. To Display the queue";
cout<<"\n\t3. To Add the data in the queue";
cout<<"\n\t4. To Delete the data from the queue";
cout<<"\n\t0. Exit";
cout<<"\n\n\t Enter your choice:- ";
cin>>option;
switch(option)
{
case '1':
{
cout<<"\n \t How many item you want to added in the queue : ";
cin>>count;
for(int i=0;i<count;i++)
{
cout<<"\n \t Enter data which is need to be added in the queue: ";
cin>>data;
s.enqueue(data);
}
break;
}
case '2':
{
cout<<"\n \t Data in the queue";
s.print();
break;
}
case '3':
{
cout<<"\n \t Enter data which is need to be added in the queue: ";
cin>>data;
s.enqueue(data);
break;
}
case '4':
{
s.dequeue();
break;
}
case '0':
{
exit(0);
break;
}
default:
{
cout<<"\n \tInvalid Option";
break;
}
}
cout<<"\n\n \tDo you want to continue (y/n):- ";
cin>>choice;
}while(choice!='n');
getch();
}
#include<conio.h>
#include<stdlib.h>
class queue
{
int array[15];
int front,rear;
public:
queue()
{
front=-1;
rear=-1;
}
void enqueue(int);
void dequeue();
void print();
};
void queue::print()
{
if(front <=rear)
{
for(int i=front;i<=rear;i++)
{
cout<<endl<<array[i];
}
}
else
cout<<"\n queue is empty ......";
}
void queue::dequeue()
{
if(rear < front)
{
rear=-1;
front=-1;
cout<<"\n queue is empty ......";
}
else
{
cout<<array[front]<<" is dequeue out form the queue";
front++;
}
}
void queue::enqueue(int data)
{
if(rear<15)
{
rear=rear+1;
array[rear]=data;
if(front<0) front=0;
}
else
cout<<"\n\t queue Full";
}
void main()
{
clrscr();
queue s;
int count,data;
char choice,option;
do
{
clrscr();
cout<<"\n\t Select Operation \n";
cout<<"\n\t1. To Create the queue";
cout<<"\n\t2. To Display the queue";
cout<<"\n\t3. To Add the data in the queue";
cout<<"\n\t4. To Delete the data from the queue";
cout<<"\n\t0. Exit";
cout<<"\n\n\t Enter your choice:- ";
cin>>option;
switch(option)
{
case '1':
{
cout<<"\n \t How many item you want to added in the queue : ";
cin>>count;
for(int i=0;i<count;i++)
{
cout<<"\n \t Enter data which is need to be added in the queue: ";
cin>>data;
s.enqueue(data);
}
break;
}
case '2':
{
cout<<"\n \t Data in the queue";
s.print();
break;
}
case '3':
{
cout<<"\n \t Enter data which is need to be added in the queue: ";
cin>>data;
s.enqueue(data);
break;
}
case '4':
{
s.dequeue();
break;
}
case '0':
{
exit(0);
break;
}
default:
{
cout<<"\n \tInvalid Option";
break;
}
}
cout<<"\n\n \tDo you want to continue (y/n):- ";
cin>>choice;
}while(choice!='n');
getch();
}
Priority Queue in Data Structure
#include<iostream.h>
#include<conio.h>
class pqueue
{
pqueue *pre;
int data;
int pri;
int num,prio;
pqueue *next;
void add(int,int);
int remove(void);
public :
void display(void);
void priority(void);
};
pqueue *front;
pqueue *rear;
void pqueue :: add(int num,int pri)
{
if((front==NULL) && (rear==NULL))
{
front=rear= new pqueue;
rear->next=NULL;
rear->data=num;
rear->pri=pri;
rear->pre=NULL;
}
else
{
rear->next= new pqueue;
rear->next->pre= rear;
rear->next->data = num;
rear->next->next=NULL;
rear->next->pri=pri;
rear = rear->next;
}
}
void pqueue :: display(void)
{
pqueue *q = front;
if(front==NULL && rear== NULL)
{
cout<<"Queue is empty"<<endl;
}
else
{
while(q !=NULL)
{
cout<<"Numbers are:"<<endl<< q->data<<endl;
q = q->next;
}
}
}
int pqueue :: remove (void)
{
int result;
// queue *q=front;
if(front==NULL && rear==NULL)
{
cout<<"Queue is empty"<<endl;
return -32767;
}
result = front->data;
front = front->next;
front->pre = NULL;
if(front != NULL)
{
delete (rear->next);
rear ->next = NULL;
}
else
{
delete (front);
front=NULL;
}
return result;
}
void pqueue :: priority(void)
{
if(front==NULL && rear == NULL)
{
cout<<"Queue is Empty" <<endl;
char ch='y';
cout<<"Do you want to add new?"<<endl;
cin>>ch;
while(ch=='y')
{
cout<<"Enter the data "<<endl;
cin>>data;
cout<<"Enter the priority"<<endl;
cin>>pri;
add(data,pri);
break;
}
}
else
{
if( rear->pri >= front->pri)
{
cout<<"Enter The data"<<endl;
cin>>data;
cout<<"Enter the priority"<<endl;
cin>>pri;
add(data,pri);
}
else
{
int r= remove();
cout<<"Removed = "<<r<<endl;
}
}
}
void main()
{
clrscr();
int num;
char choice='y';
pqueue q;
front=rear=NULL;
int option;
while(choice == 'y')
{
cout<<"1.Priority operation"<<endl;
cout<<"2.Display"<<endl;
cout<<"enter the option"<<endl;
cin>>option;
switch(option)
{
case 1:
{
q.priority();
cout<<"Do you want to cont...?"<<endl;
break;
}
case 2:
{
q.display();
cout<<"Do you want to cont...?"<<endl;
break;
}
case 5:
{
int r = q.remove();
cout<<"Removed ="<<r<<endl;
cout<<"Do you want to cnt....?"<<endl;
break;
} */
default:
{
cout<<"Wrong input"<<endl;
cout<<"Do you want to cont...?"<<endl;
break;
}
}
choice=getch();
}
}
#include<conio.h>
class pqueue
{
pqueue *pre;
int data;
int pri;
int num,prio;
pqueue *next;
void add(int,int);
int remove(void);
public :
void display(void);
void priority(void);
};
pqueue *front;
pqueue *rear;
void pqueue :: add(int num,int pri)
{
if((front==NULL) && (rear==NULL))
{
front=rear= new pqueue;
rear->next=NULL;
rear->data=num;
rear->pri=pri;
rear->pre=NULL;
}
else
{
rear->next= new pqueue;
rear->next->pre= rear;
rear->next->data = num;
rear->next->next=NULL;
rear->next->pri=pri;
rear = rear->next;
}
}
void pqueue :: display(void)
{
pqueue *q = front;
if(front==NULL && rear== NULL)
{
cout<<"Queue is empty"<<endl;
}
else
{
while(q !=NULL)
{
cout<<"Numbers are:"<<endl<< q->data<<endl;
q = q->next;
}
}
}
int pqueue :: remove (void)
{
int result;
// queue *q=front;
if(front==NULL && rear==NULL)
{
cout<<"Queue is empty"<<endl;
return -32767;
}
result = front->data;
front = front->next;
front->pre = NULL;
if(front != NULL)
{
delete (rear->next);
rear ->next = NULL;
}
else
{
delete (front);
front=NULL;
}
return result;
}
void pqueue :: priority(void)
{
if(front==NULL && rear == NULL)
{
cout<<"Queue is Empty" <<endl;
char ch='y';
cout<<"Do you want to add new?"<<endl;
cin>>ch;
while(ch=='y')
{
cout<<"Enter the data "<<endl;
cin>>data;
cout<<"Enter the priority"<<endl;
cin>>pri;
add(data,pri);
break;
}
}
else
{
if( rear->pri >= front->pri)
{
cout<<"Enter The data"<<endl;
cin>>data;
cout<<"Enter the priority"<<endl;
cin>>pri;
add(data,pri);
}
else
{
int r= remove();
cout<<"Removed = "<<r<<endl;
}
}
}
void main()
{
clrscr();
int num;
char choice='y';
pqueue q;
front=rear=NULL;
int option;
while(choice == 'y')
{
cout<<"1.Priority operation"<<endl;
cout<<"2.Display"<<endl;
cout<<"enter the option"<<endl;
cin>>option;
switch(option)
{
case 1:
{
q.priority();
cout<<"Do you want to cont...?"<<endl;
break;
}
case 2:
{
q.display();
cout<<"Do you want to cont...?"<<endl;
break;
}
case 5:
{
int r = q.remove();
cout<<"Removed ="<<r<<endl;
cout<<"Do you want to cnt....?"<<endl;
break;
} */
default:
{
cout<<"Wrong input"<<endl;
cout<<"Do you want to cont...?"<<endl;
break;
}
}
choice=getch();
}
}
Double-Ended Queue in Data Structure
#include<iostream.h>
#include<conio.h>
classsqueue
{
private:
squeue *prev,*next;
int data;
public :
voidaddrear(int);
void display(void);
voidremovefront(void);
voidaddfront(int);
voidremoverear(void);
};
squeue *front;
squeue *rear;
voidsqueue::addrear(intnum)
{
squeue *temp;
if(front==NULL && rear==NULL)
{
front=rear=new squeue;
front->prev=NULL;
front->next=NULL;
front->data=num;
}
else
{
rear->next=new squeue;
rear->next->data=num;
rear->next->prev=rear;
rear->next->next=NULL;
rear=rear->next;
voidsqueue::addfront(intnum)
{
squeue *temp=front;
if(front==NULL && rear==NULL)
{
front=rear=new squeue;
front->prev=NULL;
front->next=NULL;
front->data=num;
}
else
{
front=new squeue();
front->data=num;
front->prev=NULL;
front->next=temp;
temp->prev=front;
}}
voidsqueue::display()
{
squeue *q=front;
if(front==NULL && rear==NULL)
{
cout<<"no queue";
}
else
{
while(q!=NULL)
{
cout<<q->data<<"\t";
q=q->next;
}}}
voidsqueue::removefront()
{
squeue *temp=front;
front=front->next;
front->prev=NULL;
cout<<endl<<temp->data<<" is
removed\n";
delete(temp);
display();
}
voidsqueue::removerear()
{
squeue *temp=rear;
rear=rear->prev;
rear->next=NULL;
cout<<endl<<temp->data<<" is
removed\n";
delete(temp);
display();
}
void main(){
clrscr();
squeue s;
char c='y';
intopt,num;
while(c=='y')
{
cout<<"\n 1.add R \n 2. display \n 3. remove F \n 4. add F \n 5. Remove R \n
enter option: ";
cin>>opt;
switch(opt)
{
case 1:
cout<<"enter number in queue:";
cin>>num;
s.addrear(num);
cout<<"\ndo you want to continue:";
break;
case 2:
s.display();
cout<<"\ndo you want to continue:";
break;
case 3:
s.removefront();
cout<<"\ndo you want to continue:";
break;
case 4:
cout<<"enter number in queue:";
cin>>num;
s.addfront(num);
cout<<"\ndo you want to continue:";
break;
case 5:
s.removerear();
cout<<"\ndo you want to continue:";
break;
default:
cout<<"wrng selection";
cout<<"\ndo you want to continue:";
break;}
c=getch();
}
getch();
}
Circular Queue program in Data Structure
#include<iostream.h>
#include<conio.h>
classsqueue
{
private:
squeue *prev,*next;
int data;
public :
void add(int);
void display(void);
void remove(void);
};
squeue *front;
squeue *rear;
voidsqueue::add(intnum)
{
squeue *temp;
if(front==NULL && rear==NULL)
{
front=rear=new squeue;
front->prev=NULL;
front->next=NULL;
front->data=num;
}
else
{
temp=front;
rear->next=new squeue;
rear->next->data=num;
rear->next->prev=rear;
rear->next->next=front;
rear=rear->next;
front->prev=rear;
}}
voidsqueue::display()
{
squeue *q=front;
if(front==NULL && rear==NULL)
{
cout<<"no queue";
}
else
{do
{
cout<<q->data<<"\t";
q=q->next;
}
while(q!=rear->next);
}}
voidsqueue::remove()
{
squeue *temp=front;
front=front->next;
front->prev=NULL;
cout<<endl<<temp->data<<" is
removed\n";
delete(temp);
display();
}
void main()
{
clrscr();
squeue s;
char c='y';
intopt,num;
while(c=='y')
{
cout<<"\n 1.add \n 2. display \nenter option: ";
cin>>opt;
switch(opt)
{
case 1:
cout<<"enter number in queue:";
cin>>num;
s.add(num);
cout<<"\ndo you want to continue:";
break;
case 2:
s.display();
cout<<"\ndo you want to continue:";
break;
case 3:
s.remove();
cout<<"\ndo you want to continue:";
break;
default:
cout<<"wrng selection";
cout<<"\ndo you want to continue:";
break;
}c=getch();
}getch();}
Simple Queue program in Data Structure
#include<iostream.h>
#include<conio.h>
class queue
{
private:
int data;
queue *prev;
queue *next;
public:
void add(int);
void display(void);
void remove(void);
};
queue *front;
queue *rear;
void queue::add(intnum)
{
if((front==NULL)&&(rear==NULL))
{
front=rear=new queue;
front->data=num;
front->next=NULL;
front->prev=NULL;
}
else
{
rear->next=new queue;
rear->next->data=num;
rear->next->next=NULL;
rear->next->prev=rear;
rear=rear->next;
}}
void queue::display(void)
{
queue *q=front;
if((front==NULL)&&(rear==NULL))
{
cout<<"Queue is empty";
}
else
{
while(q!=NULL)
{
cout<<q->data<<endl;
q=q->next;
}}}
void queue::remove(void)
{
queue *q=front;
if((front==NULL)&&(rear==NULL))
{
cout<<"Queue is empty"<<endl;
}
else
{
front=front->next;
front->prev=NULL;
cout<<q->data<<"/t"<<"is
deleted"<<endl;
delete(q);
}}
void main()
{
clrscr();
queue q1;
intnum;
int option;
char choice='y';
while(choice=='y')
{
cout<<"1.ADD"<<endl;
cout<<"2.DISPLAY"<<endl;
cout<<"3.REMOVE"<<endl;
cout<<"Select the option";
cin>>option;
switch(option)
{
case 1:
{
cout<<"enter the data";
cin>>num;
q1.add(num);
cout<<"Continue?"<<endl;
break;
}
case 2:
{
q1.display();
cout<<endl;
cout<<"Continue?"<<endl;
break; }
case 3:
{
q1.remove();
cout<<endl;
cout<<"Continue?";
break;}
default:
{
cout<<"wrong
option";
cout<<"Continue";
break;
}}
choice=getch();
} }
Subscribe to:
Posts (Atom)