1)write a program to evaluates the following algebraic expressions

advertisement
COMPUTER PROGRAMMING LAB
Program-1:
Write a program to evaluates the following algebraic expressions after reading
necessary values from the user.
Program-1(a):
Aim: Compute expression (ax+b)/(ax-b).
1.start
2.Read a ,x and b
3.if((a*x-b)!=0) then
4. begin
5.
c=(a*x+b)/(a*x-b);
6.
write c
7. end if
8. otherwise
9. “division is not possible”
10. stop
Program-1(b):
Aim: Evaluate the expression 2.5logx+320+|x2-y2|+
2xy
1.start
2.Read x and y
3.if(x!=0) then
4. begin
5.
z=2.5logx+320+|x2-y2|+
2xy;
6.
write z
7. end if
8. otherwise
9. “Expression Evaluation is not possible”
10. Stop
Program-1(c):
Aim : Evaluate the expression 1/(
2)e (-(x-m)/
1.start
2.Read x,m and s
3.if(s!=0) then
4. begin
2s)) 2
5.
z=1/( 2)e (-(x-m)/
6.
write z
7. end if
8. otherwise
9. “Expression Evaluation is not possible”
10. Stop
1
2s)) 2
COMPUTER PROGRAMMING LAB
Program-2(a):
Aim : Printing three given integers in ascending order
1.start
2.Read a,b and c
3. if(a>b) then
4.
Interchange a and b
5. Otherwise if(a>c) then
6.
Interchange a and c
7.
Otherwise if(b>c) then
8.
Interchange b and c
9. Write a, b and c
10.Stop.
Program-2(b):
Aim: Compute Sum of 1 + 2+ 3 + _ _ _ _ _ n
.
1.start
2.Read n
3. initialize i=1,sum=0
4.if(i<=n) then
5.begin
6. sum=sum+i;
7. i++;
8. goto step 4.
9. end if
10. otherwise Write sum
10.Stop.
Program-2(c):
Aim: Compute sum of 1 + x2/2! + x2/ 4!+ _ _ _ _ _ upto ten terms
1.start
2.Read x
3. initialize i=1,sum=1
4.if(i<7) then
5.begin
6. sum=sum+(x*x)/fact (2*i); /* Here fact( ) is a factorial function we have to write it
in a program while implementing */
7. i++;
8.goto step 4.
9. end if
10. otherwise Write sum
11.Stop.
2
COMPUTER PROGRAMMING LAB
Program-2(d):
Aim: Compute sum x +x3/3! + x5/5!+ _ _ _ _ _ upto 7TH
digit accuracy
1.start
2.Read x
3. initialize i=0,sum=0
4.if(i<=7) then
5.begin
6. l=2*i+1;
11. sum=sum+(xl)/fact(l); /* Here fact( ) is a factorial function we have to write it in a
program while implementing */
12. i++;
13. goto step 4.
14. end if
15. otherwise Write sum
10.Stop.
Program-2(e):
Aim : Read x and compute y=1 for x>0
y=0 for x=0
y=-1 for x<0
1.start
2.Read x
3.if(x>0) then
4.
y=1;
5.
otherwise if(x==0) then
6.
y=0;
7.
otherwise y=-1;
8.write y.
9. stop
3
COMPUTER PROGRAMMING LAB
Program-3:
Aim : write c program using for statement to find the following from a given set of 20
integers
a) total number of even integers b) total number of odd integers
c) sum of all even integers
d) sum of all odd integers
Input : Read 20 integers.
Output : Write total no. of even and odd integers and also write sum of even and odd
integers.
1. Start
2. Initialize i=1,SE=SO=TE=TO=0
3. if(i<=20) then
4. begin
5. Read n;
6.
if(n%2==0)
7.
{
8.
TE++;
9.
SE=SE+n;
10 }
11. else
12. {
13.
TO++;
14.
SO=SO+n;
15.
}
16. i++;
17. goto step 3.
18. end if /* first if */
19. otherwise
20. write TE,TO,SE,SO
21. Stop.
4
COMPUTER PROGRAMMING LAB
Program-4:
Aim: Write a C program to obtain the product of two matrices A of size (3X3) and
B of size (3X2). The resultant matrix C is to be printed out along with A and
B. Assume suitable values for A & B.
Input: Read two 3 * 3 matrices say A and B.
Output : Write matrix C.
1. Start
2. Read matrices A and B of order 3*3
3. i=0;
4. if(i<3) then
5. {
6. j=0;
7. if(j<3) then
8. {
9. c[i][j]=0;
10. k=0;
11. if(k<3) then
12. {
13. c[i][j]=c[i][j]+a[i][k]*b[k][j];
14. }
15. k++; goto step 11.
16. j++; goto step 7.
17. }
18. i++; goto step 4.
19. }
20. Write matrices A,B,C.
21. Stop
5
COMPUTER PROGRAMMING LAB
Program-5:
Aim : Using switch-case statement, write a C program that takes two operands
and one operator from the user, performs the operation and then prints
the answer. (consider operators +,-,/,* and %).
Input : Two operands and one operator.
Output : Computed result of respective arithmetic exp.
1.Start
2.Read a,b and any one Operator(op)
3.if(op== '+') then
4.
c=a+b;
5.
.else if(op==’-‘) then
6.
c=a-b;
7.
else If(op==’/’) then
8.
c=a/b;
9.
else If(op==’*’) then
10.
c=a*b;
11
else If(op==’%’) then
12.
c=a%b;
13
end if
14.
Write c
15. Stop.
6
COMPUTER PROGRAMMING LAB
Program-6:
Aim: Write C procedures to add, subtract, multiply and divide two complex
numbers (x+iy) and (a+ib).Also write the main program that uses these
procedures.
1. Addition Procedure
add( )
{
z1=a+x;
z2=b+y;
Write z1,z2
}
2. Subtraction Procedure
sub( )
{
z1=a-x;
z2=b-y;
Write z1,z2
}
3. Multiplication Procedure
mul( )
{
z1=a*x-b*y;
z2=a*y+b*x;
Write z1,z2
}
5. Division Procedure
div( )
{
z1=(a*x+b*y)/(x*x+y*y);
z2=(b*x-a*y)/(x*x+y*y);
Write z1,z2
}
7
COMPUTER PROGRAMMING LAB
Program-7:
Aim The total distance traveled by vehicle in ‘t’ seconds is given by
distance = ut+1/2at2 where ‘u’ and ‘a’ are the initial velocity (m/sec.) and
acceleration (m/sec2). Write C program to find the distance traveled at
regular intervals of time given the values of ‘u’ and ‘a’. The program
should provide the flexibility to the user to select his own time intervals
and repeat the calculations for different values of ‘u’ and ‘a’.
1.Start
2.Read the regular time interval( t1 & t2 )values
3.Repeat
4. {
5. Read the values of intial velocity(u) and accelaration(a)
6. d=u*t1+0.5*a*t1*t1;
7. t1=t1+2;
8. Write d
9. }
10. until (t1<=t2);
11.Stop.
8
COMPUTER PROGRAMMING LAB
Program-8:
Aim:
A cloth show room has announced the following seasonal discounts on purchase
of items.
Purchase Amount
1-100
101-200
201-300
Above 300
Discount (Percentage)
Mill Cloth
Handloom items
5.0
5.0
7.5
7.5
10.0
10.0
15.0
Write a C program using switch and If statements to complete the net amount to
be paid by a customer.
1.Start
2.Read the type of item to be purchased.
3.Read the Purchase Amount
4.if purchased item is mill cloths then
5.
if(pa>=1 && pa<=100)
6.
dis=0;
7.
else if(pa>=101 && pa<=200)
8.
dis=5;
9.
else if(pa>=201 && pa<=300)
10.
dis=7.5;
11.
else if(pa>300)
12.
dis=10;
13.
na=na+(pa-(dis*pa)/100);
14. Otherwise if(pa>=1 && pa<=100)
15.
dis=5.0;
16.
else if(pa>=101 && pa<=200)
17.
dis=7.5;
18.
else if(pa>=201 && pa<=300)
19.
dis=10;
20.
else if(pa>300)
21.
dis=15;
22.
na=na+(pa-(dis*pa)/100);
23. Repeat the process on number of purchases.
24.Write “Net Amount To Be Paid By Customer is :” , na
25.Stop.
9
COMPUTER PROGRAMMING LAB
Program-9:
Aim : Given a number, write C program using while loop to reverse the digits of
the number. Example 1234 to be written as 4321.
1.Start
2.Read the number to be reversed.
3. Initialize rev=0.
4.if(n>0)
5. begin
6. rem=n%10;
7. rev=rev*10+rem;
8. n=n/10;
9. goto step 4.
10. end if
11.otherwise Write “the reverse number is ",rev
12.stop.
10
COMPUTER PROGRAMMING LAB
Program-10:
Aim : The Fibonacci sequence of numbers is 1,1,2,3,5,8… based on the
recurrence relation f(n) = f (n-1) + f (n-2) for n>2.
Write C program using d0-while to calculate and print the first m fibonacci
numbers.
1.start
2.Read ‘m’
3.initialize i=3,a=0,b=1.
4.write a,b.
5.if(i<=m) then
6. begin
7.
c=a+b;
8. Write c
9. a=b;
10 b=c;
11 i++;
12 goto step 5.
13 end if
14. Otherwise stop.
11
COMPUTER PROGRAMMING LAB
Program-11 :
Aim : 11. Write C programs to print the following outputs using for loop.
1
22
333
4444
1
2
3
4
2
3
4
3
4
4
Program-11(a):
Aim : Program to the Triangle Format.
#include<stdio.h>
main()
{
int i,j,n;
clrscr();
printf("enter any number\n");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(" %d",i);
}
printf("\n");
}
getch();
}
/* SAMPLE OUTPUT
enter any number
5
1
22
333
4444
*/
12
COMPUTER PROGRAMMING LAB
Program-11(b):
Aim : Program to print Equilateral Triangle Format
#include<stdio.h>
main()
{
int i,j,k,n;
clrscr();
printf("enter any number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=3;j>i-1;j--)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf(" %d",i);
}
printf("\n");
}
getch();
}
/* SAMPLE OUTPUT
enter any number
5
1
22
333
4444
*/
13
COMPUTER PROGRAMMING LAB
Program-12:
Aim : Write a C program to extract a portion of a character string and print the
extracted string. Assume that m characters are extracted starting with the
nth character
#include<stdio.h>
main()
{
char str[50],substr[30];
int m,n,i,j;
clrscr();
printf("Enter the Original String\n\n");
flushall();
gets(str);
printf("\nEnter the position and number of characters to be extracted from the string\n");
scanf("%d%d",&m,&n);
for(j=1,i=0;j<=n;m++,j++,i++)
substr[i]=str[m-1];
substr[i]='\0';
printf("\nExtracted String is : %s",substr);
getch();
}
/* Sample Output :
Enter the Original String
Vignan Inst. Of Tech. & Science
Enter the position and number of characters to be extracted from the string
3
10
Extracted String is : gnan Inst.
*/
14
COMPUTER PROGRAMMING LAB
Program-13:
Aim : A Maruthi Car dealer maintains a record of sales of various vehicles in the
following form :
Vehicle type
Month of Sales
Maruthi – 800
Maruthi – DX
Gypsy
Maruthi Van
02/87
07/87
04/88
08/88
Price (Rs).
75,000
95,000
1,10,000
85,000
Write a C program to read this data into a table of strings and output the details
of a particular vehicle sold during a specified period. The program should request
the user to input the vehicle type and the period (starting month & ending month).
#include<stdio.h>
struct car
{
char name[40];
int mm,yy;
int price;
}s[40];
main()
{
int sm,em,i=0,count=0;
char ch,c_name[20];
clrscr();
do
{
flushall();
printf("Enter the type of the car\n");
gets(s[i].name);
printf("Month , year and cost of purchasing\n");
scanf("%d%d%d",&s[i].mm,&s[i].yy,&s[i].price);
printf("Do U Want to Continue...(press Y / N)\n");
flushall();
ch=getchar();
i++;
count++;
}while(ch=='Y');
15
COMPUTER PROGRAMMING LAB
printf("CAR TYPE
MM/YY PRICE\n");
for(i=0;i<count;i++)
{
printf("%s
%d/%d
%d\n",s[i].name,s[i].mm,s[i].yy,s[i].price);
}
printf("Enter car type, starting and ending month of sold\n");
flushall();
gets(c_name);
scanf("%d%d",&sm,&em);
printf("CAR TYPE
MM/YY
PRICE\n");
for(i=0;i<count;i++)
{
if(strcmp(s[i].name,c_name)==0 && (s[i].mm>=sm &&s[i].mm<=em))
printf("%s
%d/%d
%d\n",s[i].name,s[i].mm,s[i].yy,s[i].price);
}
getch();
}
/* SAMPLE OUTPUT
Enter the type of the car
Maruthi-800
Month , year and cost of purchasing
2
98
23000
Do U Want to Continue...(press Y / N)
Y
Enter the type of the car
Van
Month , year and cost of purchasing
4
89
45000
Do U Want to Continue...(press Y / N)
N
CAR TYPE
MM/YY
PRICE
Maruthi-800
Van
Maruthi-800
2/98
4/89
5/99
23000
30000
31000
Enter car type, starting and ending month of sold
Maruthi-800
1
7
CAR TYPE MM/YY PRICE
Maruthi-800
2/98
23000
Maruthi-800
4/99
31000 */
16
COMPUTER PROGRAMMING LAB
Program-14:
Aim : Write a function that will scan a character string passed as an argument
and covert all lower case characters into their upper case equivalents.
void string_u(char *s)
{
while(*s!='\0')
{
if(*s>='a' && *s<='z')
*s=*s-32;
s++;
}
}
main()
{
char str[30];
clrscr();
printf("Enter Any String\n");
gets(str);
string_u(str);
printf("Uppar Case String Is : %s",str);
getch();
}
/*SAMPLE OUTPUT
Enter Any String
vignan
Uppar Case String Is : VIGNAN
*/
17
COMPUTER PROGRAMMING LAB
Program-15(i):
Aim : IMPLEMENTATION OF STACK USING ARRAYS.
Algorithm for Push operation:
1.void push(int x)
2.{
3. if(top==MAX-1)
4. {
5. printf("STACK IS OVERFLOW\n");
6. return;
7. }
8. stack[++top]=x;
9. }
Algorithm for Pop operation:
1.int pop()
2.{
3. if(top==-1)
4. {
5. printf("STACK IS UNDERFLOW\n");
6. return(-1);
7. }
8. return(stack[top--]);
9.}
Algorithm for stacktop operation:
1.int stacktop()
2.{
3. if(top==-1)
4. {
5. printf("STACK IS UNDERFLOW\n");
6. return(-1);
7. }
8. return(stack[top]);
9.}
Algorithm for Display operation:
1.void display()
2.{
3. int i;
4. if(top==-1)
5. {
6. printf("STACK IS UNDERFLOW\n");
7. return;
8. }
9. for(i=top;i>=0;i--)
10. printf("%d\t",stack[i]);
11.}
18
COMPUTER PROGRAMMING LAB
Program-15(ii):
Aim :Implementation of Linear Queue using arrays
Algorithm for insert operation:
1.void insert(int x)
2.{
3. if(rear==MAX-1)
4. {
5. printf(" Ovedrflow\n");
6. return;
7. }
8. queue[++rear]=x;
9. }
Algorithm for delete operation:
1.int delete()
2.{
3.if(rear<front)
4.{
5. printf("Queue Is Underflow\n");
6. return(0);
7. }
8. return(queue[front--]);
9}
Algorithm for Display operation:
1. void display()
2. {
3. int i;
4. for(i=0;i<=rear;i++)
5.
printf("%d\t",queue[i]);
6.}
19
COMPUTER PROGRAMMING LAB
Program-15(iii):
Aim :Implementation of Circular Queue using arrays
Algorithm for insert operation:
1.void c_insert(int x)
2.{
3.if(rear==MAX-1)
4. rear=0;
5. else
6. ++rear;
7. if(rear==front)
8.{
9. printf(" Ovedrflow\n");
10. rear--;
11. return;
12. }
13. cq[rear]=x;
14. }
Algorithm for delete operation:
1.int c_delete()
2.{
3. if(rear==front)
4. {
5. printf("Circular Q Underflow\n");
6. return(0);
7. }
8. if(front==MAX-1)
9. front=0;
10. else
11. ++front;
12. return(cq[front]);
13.}
Algorithm for Display operation:
1.void c_display()
2.{
3. int i;
4. if(front==MAX-1)
5. i=0;
6. else
7. i=front+1;
8. for(;i!=rear;i=(i+1)%MAX)
9.
printf("%d\t",cq[i]);
10. printf("%d",cq[i]);
}
20
COMPUTER PROGRAMMING LAB
Program-16:
Aim : . Implement binary search tree using linked list and perform the following
operations.
i) Insertion ii) Deletion iii) Inorder Traversal iv) Preorder Traversal
v) Post Order Traversal
#include<stdio.h>
struct BST
{
struct BST *l;
int data;
struct BST *r;
};
typedef struct BST NODE;
NODE *root=NULL,*temp,*prev,*cur,*new;
void create();
void insert();
void delete(int);
void inorder(NODE *);
void preorder(NODE *);
void postorder(NODE *);
main()
{
int choice,x;
clrscr();
printf("IMPLEMENTATION OF BINARY SEARCH TREE \n");
while(1)
{
printf("\n1.Create\n2.Insert\n3.Delete\n4.Inorder\n5.Preorder\n6.Postorer\n7.Exit\n");
printf("Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2: temp=(struct BST *)(malloc(sizeof(struct BST)));
printf("Which Value U Want to Enter : ");
scanf("%d",&x);
temp->l=NULL;
temp->data=x;
temp->r=NULL;
insert();
break;
21
COMPUTER PROGRAMMING LAB
case 3: printf("Which Element U Want To Delete From The BST : ");
scanf("%d",&x);
delete(x);
break;
case 4: printf("Result Of Inorder Traversal Is : \n");
inorder(root);
break;
case 5: printf("Result Of Preorder Traversal Is : \n");
preorder(root);
break;
case 6: printf("Result Of Postorder Traversal Is : \n");
postorder(root);
break;
case 7: exit(0);
default:
printf("Invalid choice\n");
}
}
getch();
}
void create()
{
int c,x;
do
{
temp=(struct BST *)(malloc(sizeof(struct BST)));
printf("Which Value U Want to Enter : ");
scanf("%d",&x);
temp->l=NULL;
temp->data=x;
temp->r=NULL;
if(root==NULL)
{
root=temp;
}
else
{
prev=cur=root;
while(cur!=NULL)
{
prev=cur;
if(temp->data<cur->data)
cur=cur->l;
else
cur=cur->r;
}
22
COMPUTER PROGRAMMING LAB
if(temp->data<prev->data)
prev->l=temp;
else
prev->r=temp;
}
printf("Do U Want To Cont...Press->1 / 0 : ");
scanf("%d",&c);
}
while(c==1);
}
void insert()
{
if(root==NULL)
{
root=temp;
}
else
{
cur=root;
while(cur!=NULL)
{
prev=cur;
if(temp->data<cur->data)
cur=cur->l;
else
cur=cur->r;
}
if(temp->data<prev->data)
prev->l=temp;
else
prev->r=temp;
}
}
void delete(int x)
{
int found=0;
temp=root;
while(1)
{
if(temp==NULL)
break;
if(x==temp->data)
{
found=1;
break;
}
23
COMPUTER PROGRAMMING LAB
if(x<temp->data)
temp=temp->l;
else
temp=temp->r;
}
if(found==0)
{
printf("%d Element Not Found In a Binary Search Tree\n",x);
return;
}
cur=root;
while(x!=cur->data)
{
prev=cur;
if(x<cur->data)
cur=cur->l;
else
cur=cur->r;
}
if(cur->l==NULL && cur->r==NULL)
{
if(prev->l==cur)
prev->l=NULL;
else
prev->r=NULL;
}
if(cur->l==NULL && cur->r!=NULL)
{
if(prev->l==cur)
prev->l=cur->r;
else
prev->r=cur->r;
}
if(cur->l!=NULL && cur->r==NULL)
{
if(prev->r==cur)
prev->r=cur->l;
else
prev->l=cur->l;
}
if(cur->l!=NULL && cur->r!=NULL)
{
if(cur==root)
root=root->r;
temp=cur->r;
while(temp->l!=NULL)
temp=temp->l;
temp->l=cur->l;
24
COMPUTER PROGRAMMING LAB
if(cur!=root)
{
if(prev->l==cur)
prev->l=prev->l->r;
else
prev->r=prev->r->r;
}
}
return;
}
void inorder(NODE *temp)
{
if(temp!=NULL)
{
inorder(temp->l);
printf("->%d ",temp->data);
inorder(temp->r);
}
}
void preorder(NODE *temp)
{
if(temp!=NULL)
{
printf("->%d ",temp->data);
preorder(temp->l);
preorder(temp->r);
}
}
void postorder(NODE *temp)
{
if(temp!=NULL)
{
postorder(temp->l);
postorder(temp->r);
printf("->%d ",temp->data);
}
}
25
COMPUTER PROGRAMMING LAB
/* SAMPLE OUTPUT
IMPLEMENTATION OF BINARY SEARCH TREE
1.Create 2.Insert 3.Delete 4.Inorder 5.Preorder 6.Postorer 7.Exit
Enter Your Choice : 1
Which Value U Want to Enter : 4
Do U Want To Cont...Press->1 / 0 : 1
Which Value U Want to Enter : 2
Do U Want To Cont...Press->1 / 0 : 1
Which Value U Want to Enter : 3
Do U Want To Cont...Press->1 / 0 : 0
1.Create 2.Insert 3.Delete 4.Inorder 5.Preorder 6.Postorer 7.Exit
Enter Your Choice :4
Result Of Inorder Traversal Is :
->2 ->3 ->4
1.Create
2.Insert
3.Delete
4.Inorder
5.Preorder
6.Postorer
7.Exit
Enter Your Choice : 2
Which Value U Want to Enter : 6
1.Create
2.Insert
3.Delete
4.Inorder
5.Preorder
6.Postorer
7.Exit
Enter Your Choice : 5
Result Of Preorder Traversal Is :
->4 ->2 ->1 ->6
1.Create
2.Insert
3.Delete
4.Inorder
5.Preorder
6.Postorer
7.Exit
Enter Your Choice : 7
*/
26
COMPUTER PROGRAMMING LAB
Program-17(i):
Aim :IMPLEMENTATION OF SINGLE LINKED LIST
#include<stdio.h>
struct SLL
{
int data;
struct SLL *next;
};
typedef struct SLL NODE;
NODE *start,*end,*temp,*prev,*cur;
void create();
void add();
void add_beg();
void add_end();
void add_mid();
void display();
void del();
void del_beg();
void del_end();
void del_mid();
main()
{
int choice;
clrscr();
printf("IMPLEMENTATION OF SINGLE LINKED LIST \n");
while(1)
{
printf(" 1.CREATE\n 2.ADD\n 3.DELETE\n 4.DISPLAY\n 5.EXIT\n");
printf("Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2:add();
break;
case 3:del();
break;
case 4:printf("List Elements Are :\n");
display();
break;
case 5:exit();
default: printf("Invalid Choice\n");
}
}
getch();
}
27
COMPUTER PROGRAMMING LAB
void create()
{
char choice;
int x;
start=NULL;
do
{
printf("Enter the data value: ");
scanf("%d",&x);
temp=malloc(sizeof(NODE));
temp->data=x;
temp->next=NULL;
if(start==NULL)
{
start=temp;
end=start;
}
else
{
end->next=temp; end=temp;
}
printf("Do U Want To Continue... press->'y':");
flushall();
choice=getchar();
}
while(choice=='y');
}
void add()
{
int x,ch;
printf("Enter the data value to be added : ");
scanf("%d",&x);
temp=malloc(sizeof(NODE));
temp->data=x;
temp->next=NULL;
printf("Postion of Elements :\n 1.Begining\n 2.Middle\n 3.End\n");
printf("In Which Location U Want To Enter The Element : ");
scanf("%d",&ch);
switch(ch)
{
case 1: add_beg();
break;
case 2: add_mid();
break;
case 3: add_end();
break;
default: printf("Invalid Choice\n");
}
}
28
COMPUTER PROGRAMMING LAB
void add_beg()
{
temp->next=start;
start=temp;
}
void add_mid()
{
int i=1,pos;
printf("In Which position U Want To Enter : ");
scanf("%d",&pos);
prev=cur=start;
while(i<pos)
{
if(cur->next!=NULL)
{
prev=cur;
cur=cur->next;
}
if(cur==NULL)
break;
i++;
}
if(i!=pos)
{
printf("Invalid Position\n");
return;
}
prev->next=temp;
temp->next=cur;
}
void add_end()
{
for(end=start;end->next!=NULL;end=end->next);
end->next=temp;
}
void del()
{
int ch;
printf("Postion of Elements To Be Deleted:\n 1.Begining\n 2.Middle\n 3.End\n");
printf("From Which Location U Want To Delete The Element : ");
scanf("%d",&ch);
29
COMPUTER PROGRAMMING LAB
switch(ch)
{
case 1: del_beg();
break;
case 2: del_mid();
break;
case 3: del_end();
break;
default: printf("Invalid Choice\n");
}
}
void del_beg()
{
temp=start;
start=start->next;
temp->next=NULL;
free(temp);
}
void del_mid()
{
int i=1,pos;
printf("From Which position U Want To Delete : ");
scanf("%d",&pos);
prev=cur=start;
while(i<pos)
{
if(cur->next!=NULL)
{
prev=cur;
cur=cur->next;
}
if(cur==NULL)
break;
i++;
}
if(i!=pos)
{
printf("Invalid Position\n");
return;
}
prev->next=cur->next;
cur->next=NULL;
free(cur);
}
30
COMPUTER PROGRAMMING LAB
void del_end()
{
for(prev=end=start;end->next!=NULL;end=end->next)
prev=end;
prev->next=NULL;
free(end);
}
void display()
{
for(end=start;end!=NULL;end=end->next)
printf(" --> %d ",end->data);
printf("\n");
}
/* SAMPLE OUTPUT
IMPLEMENTATION OF SINGLE LINKED LIST
1.CREATE
2.ADD
3.DELETE
4.DISPLAY
5.EXIT
Enter Your Choice : 1
Enter the data value: 5
Do U Want To Continue... press->'y':y
Enter the data value: 6
Do U Want To Continue... press->'y':y
Enter the data value: 7
Do U Want To Continue... press->'y':n
1.CREATE
2.ADD
3.DELETE
4.DISPLAY
5.EXIT
Enter Your Choice : 4
List Elements Are :
--> 5 --> 6 --> 7
1.CREATE
2.ADD
3.DELETE
4.DISPLAY
5.EXIT
Enter Your Choice : 5
*/
31
COMPUTER PROGRAMMING LAB
Program-17(ii):
Aim : IMPLEMENTATION OF DOUBLY LINKED LIST
#include<stdio.h>
struct DLL
{
struct DLL *left;
int data;
struct DLL *right;
};
typedef struct DLL NODE;
NODE *start,*end,*temp,*cur;
void create();
void add();
void add_beg();
void add_end();
void add_mid();
void display();
void del();
void del_beg();
void del_end();
void del_mid();
main()
{
int choice;
clrscr();
printf("IMPLEMENTATION OF DOUBLE LINKED LIST \n");
while(1)
{
printf(" 1.CREATE\n 2.ADD\n 3.DELETE\n 4.DISPLAY\n 5.EXIT\n");
printf("Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2:add();
break;
case 3:del();
break;
case 4:printf("List Elements Are :\n");
display();
break;
case 5:exit();
default: printf("Invalid Choice\n");
}
}
getch();
}
32
COMPUTER PROGRAMMING LAB
void create()
{
char choice;
int x;
start=NULL;
do
{
printf("Enter the data value: ");
scanf("%d",&x);
temp=malloc(sizeof(NODE));
temp->left=NULL;
temp->data=x;
temp->right=NULL;
if(start==NULL)
{
start=temp;
end=start;
}
else
{
end->right=temp;
temp->left=end;
end=temp;
}
printf("Do U Want To Continue... press->'y':");
flushall();
choice=getchar();
}
while(choice=='y');
}
void add()
{
int x,ch;
printf("Enter the data value to be added : ");
scanf("%d",&x);
temp=malloc(sizeof(NODE));
temp->left=NULL;
temp->data=x;
temp->right=NULL;
printf("Postion of Elements :\n 1.Begining\n 2.Middle\n 3.End\n");
printf("In Which Location U Want To Enter The Element : ");
scanf("%d",&ch);
33
COMPUTER PROGRAMMING LAB
switch(ch)
{
case 1: add_beg();
break;
case 2: add_mid();
break;
case 3: add_end();
break;
default: printf("Invalid Choice\n");
}
}
void add_beg()
{
if(start==NULL)
start=temp;
else
{
temp->right=start;
start->left=temp;
start=temp;
}
}
void add_mid()
{
int i=1,pos;
printf("In Which position U Want To Enter : ");
scanf("%d",&pos);
cur=start;
while(i<pos)
{
if(cur!=NULL)
{
cur=cur->right;
}
if(cur==NULL)
break;
i++;
}
if(i!=pos)
{
printf("Invalid Position\n");
return;
}
temp->left=cur->left;
temp->right=cur;
cur->left->right=temp;
cur->left=temp;
}
34
COMPUTER PROGRAMMING LAB
void add_end()
{
for(end=start;end->right!=NULL;end=end->right);
end->right=temp;
temp->left=end;
end=temp;
}
void del()
{
int ch;
printf("Postion of Elements To Be Deleted:\n 1.Begining\n 2.Middle\n 3.End\n");
printf("From Which Location U Want To Delete The Element : ");
scanf("%d",&ch);
switch(ch)
{
case 1: del_beg();
break;
case 2: del_mid();
break;
case 3: del_end();
break;
default: printf("Invalid Choice\n");
}
}
void del_beg()
{
temp=start;
start=start->right;
temp->left=temp->right=NULL;
free(temp);
}
void del_mid()
{
int i=1,pos;
printf("From Which position U Want To Delete : ");
scanf("%d",&pos);
cur=start;
while(i<pos)
{
if(cur!=NULL)
{
cur=cur->right;
}
35
COMPUTER PROGRAMMING LAB
if(cur==NULL)
break;
i++;
}
if(i!=pos)
{
printf("Invalid Position\n");
return;
}
temp=cur;
cur->left->right=cur->right;
cur->right->left=cur->left;
temp->left=temp->right=NULL;
free(temp);
}
void del_end()
{
for(end=start;end->right!=NULL;end=end->right);
temp=end;
end->left->right=NULL;
temp->left=temp->right=NULL;
end=end->left;
free(temp);
}
void display()
{
for(end=start;end!=NULL;end=end->right)
printf(" --> %d ",end->data);
printf("\n");
}
36
COMPUTER PROGRAMMING LAB
/* SAMPLE OUTPUT
IMPLEMENTATION OF DOUBLE LINKED LIST
1.CREATE
2.ADD
3.DELETE
4.DISPLAY
5.EXIT
Enter Your Choice : 1
Enter the data value: 1
Do U Want To Continue... press->'y':y
Enter the data value: 2
Do U Want To Continue... press->'y':n
1.CREATE
2.ADD
3.DELETE
4.DISPLAY
5.EXIT
Enter Your Choice : 4
List Elements Are :
--> 1 --> 2
1.CREATE
2.ADD
3.DELETE
4.DISPLAY
5.EXIT
Enter Your Choice :3
Postion of Elements To Be Deleted:
1.Begining
2.Middle
3.End
From Which Location U Want To Delete The Element : 1
1.CREATE
2.ADD
3.DELETE
4.DISPLAY
5.EXIT
Enter Your Choice : 4
List Elements Are :
--> 2
1.CREATE
2.ADD
3.DELETE
4.DISPLAY
5.EXIT
Enter Your Choice : 5
*/
37
COMPUTER PROGRAMMING LAB
Program-18(i):
Aim :IMPLEMENTATION OF STACK USING SINGLE LINKED LIST
#include<stdio.h>
struct SLL
{
int data;
struct SLL *next;
};
typedef struct SLL NODE;
NODE *start=NULL,*top,*temp,*prev;
void push();
int pop();
int stacktop();
void display();
main()
{
int choice,x;
clrscr();
printf("IMPLEMENTATION OF STACK USING SINGLE LINKED LIST \n");
while(1)
{
printf(" 1.PUSH\n 2.POP\n 3.STACKTOP\n 4.DISPLAY\n 5.EXIT\n");
printf("Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
temp=malloc(sizeof(NODE));
printf("Which element U Want To Push into the stack: ");
scanf("%d",&temp->data);
temp->next=NULL;
push();
break;
case 2:
x=pop();
if(x!=0)
printf("Popped Element is : %d\n",x);
break;
case 3:
x=stacktop();
if(x!=0)
printf("Top Of The Stack Element is : %d\n",x);
break;
38
COMPUTER PROGRAMMING LAB
case 4: printf("List Of Stack Elements Are :\n");
display();
break;
case 5: exit(0);
default: printf("Invalid Choice\n");
}
}
getch();
}
void push()
{
if(top==NULL)
{
top=temp;
}
else
{
temp->next=top;
top=temp;
}
}
int pop()
{
int x;
if(top==NULL)
{
printf("Stack Is Empty\n");
return(0);
}
temp=top;
x=temp->data;
top=top->next;
temp->next=NULL;
free(temp);
return(x);
}
int stacktop()
{
int x;
if(top==NULL)
{
printf("Stack Is Empty\n");
return(0);
}
x=top->data;
return(x);
}
39
COMPUTER PROGRAMMING LAB
void display()
{
if(top==NULL)
{
printf("Stack Is Empty\n");
return;
}
for(temp=top;temp!=NULL;temp=temp->next)
printf(" %d <-- ",temp->data);
printf("\n");
}
/* SAMPLE OUTPUT
IMPLEMENTATION OF STACK USING SINGLE LINKED LIST
1.PUSH
2.POP
3.STACKTOP
4.DISPLAY
5.EXIT
Enter Your Choice : 1
Which element U Want To Push into the stack: 1
1.PUSH
2.POP
3.STACKTOP
4.DISPLAY
5.EXIT
Enter Your Choice : 1
Which element U Want To Push into the stack: 2
1.PUSH
2.POP
3.STACKTOP
4.DISPLAY
5.EXIT
Enter Your Choice :4
List Of Stack Elements Are :
2 <-- 1 <-1.PUSH
2.POP
3.STACKTOP
4.DISPLAY
5.EXIT
Enter Your Choice : 5
*/
40
COMPUTER PROGRAMMING LAB
Program-18(ii):
Aim :IMPLEMENTATION OF LINEAR QUEUE USING SINGLE LINKED LIST
#include<stdio.h>
struct SLL
{
int data;
struct SLL *next;
};
typedef struct SLL NODE;
NODE *rear=NULL,*front=NULL,*temp;
void insert();
int delete();
void display();
main()
{
int choice,x;
clrscr();
printf("IMPLEMENTATION OF LINEAR QUEUE USING SINGLE LINKED LIST \n");
while(1)
{
printf("\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT\n");
printf("Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: x=delete();
if(x!=0)
printf("Element Deleted From The Queue Is : %d",x);
break;
case 3:
display();
break;
case 4:exit(0);
default: printf("Invalid Choice\n");
}
}
getch();
}
41
COMPUTER PROGRAMMING LAB
void insert()
{
int x;
temp=malloc(sizeof(NODE));
if(temp==NULL)
{
printf("Queue Is Overflow\n");
return;
}
printf("Enter the data value to be inserted into the Queue : ");
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
if(front==NULL)
{
front=rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}
int delete()
{
int x;
if(front==NULL)
{
printf("Queue is Underflow\n");
return(0);
}
temp=front;
x=temp->data;
front=front->next;
temp->next=NULL;
free(temp);
return(x);
}
42
COMPUTER PROGRAMMING LAB
void display()
{
if(front==NULL)
{
printf("Queue Is Empty\n");
}
else
{
printf("List of Queue Elements are: \n");
for(temp=front;temp!=NULL;temp=temp->next)
printf(" --> %d ",temp->data);
printf("\n");
}
}
/* SAMPLE OUTPUT
IMPLEMENTATION OF LINEAR QUEUE USING SINGLE LINKED LIST
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice : 1
Enter the data value to be inserted into the Queue : 1
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice : 1
Enter the data value to be inserted into the Queue : 2
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice : 1
Enter the data value to be inserted into the Queue : 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice : 3
List Of Queue Elements Are :
--> 1 --> 2 --> 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice : 4 */
43
COMPUTER PROGRAMMING LAB
Program-19 :
Aim:Implement the following sorting techniques.
i) Bubble sort ii) Insertion Sort iii) Quick Sort iv) Heap Sort.
Program-19(i):
Aim :Algorithm for Bubble Sort Technique
1.Start
2.Read ‘n’ elements to be sorted.
3.Sort the elements by using following bubble sort technique.
4.Compare the first element with second element if first element is greater than second
element then swap the two elements and then compare first with third element and
swap the elements if first element is greater than third element.
Repeat the process for ‘n’ elements.
5.Repeat step 4 ‘n-1’ times.
6.Now elements are will be in sorted order.
7.Stop.
Program-19(ii):
Aim :Algorithm for Insertion Sort Technique
1.Start
2.Read ‘n’ elements to be sorted.
3.Sort the elements by using following insertion sort technique.
4.Consider one element in the list and assume that the list is in sorted order.
5.Take 2nd element and Compare second element with all the elements in the list.
And insert an element in an appropriate place in the list.
6.Repeat step 5 for all elements.Then the list will be in the sorted order.
7..Stop.
44
COMPUTER PROGRAMMING LAB
Program-19(iii):
Aim :Algorithm for Quick Sort Technique
Algorithm for quick sort :
1.void quick_sort(int left,int right)
2.{
3.int l,r,pivot;
4. pivot=a[left];
5. l=left;
6. r=right;
7. while(left<right)
8. {
9. while((left<right) && (a[right]>=pivot))
10.
right--;
11. if(left!=right)
12. {
13. a[left]=a[right];
14. left++;
15.}
16. while((left<right) && (a[left]<=pivot))
17. left++;
18. if(left!=right)
19. {
20. a[right]=a[left];
21. right--;
22. }
23. }
24. a[left]=pivot;
25. pivot=left;
26. left=l;
27. right=r;
28. if(left<pivot)
29. quick_sort(left,pivot-1);
30. if(right>pivot)
31. quick_sort(pivot+1,right);
32.}
45
COMPUTER PROGRAMMING LAB
Program-20 :
Aim: Implement the following searching method.
i)Sequential Search ii) Binary Search
Program-20(i) :
Aim :Algorithm for Linear Search Technique.
1.Start
2.Read ‘n’ elements.
3.Read ‘key’ value to be searched.
4.Compare each element of the list with key element, if it is equal to any one of the
element then the Search is Successful and display the position of the key element;
otherwise Search is Unsuccessful.
5.Stop.
Program-20(ii) :
Aim :Algorithm for Binary Search Technique
1.Start
2.Read ‘n’ elements.
3.Read ‘key’ value to be searched.
4.Sort all the elements by using any one of the sorting technique.
5.Apply following binary search technique on the key element, if the element is found
then the Search is Successful and display the position of the key element; otherwise
Search is Unsuccessful.
5.Stop.
Algorithm for Binary Search :
1.int bin_search(int key)
2.{
3. int low,high,mid;
4. low=0;
5. high=n-1;
6.while(low<=high)
7. {
8. mid=(low+high)/2;
9. if(key==a[mid])
10.
return(mid);
11. else if(key<a[mid])
12.
high=mid-1;
13.
else
14.
low=mid+1;
15. }
16. return(-1);
17. }
46
COMPUTER PROGRAMMING LAB
Program-21 :
Aim : i) Conversion of Infix expression to Postfix notation.
ii) Simple expression evaluator, that can handle +,-,/ and *.
Program-21(i) :
Aim : Conversion of Infix expression to Postfix notation.
#include<stdio.h>
#include<ctype.h>
#define MAX 100
char stack[MAX];
int top=-1;
main()
{
char infix[MAX],postfix[MAX];
int ip,op;
clrscr();
ip=op=-1;
printf("Enter the infix expression:\n");
scanf("%s",infix);
stack[++top]='(';
while(infix[++ip]!='\0')
{
if(isalpha(infix[ip]))
postfix[++op]=infix[ip];
else if(isoperator(infix[ip]))
{
while(prec(infix[ip])<=prec(stack[top]) && !(infix[ip]=='^'&&stack[top]=='^'))
{
postfix[++op]=stack[top--];
}
stack[++top]=infix[ip];
}
else if(infix[ip]=='(')
stack[++top]='(';
else if(infix[ip]==')')
{
while(stack[top]!='(')
{
postfix[++op]=stack[top--];
}
top--;
}
}
47
COMPUTER PROGRAMMING LAB
while(top>0)
postfix[++op]=stack[top--];
postfix[++op]='\0';
printf("\n\n%s Equivalent Postfix Expression Is : %s\n",infix,postfix);
getch();
}
prec(char ch)
{
switch(ch)
{
case '^' : return(3);
case '*' :
case '/' :
case '%' : return(2);
case '+' :
case '-' : return(1);
case '(' : return(0);
}
}
isoperator(char ch)
{
switch(ch)
{
case '^' :
case '*' :
case '/' :
case '%' :
case '+' :
case '-' : return(1);
default : return(0);
}
}
/*
SAMPLE OUTPUT
Enter the infix expression:
((a+b*c)^(d-e))^(f/g)
((a+b*c)^(d-e))^(f/g) Equivalent Postfix Expression Is : abc*+de-^fg/^
*/
48
COMPUTER PROGRAMMING LAB
Program-21(ii):
Aim : Simple expression evaluator, that can handle +,-,/ and *..
#include<stdio.h>
#include<ctype.h>
#include<math.h>
int compute(char,int,int);
main()
{
char postfix[40];
int stack[40],top=-1;
int i=-1,op1,op2,value;
clrscr();
printf("Implementation Of Postfix Expression Evaluation \n\n");
printf("Enter The Valid Postfix Expression : ");
flushall();
gets(postfix);
while(postfix[++i]!='\0')
{
if(isdigit(postfix[i]))
stack[++top]=postfix[i]-'0';
else
{
op2=stack[top--];
op1=stack[top--];
value=compute(postfix[i],op1,op2);
stack[++top]=value;
}
}
printf("\n\nResult Of The Postfix Exp : %s Is : %d",postfix,stack[top]);
getch();
}
int compute(char ch,int a,int b)
{
switch(ch)
{
case '+' :return(a+b);
case '-' :return(a-b);
case '*' :return(a*b);
case '/' :return(a/b);
case '%' :return(a%b);
case '^' :return((int)pow(a,b));
}
}
/*SAMPLE OUTPUT
Implementation Of Postfix Expression Evaluation
Enter The Valid Postfix Expression : 23+4^
Result Of The Postfix Exp : 23+4^ Is : 625 */
49
Download