Ece2rd Year
Ece2rd Year
Ece2rd Year
AIM:
To write a C program find whether the given number is odd or even.
ALGORITHM:
Step 1 : Start.
Step 2 : Read num, c.
Step3 : if(num= = 0), then print it is neither odd or even
otherwise go to step-4.
Step4 : else calculate c=num%2.
Step5 : if(c= =0), then print the given number is even,
else print the given number is odd.
Step6 : Stop.
1
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num,c;
printf("\n Enter the no. :");
scanf("%d",&num);
if(num= =0)
printf("it is neither odd nor even");
else{
c=num%2;
if(c= =0)
printf("\nThe given no. is even");
else
printf("\nThe given no. is odd");
}
getch();
}
2
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
3
Ex. No:1(b) LOOPING - ARMSTRONG OR NOT USING WHILE
DATE:
AIM:
To write a C program to find whether the given number is Armstrong or not.
ALGORITHM:
Step 1 : Start.
Step 2 : Read n,t,s,r.
Step3 : Let t=n & s=0.
Step 4 : while(n>0), calculate r =n%10, s=s+(r*r*r) & n=n/10.
Step 5 : if(t = = s), print „t‟ is an amstrong number , else print
„t‟ is not an amstrong number.
Step 6 : Stop.
4
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,t,s,r;
printf("\n Enter the number");
scanf("%d",&n);
t=n;
s=0;
while(n>0)
{
r =n%10;
s =s+(r*r*r);
n =n/10;
}
if(t= =s)
printf("\n %d is an amstrong number",t);
else
printf("\n %d is not an amstrong number ",t);
getch();
}
5
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
6
Ex. No: 1(c)
DATE: LOOPING - SUM OF DIGITS USING DO-WHILE
AIM:
To write a C program to print the sum of digits of a given number.
ALGORITHM:
Step 1 : Start.
Step 2 : Read num,sum=0,rem.
Step 3 : do and calculate rem = num%10,sum =sum+rem, num=num/10
Step 4: Check while(num>0); until condition become false
Step 4 : Print sum.
Step 5: Stop.
7
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
long int num, rem, sum=0;
clrscr();
printf("Enter the Number: ");
scanf("%ld", &num);
do
{
rem = num%10;
sum = sum + rem;
num = num/10;
} while(num>0);
printf("The Sum of digits of the Given Number is: %ld ",sum);
getch();
}
8
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
9
Ex. No:1(D)
DATE: LOOPING - FIBONACCI SERIES USING FOR LOOP
AIM:
To write a C program to print the Fibonacci Series.
ALGORITHM:-
Step 1 : Start.
Step 2 : Read l=-1,j=1,n,k,i.
Step 3 : for(i=0,i<n,i++)
Step 4: k=l+j
Step 5 : Print sum.
Step 6 : l=j;
Step 7 : j=k;
Step 8 : Go to STEP 3.
Step 9: Stop.
10
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int l = -1,j = 1,i,n,k;
clrscr();
printf("Enter the number of terms: ");
scanf("%d",&n);
printf("\n\t\t\t FIBONACCI SERIES\t\t\t\n ");
for(i = 0; i<n ; i++)
{
k =l+j;
printf(" %d \t",k);
l = j;
j = k;
}
getch();
}
11
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
12
Ex. No:1(E)
DATE: DATA MANIPULATIONS-FILE HANDLING-SEQUENTIAL ACCESS
AIM:
To write a C program for file handling.
ALGORITHM:
Step-1 : Start.
Step -2 : Open a file in write mode and read mode.
Step-3 : Read a string and end with # symbol.
Step -4 : Use putc() function to write the given message in file.
Step-5: Use getc() function for read the given message as one by one character from
file.
Step-6: Display the message in the window read from file.
Step -7: Stop.
13
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main(){
char c;
FILE *fp= fopen("write.c","w");
printf("Enter contents to store in file (Enter # at end):\n");
while((c=getchar())!='#')
putc(c,fp);
fclose(fp);
fp = fopen("write.c","r");
while (c!= EOF){
putchar(c);
c = getc(fp);
}
fclose(fp);
getch();
}
14
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
15
Ex. No:1(F) DATA MANIPULATIONS-FILE HANDLING-RANDOM ACCESS
DATE:
AIM:
To write a C program random access in file handling.
ALGORITHM:
Step-1: Start.
Step -2 : Declare two character array.
Step-3 : Create a file in write mode.
Step -4 : Check EOF is reached or not.
Step-5: Use the function for handling the file.
Step -6: Display the output.
Step-7: Stop.
16
PROGRAM:
#include <stdlib.h>
#include <stdio.h>
#define BUFLEN 6
void main(){
char msg[] = "abcdefghijklmnopqrstuvwxyz";
char buf[BUFLEN];
FILE *fp;
clrscr();
// Read in 5 characters.
17
fgets(buf, BUFLEN, fp);
printf("\nAfter reading in %s, position = %ld", buf, ftell(fp));
// Read in 5 characters.
fgets(buf, BUFLEN, fp);
printf("\nReading starts at the beginning again: %s\n", buf);
fclose(fp);
getch();
}
18
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
19
Ex. No:1 (G) SUM OF ARRAY ELEMENTS
DATE:
AIM:
To write a C program to print the sum of array of a given number.
ALGORITHM:
Step 1: Start.
Step 2: Read a[5],b[5],c[5],i,j,k.
Step 3: Get values for Array a[5].
Step 4: Get values for Array b[5].
Step 5: Array c[5] is Sum of Array a[5] and b[5].
Step 6: Print Array c[5].
Step 7: Stop.
20
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],b[5],c[5],i,j,k;
clrscr();
printf("\n\t Enter 5 numbers in array 1:\n\t\t");
for(i=0;i<5;i++)
{ scanf("\t%d",&a[i]);}
printf("\n\t Enter 5 numbers in array 2:\n\t\t");
for(j=0;j<5;j++){
scanf("\t%d",&b[j]);
}
for(k=0;k<5;k++)
{
c[k]=a[k]+b[k];
}
printf("\n\t Sum of the given array:\n\n" );
for(k=0;k<5;k++){
printf("\t%d",c[k]);
}
getch();
}
21
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
22
Ex. No:1(H)
DATE: MATRIX ADDITION USING 2D ARRAY
AIM:
To write a C program to add two matrices.
ALGORITHM:
Step-1 : Start.
Step -2 : Declare a two dimensional array
Step-3 : Read two matrices
Step -4 : Use nested for loop
Step-5: Add two matrices
Step-6 : store the answer in array variable
Step -7 : Print result.
Step-8: Stop.
23
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ( ) {
int a[2][2],b[2][2],s[2][2];
int i,j;
clrscr ();
printf("enter first matrix: \n");
for ( i=1; i<=2; i++) {
for ( j=1; j<=2; j++) {
printf("Enter %d%d", i,j, "element:");
scanf("%d",&a[i][j]);
}
}
printf("enter second matrix: \n");
for(i=1;i<=2;i++) {
for(j=1; j<=2;j++) {
printf( "enter %d%d",i + 1 ,j + 1 , "element:");
scanf("%d",&b[i][j]) ;
}
}
for (i=1;i<=2;i++) {
for (j=1;j<=2;j++) {
s[i][j]= a[i][j]+b[i][j];
}
}
printf("The addition matrix is:\n");
for (i=1;i<=2;i++) {
for (j=1;j<=2;j++) {
printf("%d\t",s[i][j] );
24
}
printf("\n");
}getch ();
}
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
25
Ex. No:1(I) MATRIX MULTIPLICATION USING 2D ARRAY
DATE:
AIM:
To write a C program to multiply two matrices.
ALGORITHM:
Step-1 : Start.
Step -2 : Declare a two dimensional array
Step-3 : Read two matrices
Step -4: Use nested for loop
Step-5: Multiply two matrices
Step -6 : store the answer in array variable
Step-7 : Print result.
Step -8: Stop.
26
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( ) {
int a[2][2], b[2][2],s[2][2];
int i,j,k;
printf("Enter first matrix:\n" );
for( i=1;i<=2;i++) {
for( j=1;j<=2;j++) {
printf("Enter%d%d:",i,j , "element:");
scanf("%d",&a[i][j]);
}
}
printf("Enter second matrix:\n");
for(i=1;i<=2;i++) {
for(j=1;j<=2;j++) {
printf("Enter %d%d:",i,j , "element:");
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=2;i++) {
for(j=1;j<=2;j++) {
s[i][j]=0;
for(k=1;k<=2;k++) {
s[i][j] =s[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Matrix Multiplication Is: \n");
for(i=1;i<=2;i++) {
27
for (j=1;j<=2;j++){
printf("%d\t",s[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
28
Ex. No: 2 STRING FUNCTION IMPLEMENTATION
DATE:
AIM: -
To perform following operation on strings using string functions
1. Addition 2. Copying 3. Reverse 4. Length of String.
ALGORITHM:
Step-1 : Start.
Step -2 : Declare a three character variable as a,b,c
Step-3 : Get two string as a input and stored in a &b
Step -4: Concatenated two string using strcat() function and print them
Step-5: Calculate length of the first string using strlen() function and print them
Step -6 : Copy first string into c variable using strcopy() function and print them
Step-7 : Reverse a string using strrev() function and print them
Step -8: Stop.
29
PROGRAM
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char a[20],b[20],c[20];
int l;
clrscr();
printf("Enter the First String");
scanf("%s",&a);
printf("Enter the Second String");
scanf("%s",&b);
strcat(a,b);
printf("\nConcatenation of String a and b is:%s",a);
l=strlen(a);
printf("\nLength of String is %d",l);
strcpy(c,a);
printf("\nthe Copied String is %s",c);
strrev(a);
printf("\nreverse of String is %s",a);
getch();
}
30
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
31
Ex. No: 3(A)
DATE: IMPLEMENTATION OF STUDENT DETAILS USING STRUCTURES
AIM:
To write a C program to use structures to print name and addressof student.
ALGORITHM:
Step-1 : Start.
Step -2 : Read Structure address,student.
Step-3 : Read structure members name,doorno,strtname,place,city.
Step -4 : Print structure members name,doorno,strtname,place,city.
Step-5 : Stop.
32
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct addr{
int doorno;
char strtname[15];
char place[20];
char city[20];
};
struct student{
char name[15];
struct addr address;
}s1;
void main(){
clrscr();
printf("\n\t\t Enter Student Name: ");
gets(s1.name);
printf("\n\t Enter Address: \n");
printf("\n\t\t Enter Door number : ");
gets(s1.address.doorno);
printf("\n\t\t Enter Street name : ");
gets(s1.address.strtname);
printf("\n\t\t Enter Place : ");
gets(s1.address.place);
printf("\n\t\t Enter City : ");
33
gets(s1.address.city);
printf("\n\n\n\t\t Student Name: ");
puts(s1.name);
printf("\n\t Address: \n");
printf("\n\t\t Door number : ");
puts(s1.address.doorno);
printf("\n\t\t Street name : ");
puts(s1.address.strtname);
printf("\n\t\t Place : ");
puts(s1.address.place);
printf("\n\t\t City : ");
puts(s1.address.city);
getch();
}
34
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
35
Ex. No: 3(B) CALL BY REFERENCE USING POINTER
D
AIM:
To write a C program that calls a function by call by reference.
ALGORITHM:
Step-1 : Start.
Step-2 : Read a,b.
Step-3 : Print a,b.
Step-4 : call function (&a,&b)
Step-5 : Print a,b
Step-6 : Stop
function(&a, &b)
Step-1 : Start function(*a,*b).
Step-2 : temp =* a, *a =*b,*b =temp.
Step -3 : Print a,b.
Step-4 : Return.
36
PROGRAM:
#include<stdio.h>
#include<conio.h>
void function(int *, int *);
void main(){
int a,b;
clrscr();
printf("\n\t Enter a value for A: ");
scanf("%d",&a);
printf("\n\t Enter a value for B: ");
scanf("%d",&b);
printf("\n\n The values before swap are:\n");
printf("\n\t Now the value for A: %d ",a);
printf("\n\t Now the value for B: %d ",b);
function(&a,&b);
printf("\n\n The values after swap are:\n");
printf("\n\t Now the value for A: %d ",a);
printf("\n\t Now the value for B: %d ",b);
getch();
}
37
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
38
Ex. No: 4
DATE: DYNAMIC MEMORY ALLOCATIONS
AIM
To writ C program to read and print the N student details using structure and Dynamic Memory
Allocation.
ALGORITHMS
Step 1: Start
Step 2: Create a structure as name, roll, perc
Step 3: Dynamically allocate memory to structure variables using malloc() function
Step 4: Get name, roll and perc from user and stored in corresponding memory location
Step 5: Display Student name, roll and perc as a output
Step 6: Stop
39
PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[30];
int roll;
float perc;
};
int main()
{
struct student *pstd;
int n,i;
if(pstd==NULL)
{
printf("Insufficient Memory, Exiting... \n");
return 0;
}
40
}
return 0;
}
OUTPUT
RESULT:
Thus the program was executed and output is verified successfully.
41
Ex. No: 5 (A) ARRAY IMPLEMENTATION OF STACK
DATE:
AIM:
To write a C program to implement Stack operations such as push, pop and display using
array.
ALGORITHM:
Step 1: Start.
Step 3: push operation increases top by one and writes pushed element to storage[top];
Step 4: pop operation checks that top is not equal to -1 and decreases top variable by 1;
Step 5: display operation checks that top is not equal to -1 and returns storage[top];
Step 6: Stop.
42
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define size 5
int item;
int s[10];
int top;
void display()
{
int i;
if(top==-1)
{
printf("\nstack is empty");
return;
}
printf("\nContent of stack is:\n");
for(i=0;i<=top;i++)
printf("%d\t",s[i]);
}
void push()
{
if(top==size-1)
{
printf("\nStack is full");
return;
}
printf("\nEnter item:\n");
scanf("%d",&item);
43
s[++top]=item;
}
void pop()
{
if(top==-1)
{
printf("\nstack is empty");
return;
}
printf("\nDeleted item is: %d",s[top]);
top--;
}
void main()
{
int ch;
top=-1;
clrscr();
printf("\n1.push\t\t2.pop\n3.display\t4.exit\n");
do{
printf("\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:// printf("Enter item:\n");
//scanf("%d",&item);
push();
break;
case 2: pop();
break;
44
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong entry ! try again");
}}while(ch<=4);
getch();
}
45
OUTPUT:
1.push 2.pop
3.display 4.exit
Enter your choice:
1
Enter item:
100
Enter your choice:
1
Enter item:
200
Enter your choice:
1
Enter item:
300
Enter your choice:
2
Deleted item is: 300
Enter your choice:
3
RESULT:
Thus a C program for Stack using array was implemented successfully.
46
Ex. No: 5(B)
DATE: ARRAY IMPLEMENTATION OF QUEUE
AIM:
To write a C program to implement Queue operations such as enqueue, dequeue and
display using array.
ALGORITHM:
Step 1: Start.
Step 3: Enqueue operation moves a rear by one position and inserts a element at the rear.
Step 4: Dequeue operation deletes a element at the front of the list and moves the front by one
position
Step 6: Stop.
47
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define SIZE 5 /* Size of Queue */
int Q[SIZE],f=0,r=-1; /* Global declarations */
Qinsert(int elem)
{ /* Function for Insert operation */
if( Qfull())
printf("\n\n Overflow!!!!\n\n");
else
{
++r;
Q[r]=elem;
}
}
int Qdelete()
{ /* Function for Delete operation */
int elem;
if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");
return(-1); }
else
{
elem=Q[f];
f=f+1;
return(elem);
}
}
48
int Qfull()
{ /* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}
int Qempty()
{ /* Function to Check Queue Empty */
if(f > r) return 1;
return 0;
}
display()
{ /* Function to display status of Queue */
int i;
if(Qempty()) printf(" \n Empty Queue\n");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("%d ",Q[i]);
printf("<-Rear");
}
}
void main()
{ /* Main Program */
int opn,elem;
do
{
clrscr();
49
printf("\n ### Queue Operations using Arrays### \n\n");
printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");
printf("\n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("\n\nRead the element to be Inserted ?");
scanf("%d",&elem);
Qinsert(elem); break;
case 2: elem=Qdelete();
if( elem != -1)
printf("\n\nDeleted Element is %d \n",elem);
break;
case 3: printf("\n\nStatus of Queue\n\n");
display(); break;
case 4: printf("\n\n Terminating \n\n"); break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
printf("\n\n\n\n Press a Key to Continue . . . ");
getch();
}while(opn != 4);
getch();
}
50
OUTPUT:
51
Ex. No: 6(A)
DATE: LINKED LIST IMPLEMENTATION OF STACK
AIM:
To write a C program to implement Stack operations such as push, pop and display using
linked list.
ALGORITHM:
Step 1: Start.
Step 6: Stop.
52
PROGRAM:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
void pop();
void push(int value);
void display();
struct node
{
int data;
struct node *link;
};
void main()
{
int choice,data;
53
scanf("%d",&data);
push(data);
break;
}
getch();
//return 0;
}
void display()
{
temp=top;
if(temp==NULL)
{
printf("\nStack is empty\n");
}
printf("\n The Contents of the Stack are...");
while(temp!=NULL)
{
printf(" %d ->",temp->data);
temp=temp->link;
54
}
}
void push(int data)
{
temp=(struct node *)malloc(sizeof(struct node)); // creating a space for the new element.
temp->data=data;
temp->link=top;
top=temp;
display();
void pop()
{
if(top!=NULL)
{
printf("The poped element is %d",top->data);
top=top->link;
}
else
{
printf("\nStack Underflow");
}
display();
}
55
OUTPUT:
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :10
The Contents of the Stack are... 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :20
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :30
The Contents of the Stack are... 30 -> 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:2
56
The poped element is 30
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:3
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:4
RESULT:
Thus a C program for Stack using linked list was implemented successfully.
57
Ex. No: 6(B) LINKED LIST IMPLEMENTATION OF QUEUE
DATE:
AIM:
To write a C program to implement Queue operations such as enqueue, dequeue and
display using linked list.
ALGORITHM:
Step 1: Start.
Step 6: Stop.
58
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *link;
}*front = NULL, *rear = NULL;
void insert();
void delet();
void display();
int item;
void main()
{
int ch;
do
{
printf("\n\n1.\tEnqueue\n2.\tDequeue\n3.\tDisplay\n4.\tExit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
59
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid choice. Please try again...\n");
}
} while(1);
getch();
}
void insert()
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if(rear == NULL)
{
rear = (struct node *)malloc(sizeof(struct node));
rear->info = item;
rear->link = NULL;
front = rear;
}
else
{
rear->link = (struct node *)malloc(sizeof(struct node));
rear = rear->link;
rear->info = item;
rear->link = NULL;
60
}
}
void delet()
{
struct node *ptr;
if(front == NULL)
printf("\n\nQueue is empty.\n");
else
{
ptr = front;
item = front->info;
front = front->link;
free(ptr);
printf("\nItem deleted: %d\n", item);
if(front == NULL)
rear = NULL;
}
}
void display()
{
struct node *ptr = front;
if(rear == NULL)
printf("\n\nQueue is empty.\n");
else
{
printf("\n\n");
while(ptr != NULL)
{
printf("%d\t",ptr->info);
61
ptr = ptr->link;
}
}}
OUTPUT:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 15
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Item deleted: 12
1. Enqueue
2. Dequeue
62
3. Display
4. Exit
RESULT:
Thus a C program for Queue using linked list was implemented successfully.
63
Ex. No: 7(A) APPLICATION OF STACKS (INFIX TO POSTFIX EXPRESSION)
DATE:
AIM:
To write a C program to implement the conversion of infix to postfix expression using
Stack.
ALGORITHM:
Step 1: Start.
Step 2: Create a stack to store operand and operator.
Step 3: In Postfix notation the operator follows the two operands and in the infix notation the
operator is in between the two operands.
Step 4: Consider the sum of A and B. Apply the operator “+” to the operands A and B and write
the sum as A+B is INFIX. + AB is PREFIX. AB+ is POSTFIX
Step 5: Get an Infix Expression as input and evaluate it by first converting it to postfix and then
evaluating the postfix expression.
Step 6: The expressions with in innermost parenthesis must first be converted to postfix so that
they can be treated as single operands. In this way Parentheses can be successively
eliminated until the entire expression is converted.
Step 7: The last pair of parentheses to be opened with in a group of parentheses encloses the first
expression with in that group to be transformed. This last-in first-out immediately
suggests the use of Stack. Precedence plays an important role in the transforming infix to
postfix.
Step 8: Stop.
64
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
int top=-1;
char pop();
char stack[MAX];
void push(char item);
case '*':
case '/':return 4;
break;
case '^':
case '$':return 6;
break;
case '(':
case ')':
65
case '#':return 1;
break;
}
}
int isoperator(char symbol){
switch(symbol){
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '(':
case ')':return 1;
break;
default:
return 0;
}
}
void convertip(char infix[],char postfix[]){
int i,symbol,j=0;
stack[++top]='#';
for(i=0;i<strlen(infix);i++){
symbol=infix[i];
if(isoperator(symbol)==0){
postfix[j]=symbol;
j++;
}
else{
if(symbol=='(')
push(symbol);
66
else if(symbol==')'){
while(stack[top]!='('){
postfix[j]=pop();
j++;
}
pop();//pop out (.
}
else{
if(prcd(symbol)>prcd(stack[top]))
push(symbol);
else{
while(prcd(symbol)<=prcd(stack[top])){
postfix[j]=pop();
j++;
}
push(symbol);
}//end of else.
}//end of else.
}//end of else.
}//end of for.
while(stack[top]!='#'){
postfix[j]=pop();
j++;
}
postfix[j]='\0';//null terminate string.
}
void main(){
char infix[20],postfix[20];
clrscr();
printf("Enter the valid infix string:\n");
67
gets(infix);
convertip(infix,postfix);
printf("The corresponding postfix string is:\n");
puts(postfix);
getch();
}
void push(char item){
top++;
stack[top]=item;
}
char pop(){
char a;
a=stack[top];
top--;
return a;
}
68
OUTPUT:
RESULT:
Thus a C program to implement the conversion of infix to postfix expression using Stack.
69
Ex.NO: 7(B)
DATE: EVALUATION OF POSTFIX EXPRESSION USING STACK
AIM:
To write a C program to implement the evaluation of postfix expression using Stack.
ALGORITHM:
Step-1: Start.
Step 2: Scan the Postfix string from left to right.
Step 3: Initialize an empty stack.
Step 4: If the scanned character is an operand, add it to the stack. If the scanned character is an
operator, there will be atleast two operands in the stack.
Step 5: If the scanned character is an Operator, then we store the top most element of the
stack(topStack) in a variable temp. Pop the stack. Now evaluate topStack(Operator)temp.
Let the result of this operation be retVal. Pop the stack and Push retVal into the stack.
Repeat this step till all the characters are scanned.
Step 6: After all characters are scanned, we will have only one element in the stack. Return
topStack.
Step 7: Stop.
70
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct stack
{
int top;
float a[50];
}s;
void main()
{
char pf[50];
float d1,d2,d3;
int i;
clrscr();
s.top=-1;
printf("\n\n Enter the postfix expression:");
gets(pf);
for(i=0;pf[i]!='\0';i++){
switch(pf[i]){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
71
case '6':
case '7':
case '8':
case '9':
s.a[++s.top]=pf[i]-'0';
break;
case '+':
d1=s.a[s.top--];
d2=s.a[s.top--];
s.a[++s.top]=d1+d2;
break;
case '-':
d2=s.a[s.top--];
d1=s.a[s.top--];
s.a[++s.top]=d1-d2;
break;
case '*':
d2=s.a[s.top--];
d1=s.a[s.top--];
s.a[++s.top]=d1*d2;
break;
case '/':
d2=s.a[s.top--];
d1=s.a[s.top--];
s.a[++s.top]=d1/d2;
break;
}
}
printf("\n\n The value of expression is%f",s.a[s.top]);
getch();
72
}
OUTPUT:
RESULT:
Thus a C program to implement the evaluation of postfix expression using Stack was
implemented successfully.
73
Ex. No: 7(C) CPU SCHEDULING USING QUEUE (APPLICATION OF QUEUE)
DATE:
AIM:
To write a C program to implement the CPU scheduling FCFS using queue.
ALGORITHM:
Step-1: Start.
Step 2: Get total number of processor from user.
Step 3: Calculate waiting time using wt[i]+=bt[j].
Step 4: Calculate turnaround time using tat[i]=bt[i]+wt[i].
Step 5: Calculate average waiting time using avwt/=i.
Step 6: Calculate average turnaround time using avtat/=i.
Step 7: Display process, burst time, waiting time and turnaround time
Step 8: Display average waiting time and turnaround time.
Step 9: Stop
74
PROGRAM
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
75
return 0;
}
OUTPUT
RESULT:
Thus a C program to implement the CPU scheduling using queue was implemented
successfully
76
Ex. No: 8 IMPLEMENTATION OF TREE, TREE TRAVERSALS
DATE:
AIM
To write a C program to implement tree and tree traversals.
ALGORITHM
Step-1: Start.
Step 2: Create a structure as data, left and right pointer link.
Step 3: Allocate memory for structure using malloc() function.
Step 4: Get input from user.
Step 5: Call insert() function to insert data into corresponding memory location.
Step 6: Call inorder() function to display data as inorder.
Step 7: Call preorder() function to display data as preorder.
Step 8: Call postorder() function to display data as postorder.
Step 9: Stop
77
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *left,*right;
};
78
if (t->data >x)
if (t->left==NULL)
ins(t,x,1);
else
inser(t->left,x);
else if (t->data < x)
if (t->right==NULL)
ins(t,x,2);
else
inser(t->right,x);
else
printf("\n Element is already present in the list\n");
}
void main()
79
{
int op,n;
root=(struct node *)malloc(sizeof(struct node));
root->data=30;
root->right=root->left=NULL;
clrscr();
do
{
printf("\n 1.Insertion");
printf("\n 2.Preorder");
printf("\n 3.Inorder");
printf("\n 4.Postorder");
printf("\n 5.Quit");
printf("\n Enter your choice\n");
scanf("%d",&op);
switch (op)
{
case 1: printf("\n Enter the element to insert\n");
scanf("%d",&n);
inser(root,n);
break;
80
OUTPUT:
1. Insertion
2. Preorder
3. Inorder
4. Postorder
5. Quit
1. Insertion
2. Preorder
3. Inorder
4. Postorder
5. Quit
1. Insertion
2. Preorder
3. Inorder
4. Postorder
5. Quit
81
1. Insertion
2. Preorder
3. Inorder
4. Postorder
5. Quit
1. Insertion
2. Preorder
3. Inorder
4. Postorder
5. Quit
1. Insertion
2. Preorder
3. Inorder
4. Postorder
5. Quit
1. Insertion
2. Preorder
3. Inorder
82
4. Postorder
5. Quit
1. Insertion
2. Preorder
3. Inorder
4. Postorder
5. Quit
9
4
2
6
15
12
17
1. Insertion
2. Preorder
3. Inorder
4. Postorder
5. Quit
2
4
6
9
12
15
17
83
1. Insertion
2. Preorder
3. Inorder
4. Postorder
5. Quit
2
6
4
12
17
15
9
RESULT:
Thus a C program for the concept of tree traversal was implemented successfully.
84
Ex. No: 9 IMPLEMENTATION OF BINARY SEARCH TREE
DATE:
AIM
To write a C program to implement binary search tree.
ALGORITHM
Step-1: Start.
Step 2: Create a structure as element, left and right pointer link.
Step 3: Allocate memory for structure using malloc() function.
Step 4: Get input from user.
Step 5: Call insert() function to insert element into corresponding memory location.
Step 6: Call delet() function to delete specified data from memory location.
Step 7: Call findmin() function to find minimum element.
Step 8: Call findmax() function to find maximum element.
Step 9: Call display() function to display element.
Step 10: Stop
85
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct searchtree
{
int element;
struct searchtree *left,*right;
}*root;
typedef struct searchtree *node;
typedef int ElementType;
node insert(ElementType, node);
node delet(ElementType, node);
void makeempty();
node findmin(node);
node findmax(node);
node find(ElementType, node);
void display(node, int);
void main()
{
int ch;
ElementType a;
node temp;
makeempty();
while(1)
{
printf("\n1. Insert\n2. Delete\n3. Find\n4. Find min\n5. Find max\n6.Display\n7. Exit\nEnter
Your Choice : ");
scanf("%d",&ch);
86
switch(ch)
{
case 1:printf("Enter an element : ");
scanf("%d", &a);
root = insert(a, root);
break;
case 2:printf("\nEnter the element to delete : ");
scanf("%d",&a);
root = delet(a, root);
break;
case 3:printf("\nEnter the element to search : ");
scanf("%d",&a);
temp = find(a, root);
if (temp != NULL)
printf("Element found");
else
printf("Element not found");
break;
case 4:
temp = findmin(root);
if(temp==NULL)
printf("\nEmpty tree");
else
printf("\nMinimum element : %d", temp->element);
break;
case 5:
temp = findmax(root);
if(temp==NULL)
printf("\nEmpty tree");
else
printf("\nMaximum element : %d", temp->element);
87
break;
case 6:
if(root==NULL)
printf("\nEmpty tree");
else
display(root, 1);
break;
case 7:
exit(0);
default:
printf("Invalid Choice");
}
}
}
node insert(ElementType x,node t)
{
if(t==NULL)
{
t = (node)malloc(sizeof(node));
t->element = x;
t->left = t->right = NULL;
}
else
{
if(x < t->element)
t->left = insert(x, t->left);
else
if(x > t->element)t->right = insert(x, t->right);
}
return t;
}
88
node delet(ElementType x,node t)
{
node temp;
if(t == NULL)
printf("\nElement not found");
else
{
if(x < t->element)
t->left = delet(x, t->left);
else
if(x > t->element)
t->right = delet(x, t->right);
else
{
if(t->left && t->right)
{
temp = findmin(t->right);
t->element = temp->element;
t->right = delet(t->element,t->right);
}
else
if(t->left == NULL)
{
temp = t;
t=t->right;
free (temp);
}
else
{
temp = t;
t=t->left;
89
free (temp);
}
}
}
return t;
}
void makeempty()
{
root = NULL;
}
node findmin(node temp)
{
if(temp == NULL || temp->left == NULL)
return temp;
return findmin(temp->left);
}
node findmax(node temp)
{
if(temp==NULL || temp->right==NULL)
return temp;
return findmax(temp->right);
}
node find(ElementType x, node t)
{
if(t==NULL)
return NULL;
if(x<t->element)
return find(x,t->left);
if(x>t->element)
return find(x,t->right);
return t;
90
}
void display(node t,int level)
{
int i;
if(t)
{
display(t->right, level+1);
printf(“ \n”);
for(i=0;i<level;i++)
printf(" ");
printf("%d", t->element);
display(t->left, level+1);
}
}
91
OUTPUT
Sample Output:
1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 1
Enter an element : 10
1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 1
Enter an element : 20
1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
92
1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 4
The smallest Number is 5
1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 3
Enter an element : 100
Element not Found
1. Insert 2. Delete 3. Find 4. Find Min 5. Find Max 6. Display 7. Exit
Enter your Choice : 2
Enter an element : 20
1. Insert2. Delete3. Find4. Find Min5. Find Max 6. Display 7. Exit
Enter your Choice : 6
20
10
1. Insert 2. Delete 3. Find 4. Find Min 5. Find Max 6. Display 7. Exit
Enter your Choice : 7
RESULT:
Thus a C program for the concept of binary search trees was implemented successfully.
93
Ex. No: 10(A) LINEAR SEARCH
DATE:
AIM:
To write a C program to implement the concept of linear search.
ALGORITHM:
Step 3: If the current value matches the target then we declare victory and stop.
Step 4: If the current value is less than the target then set the current item to be the next item and
repeat from 2.
Step 5: Stop.
94
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main() {
int arr[20];
int i,size,sech;
printf("\n\t-- Linear Search --\n\n");
printf("Enter total no. of elements : ");
scanf("%d",&size);
for(i=0; i<size; i++) {
printf("Enter %d element : ",i+1);
scanf("%d",&arr[i]);
}
printf("Enter the element to be searched: ");
scanf("%d",&sech);
for(i=0; i<size; i++) {
if(sech==arr[i]) {
printf("Element exits in the list at position : %d",i+1);
break;
}
}
getch();
}
95
OUTPUT:
-- Linear Search --
RESULT:
Thus a C program for the concept of linear search was implemented successfully.
96
Ex. No: 10(B)
DATE: BINARY SEARCH
AIM:
To write a C program to implement the concept of binary search.
ALGORITHM:
Step 3: If the middle value is equal to the target then we declare victory and stop.
Step 4: If the middle item is less than the target, then we set the new list to be the upper half of
the old list and we repeat from step 2 using the new list.
Step 5: If the middle value is greater than the target, then we set the new list to be the bottom half
of the list, and we repeat from step 2 with the new list.
Step 6: Stop.
97
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main(){
int n,i,search,f=0,low,high,mid,a[20];
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("Enter the number in ascending order a[%d]=",i);
scanf("%d",&a[i]);
}
printf("Enter the search element:");
scanf("%d",&search);
low=1;
high=n;
while(low<=high){
mid=(low+high)/2;
if(search<a[mid]){
high=mid-1;
}
else if(search>a[mid]){
low=mid+1;
}
else{
f=1;
printf("obtained in the position %d:",mid);
getch();
exit();
98
}
}
if(f==0)
printf("not present");
getch();
}
OUTPUT:
RESULT:
Thus a C program for the concept of binary search was implemented successfully.
99
Ex. No: 11(A)
DATE: INSERTION SORT
AIM
Arrange a list of integers in ascending order using Insertion sort
ALGORITHM:
Step 1: Start
Step 2: Start with an empty left hand [sorted array] and the cards face down on the table
[unsorted array].
Step 3: Then remove one card [key] at a time from the table [unsorted array], and insert it into
the correct position in the left hand [sorted array].
Step 4: To find the correct position for the card, we compare it with each of the cards already in
the hand, from right to left.
Step 5: Print sorted number
Step 6: Stop
100
PROGRAM
#include<stdio.h>
void inst_sort(int[]);
void main()
{
int num[5],count;
printf("\n enter the five elements to sort:\n");
for(count=0;count<5;count++)
scanf("%d",&num[count]);
inst_sort(num); /*function call for insertion sort*/
printf("\n\n elements after sorting:\n");
for(count=0;count<5;count++)
printf("%d\n",num[count]);
}
void inst_sort(int num[])
{
/* function definition for insertion sort*/
int i,j,k;
for(j=1;j<5;j++)
{
k=num[j];
for(i=j-1;i>=0&&k<num[i];i--)
num[i+1]=num[i];
num[i+1]=k;
}
}
101
OUTPUT
RESULT:
Thus a C program for the concept of insert sort was implemented successfully.
102
Ex. No: 11(B)
DATE: BUBBLE SORT
AIM:
To write a C program to implement the concept of bubble sort.
ALGORITHM:
Step 1: Start.
Step 2: Repeat Steps 3 and 4 for i=1 to 10
[End of if]
Step 5: Stop.
103
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main(){
int n, i, j, temp , a[100];
printf("Enter the total integers you want to enter (make it less than 100):\n");
scanf("%d",&n);
printf("Enter the %d integer array elements:\n",n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(a[j+1]<a[j]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("The sorted numbers are:");
for(i=0;i<n;i++){
printf("%3d",a[i]);
}
getch();
}
104
OUTPUT:
Enter the total integers you want to enter (make it less than 100):
5
Enter the 5 integer array elements:
99
87
100
54
150
The sorted numbers are: 54 87 99 100 150
RESULT:
Thus a C program for the concept of bubble sort was implemented successfully.
105
Ex. No: 11(C) QUICK SORT
DATE:
AIM:
To write a C program to implement the concept of Quick sort.
ALGORITHM:
Step 1: Start.
Step 3: Divide all other elements (except the pivot) into two partitions.
o All elements less than the pivot must be in the first partition.
o All elements greater than the pivot must be in the second partition.
Step 5: Join the first sorted partition, the pivot, and the second sorted partition.
Step 6: Stop.
106
PROGRAM:
#include<stdio.h>
#include<conio.h>
void qsort(int arr[20], int fst, int last);
void main(){
int arr[30];
int i,size;
printf("Enter total no. of the elements : ");
scanf("%d",&size);
printf("Enter total %d elements : \n",size);
for(i=0; i<size; i++)
scanf("%d",&arr[i]);
qsort(arr,0,size-1);
printf("Quick sorted elements are as : \n");
for(i=0; i<size; i++)
printf("%d\t",arr[i]);
getch();
}
void qsort(int arr[20], int fst, int last){
int i,j,pivot,tmp;
if(fst<last){
pivot=fst;
i=fst;
j=last;
while(i<j){
while(arr[i]<=arr[pivot] && i<last)
i++;
107
while(arr[j]>arr[pivot])
j--;
if(i<j){
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
}
tmp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=tmp;
qsort(arr,fst,j-1);
qsort(arr,j+1,last);
}}
108
OUTPUT:
RESULT:
Thus a C program for the concept of Quick sort was implemented successfully.
109
Ex. No: 11(D)
DATE: MERGE SORT
AIM:
To write a C program to implement the concept of merge sort.
ALGORITHM:
Step 1: Start.
Step 2: First you divide the number of elements by 2 and seperate them as two.
Step 3: Divide those two which are divided by 2.
Step 4: Divide them until you get a single element.
Step 5: Start comparing the starting two pair of elements with each other and place them in
ascending order.
Step 6: When you combine them compare them so that you make sure they are sorted.
Step 7: When all the elements are compared the array will be surely sorted in an ascending order.
Step 8: Stop.
110
PROGRAM:
#include<stdio.h>
#include<conio.h>
void merge(int [],int ,int ,int );
void part(int [],int ,int );
void main(){
int arr[30];
int i,size;
printf("\n\t------- Merge sorting method -------\n\n");
printf("Enter total no. of elements : ");
scanf("%d",&size);
for(i=0; i<size; i++){
printf("Enter %d element : ",i+1);
scanf("%d",&arr[i]);
}
part(arr,0,size-1);
printf("\n\t------- Merge sorted elements -------\n\n");
for(i=0; i<size; i++)
printf("%d ",arr[i]);
getch();
}
111
part(arr,mid+1,max);
merge(arr,min,mid,max);}}
void merge(int arr[],int min,int mid,int max){
int tmp[30];
int i,j,k,m;
j=min;
m=mid+1;
for(i=min; j<=mid && m<=max ; i++){
if(arr[j]<=arr[m]){
tmp[i]=arr[j];
j++;
}
else{
tmp[i]=arr[m];
m++;
}
}
if(j>mid){
for(k=m; k<=max; k++){
tmp[i]=arr[k];
i++;
}
}
else{
for(k=j; k<=mid; k++){
tmp[i]=arr[k];
i++;
}
}
for(k=min; k<=max; k++)
arr[k]=tmp[k]; }
112
OUTPUT:
11 13 35 43 48 75 99 100 155
RESULT:
Thus a C program for the concept of merge sort was implemented successfully.
113
Ex. No: 12
DATE: IMPLEMENTATION OF HASH FUNCTION COLLISION
RESOLUTION TECHNIQUE
AIM
To implement all the functions of a dictionary (ADT) using hashing
ALGORITHM
Step 1: Start
Step 2: Read the key elements from the dictionary
Step 3: Use the hash function to implement dictionary
Step 4: Apply required operation on the dictionary
Step 5: Print the result
Step 6: Stop
114
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int b;
int
hsearch(int key,int d,int *ht,int *empty)
{
int i=key%(d);
int j=i;
int c=0;
do
{
if(empty[j]||(*(ht+j)==key))
return j;
c++;
j=(i+c)%(d);
}while(j!=i
);
return 0;
}
int search(int key,int d,int *ht,int *empty)
{
b=hsearch(key,d,ht,empty);
printf("%d",b);
if(empty[b]==1)
115
return -1;
else if(b==0)
return 1;
else
return b;
}
/*insertion
operation*/
void insert(int key,int d,int *ht,int *empty)
{
b=hsearch(key,d,ht,empty);
if(empty[b])
{
empty[b]=0;
*(ht+b)=key;
printf("elements is inserted\n");
}
}
/*deletion operation*/
void delete(int key,int d,int *ht,int *empty)
{
int b=hsearch(key,d,ht,empty);
*(ht+b)=0;
empty[b]=1;
printf("element is deleted\n");
}
void display(int d,int *ht,int *empty)
{
int i;
printf("hash table elements are\n");
for(i=0;i<d;i++)
116
{
if(empty[i])
printf(" 0");
else
printf("%5d",*(ht+i));
}
printf("\n");
}
/*main program*/
void main()
{
int choice=1;
int key;
int d,i,s;
int *empty,*ht;
printf("enter the hash table size:");
scanf("%d",&d);
ht=(int *)malloc(d *sizeof(int));
empty=(int *)malloc(d *sizeof(int));
for(i=0;i<d;i++)
empty[i]=1;
while(1)
{
printf("\n");
printf("\n LINEAR PROBING");
printf("\n 1:insert hash table:");
printf("\n 2:delete hash table");
printf("\n 3:search hash table");
printf("\n 4:display hash table");
printf("\n 5:exit");
printf("enter your choice");
117
scanf("%d",&choice);
switch(choice)
{
case 1:printf("enter the elemants:");
scanf("%d",&key);
insert(key,d,ht,empty);
break;
case 2:printf("enter to remove from hash table:");
scanf("%d",&key);
delete(key,d,ht,empty);
break;
case 3:printf("enter the search elements:");
scanf("%d",&key);
s=search(key,d,ht,empty);
if(s==-1||s==0)
printf("not found\n");
else
printf("element found at index %d",hsearch(key,d,ht,empty));
break;
case 4:display(d,ht,empty);
break;
case 5:exit(0);
}
}return;
}
118
OUTPUT
119
120
RESULT:
Thus a C program for the concept of hash function was implemented successfully.
121
Ex. No: 13
DATE: SINGLY LINKED LIST
AIM:
To write a C program to implement singly linked list.
ALGORITHM:
Step 1: Start
Step 2: Creation: Get the number of elements, and create the nodes having structures
DATA,LINK and store the element in Data field, link them together to form a
linked list.
Step 3: Insertion: Get the number to be inserted and create a new node store the value in
DATA field. And insert the node in the required position.
Step 4: Deletion: Get the number to be deleted. Search the list from the beginning and locate
the node then delete the node.
Step 5: Display: Display all the nodes in the list.
Step 6: Stop.
122
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
void main()
{
int n,i=1,opt;
clrscr();
p = NULL;
printf("%d",sizeof(LIST));
printf( "Enter the no of nodes :\n " );
scanf( "%d",&n );
123
count = n;
while( i <= n)
{
create();
i++;
}
printf("\nEnter your option:\n");
printf("1.Insert \t 2.Delete \t 3.Display \t 4.Exit\n");
do
{
scanf("%d",&opt);
switch( opt )
{
case 1:
insert();
count++;
break;
case 2:
delet();
count--;
if ( count == 0 )
{
printf("\n List is empty\n");
}
break;
case 3:
printf("List elements are:\n");
display();
break;
}
124
printf("\nEnter your option \n");
}while( opt != 4 );
getch();
}
void create ( )
{
if( p== NULL )
{
p = ( LIST * ) malloc ( sizeof ( LIST ) );
printf( "Enter the element:\n" );
scanf( "%d",&p->no );
p->next = NULL;
h = p;
}
else
{
t= ( LIST * ) malloc (sizeof( LIST ));
printf( "\nEnter the element" );
scanf( "%d",&t->no );
t->next = NULL;
p->next = t;
p = t;
}
}
void insert()
{
t=h;
p = ( LIST * ) malloc ( sizeof(LIST) );
printf("Enter the element to be inserted:\n");
125
scanf("%d",&p->no);
printf("Enter the position to insert:\n");
scanf( "%d",&pos );
if( pos == 1 )
{
h = p;
h->next = t;
}
else
{
for(j=1;j<(pos-1);j++)
t = t->next;
p->next = t->next;
t->next = p;
t=p;
}
}
void delet(){
printf("Enter the position to delete:\n");
scanf( "%d",&pos );
if( pos == 1 )
{
h = h->next ;
}
else
{
t= h;
for(j=1;j<(pos-1);j++)
t = t->next;
pt=t->next->next;
126
free(t->next);
t->next= pt;
}
}
void display()
{
t= h;
while( t->next != NULL )
{
printf("\t%d",t->no);
t = t->next;
}
printf( "\t %d\t",t->no );
}
127
OUTPUT:
128
12 1 13 2 3 14
Enter your option 2
Enter the position to delete:1
Enter your option 3
List elements are:
1 13 2 3 14
Enter your option 2
Enter the position to delete:3
Enter your option 3
List elements are:
1 13 3 14
Enter your option 2
Enter the position to delete:4
Enter your option 3
List elements are:
1 13 3
Enter your option: 6
RESULT:
Thus, a C program for Singly Linked List was implemented successfully.
129
Ex. No: 14
DATE: DOUBLY LINKED LIST
AIM:
To write a C program to implement doubly linked list with Insert, Delete and Display
operations.
ALGORITHM:
Step 1: Start
Step 2: Creation: Get the number of elements to create the list. Then create the node having the
Structure: BLINK, DATA , FLINK and store the elements in Data field. Link them
together to form a doubly linked list.
Step 3: Insertion: Get the number to be Inserted, create a new node to store the value. Search
the list and insert the node in its right position.
Step 4: Deletion: Get the number to be deleted. Search the list from the beginning and try to
locate node p with DATA. If found then delete the node.
Step 5: FLINK P‟s previous node to P‟s Next node. BLINK P‟s Next node to P‟s Previous node
else display “Data not Found”.
Step 6: Display: Display all the nodes in the list.
Step 7: Stop.
130
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
int j,pos,k=1,count;
void main()
{
int n,i=1,opt;
clrscr();
p = NULL;
131
printf( "Enter the no of nodes :\n " );
scanf( "%d",&n );
count = n;
while( i <= n)
{ create();
i++;
}
printf("\nEnter your option:\n");
printf("1.Insert \t 2.Delete \t 3.Display \t 4.Exit\n");
do
{
scanf("%d",&opt);
switch( opt ){
case 1:
insert();
count++;
break;
case 2:
delet();
count--;
if ( count == 0 )
{
printf("\n List is empty\n");
}
break;
case 3:
printf("List elements are:\n");
display();
break;
132
}
printf("\nEnter your option \n");
}while( opt != 4 );
getch();
}
void create ( )
{
if( p == NULL )
{
p = ( LIST * ) malloc ( sizeof ( LIST ) );
printf( "Enter the element:\n" );
scanf( "%d",&p->no );
p->next = NULL;
p->pre = NULL;
h = p;
}
else
{
t= ( LIST * ) malloc (sizeof( LIST ));
printf( "\nEnter the element" );
scanf( "%d",&t->no );
t->next = NULL;
p->next = t;
t->pre = p;
p = t;
}
}
void insert()
{
133
t=h;
p = ( LIST * ) malloc ( sizeof(LIST) );
printf("Enter the element to be insrted:\n");
scanf("%d",&p->no);
printf("Enter the position to insert:\n");
scanf( "%d",&pos );
if( pos == 1 )
{
h = p;
h->next = t;
t->pre = h;
h->pre = NULL;
}
else
{
for(j=1;j<(pos-1);j++)
t = t->next;
p->next = t->next;
t->next = p;
p->pre = t;
}
}
void delet()
{
printf("Enter the position to delete:\n");
scanf( "%d",&pos );
if( pos == 1 )
{
h = h->next ;
h->pre = NULL;
134
}
else
{
t= h;
for(j=1;j<(pos-1);j++)
t = t->next;
t->next = t->next->next;
t->next->pre = t;
free( t->next );
}
}
void display()
{
t= h;
while( t->next != NULL )
{
printf("%d\n",t->no);
t = t->next;
}
printf( "%d",t->no );
}
135
OUTPUT:
RESULT:
Thus a C program for Doubly Linked List was implemented successfully.
136
Ex. No: 15
DATE: HEAP SORTING
AIM
To write a C program to implement heap sort algorithm.
ALGORITHM
Step 1: Start.
Step 2: Get element from user.
Step 3: Create a new node at the end of heap.
Step 4: Assign new element to the node.
Step 5: Compare the value of this child node with its parent.
Step 6: If value of parent is less than child, then swap them.
Step 7: Repeat step 3 & 4 until Heap property holds.
Step 8: Display sorted element
Step 9: Stop
137
PROGRAM
#include<stdio.h>
void main()
{
int heap[30],n,i,last,temp;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("\nEnter elements:");
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);
//create a heap
heap[0]=n;
create(heap);
//sorting
while(heap[0] > 1)
{
//swap heap[1] and heap[last]
last=heap[0];
temp=heap[1];
heap[1]=heap[last];
heap[last]=temp;
heap[0]--;
down_adjust(heap,1);
}
138
void down_adjust(int heap[],int i)
{
int j,temp,n,flag=1;
n=heap[0];
OUTPUT
Enter elements:12 8 46 23 7
RESULT:
Thus a C program for heap sort was implemented successfully.
139