PROGRAMS BASED ON STACKS AND QUEUES 1. Program to implement stacks without classes. s=[] c="y" while (c=="y"): print "1. PUSH" print "2. POP " print "3. Display" choice=input("Enter your choice: ") if (choice==1): a=input("Enter any number :") s.append(a) elif (choice==2): if (s==[]): print "Stack Empty" else:
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
PROGRAMS BASED ON STACKS AND QUEUES
1. Program to implement stacks without classes.
s=[]
c="y"
while (c=="y"):
print "1. PUSH"
print "2. POP "
print "3. Display"
choice=input("Enter your choice: ")
if (choice==1):
a=input("Enter any number :")
s.append(a)
elif (choice==2):
if (s==[]):
print "Stack Empty"
else:
print "Deleted element is : ",s.pop()
elif (choice==3):
l=len(s)
for i in range(l-1,-1,-1):
print s[i]
c=raw_input("Do you want to continue or not?(y/n)\n")
2. Program to implement stacks with classes.
class stack:
s=[]
def push(self):
a=input('Enter any number : ')
stack.s.append(a)
def display(self):
l=len(stack.s)
for i in range(l-1,-1,-1):
print stack.s[i]
a=stack()
c="y"
while (c=="y"):
print "1. PUSH"
print "2. POP "
print "3. Display"
choice=input("Enter your choice: ")
if (choice==1):
a.push()
elif (choice==2):
if (a.s==[]):
print "Stack Empty"
else:
print "Deleted element is : ",a.s.pop()
elif (choice==3):
a.display()
3. Program to implement Queue’s without classes.
a=[]
c='y'
while c=='y':
print "1. INSERT"
print "2. DELETE "
print "3. Display"
choice=input("Enter your choice : ")
if (choice==1):
b=input("Enter new number : ")
a.append(b)
elif (choice==2):
if (a==[]):
print "Queue Empty"
else:
print "Deleted element is : ",a[0]
del a[0]
elif (choice==3):
l=len(a)
for i in range(0,l):
print a[i]
else:
print "Wrong input"
c=raw_input("Do you want to continue or not? (y/n)\n")
4. Program to implement Queue’s with classes.
class queue:
q=[]
def insertion(self):
a=input("Enter new number : ")
queue.q.append(a)
def deletion(self):
if (queue.q==[]):
print "Queue Empty"
else:
print "Deleted element is : ",queue.q[0]
del queue.q[0]
def display(self):
l=len(queue.q)
for i in range(0,l):
print queue.q[i]
a=queue()
c='y'
while c=='y':
print "1. INSERT"
print "2. DELETE "
print "3. Display"
choice=input("Enter your choice : ")
if (choice==1):
a.insertion()
elif (choice==2):
a.deletion()
elif (choice==3):
a.display()
else:
print "Wrong input"
c=raw_input("Do you want to continue or not? (y/n)\n")
SQL QUERIES
1. Display all records from table EMP.SQL> select* from emp;
2. Display empno and ename of all employees from table emp.
SQL> select empno, ename from emp;
EMPNO ENAME
--------- ----------
8877 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
9999 FORD
7934 MILLER
14 rows selected.
3. Display ename, sal, sal added with comm from emp.
SQL> select ename, sal, sal+comm from emp;
ENAME SAL SAL+COMM
---------- --------- ---------
SMITH 800
ALLEN 1600 1900
WARD 1250 1750
JONES 2975
MARTIN 1250 2650
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
TURNER 1500 1500
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
14 rows selected.
4. Display ename joined with job with heading "Employee", sal*12 as "Total Salary" from table emp.
SQL> select ename ||job "Employee", sal*12 "Total Salary" from emp;
Employee Total Salary
------------------- ------------
SMITHCLERK 9600
ALLENSALESMAN 19200
WARDSALESMAN 15000
JONESMANAGER 35700
MARTINSALESMAN 15000
BLAKEMANAGER 34200
CLARKMANAGER 29400
SCOTTANALYST 36000
KINGPRESIDENT 60000
TURNERSALESMAN 18000
ADAMSCLERK 13200
JAMESCLERK 11400
FORDANALYST 36000
MILLERCLERK 15600
14 rows selected.
5. Display distinct sal of employees from table emp.
SQL> select distinct sal from emp;
SAL
---------
800
950
1100
1250
1300
1500
1600
2450
2850
2975
3000
5000
12 rows selected.
6. Show the structure of table Dept.
SQL> describe dept;
Name Null? Type
------------------------------- -------- ----
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR(12)
LOC VARCHAR2(13
7. Write a Query to display Ename and sal of employees whose salary is greater than or equal to 3000 from table emp.
SQL> select ename ,sal from emp where sal>=3000;
ENAME SAL
---------- ---------
SCOTT 3000
KING 5000
FORD 3000
8. Write a query to display employee name, salary and department number who are not getting commission from table emp.
SQL> select ename, sal, deptno from emp where comm is null;
ENAME SAL DEPTNO
---------- --------- ---------
SMITH 800 20
JONES 2975 20
BLAKE 2850 30
CLARK 2450 10
SCOTT 3000 20
KING 5000 10
ADAMS 1100 20
JAMES 950 30
FORD 3000 20
MILLER 1300 10
10 rows selected.
9. Write a query to display employee Number, name , sal, sal*12 as annual Salary whose commission is not null from table emp.
SQL> select ename, sal, deptno from emp where comm is not null;
ENAME SAL DEPTNO
---------- --------- ---------
ALLEN 1600 30
WARD 1250 30
MARTIN 1250 30
TURNER 1500 30
10. Write a query to display employee name and salary of those employees who do not have their salary in the range of 1500 to 2000.
SQL> select ename, sal from emp where sal not between 1500 and 2000;
ENAME SAL
---------- ---------
SMITH 800
WARD 1250
JONES 2975
MARTIN 1250
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
12 rows selected.
11. Write a query o display name, job, salary, and hiredate of employees who are hired between February 20 1981 and May 1 1981. Order the query in ascending order of HireDate.
SQL>select ename, job, hiredate from emp where hiredate between '20-FEB-1981' and '1-MAY-1981'
>order by hiredate;
ENAME JOB HIREDATE
---------- --------- ---------
ALLEN SALESMAN 20-FEB-81
WARD SALESMAN 22-FEB-81
JONES MANAGER 02-APR-81
BLAKE MANAGER 01-MAY-81
12. Write a query to display the name and hiredate of all the employees who were hired in 1982.
SQL> select ename , hiredate from emp where hiredate between '1-JAN-1982' and '31-DEC-1982';
ENAME HIREDATE
---------- ---------
MILLER 23-JAN-82
13. Write a query to display the name, job title and salary of those employees who do not have manager.
SQL> select ename, job, sal from emp where mgr is null;
ENAME JOB SAL
---------- --------- ---------
KING PRESIDENT 5000
14. Write a query to display the name of employee whose name contains 'A' as third alphabet.
SQL> select ename from emp where ename like '__A%';
ENAME
----------
BLAKE
CLARK
ADAMS
15. Write a query to display the name of the employee whose name contains 'T' as the last alphabet.
SQL> select ename from emp where ename like '%T';
ENAME
----------
SCOTT
16. Write a Query to display the name of the employee whose name contains 'M' as first alphabet and 'L' as third alphabet.
SQL> select ename from emp where ename like 'M_L%';
ENAME
----------
MILLER
17. Write a Query to display the name of the employee who is having 'L' as any alphabet of the name.
SQL> select ename from emp where ename like '%L%';
ENAME
----------
ALLEN
BLAKE
CLARK
MILLER
18. WAQ to display employee name, salary, salary increased by 15% expressed as a whole number Label the column as 'New Salary'.
SQL> select ename,sal, round (sal + sal * 0.15,0) "New Salary" from emp;
ENAME SAL New Salary
---------- --------- ----------
SMITH 800 920
ALLEN 1600 1840
WARD 1250 1438
JONES 2975 3421
MARTIN 1250 1438
BLAKE 2850 3278
CLARK 2450 2818
SCOTT 3000 3450
KING 5000 5750
TURNER 1500 1725
ADAMS 1100 1265
JAMES 950 1093
FORD 3000 3450
MILLER 1300 1495
14 rows selected.
19. WAQ to display ename, salary review date which is the date after six months of hiredate in format of 'Sunday, 7 SEP, 1981'.