Data Science Lab Master Record
Data Science Lab Master Record
MASTER RECORD
FACULTY INCHARGES
Mrs.S.Sangeetha,
Mr.B.Guruprakash,
Dr.Yesubai Rubavathi,
Mrs.M.Mathinakani,
Dr.V.Vivek,
Mrs.S.Meenakshi.
SETHU INSTITUTE OF TECHNOLOGY
(AN AUTONOMOUS INSTITUTION)
INSTITUTE VISION
To promote excellence in technical education and scientific research for the benefit of the
society
INSTITUTE MISSION
To provide quality technical education to fulfill the aspiration of the student and to meet
the needs of the Industry
To provide holistic learning ambience
To impart skills leading to employability and entrepreneurship
To establish effective linkage with industries
To promote Research and Development activities
To offer services for the development of society through education and technology
CORE VALUES
Quality
Commitment
Innovation
Team work
Courtesy
QUALITY POLICY
To provide Quality technical education to the students.
To produce competent professionals and contributing citizens.
To contribute for the upliftment of the society.
DEPARTMENT VISION
To produce globally innovative computer technocrats and promote research to contribute
to the society
DEPARTMENT MISSION
Transforming the students into Technocrats in Computer Technology.
Cultivating the skills essential for employability and higher education.
Offering computer educational programs to fulfill the needs of the society.
Establishing strong relationship with the industries.
Promoting research activities among the students and the faculty.
Encouraging students to do projects with innovative ideas to solve real world problems.
PROGRAMME OUTCOMES
OBJECTIVES:
To familiarize the implementation of programs in R and Hadoop.
LIST OF EXPERIMENTS
Programs in R
Programs in Hadoop
5.Hadoop Distributed File System Commands
6.Hadoop Word Count Implementation using MapReduce
T OTAL: 30 Periods
COURSE OUTCOME ASSESSMENT METHODS : MODEL EXAM, OBSERVATION ,VIVA
& RECORD
COURSE OUTCOMES
After the successful completion of this course, the student will be able to
Work With Data Analytic Methods. (Apply)
Implement Clustering Analysis In R. (Apply)
Manipulate Naïve Classification Analysis Using R. (Apply)
Work With Hadoop Commands. (Apply)
Implement Map Reduce Techniques. (Apply)
LIST OF EXPERIMENTS
4. Preparing and training data based on Naïve Bayes Classification analysis using R
Problem Statement:
The Iris data set is a dataset describing key characteristics of Iris flowers. Fisher’s Iris data
base is perhaps the best known database to be found in the pattern recognition literature.
The data set contains 3 classes of 50 instances each, where each class refers to a type of
iris plant. One class is linearly separable from the other two; the latter are not linearly
separable from each other. Analyze the Iris dataset, using Naïve Bayes classification
Techniques.
The data base contains the following attributes:
1). sepal length in cm
2). sepal width in cm
3). petal length in cm
4). petal width in cm
5). class:
- Iris Setosa
- Iris Versicolou
Programs in Hadoop
5. Hadoop Distributed File System Commands
6. Hadoop Word Count Implementation using MapReduce
7. Implementation of Matrix Multiplication using MapReduce
.
SETHU INSTITUTE OF TECHNOLOGY
(AN AUTONOMOUS INSTITUTION)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEEERING
Aim:
To study about various R Commands and their purpose.
Description:
Result:
Thus the various commands in R has been executed successfully.
Ex.No.1b Basic Data Analytic Methods using R
Aim:
To analyze the diamond dataset using Exploratory and Descriptive Statistics Methods.
Problem Statement:
The diamond dataset contains the prices and other attributes of almost 54,000 diamonds and is
included in the ggplot2 package. Use the methods of descriptive statistics and exploratory
analysis, to visualize and analyze the data.
Content
price price in US dollars (\$326--\$18,823)
carat weight of the diamond (0.2--5.01)
cut quality of the cut (Fair, Good, Very Good, Premium, Ideal)
color diamond colour, from J (worst) to D (best)
clarity a measurement of how clear the diamond is (I1 (worst), SI2, SI1, VS2, VS1,
VVS2,VVS1, IF (best))
x length in mm (0--10.74) ,y width in mm (0--58.9)
z depth in mm (0--31.8) ,depth total depth percentage = z / mean(x, y) = 2 * z / (x + y) (43--79) ,
table width of top of diamond relative to widest point (43--95)
Algorithm:
Code
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.1.3
data(diamonds)
View(diamonds)
nrow(diamonds)
ncol(diamonds)
levels(diamonds$color)
subset(diamonds, price == max(price))
subset(diamonds, price == min(price))
summary(diamonds$price)
mean(diamonds$price)
median(diamonds$price)
summary(diamonds)
ggplot(diamonds,aes(x=carat,y=price))+ geom_point(color='blue',fill='blue')+
xlim(0,quantile(diamonds$carat,0.99))+ylim(0,quantile(diamonds$price,0.99))+
ggtitle('Diamond price vs. carat')
Output
## [1] 53940
## [1] 10
mean(diamonds$price)
## [1] 3932.8
median(diamonds$price)
## [1] 2401
Result:
Aim:
To analyze the student performance dataset to perform k – means clustering analysis.
Problem Statement:
The Student Performance dataset describing the task is to group 620 high school seniors based
on their grades in three subject areas: English, mathematics, and science. The grades are
averaged over their high school career and assume values from 0 to 100. Analyze the student
data set, using R to perform K-means clustering.
Algorithm:
Code
R code establishes the necessary R libraries and imports the CSV file containing the
grades.
library(plyr)
library(ggplot2)
library(cluster)
library(lattice)
library(graphics)
library(grid)
library(gridExtra)
grade_input = as.data.frame(read.csv(“c:/data/grades_km_input.csv”))
R code formats the grades for processing. The data file contains four columns. The first
column holds a student identification (ID) number, and the other three columns are for
the grades in the three subject areas. Because the student ID is not used in the
clustering analysis, it is excluded from the k-means input matrix, kmdata.
kmdata_orig = as.matrix(grade_input[,c(“Student”,“English”,“Math”,“Science”)])
kmdata <- kmdata_orig[,2:4]
kmdata[1:10,]
Output
R code loops through several k-means analyses for the number of centroids,k, varying
from 1 to 15. For each k, the option nstart=25 specifies that the k-means algorithm will
be repeated 25 times, each starting with k random initial centroids. The corresponding
value of WSS for each k-mean analysis is stored in the wss vector.
km = kmeans(kmdata,3, nstart=25)
km
c( wss[3] , sum(km$withinss) )
[1] 64483.06 64483.06
Using the basic R plot function, each WSS is plotted against the respective
number of centroids, 1 through 15.
In determining the value of k, the data scientist should visualize the data and assigned
clusters. In the following code, the ggplot2 package is used to visualize the identified
student clusters and centroids.
df = as.data.frame(kmdata_orig[,2:4])
df$cluster = factor(km$cluster)
centers=as.data.frame(km$centers)
g1= ggplot(data=df, aes(x=English, y=Math, color=cluster )) +
geom_point() + theme(legend.position=“right”) +
geom_point(data=centers,
aes(x=English,y=Math, color=as.factor(c(1,2,3))),
size=10, alpha=.3, show_guide=FALSE)
g2 =ggplot(data=df, aes(x=English, y=Science, color=cluster )) +
geom_point() +
geom_point(data=centers,
aes(x=English,y=Science, color=as.factor(c(1,2,3))),
size=10, alpha=.3, show_guide=FALSE)
g3 = ggplot(data=df, aes(x=Math, y=Science, color=cluster )) +
geom_point() +
geom_point(data=centers,
aes(x=Math,y=Science, color=as.factor(c(1,2,3))),
size=10, alpha=.3, show_guide=FALSE)
tmp = ggplot_gtable(ggplot_build(g1))
grid.arrange(arrangeGrob(g1 + theme(legend.position=“none”),
g2 + theme(legend.position=“none”),
g3 + theme(legend.position=“none”),
main =“High School Student Cluster Analysis”,
ncol=1))
> View(Marks)
> library(readxl)
Warning message:
package ‘readxl’ was built under R version 3.3.3
> Marks1 <- read_excel("C:/Users/Admin/Desktop/Marks1.xlsx")
> View(Marks1)
> data(Marks1)
Warning message:
In data(Marks1) : data set ‘Marks1’ not found
> str(Marks1)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 12 obs. of 5 variables:
$ S.No : num 1 2 3 4 5 6 7 8 9 10 ...
$ Name : chr "Kapoor" "Lav" "Foo" "Xin" ...
$ English: num 56 34 90 100 89 90 76 56 45 78 ...
$ Maths : num 74 45 78 90 81 73 63 78 99 100 ...
$ Science: num 87 87 90 78 46 90 45 100 56 98 ...
> head(Marks1)
# A tibble: 6 x 5
S.No Name English Maths Science
<dbl> <chr> <dbl> <dbl> <dbl>
1 1 Kapoor 56 74 87
2 2 Lav 34 45 87
3 3 Foo 90 78 90
4 4 Xin 100 90 78
5 5 Eddv 89 81 46
6 6 Faran 90 73 90
> summary(Marks1)
S.No Name English Maths
Min. : 1.00 Length:12 Min. : 34.00 Min. : 45.00
1st Qu.: 3.75 Class :character 1st Qu.: 56.00 1st Qu.: 64.75
Median : 6.50 Mode :character Median : 78.00 Median : 76.00
Mean : 6.50 Mean : 73.25 Mean : 75.83
3rd Qu.: 9.25 3rd Qu.: 89.25 3rd Qu.: 83.25
Max. :12.00 Max. :100.00 Max. :100.00
Science
Min. : 45.00
1st Qu.: 71.00
Median : 82.50
Mean : 77.58
3rd Qu.: 90.00
Max. :100.00
> plot(Mark1)
Error in plot(Mark1) : object 'Mark1' not found
> plot(Marks1)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In data.matrix(x) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
> abc=Marks1
> abc1=abc
> View(abc1)
> abc1$Name<-NULL
> View(abc1)
> x=kmeans(abc1,5)
> table(abc$Name,x$cluster)
1 2 3 4 5
Ahi 1 0 0 0 0
Arun 0 0 0 0 1
Arvind 0 0 0 1 0
Duk 1 0 0 0 0
Eddv 0 0 1 0 0
Faran 1 0 0 0 0
Foo 1 0 0 0 0
Kapoor 0 0 0 0 1
Lav 0 0 0 0 1
Mashoo 0 0 1 0 0
Matew 0 1 0 0 0
Xin 1 0 0 0 0
> plot(abc[c("English","Maths")],col=x$cluster)
> plot(abc[c("English","Maths","Science")],col=x$cluster)
> plot(abc[c("S.No","English","Maths","Science")],col=x$cluster)
> abc1
# A tibble: 12 x 4
S.No English Maths Science
<dbl> <dbl> <dbl> <dbl>
1 1 56 74 87
2 2 34 45 87
3 3 90 78 90
4 4 100 90 78
5 5 89 81 46
6 6 90 73 90
7 7 76 63 45
8 8 56 78 100
9 9 45 99 56
10 10 78 100 98
11 11 78 64 78
12 12 87 65 76
> x
K-means clustering with 5 clusters of sizes 5, 1, 2, 1, 3
Cluster means:
S.No English Maths Science
1 7.200000 89.00000 74.00000 82.40000
2 10.000000 78.00000 100.00000 98.00000
3 6.000000 82.50000 72.00000 45.50000
4 9.000000 45.00000 99.00000 56.00000
5 3.666667 48.66667 65.66667 91.33333
Clustering vector:
[1] 5 5 1 1 3 1 3 5 4 2 1 1
Available components:
>
Result:
Aim:
To analyze the iris dataset and construct a decision tree using classification techniques.
Problem Statement:
The Iris data set is a dataset describing key characteristics of Iris flowers. Fisher’s Iris
data base is perhaps the best known database to be found in the pattern recognition
literature. The data set contains 3 classes of 50 instances each, where each class
refers to a type of iris plant. One class is linearly separable from the other two; the latter
are not linearly separable from each other. Analyze the Iris dataset, construct a decision
tree using classification techniques
.The data base contains the following attributes:
1). sepal length in cm
2). sepal width in cm
3). petal length in cm
4). petal width in cm
5). class:
- Iris Setosa
- Iris Versicolou
Algorithm:
Code
> install.packages("rpart")
> install.packages("rpart.plot")
> require("rpart")
> require("rpart.plot")
> data(iris)
> str(iris)
> table(iris$Species)
> head(iris)
> set.seed(9850)
> q<-runif(nrow(iris))
> irisr<-iris[order(q),]
> str(irisr)
> m3<-rpart(Species~.,data=irisr[1:100,],method="class")
> m3
> rpart.plot(m3)
> rpart.plot(m3,type=3,extra=101,fallen.leaves = T)
> summary(m3)
> p3<-predict(m3,irisr[101:150,],type="class")
> table(irisr[101:150,5],predicted=p3)
Output
n= 100
Warning message:
Bad 'data' field in model 'call'.
To silence this warning:
Call rpart.plot with roundint=FALSE,
or rebuild the rpart model with model=TRUE.
Warning message:
Bad 'data' field in model 'call'.
To silence this warning:
Call rpart.plot with roundint=FALSE,
or rebuild the rpart model with model=TRUE.
Call:
rpart(formula = Species ~ ., data = irisr[1:100, ], method = "class")
n= 100
Variable importance
Petal.Width Petal.Length Sepal.Length Sepal.Width
34 31 21 14
predicted
setosa versicolor virginica
setosa 16 0 0
versicolor 0 13 2
virginica 0 2 17
Result:
Thus the decision tree using classification techniques in R has been executed successfully.
Problem Statement:
The bank marketing dataset includes 2,000 instances randomly drawn from the original dataset,
and each instance corresponds to a customer. To make the example simple, the subset only
keeps the following categorical variables: (1) job, (2) marital status, (3) education level, (4) if the
credit is in default, (5) if there is a housing loan, (6) if the customer currently has a personal
loan, (7) contact type, (8) result of the previous marketing campaign contact (poutcome), and
finally (9) if the client actually subscribed to the term deposit. Attributes (1) through (8) are input
variables, and (9) is considered the outcome. The outcome subscribed is either yes (meaning
the customer will subscribe to the term deposit) or no (meaning the customer won’t subscribe).
All the variables listed earlier are categorical. Analyze the bank marketing dataset, using Naïve
Bayes classification Techniques
Algorithm:
Code
In R, first set up the working directory and initialize the packages.
setwd(“c:/”)
install.packages(“e1071”) # install package e1071
library(e1071) # load the library
library(ROCR)
# training set
banktrain <- read.table(“bank-sample.csv”,header=TRUE,sep=”,”)
# drop a few columns
drops <- c(“balance”, “day”, “campaign”, “pdays”, “previous”, “month”)
banktrain <- banktrain [,!(names(banktrain) %in% drops)]
# testing set
banktest <- read.table(“bank-sample-test.csv”,header=TRUE,sep=”,”)
banktest <- banktest [,!(names(banktest) %in% drops)]
# build the naïve Bayes classifier
nb_model <- naiveBayes(subscribed˜.,
data=banktrain)
# perform on the testing set
nb_prediction <- predict(nb_model,
# remove column “subscribed”
banktest[,-ncol(banktest)],
type=‘raw’)
score <- nb_prediction[, c(“yes”)]
actual_class <- banktest$subscribed == ‘yes’
pred <- prediction(score, actual_class)
perf <- performance(pred, “tpr”, “fpr”)
plot(perf, lwd=2, xlab=“False Positive Rate (FPR)”,
ylab=“True Positive Rate (TPR)”)
abline(a=0, b=1, col=“gray50”, lty=3)
The following R code shows that the corresponding AUC score of the ROC curve is
about
0.915.
auc <- performance(pred, “auc”)
auc <- unlist(slot(auc, “y.values”))
auc
[1] 0.9152196
Result:
Thus the naïve Bayes classification techniques in R has been executed successfully
Aim:
To study about various Hadoop distributed File System Commands .
Algorithm:
1. Append single src, or multiple srcs from local file system to the destination file system
using appendToFile
2. Copies source paths to STDOUT USING CAT
3. Changes the group association of files using chgrp
4. Changes the permissions of files using chmod
5. Changes the owner of files with chown
6. Copy single src, or multiple srcs from local file system to the destination file system
where the source is restricted to a local file reference using copyFromLocal
7. Copy files to the local file system where the destination is restricted to a local file
reference using copyToLocal
8. Count the number of directories, files and bytes under the paths that match the specified
file pattern with count
9. Copy files from source to destination with cp command
10. Displays sizes of files and directories contained in the given directory or the length of a
file in case its just a file using du command
11. Displays a summary of file lengths using dus
12. Empty the Trash with expunge
13. Copy files to the local file system with get
14. Displays the Access Control Lists (ACLs) of files and directories using getfacl
15. Displays the extended attribute names and values (if any) for a file or directory with
getfattr
16. Takes a source directory and a destination file as input and concatenates files in src into
the destination local file with getmerge
17. returns list of its direct children as in Unix using ls
18. Recursive version of ls with lsr
19. Takes path uri's as argument and creates directories using mkdir
20. Move file from local using moveFromLocal
21. Displays a "Not implemented yet" message using moveToLocal
22. Moves files from source to destination using mv
Commands
Result:
Aim:
To write a Hadoop word count program to count the occurrence of similar words in a file
using Map Reduce.
Algorithm:
1. Check Points
2. Writing mapper & reducer in R
3. Checking mapper file
4. Checking / Running on command line with separate mappers and reducers
5. Copy any text file to HDFS
6. Running MapReduce scripts on Hadoop
7. View WordCount output
8. Copy output to local filesystem
9. You can view wcOutputfile in any editor
Input Data :
Introduction to Hadoop
Introdiction to Hive
Hive Session
Pig Session
Result:
Thus the program Hadoop word count using Map Reduce has been executed successfully.
Aim:
To write a Matrix Multiplication program using Map Reduce.
Algorithm:
package www.ehadoopinfo.com;
package www.ehadoopinfo.com;
8. Uploading the M, N file which contains the matrix multiplication data to HDFS.
9. Executing the jar file using hadoop command and thus how fetching record from HDFS and
storing output in HDFS.
10. Getting Output from part-r-00000 that was generated after the execution of the hadoop
command.
Code
import java.io.IOException;
import java.util.*;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public void map(LongWritable key, Text value, Context context) throws IOException,
InterruptedException {
String line = value.toString();
if (indicesAndValue[0].equals("A")) {
outputKey.set(indicesAndValue[2]);
context.write(outputKey, outputValue);
} else {
outputKey.set(indicesAndValue[1]);
context.write(outputKey, outputValue);
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException,
InterruptedException {
String[] value;
if (value[0].equals("A")) {
} else {
String i;
float a_ij;
String k;
float b_jk;
i = Integer.toString(a.getKey());
a_ij = a.getValue();
k = Integer.toString(b.getKey());
b_jk = b.getValue();
context.write(null, outputValue);
}
}
job.setJarByClass(TwoStepMatrixMultiplication.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.waitForCompletion(true);
}
Result:
Thus the matrix multiplication program using Map Reduce has been executed successfully.
Algorithm:
1. Start the program.
2. Declare an integer array and read the values from the user.
3. Define a function rev( ) which accepts one pointer value (pointing to an element in the
array)and perform rearrangement of elements inside the function.
4. Display the reversed array.
5. Stop the program.
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, n, *num, temp;
printf("Enter how many
numbers:"); scanf("%d", &n);
num = (int *)malloc(sizeof(int) * n);
printf("Enter %d numbers:\n");
for (i = 0; i < n; i++)
{
scanf("%d",(num + i));
}
i = 0, j = n -
1; while (i < j)
{
temp = *(num + i);
*(num + i) = *(num + j);
*(num + j) = temp;
j--, i++;
}
printf("\nReversed array elements are:\n");
for (i = 0; i < n; i++)
{
printf("%d ", *(num + i));
}
printf("\n");
return 0;
}
Sample Input and Output:
Enter how many numbers:5
Enter 5 numbers: 10 30 25 65 39
The array elements are: 10 30 25 65 39
The reversed array elements are: 39 65 25 30 10.
Result:
Thus the program to read and reverse an array using pointers has been executed successfully.
Ex.No.6 Programs using Files – File Manipulation
Aim:
To create a file for maintaining employee details and to perform file manipulations(file
read ,write , append and copy).
Algorithm:
1. Start the program.
2. Create a file named “salary.dat” which maintains employee name and salary details for
10 employees.
3. Read “salary.dat” using a file pointer „fp1‟ and find employees getting highest and lowest
salary. Write these information in another file named “salary1.dat” using file pointer „fp2‟.
4. Append “salary.dat” with two more employee details.
5. Copy the contents of “salary2.dat” into “copysalary.dat” using file pointer „fp4‟.
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
char name[20];
float salary,high,low;
int i;
FILE *fptr1,*fptr2;
clrscr();
fptr1=(fopen("C:\\salary.dat","w"));
if(fptr1==NULL)
{
printf("Error!");
exit(1);
}
for(i=0;i<10;i++)
{
printf("For Employee %d\nEnter name:
",i+1); scanf("%s",name);
printf("Enter Salary: ");
scanf("%f",&salary);
fprintf(fptr1,"\nName : %s \nSalary:%.2f \n",name,salary);
}
close(fptr1);
fptr1=(fopen("C:\\salary.dat","r"));
if(fptr1==NULL){
printf("Error!");
exit(1);
}
fptr2=(fopen("C:\\salary1.dat","w"));
if(fptr2==NULL){
printf("Error!");
exit(1);
}
fscanf(fptr1,"%s %f",name,&salary); low=high=salary;
for(i=1;i<10;i++)
{
fscanf(fptr1,"%s
%f",name,&salary); if (salary>high)
high=salary; if
(salary<low)
low=salary;
}
printf("High Salary :%.2f\n",high);
printf("Low Salary :%.2f\n",low);
fprintf(fptr2,"\nHighest Salary:%.2f\nLowest Salary:%.2f\n",high,low);
fclose(fptr2);
fclose(fptr1);
fptr1=(fopen("C:\\salary.dat","a"));
if(fptr1==NULL){
printf("Error!");
exit(1);
}
for(i=0;i<2;i++)
{
printf("For Employee %d\nEnter name:
",i+1); scanf("%s",name);
printf("Enter Salary: ");
scanf("%f",&salary);
fprintf(fptr1,"\nName : %s \nSalary:%.2f \n",name,salary);
}
close(fptr1);
getch();
Details appended
>>type salary2.dat
aaa 21300
bbb 13546
ccc 74545
ddd 9898
eee 28767
fff 12356
ggg 15321
hhh 27643
iii 32451
jjj 29180
kkk 45362
lll 17632
>>type copysalary.dat
aaa 21300
bbb 13546
ccc 74545
ddd 9898
eee 28767
fff 12356
ggg 15321
hhh 27643
iii 32451
jjj 29180
kkk 45362
lll 17632
Result:
Thus the program to illustrate various file operations (read, write, append, copy) has been
executed successfully.
Aim:
To create a singly Linked list and to perform insertion and deletion operations.
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
typedefstruct list
{
intnum;
struct list *next;
}LIST;
LIST *head,*node,*temp;
void create();
void insert();
void del();
void display();
voidmain()
{
intch;
clrscr();
head=NULL;
while(1)
{
printf("enter the choice \n");
printf(" 1:create list \n 2:insert an element to list \n 3:delete an element \n 4.display \n 5.exit \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("wrong choice");
break;
}}}
void create()
{
int k=1;
printf("\n enter the element( else enter -1 to stop):");
scanf("%d",&k);
while(k!=-1)
{
LIST *s1,*p;
s1=(struct list *)malloc(sizeof(LIST));
s1->num=k;
s1->next=NULL;
if(head==NULL)
head=s1;
else p-
>next=s1;
p=s1;
printf("\n enter the element(else enter -1 to
stop):"); scanf("%d",&k);
}}
void display()
{
temp=head;
printf("\n List elements are \n");
while(temp!=NULL)
{
printf("\n %d--->",temp-
>num); temp=temp->next;
}
getch();
}
void del()
{
int i=0,pos;
temp=head;
printf("Enter the position to delete \n");
scanf("%d",&pos); while(temp!
=NULL)
{
if(pos==1)
{
head=temp->next;
printf("node
deleted"); break;
}
else
{
i++;
if(i+1==pos)
{
temp->next=temp->next-
>next; printf("node deleted");
}
}
temp=temp->next;
}
getch();
}
void insert()
{
LIST *p;
int y;
temp=head;
printf("Enter the element after which new element to be inserted
\n"); scanf("%d",&y);
printf("Enter the node value to insert \n");
p=(struct list *)malloc(sizeof(LIST));
scanf("%d",&p->num); while(temp!
=NULL)
{
if(temp->num==y)
{
p->next=temp->next;
temp->next=p;
printf("value inserted \n");
}
temp=temp->next;
}}
Enter
choice 1
Enter the element -1 to stop
10
Enter the element -1 to stop
20
Enter the element -1 to stop
30
Enter the element -1 to stop
-1
Enter
choice 3
Enter the position to be
deleted 1
Enter
choice 4
List elements are
10
30
Enter
choice 5
RESULT:
Thus the program to perform operations of singly linked list was successfully executed.
Aim:
To create a Doubly Linked list and to perform insertion and deletion operations.
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
typedefstruct list
{
intnum;
struct list *prev;
struct list
*next; }LIST;
LIST *head,*node,*temp;
void create();
void insert();
void del();
void display();
voidmain()
{
intch;
clrscr();
head=NULL;
while(1)
{
printf("enter the choice \n");
printf(" 1:create list \n 2:insert an element to list \n 3:delete an element \n 4.display \n 5.exit \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("wrong choice");
break;
}
}
}
void create()
{
int k=1;
printf("\n enter the element(Else enter -1 to stop:");
scanf("%d",&k);
while(k!=-1)
{
LIST *s1,*p;
s1=(struct list *)malloc(sizeof(LIST));
s1->num=k;
s1->next=NULL;
s1->prev=NULL;
if(head==NULL)
head=s1;
else
{
p->next=s1;
s1->prev=p;
}
p=s1;
printf("\n enter the element (Else enter -1 to stop:");
scanf("%d",&k);
}
}
void display()
{
temp=head;
printf("\n List elements are
\n\n"); while(temp!=NULL)
{
printf("%d--->",temp->num);
temp=temp->next;
}
getch();
}
void del()
{
intpos,i=0;
LIST *temp1;
temp=head;
printf("Enter the position to delete \n");
scanf("%d",&pos); while(temp!
=NULL)
{
i++;
if(pos==1)
{
head=temp->next;
head->prev=NULL;
printf("Node deleted
\n"); break;
}
else if(i==pos)
{
temp->prev->next=temp->next;
temp->next->prev=temp-
>prev; printf("node deleted");
}
temp=temp->next;
}
getch();
}
void insert()
{
LIST *p;
int y;
temp=head;
printf("Enter the element after which new element to be inserted
\n"); scanf("%d",&y);
printf("Enter the node value to insert \n");
p=(struct list *)malloc(sizeof(LIST));
scanf("%d",&p->num); while(temp!
=NULL)
{
if(temp->num==y)
{
p->next=temp->next;
temp->next->prev=p;
temp->next=p; p-
>prev=temp; printf("value
inserted \n");
}
temp=temp->next;
}
}
Enter
choice 1
Enter the element(Else enter -1 to stop)
10
Enter the element(Else enter -1 to stop)
20
Enter the element (Else enter -1 to stop)
30
Enter the element (Else enter -1 to stop)
-1
Enter
choice 3
Enter the element to be
deleted 10
Enter
choice 4
List elements are
20 30
Enter
choice 5
RESULT:
Thus the program to perform operations of doubly linked list was successfully executed.
Aim:
To create a queue in array and to perform enqueue and dequeue operations.
Algorithm:
1) Start the program.
2) Get the choice of operation as either enqueue or dequeue.
3) To enqueue an element into the queue do as follows.
a) Check for the maximum array size of the queue; if full then no element can be inserted.
b) If the queue is initially empty, increment both rear and front pointers byone. Otherwise
increment the rear pointer by one
4) To delete an element from the queue do as follows.
a) Check for queue empty condition; if empty then no element can be deleted.
b) Otherwise delete the element pointed by front by incrementing front pointer by one.
5) Display the elements from positions pointed from front to rear.
6) Stop the program.
Program:
#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Enqueue 2.Dequeue 3.Exit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
display();
break;
case 2:
del();
display();
break;
case 3:
exit(0);
default:
printf("Wrong choice \n");
}
}
}
insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("Enter the item to be inserted :\n ");
scanf("%d", &add_item); rear = rear + 1;
queue_array[rear] = add_item;
}
}
del()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("The deleted element is : %d\n",
queue_array[front]); front = front + 1;
}
}
display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("queue elements are :
\n"); for (i = front; i <= rear; i++)
printf("%d ",
queue_array[i]); printf("\n");
}
}
1-Enqueue,2-Dequeue,3-
Exit enter the choice 1
Enter the item to be inserted 11
queue elements are
11
1-Enqueue,2-Dequeue,3-
Exit enter the choice 1
Enter the item to be inserted 12
queue elements are
11 12
1-Enqueue,2-Dequeue,3-
Exit enter the choice 2
The deleted element is 11
queue elements are
12
1-Enqueue,2-Dequeue,3-Exit
enter the choice 2
The deleted element is 12
queue elements are
1-Enqueue,2-Dequeue,3-
Exit enter the choice 2
Queue Underflow
RESULT:
Thus the program to implement queue using array was successfully executed.
Aim:
To create astack in array and perform push and pop operations.
Algorithm:
1. Start the program.
2. Get the choice of operation as either push or pop.
3. To pushan element into the stack do as follows.
a. Check for the maximum array size of the stack, if full then no element can be
inserted.
b. Otherwise increment TOP by one and place the element in the position pointed
by TOP.
4. To popan element from the stack do as follows.
a. Check for stack empty condition; if empty then no element can be deleted.
b. Otherwise delete the element and decrement TOP by one.
5. Display the elements from TOP tostarting position of the array.
6. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
# define MAX 10
int q[10],top=-1;
void push( );
voidpop( ); void
display( );
void main( )
{
int choice;
clrscr( );
while(1)
{
printf(“1-Push,2-Pop,3-Exit\n”)
printf(“\nenter the choice\n”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:push( );
break;
case 2:pop( );
break; case
3:exit(0);
break;
}
}
getch( );
}
voidpush( )
{
int item;
printf(“Enter the item to be pushed\n”);
scanf(“%d”,&item);
if(top = = MAX)
{
printf(“stack is full\n”);
}
else
{
top++ ;
q[top]=item;
display( );
}
}
voidpop( )
{
int item;
if(top = = -1)
{
printf(“stack is empty\n”);
}
else
{
item=q[top];
top-- ;
printf(“The popped item is %d\n”,item);
display( );
}
}
void display( )
{
int i;
printf(“\nstack items are \n”);
for(i=top;i>=0;i--)
{
printf(“%d”,q[i]);
}
printf(“\n”);
}
OUTPUT:-
1-Push,2-Pop,3-Exit
1-Push,2-Pop,3-Exit
1-Push,2-Pop,3-Exit
RESULT:
Thus the program to implement stack using arrays was successfully executed.
Aim
To implement the Circular Queue using arrays
Algorithm
1. Declare an array Q of size N.
2. Assign F and R to be the front and rear pointers of the queue and assign -1 to F
and R.
3. Get the new element Y to be inserted in to the queue
4. If R is less than N, insert Y at the end, by incrementing R by 1.
Otherwise display Queue is Full.
5. If F is zero then assign F to be 1.
6. To delete an element check whether F is greater than zero, then delete an
element pointed by F, Otherwise display Queue is Empty.
7. If F and R are equal the set F = R=0;otherwise F=F+1;
8. Display the queue Q from F to R.
Program:
#include<stdio.h>
#include <conio.h>
#include<math.h> #define MAXSIZE 4 intcq[MAXSIZE]; intfront,rear;
void main( )
{
void add(int,int);
voiddel(int); void
display( ); int
will=1,i,num;
front = -1;rear =
-1; clrscr( );
printf("\nProgram for Circular Queue demonstration through
array"); while(1)
{
printf("\n\nMAIN MENU\n1.INSERTION\n2.DELETION\n3.EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&will);
switch(will)
{
case 1:
printf("\n\nENTER THE QUEUE ELEMENT : ");
scanf("%d",&num);
add(num,MAXSIZE);
display();
break;
case 2:
del(MAXSIZE);
display();
break;
case 3:
exit(0);
default:
printf("\n\nInvalid Choice . ");
}
}
}
void add(intitem,int MAX)
{
if(front = =(rear+1)%MAX)
{
printf("\n\nCIRCULAR QUEUE IS OVERFLOW");
}
else
{
if(front= =-1)
front=rear=0;
else rear=(rear+1)%MAX;
cq[rear]=item;
printf("\n\nRear = %d Front = %d ",rear,front);
}
}
voiddel(int MAX)
{
int a;
if(front = = -1)
{
printf("\n\nCIRCULAR QUEUE IS UNDERFLOW");
}
else
{
a=cq[front];
if(front= =rear)
front=rear=-1;
else
front = (front+1)%MAX;
printf("\n\nDELETED ELEMENT FROM QUEUE IS : %d ",a);
printf("\n\nRear = %d Front = %d ",rear,front);
}
}
void display( )
{
int i; if(front=
=-1)
{
printf("\n **CIRCULAR QUEUE IS UNDERFLOW**");
return;
}
else
{
printf("\n The status of queue ");
for(i=front;i<=rear;i++)
printf("\n%d",cq[i]);
}
if(front>rear)
{
for(i=front;i<MAXSIZE;i++)
printf("\n%d",cq[i]);
for(i=0;i<=rear;i++)
printf("\n%d",cq[i]);
}
}
SAMPLE OUTPUT:
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 1
Rear=0 Front=0
The status of queue 10
MAIN MENU 1.
INSERTION
2.DELETION
3.EXIT
Rear=1 Front=0
The status of
queue 10 20
MAIN MENU 1.
INSERTION
2.DELETION
3.EXIT
Rear=2 Front=0
The status of
queue 10 20 30
MAIN MENU 1.
INSERTION
2.DELETION
3.EXIT
Rear=3 Front=0
The status of
queue 10 20 30 40
MAIN MENU 1.
INSERTION
2.DELETION
3.EXIT
MAIN MENU 1.
INSERTION
2.DELETION
3.EXIT
MAIN MENU 1.
INSERTION
2.DELETION
3.EXIT
Rear =4 Front=1
The status of
queue 20 30 40 50
MAIN MENU 1.
INSERTION
2.DELETION
3.EXIT
Rear =4 Front=2
The status of
queue 30 40 50
RESULT:
Thus the program to implement array based circular queue was successfully executed.
Aim:
To create a Double ended Queue and to perform insertion and deletion operations.
Algorithm:
1. Start the program.
2. Create a array with two pointers, front and rear.
a. Initialize rear pointer value as -1.
b. Initialize front pointer value as 0.
3. Get the choice of operation - either insertion at rear end or front end and similarly
deletion at rear end or front end.
4. For insertion at the rear end ,check whether rear=arraysize, if so dequeue is full at rear
end, and not possible to insert at rear end. Otherwise increment the rear pointer and
insert the new item at the current position of rear pointer.
5. For insertion at the front end, check whether front > 0, if so decrement the front pointer
and insert the new item at the current position of front pointer.
6. For deletion at the rear end, check whether rear>=0,if so delete the item from the current
rear position and decrement the rear pointer, otherwise not possible to delete the item
from the rear end.
7. For deletion at the front end, check whether front <= rear, if so delete the item from the
current front position and increment the front pointer, otherwise not possible to delete the
item from the front end.
8. Using display option, list the elements of the dequeue.
9. Stop the program.
Program
#include
<stdio.h> #define
SIZE 20 structdq
{
intfront,rear;
int
item[SIZE]; }
deque;
void create(deque *);
void display(deque *);
void insert_rear(deque *, int);
void insert_front(deque *,
int); intdelete_front(deque *,
int); intdelete_rear(deque *,
int); void main()
{
intx,data,ch;
deque DQ;
clrscr();
create(&DQ);
printf(“\n\t\t Program shows working of double ended queue”);
do
{
printf(“\n\t\t Menu”);
printf(“\n\t\t 1: insert at rear end”);
printf(“\n\t\t 2: insert at front end”);
printf(“\n\t\t 3: delete from front
end”); printf(“\n\t\t 4: delete from rear
end”); printf(“\n\t\t 5: exit. “);
printf(“\n\t\t Enter choice :”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
if (DQ.rear>= SIZE)
{
printf(“\n Deque is full at rear
end”); continue;
}
else
{
printf(“\n Enter element to be added at rear
end :”); scanf(“%d”,&data);
insert_rear(&DQ,data);
printf(“\n Elements in a deque are :”);
display(&DQ);
continue;
}
case 2:
if (DQ.front<=0)
{
printf(“\n Deque is full at front end”);
continue;
}
else
{
printf(“\n Enter element to be added at front end :”);
scanf(“%d”,&data);
insert_front(&DQ,data);
printf(“\n Elements in a deque are :”);
display(&DQ);
continue;
}
case 3:
x = delete_front(&DQ,data);
if (DQ.front==0)
{
continue;
}
else
{
printf(“\n Elements in a deque are :”);
display(&DQ);
continue;
}
case 4:
x = delete_rear(&DQ,data);
if (DQ.rear==0)
{
continue;
}
else
{
printf(“\n Elements in a deque are :”); display(&DQ);
continue;
}
case 5: printf(“\n finish”); return;
}
}while(ch!=5);
getch();
}
void create(deque *DQ)
{
DQ->front=0;
DQ->rear =0;
}
void insert_rear(deque *DQ, int data)
{
if ((DQ->front == 0) &&(DQ->rear == 0))
{
DQ->item[DQ->rear] = data;
DQ->rear = DQ->rear +1;
}
else
{
DQ->item[DQ->rear] = data;
DQ->rear = DQ->rear +1;
}
}
intdelete_front(deque *DQ, int data)
{
if ((DQ->front == 0) && (DQ->rear == 0))
{
printf(“\n Underflow”);
return(0);
}
else
{
data = DQ->item[DQ->front];
printf(“\n Element %d is deleted from front :”,data);
DQ->front = DQ->front +1;
}
if (DQ->front==DQ->rear)
{
DQ->front =0;
DQ->rear = 0;
printf(“\n Deque is empty (front end)”);
}
return data;
}
void insert_front(deque *DQ, int data)
{
if(DQ->front > 0)
{
DQ->front = DQ->front-1;
DQ->item[DQ->front] = data;
}
}
intdelete_rear(deque *DQ, int data)
{
if (DQ->rear == 0)
{
printf(“\n Underflow”);
return(0);
}
else
{
DQ->rear = DQ->rear -1;
data = DQ->item[DQ->rear];
printf(“\n Element %d is deleted from rear:”,data);
}
if (DQ->front==DQ->rear)
{
DQ->front =0;
DQ->rear = 0;
printf(“\n Deque is empty(rear end)”);
}
return data;
}
void display(deque *DQ)
{
int x; for(x=DQ-
>front;xrear;x++)
{
printf(“%d\t”,DQ->item[x]);
}
printf(“\n\n”);
}
SAMPLE OUTPUT:
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :1
Enter element to be added at rear end :11
Elements in a deque are :11
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :1
Enter element to be added at rear end :22
Elements in a deque are :11 22
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :1
Enter element to be added at rear end :33
Elements in a deque are :11 22 33
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :2
Deque is full at front end
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :3
Element 11 is deleted from front :
Elements in a deque are :22 33
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :2
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :5
RESULT:
Thus the program to implement Double Ended Queue was successfully executed