CS3362 - CPDS Lab Manual - 1
CS3362 - CPDS Lab Manual - 1
ARMCET
(Approved by AICTE & Affiliated to Anna University)
Registration Number :
Department :
Year of Study :
Semester :
ARM COLLEGE OF ENGINEERING AND TECHNOLOGY
SATTAMANGALAM, MARAIMALAI NAGAR, CHENNAI, TAMIL NADU. PIN-603 209.
ARMCET
(Approved by AICTE & Affiliated to Anna University)
BONAFIDE CERTIFICATE
Reg. No.
a) Using if
Algorithm:
1. create a two integer variable x,y
2.assign the values to that variable
3.check the condition
4. condition is true print the value
Program:
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
OUTPUT:
Variable x is less than y
b)If else
Algorithm:
1.create a two integer variable x,y
2.assign the values to that variable
3.check the condition using else if
4.condition is true execute if otherwise execute else statement
PROGRAM:
#include<stdio.h>
int main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
if(age >=18)
{
/* This statement will only execute if the
* above condition (age>=18) returns true
*/
printf("You are eligible for voting");
}
else
{
/* This statement will only execute if the
* condition specified in the "if" returns false.
*/
printf("You are not eligible for voting");
}
return0;
}
OUTPUT:
Enter your age:14
You are not eligible for voting
AIM
Write a program to implement a if else ladder
ALGORITHM:
1. Create a variable x & y
OUTPUT:
enter the value of x:20
enter the value of y:20
x is equal to y
End of Program
d)Nested if else
AIM:
Write a program to implement a nested if else
ALGORITHM:
1. Declare the integer variable n and get the value for n
2. First check the value even or not and then print the value
int main() {
return 0;
}
Input
14
Output
e) switch case:
AIM:
Write a program to check whether the operator is available not in a switch case
ALGORITHM:
1. Declare the two variable n1,n2
2. Get the operator and operands from user
3.open the switch case operation
4. In each case check whether the operator available or not
5.Otherwise print the default case statement
PROGRAM:
// Program to create a simple calculator
#include <stdio.h>
int main() {
char operation;
double n1, n2;
switch(operation)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;
return 0;
}
OUTPUT:
RESULT:
Thus the program implementation using file has been executed successfully
Ex.No: 2 Practice a C programming using Functions & Array
Date:
AIM:
Write a program to implement a function using an one dimensional array
ALGORITHM:
1.Declare the one dimensional array named
score2.And assign the values to that array
4. Declare the function find Average with function variable int[], and int
PROGRAM:
#include <stdio.h>
float findAverage(int [], int);
int main(void) {
int score[5] = {90, 80, 70, 75, 85};
int papers = 5;
float avg = findAverage(score, papers);
printf("Average: %f\n", avg);
return 0;
}
float findAverage(int arr[], int size) {
// variables
int i;
float sum = 0, avg = 0;
// find total
for (i = 0; i < size; i++)
{
sum += arr[i];
}
// find average
avg = sum / size;
// return average
}
OUTPUT:
Average: 80.000000
RESULT:
Thus the program implementation using file has been executed successfully
b)Function with Two dimensional array
AIM:
Write a program to implement a function using an two dimensional array
ALGORITHM:
1.Declare the two dimensional array named score[5][3]
2.And assign the values to that array
3. And declare integer variable score
4. Declare the function print Average with function variable int[][3], and int,
PROGRAM:
#include <stdio.h>
void printAverage(int [][3], int, int);
int main(void) {
// variables
int ROWS = 5, COLS = 3;
int score[5][3] = {
{60, 70, 80},
{90, 50, 70},
{80, 75, 75},
{90, 85, 81},
{60, 75, 80}
};
printAverage(score, ROWS, COLS);
return 0;
}
void printAverage(int arr[][3], int rows, int cols) {
// variables
int r, c;
Float sum, avg;
// find average and print it
for (r = 0; r < rows; r++)
{
sum = 0;
for (c = 0; c < cols; c++) {
sum += arr[r][c];
}
avg = sum / cols;
printf("Average on Day #%d = %f\n", (r + 1), avg);
}
}
OUTPUT:
Average on Day #1 = 70.000000
Average on Day #2 = 70.000000
Average on Day #3 = 76.666664
Average on Day #4 = 85.333336
Average on Day #5 = 71.666664
RESULT:
Thus the program implementation using file has been executed successfully
Ex.No: 3
Date: Implement C Program using Pointers and Structure
AIM:
write a program to implement a structure union a pointer variable
ALGORITHM:
1. Create the structure employee with its variable name[30],id age, and character gender[30],and
city[40]
2. And create the structure pointers *p1 and *p2 and its corresponding structure variable emp1 and
emp2
3. Store the address of the emp1 and emp2 structure variable
PROGRAM:
include<stdio.h>
struct Employee
char name[30];
int id;
int age;
char gender[30];
char city[40];
};
int main()
{
ptr1=&emp1;
ptr2=&emp2;
scanf("%s",&ptr1->name);
scanf("%d",&ptr1->id);
scanf("%d",&ptr1->age);
scanf("%s",&ptr1->gender);
scanf("%s",&ptr1->city);
scanf("%s",&ptr2->name);
scanf("%d",&ptr2->id);
scanf("%d",&ptr2->age);
scanf("%s",&ptr2->gender);
scanf("%s", &ptr2->city);
printf("Id:%d\n",ptr1->id);
printf("Age:%d\n",ptr1->age);
printf("Gender:%s\n",ptr1->gender);
printf("City:%s\n",ptr1->city);
printf("Gender:%s\n",ptr2->gender);
printf("City:%s\n",ptr2->city);
return 0;
OUTPUT:
Enter the name of the Employee (emp1): John
Enter the id of the Employee (emp1): 1099
Enter the age of the Employee (emp1): 28
Enter the gender of the Employee (emp1): Male
Enter the city of the Employee (emp1): California
Second Employee:
Enter the name of the Employee (emp2): Maria
Enter the id of the Employee (emp2): 1109
Enter the age of the Employee (emp2): 23
Enter the gender of the Employee (emp2): Female
Enter the city of the Employee (emp2): Los Angeles
Display the Details of the Employee using Structure Pointer
Details of the Employee (emp1)
Name: John
Id: 1099
Age: 28
Gender: Male
City: California
Details of the Employee (emp2) Name: Maria
Id: 1109
Age: 23
Gender: Female
City: Los Angeles
RESULT:
Thus the implementation of pointer structure was executed successfully
Ex.No:4
AIM:
ALGORITHM:
SOURCE CODE :
#include <stdio.h>
#include <stdlib.h>
struct person
{
int id;
char fname[20];
char lname[20];
};
int main ()
{
FILE *infile;
struct person input;
// Open person.dat for reading
infile = fopen ("person.dat", "r");
if (infile == NULL)
{
fprintf(stderr, "\nError opening file\n");
exit (1);
} // read file contents till end of file
while(fread(&input, sizeof(struct person), 1, infile))
printf ("id = %d name = %s %s\n", input.id,
input.fname, input.lname);
// close file
fclose (infile);
return 0;
}
OUTPUT:
d = 1 name = rohan sharma
id = 2 name = mahendra dhoni
RESULT:
Thus the program implementation using file has been executed successfully
Ex.No:5
Date: Implementation of C program in real time
AIM:
Write a c program to implement the real time implementation by creating a text editor
ALGORITHM:
1. Display options new, open and exit and get choice.
2. If choice is 1 , call Create() function.
3. If choice is 2, call Display() function.
4. If choice is 3, call Append() function.
5. If choice is 4, call Delete() function.
6. If choice is 5, call Display() function.
7. Create ()
Get the file name and open it in write mode.
Get the text from the user to write it.
8.Display()
Get the file name from user.
Check whether the file is present or not.
8.2 If present then display the contents of the file.
9. Append()
Get the file name from user.
Check whether the file is present or not.
If present then append the file by getting the text to add with the existing file.
10. Delete()
Get the file name from user.
Check whether the file is present or not.
If present then delete the existing file.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
int i,j,ec,fg,ec2;
char fn[20],e,c;
FILE *fp1,*fp2,*fp;
void Create();
void Append();
void Delete();
void Display();
void main()
{
do {
clrscr();
printf("\n\t\t***** TEXT EDITOR *****");
printf("\n\n\tMENU:\n\t ---- \n ");
printf("\n\t1.CREATE\n\t2.DISPLAY\n\t3.APPEND\n\t4.DELETE\n\t5.EXIT\n");
printf("\n\tEnter your choice: ");
scanf("%d",&ec);
switch(ec)
{
case 1:
Create();
break;
case 2:
Display();
break;
case 3:
Append();
break;
case 4:
Delete();
break;
case 5:
exit(0);
}
}while(1);
}
void Create()
{
fp1=fopen("temp.txt","w");
printf("\n\tEnter the text and press '.' to save\n\n\t");
while(1)
{
c=getchar();fputc(c,fp1);
if(c == '.')
{
fclose(fp1);
printf("\n\tEnter then new filename: ");
scanf("%s",fn);
fp1=fopen("temp.txt","r");
fp2=fopen(fn,"w");
while(!feof(fp1))
{
c=getc(fp1);
putc(c,fp2);
}
fclose(fp2);
break;
}}
}
void Display()
{
printf("\n\tEnter the file name: ");
scanf("%s",fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf("\n\tFile not found!");
goto end1;
}
while(!feof(fp1))
{
c=getc(fp1);
printf("%c",c);
}
end1:
fclose(fp1);
printf("\n\n\tPress any key to continue...");
getch();
}
void Delete()
{
printf("\n\tEnter the file name: ");
scanf("%s",fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf("\n\tFile not found!");
goto end2;
}
fclose(fp1);
if(remove(fn)==0)
{
printf("\n\n\tFile has been deleted successfully!");
goto end2;
}
else
printf("\n\tError!\n");
end2: printf("\n\n\tPress any key to continue...");
getch();
}
void Append()
{
printf("\n\tEnter the file name: ");
scanf("%s",fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf("\n\tFile not found!");
goto end3;
}
while(!feof(fp1))
{
c=getc(fp1);
printf("%c",c);
}
fclose(fp1);
printf("\n\tType the text and press 'Ctrl+S' to append.\n");
fp1=fopen(fn,"a");
while(1)
{
c=getch();
if(c==19)
goto end3;
if(c==13)
{
c='\n';
printf("\n\t");
fputc(c,fp1);
}
else
{
printf("%c",c);
fputc(c,fp1);
}
}
end3: fclose(fp1);
getch();
}
OUTPUT:
FILE CREATION:
DISPLAY:
APPENDAGE:
DISPLAY (AFTER APPENDAGE):
FILE DELETION:
RESULT:
Thus the implementation of text editor was successfully created and executed successfully
Ex.No:6
Date: Array implementation of List ADT
AIM:
Write a program to implement a list ADT c program to create and edit the list by using the structure
list
ALGORITHAM
1. create a file named larray.h
PROGRAM:
“Larray.h” File:
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct list
{
int capacity;
int size;
int *array;
};
typedef struct list *ptrToNode;
typedef ptrToNode LIST;
typedef int POSITION;
int Isempty(LIST L)
{
return L->size==0;
}
void MakeEmpty(LIST L)
{
if(Isempty(L))
printf("\n LIST is already Empty");
else
{
L->size=0;
printf("\n Now List becomes Empty");
}
}
L->capacity=max;
L->array=(int*)malloc(sizeof(int)*max);
if(L->array==NULL)
printf("\n Fatal Error");
else
{
L->size=0;
printf("\n List is Created successfully");
}
}
return L;
}
int Isfull(LIST L)
{
return L->size==L->capacity;
}
L->array[i]=L->array[i+1];
L->size--;
}
}
LIST Deletelist(LIST L)
{
MakeEmpty(L);
free(L);
L=NULL;
return L;
}
void Display(LIST L)
{
int i;
for(i=0;i<L->size;i++)
printf("\n %d",L->array[i]);
}
“Larray.c” File:
#include"Larray.h"
#include<stdlib.h>
void main()
{
LIST L=NULL;
POSITION P;
int a,choice,ch,element;
clrscr();
printf("\n\n1.Create\n2.Insert\n3.Delete\n4.Display\n5.MakeEmpty\n6.Find\n7.IsEmpty\n8.IsFull\n9.
Deletelist\n10.Exit\n");
A:
printf("\n Enter Ur Option:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(L==NULL)
L=Createlist(5);
else
printf("\nList is already created");
break;
case 2:
if(L==NULL)
printf("\nList is not yet created");
else
{
printf("\nEnter the Element to insert:\t");
scanf("%d",&element);
if(L->size==0)
Insert(element,L,0);
else
{
printf("\n where u want to insert?\t1:Front\t2:Back\t3:middle\t::: ");
scanf("%d",&ch);
if(ch==1)
Insert(element,L,0);
else
if(ch==2)
Insert(element,L,L->size);
else
if(ch==3)
{
printf("\nWhere you want to insert:\t");
scanf("%d",&a);
P=Find(a,L);
if(P<L->size)
Insert(element,L,P);
else
printf("\nElement is not in the list");
}
else
printf("\n Ur choice is not available");
}
}
break;
case 3:
if(L==NULL)
printf("\nList is not yet created");
if(Isempty(L))
printf("\nList is empty");
else
{
printf("\nEnter the element to delete:\t");
scanf("%d",&a);
Delete(a,L);
}
break;
case 4:
if(L==NULL)
printf("\nList is not yet created");
else
if(Isempty(L))
printf("\nList is empty");
else
{
printf("\nElements present in the list are:");
Display(L);
}
break;
case 5:
if(L==NULL)
printf("\n List is not yet created ");
else
MakeEmpty(L);
break;
case 6:
if(L==NULL)
printf("\n Not yet created");
else
if(Isempty(L))
printf("\n List is empty");
else
{
printf("\n which element is to find:\t");
scanf("%d",&a);
P=Find(a,L);
printf("\n Element is at %d\t[0 to 4 means present]\t[5 means not present]",P);
}
break;
case 7:
if(L==NULL)
printf("\n Not yet created");
else
if(Isempty(L))
printf("\n List is empty");
else
printf("\n List is not empty");
break;
case 8:
if(L==NULL)
printf("\n Not yet created");
else
if(Isfull(L))
printf("\n List is FULL");
else
printf("\n List is not FULL");
break;
case 9:
if(L==NULL)
printf("\n Not yet created");
else
{
L=Deletelist(L);
printf("\n List is Deleted");
}
break;
case 10:
exit (0);
break;
default:
printf("\n\n *******WRONG ENTRY*******");
break;
}
goto A;
}
OUTPUT:
1.Create
2.Insert
3.Delete
4. Display
5. MakeEmpty
6.Find
7.IsEmpty
8.IsFull
9.Deletelist
10.Exit
Enter Ur Option: 1
List is created successfully
Enter Ur Option: 2
Enter the element to insert: 300
Enter Ur Option: 2
Enter the element to insert: 100
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 1
Enter Ur Option: 2
Enter the element to insert: 200
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 3
Enter Ur Option: 2
Enter the element to insert: 400
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 2
Enter Ur Option: 2
Enter the element to insert: 500
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 2
Enter Ur Option: 2
Enter the element to insert: 600
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 1
List is Full
Enter Ur Option: 4
Elements present in the list are
100
200
300
400
500
Enter Ur Option: 7
List is not empty
Enter Ur Option: 6
Which element is to find: 500
Element at 4 [0 to 4 – present] [5 – not present]
Enter Ur Option: 3
Enter the element to delete: 300
Enter Ur Option: 4
Elements present in the list are:
100
200
400
500
Enter Ur Option: 8
List is not Full
Enter Ur Option: 5
Now List becomes Empty
Enter Ur Option: 9
List is Deleted
Enter Ur Option: 2
List is not yet created
Enter Ur Option: 12
*******WRONG ENTRY*******
RESULT:
Thus the program implementation using file has been executed successfully
Ex.No:7(a)
Date: Array implementation of stack
Aim:
3. write a push() function this is used to insert the value to the stack
Source Code:
#include<stdio.h>
#include<conio.h>
#define max 5
int st[max],top=-1;
void push()
{
int x;
if(top==max-1)
{
printf("stack is full");
return;
}
printf("enter element");
scanf("%d",&x);
top++;
st[top]=x;
}
void pop()
{
int x;
if(top==-1)
{
printf("stack is empty");
return;
}
printf("enter element");
scanf("%d",&x);
top--;
printf("enter deleted element=%d",x);
}
void display()
{
int i;
if(top==-1)
{
printf("stack is empty");
return;}
for(i=top;i>=0;i--)
{
printf("%d",st[i]);
}}
int main()
{
int ch;
while(1)
{
printf("enter \n1.push\n2.pop\n3.display\n4.exit");
scanf("%d",&ch);
switch(ch)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4:exit(1);break;
}}
return 1;
Output:
RESULT:
Thus the stack operation of push and pop operation was performed successfully
b) Array implementation using queue
Aim:
Write a program that implement Queue (its operations) using Arrays
ALGORITHM:
1.First create a queue with size
2.And then mention the front and rear
3. If the front reaches the firs position in the queue the rear will be added by 1
4. If the rear reaches the last position of the queue it indicates thus the queue reached the last position
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define SIZE 5
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}
void main()
{
int value, choice;
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
Output:
RESULT:
Thus the queue operations are performed successfully
Ex.No:8(a)
Date: Linked list implementation of list stack
AIM:
Write a program to implement the stack operation using the liked list
ALGORITHM:
1. Create a structure named node
3. These functions are all performed in a while condition to insert or delete or display our stack
operation
4. If the stack pointer reaches the top of the stack value thus it indicates thus the stack is full\
5.When we remove the one value from the stack the stack pointer value is decremented by one
6.Whenwe enter the one element in a stack the stack pointer has been incremented by one
7.If the stack pointer reaches the top it tells us stack is full we cannot add the value to the stack
8.If it reaches the bottom of the stack it indicates thus the stack is empty
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while(1)
{
printf("\n Enter choice : ");
scanf("%d",&ch);
switch(ch)
{
case1:
printf("Enter data : ");
scanf("%d",&no);
push(no);
break;
case2:
pop();
break;
case3:
if(top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case4:
empty();
break;
case5:
exit(0);
case6:
display();
break;
case7:
stack_count();
break;
case8:
destroy();
break;
default:
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
if(top1 == NULL)
{
printf("Stack is empty");
return;
}
while(top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
if(top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
while(top1 != NULL)
{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;
OUTPUT:
1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Dipslay
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56
Enter choice : 1
Enter data : 80
Enter choice : 2
Popped value : 80
Enter choice : 3
Top element : 56
Enter choice : 1
Enter data : 78
Enter choice : 1
Enter data : 90
Enter choice : 6
907856
Enter choice : 7
Stack is empty
Enter choice : 5
RESULT:
Thus the program for stack using linked list has been implemented successfully.
b) Program using queue:
AIM:
Write a program to implement a linked list operation by using a queue
ALGORITHM:
1. Create a structure node with structure pointer front and back
2. And create a queue to insert and delete the queue with the queue size
3. If the front of the queue is null and thus the queue is ready to intake the input
4.And the back of the queue is full it indicates thus the queue if full
5.Thus the front and back of the queue is indicates thus the stack pointer
6.If it moves front means thus the stack pointer is incremented by one
7.If it moves back means in the queue thus the stack pointer is decremented by one
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *front, *back;
/* Create an empty queue */
void initialize() {
front = back = NULL;
}
/* Returns queue size */
int getQueueSize() {
struct node *temp = front;
int count = 0;
if(front == NULL && back == NULL)
return 0;
while(temp != back){
count++;
temp = temp->next;
}
if(temp == back)
count++;
return count;
}
/* Returns Frnt Element of the Queue */
int getFrontElement() {
return front->data;
}
/* Returns the Rear Element of the Queue */
int getBackElement() {
return back->data;
}
/*Check's if Queue is empty or not */
void isEmpty() {
if (front == NULL && back == NULL)
printf("Empty Queue\n");
else
printf("Queue is not Empty\n");
}
/*Adding elements in Queue*/
void enqueue(int num) {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
temp->next = NULL;
if (back == NULL)
{
front = back = temp;
} else {
back->next = temp;
back = temp;
}
}
/*Removes an element from front of the queue*/
void dequeue() {
struct node *temp;
if (front == NULL) {
printf("\nQueue is Empty \n");
return;
} else {
temp = front;
front = front->next;
if(front == NULL){
back = NULL;
}
printf("Removed Element : %d\n", temp->data);
free(temp);
}
}
/*Print's Queue*/
void printQueue() {
struct node *temp = front;
if ((front == NULL) && (back == NULL)) {
printf("Queue is Empty\n");
return;
}
while (temp != NULL) {
printf("%d", temp->data);
temp = temp->next;
if(temp != NULL)
printf("-->");
}
}
int main() {
/* Initializing Queue */
initialize();
/* Adding elements in Queue */
enqueue(1);
enqueue(3);
enqueue(7);
enqueue(5);
enqueue(10);
/* Printing Queue */
printQueue();
/* Printing size of Queue */
printf("\nSize of Queue : %d\n", getQueueSize());
/* Printing front and rear element of Queue */
printf("Front Element : %d\n", getFrontElement());
printf("Rear Element : %d\n", getBackElement());
OUTPUT:
1-->3-->7-->5-->10
Size of Queue : 5
Front Element : 1
Rear Element : 10
Removed Element : 1
Removed Element : 3
Removed Element : 7
Removed Element : 5
Removed Element : 10
Queue is Empty
RESULT:
Thus the program to implement the queue using linked list has been implemented sucessfully
Ex.No:9
AIM:
Write a program to implement a infix to postfix using the stack operation
ALGORITHM:
1. Start the program
2. create the stack size of 100
3. An create the push and pop operation to insert and remove the element from stack
4.In the main program create the character variable size of 100
5. Create the pointer variable for that
6. Based on that we can call the function puh and pop
7.Execute that in a while statement
8.Print that case statement output
9.Stop the pogram
PROGRAM:
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top =-1;
voidpush(char x)
{
stack[++top]= x;
}
charpop()
{
if(top ==-1)
return-1;
else
return stack[top--];
}
intpriority(char x)
{
if(x =='(')
return0;
if(x =='+'|| x =='-')
return1;
if(x =='*'|| x =='/')
return2;
return0;
}
intmain()
{
char exp[100];
char*e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
while(*e !='\0')
{
if(isalnum(*e))
printf("%c ",*e);
elseif(*e =='(')
push(*e);
elseif(*e ==')')
{
while((x =pop())!='(')
printf("%c ", x);
}
else
{
while(priority(stack[top])>=priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}
while(top !=-1)
{
printf("%c ",pop());
}return0;
}
OUTPUT:
a bc*+
Output Test Case 2:
Enter the expression : (a+b)*c+(d-a)
ab+c*da -+
Output Test Case 3:
Enter the expression : ((4+8)(6-5))/((3-2)(2+2))
48+65-32-22+/
RESULT:
Thus the implementation of infix to postfix using stack operation was performed successfully
ii)Queue application:
AIM:
Write a program to implement a queue application to display the queue items
ALGORITHM:
1. Start the program
2. Create the function enqueue(), and dequeue(), and show() f
3.Based on the enqueuer() we can add the element in the queue
4.In the dequeuer() is used to revoke the elment in the queue
5.The show() is used to display the elements available in the queue
6.And then the final exit function is used to terminate the queue items
PROGRAM:
#include <stdio.h>
# define SIZE 100
void enqueue();
void dequeue();
void show();
int inp_arr[SIZE];
int Rear = - 1;
int Front = - 1;
main()
{
int ch;
while (1)
{
printf("1.Enqueue Operation\n");
printf("2.Dequeue Operation\n");
printf("3.Display the Queue\n");
printf("4.Exit\n");
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("Incorrect choice \n");
}
}
}
void enqueue()
{
int insert_item;
if (Rear == SIZE - 1)
printf("Overflow \n");
else
{
if (Front == - 1)
Front = 0;
printf("Element to be inserted in the Queue\n : ");
scanf("%d", &insert_item);
Rear = Rear + 1;
inp_arr[Rear] = insert_item;
}
}
void dequeue()
{
if (Front == - 1 || Front > Rear)
{
printf("Underflow \n");
return ;
}
else
{
printf("Element deleted from the Queue: %d\n", inp_arr[Front]);
Front = Front + 1;
}
}
void show()
{
if (Front == - 1)
printf("Empty Queue \n");
else
{
printf("Queue: \n");
for (int i = Front; i <= Rear; i++)
printf("%d ", inp_arr[i]);
printf("\n");
}
}
OUTPUT:
1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 1
Element to be inserted in the Queue: 10
1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 1
Element to be inserted in the Queue: 20
1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 3
Queue:
10 20
1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 2
Element deleted from the Queue: 10
1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations: 3
Queue:
20
RESULT:
Thus the application of the queue operation has been implemented successfully
Ex.No:
AIM:
Write a program to implement the binary tree with its operations
ALGORITHM:
1: Start the Program
2: Declare a Template for Key and Elements
3: Insert the Key-Element Pair into the Tree, overwriting Existing pair, if any, with same key
4: Find place to Insert. While P != NULL
5: Examine Element pointed to by Pair. Move P to a Child
6: If Pair First Element is Less Than Element of first P, assign P as Left Child.
7: If Pair First Element is Greater than Element of first P, assign P as Right Child
8: Else, Replace Old Value
9: Retrieve a Node for the Pair and assign to Pair Pointer Element
10: If Root != NULL, the tree is not Empty.
11: Now, Handle Input Operation – Insert, Delete, ListOrder Accordingly
12: If Pair First Element is Less Than Pair Pointer First Element; LeftChildNode = NewNode
13: Else, Pair Pointer First Element RightChildNode = NewNode. Else, Root = NewNode
14: Insert Operation Into Tree Successful.
15: Stop
PROGRAM:
# include <stdio.h>
# include <malloc.h>
struct node
{
int info;
struct node *lchild;
struct node *rchild;
}*root;
void find(int item,struct node **par,struct node **loc)
{
struct node *ptr,*ptrsave;
if(root==NULL) /*tree empty*/
{
*loc=NULL;
*par=NULL;
return;
}
if(item==root->info) /*item is at root*/
{
*loc=root;
*par=NULL;
return;
}
/*Initialize ptr and ptrsave*/
if(item<root->info)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;
while(ptr!=NULL)
{
if(item==ptr->info)
{ *loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}/*End of while */
*loc=NULL; /*item not found*/
*par=ptrsave;
}/*End of find()*/
void insert(int item)
{ struct node *tmp,*parent,*location;
find(item,&parent,&location);
if(location!=NULL)
{
printf("Item already present");
return;
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=item;
tmp->lchild=NULL;
tmp->rchild=NULL;
if(parent==NULL)
root=tmp;
else
if(item<parent->info)
parent->lchild=tmp;
else
parent->rchild=tmp;
}/*End of insert()*/
void case_a(struct node *par,struct node *loc )
{
if(par==NULL) /*item to be deleted is root node*/
root=NULL;
else
if(loc==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL;
}/*End of case_a()*/
void case_b(struct node *par,struct node *loc)
{
struct node *child;
/*Initialize child*/
if(loc->lchild!=NULL) /*item to be deleted has lchild */
child=loc->lchild;
else /*item to be deleted has rchild */
child=loc->rchild;
if(par==NULL ) /*Item to be deleted is root node*/
root=child;
else
if( loc==par->lchild) /*item is lchild of its parent*/
par->lchild=child;
return 0;
}
if(ptr!=NULL)
{
printf("%d ",ptr->info);
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}/*End of preorder()*/
void inorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/
void postorder(struct node *ptr)
if(location==NULL)
{
printf("Item not present in tree");
return 0;
}
if(location->lchild==NULL && location->rchild==NULL)
case_a(parent,location);
if(location->lchild!=NULL && location->rchild==NULL)
case_b(parent,location);
if(location->lchild==NULL && location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL && location->rchild!=NULL)
case_c(parent,location);
free(location);
}/*End of del()*/
int preorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return 0;
}
if(ptr!=NULL)
{
printf("%d ",ptr->info);
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}/*End of preorder()*/
void inorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/
void postorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
printf("%d ",ptr->info);
}
}/*End of postorder()*/
void display(struct node *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf("");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}/*End of if*/
}/*End of display()*/
main()
{
int choice,num;
root=NULL;
while(1)
{
printf("\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Inorder Traversal\n");
printf("4.Preorder Traversal\n");
printf("5.Postorder Traversal\n");
printf("6.Display\n");
printf("7.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);
insert(num);
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
display(root,1);
break;
case 7:
break;
default:
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main()*
OUTPUT:
RESULT:
Thus the C program for Implementation of Implement Binary-Tree Algorithm was written, executed andthe
output was verified successful
Ex.No.11 Implementation of binary search tree
Date:
AIM:
Write a program to implement to a binary search tree
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
typedef struct BST
{
int data;
struct BST *left;
struct BST *right;
}node;
node *create();
void insert(node *,node *);
void preorder(node *);
int main()
{
char ch;
node *root=NULL,*temp;
do
{
temp=create();
if(root==NULL)
root=temp;
else
insert(root,temp);
printf("nDo you want to enter more(y/n)?");
getchar();
scanf("%c",&ch);
}while(ch=='y'|ch=='Y');
printf("nPreorder Traversal: ");
preorder(root);
return 0;
}
node *create()
{
node *temp;
printf("nEnter data:");
temp=(node*)malloc(sizeof(node));
scanf("%d",&temp->data);
temp->left=temp->right=NULL;
return temp;
}
RESULT:
Thus the c program for binary search tree has been implemented successfully
EX.NO.12 Implementation of searching techniques
Date:
AIM:
Write a program to implement searching techniques in a tree
ALGORITHM:
4. mid holds the middle value calculated by adding high and low and dividing it by 2.
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}
OUTPUT:
PROGRAM:
#include <stdio.h>
int binaryScr(int a[], int low, int high, int m)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if (a[mid] == m)
return mid;
if(a[mid] > m)
return binaryScr(a, low, mid - 1, m);
return binaryScr(a, mid + 1, high, m);
}
return -1;
}
int main(void)
{
int a[] = { 12, 13, 21, 36, 40 };
int i,m;
for(i=0;i<5;i++)
{
printf(" %d",a[i]);
}
printf(" n");
int n = sizeof(a) / sizeof(a[0]);
printf("Enter the number to be searchedn");
scanf("%d", &m);
int result = binaryScr(a, 0, n - 1, m);
(result == -1) ? printf("The element is not present in array")
printf("The element is present at index %d",
result);
return 0;
}
OUTPUT:
RESULT:
Thus the tree search program was executed successfully
EX.NO.13 Implementation of sorting algorithm
i) Insertion sort:
ALGORITHM:
Step 1: 89 17 8 12 0 (the bold elements are sorted list and non-bold unsorted list)
Step 2: 17 89 8 12 0 (each element will be removed from unsorted list and placed at the right
position in the sorted list)
Step 3: 8 17 89 12 0
Step 4: 8 12 17 89 0
Step 5: 0 8 12 17 89
PROGRAM:
#include<stdio.h>
int main(){
return0;
}
OUTPUT:
Algorithm:
1. If n < = 1, then return.
2. Pick any element V in a[]. This is called the pivot.
3. Rearrange elements of the array by moving all elements xi > V right of V and all elements xi < = V
left of V. If the place of the V after re-arrangement is j, all elements with value less than V, appear in
a[0], a[1] . . . . a[j – 1] and all those with value greater than V appear in a[j + 1]......... a[n – 1].
4. Apply quick sort recursively to a[0] . . . . a[j – 1] and to a[j + 1].........a[n – 1].
PROGRAM:
#include <stdio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
int a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("\nArray after sorting:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
RESULT:
program:
#include<stdio.h>
#define max 10
int a[11]={10,14,19,26,27,31,33,35,42,44,0};
int b[10];
for(l1 = low, l2 = mid +1, i = low; l1 <= mid && l2 <= high; i++)
{
if(a[l1]<= a[l2])
b[i]= a[l1++];
else
b[i]= a[l2++];
}
while(l1 <= mid)b[i++]=
a[l1++];
while(l2 <= high)b[i++]= a[l2++];
int main(){
int i;
printf("List before sorting\n");
for(i =0; i <= max; i++)printf("%d ", a[i]);
sort(0, max);
printf("\nList after sorting\n");
for(i =0; i <= max; i++)printf("%d ", a[i]);
}
OUTPUT:
RESULT:
Thus the merge sort algorithm of c program has been executed successfully
EX.NO.14 Implementation of hashing techniques
Date:
AIM:
Write a program to implement of hashing techniques
ALGORITHM:
1. Create an array of structure, data (i.e a hash table).
2. Take a key to be stored in hash table as input.
3. Corresponding to the key, an index will be generated.
4. In case of absence of data in that index of array, create one and insert the data item (key and value)
into it and increment the size of hash table.
5. In case the data already exists, the new data cannot be inserted if the already present data does not
match given key.
6. To display all the elements of hash table, data at each index is extracted and elements (key and
value) are read and printed.
7. To remove a key from hash table, we will first calculate its index and extract its data, delete the key
in case it matches the given key.
8. Exit
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct data
{
int key;
int value;
};
case1:
break;
case2:
break;
case3:
n = size_of_hashtable();
printf("Size of Hash Table is-:%d\n", n);
break;
case4:
display();
break;
default:
printf("Wrong Input\n");
}while(c ==1);
getch();
}
OUTPUT:
RESULT:
Thus the c program for hash table has been executed successfully