PROGRAM 1
/*LINEAR SEARCH*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int a[30],n,k,i;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tLINEAR SEARCH\n");
printf("\t\--------------\n");
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\nEnter the searching element:");
scanf("%d",&k);
for(i=0;i<n;++i)
{
if(a[i]==k)
{
printf("\nThe element is found in the list");
getch();
exit(0);
}
}
printf("\nElement is not found");
getch();
}
OUTPUT
PROGRAM 2
/* BINARY SEARCH*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int a[30],n,k,i,l,mid,u;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tBINARY SEARCH\n");
printf("\t\--------------\n");
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\nEnter the sorted elements :");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\nEnter the searching element:");
scanf("%d",&k);
l=0;u=n-1;
for(i=0;i<n;++i)
{
mid=(l+u)/2;
if(a[mid]==k)
{
printf("\nThe element is found in the list");
getch();
exit(0);
}
else if(a[mid]>k)
u=mid-1;
else
l=mid+1;
}
printf("\nElement is not found");
getch();
}
OUTPUT
PROGRAM 3
/*SELECTION SORT*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],j,i,n,temp,min,pos;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tSELECTION SORT\n");
printf("\t\--------------\n");
printf("enter the limit");
scanf("%d",&n);
printf("enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
min=a[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
pos=j;
}
}
if(pos!=i)
{
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}
printf("\n array after sorting");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
OUTPUT
PROGRAM 4
/*BUBBLE SORT*/
#include <stdio.h>
#include <conio.h>
void main()
{
int a[30],n,i,j,temp;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tBUBBLE SORT\n");
printf("\t\------------\n");
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\nEnter the elements :");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n-1;++i)
for(j=i+1;j<n;++j)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("\nSorted array is:");
for(i=0;i<n;++i)
printf("%d ",a[i]);
getch();
}
OUTPUT
PROGRAM 5
/*INSERTION SORT*/
#include <stdio.h>
#include <conio.h>
void main()
{
int a[30],n,i,j,temp,k;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tINSERTION SORT\n");
printf("\t\---------------\n");
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\nEnter the elements :");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=1;i<n;++i)
{
printf("\n");
for(k=0;k<n;++k)
temp=a[i];
for(j=i-1;j>=0;--j)
{
if(temp<a[j])
a[j+1]=a[j];
else
break;
}
a[j+1]=temp;
}
printf("\nSorted array is:");
for(i=0;i<n;++i)
printf("%d ",a[i]);
getch();
}
OUTPUT
PROGRAM 6
/*POLYNOMIAL ADDITION USING ARRAY*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[10],i,m,n,cnt=0;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tPOLYNOMIAL ADDITION USING
ARRAY\n");
printf("\t\---------------------------------\n");
for(i=0;i<=9;i++)
a[i]=0;
for(i=0;i<=9;i++)
b[i]=0;
printf("enter the order of ploy1");
scanf("%d",&m);
printf("\n enter the coeff");
for(i=m;i>=0;i--)
{
scanf("%d",&a[i]);
}
printf("\n first poly is:");
for(i=m;i>0;i--)
{
printf("%dx^%d+",a[i],i);
}
printf("%d",a[i]);
printf("\n\nenter the order of poly2");
scanf("%d",&n);
printf("\n enter the coeff");
for(i=n;i>=0;i--)
{
scanf("%d",&b[i]);
}
printf("\n\n second poly is:");
for(i=n;i>0;i--)
{
printf("%dx^%d+",b[i],i);
}
printf("%d",b[i]);
if(m>=n)
{
for(i=m;i>=0;i--)
{
c[i]=a[i]+b[i];
cnt++;
}
}
else
{
for(i=n;i>=0;i++)
{
c[i]=a[i]+b[i];
}
}
printf("\n\nresultant poly is:");
for(i=cnt-1;i>0;i--)
{
printf("%dx^%d+",c[i],i);
}
printf("%d",c[i]);
getch();
}
OUTPUT
PROGRAM 7
\*POLYNOMIAL MULTIPLICATION USING ARRAY*/
#include <stdio.h>
#include <conio.h>
void main()
{
int eq1[20],n1,i,eq2[20],n2,ans[20][20],j,eq[20]
[20],k=0,temp;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("POLYNOMIAL MULTIPLICATION\n");
printf("------------------------------------------");
printf("\nEnter no of terms of first polynomial:");
scanf("%d",&n1);
printf("\nEnter coefficients");
for(i=0;i<n1;++i)
scanf("%d",&eq1[i]);
printf("\nEnter no of terms of second polynomial:");
scanf("%d",&n2);
printf("\nEnter coefficients");
for(i=0;i<n2;++i)
scanf("%d",&eq2[i]);
printf("\nEquation 1:");
for(i=0;i<n1;++i)
printf("%dx^%d + ",eq1[i],i);
printf("\b\b\b ");
printf("\nEquation 2:");
for(i=0;i<n2;++i)
printf("%dx^%d + ",eq2[i],i);
printf("\b\b\b ");
for(i=0;i<n1;i++)
{
for(j=0;j<n2;++j)
{
ans[k][0]=eq1[i]*eq2[j];
ans[k][1]=i+j;
k++;
}
}
printf("\nResult is ");
for(i=0;i<(n1*n2)-1;i++)
{
temp=ans[i][1];
for(j=i+1;j<(n1*n2);++j)
{
if((ans[j][1]==temp)&&(ans[j][0]!=0)&&
(ans[i][0]!=0))
{
ans[i][0]=ans[i][0]+ans[j][0];
ans[j][0]=ans[j][1]=0;
}
}
}
for(i=0;i<(n1*n2);++i)
{
if(ans[i][0]!=0)
printf("%d X^%d + ",ans[i][0],ans[i][1]);
}
printf("\b\b\b ");
getch();
}
OUTPUT
PROGRAM 8
/*SPARSE MATRIX ADDITION*/
#include <stdio.h>
#include <conio.h>
void main()
{
int c,m,n,mat1[20][20],mat2[20]
[20],i,j,c1=0,c2=0,sp1[40][3],k,sp2[40][3],res[40][3];
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tSPARSE MATRIX ADDITION");
printf("\n\t----------------------");
printf("\nEnter the order of square matrix:");
scanf("%d",&m);
printf("\nEnter the sparse matrix 1:");
for(i=0;i<m;++i)
{
for(j=0;j<m;++j)
{
scanf("%d",&mat1[i][j]);
if(mat1[i][j]!=0)
c1++;
}
}
printf("\nEnter the order of square matrix:");
scanf("%d",&n);
printf("\nEnter the sparse matrix 2:");
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&mat2[i][j]);
if(mat2[i][j]!=0)
c2++;
}
}
k=1;
sp1[0][0]=m;
sp1[0][1]=m;
sp1[0][2]=c1;
for(i=0;i<m;++i)
{
for(j=0;j<m;++j)
{
if(mat1[i][j]!=0)
{
sp1[k][0]=i+1;
sp1[k][1]=j+1;
sp1[k][2]=mat1[i][j];
k++;
}
else
continue;
}
}
k=1;
sp2[0][0]=n;
sp2[0][1]=n;
sp2[0][2]=c2;
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
if(mat2[i][j]!=0)
{
sp2[k][0]=i+1;
sp2[k][1]=j+1;
sp2[k][2]=mat2[i][j];
k++;
}
else
continue;
}
}
printf("\n3 tuple rep:Sparse matrix 1") ;
for(i=0;i<=c1;++i)
{
printf("\n");
for(j=0;j<3;++j)
{
printf("%d ",sp1[i][j]);
}
}
printf("\n 3 tuple rep :Sparse matrix 2") ;
for(i=0;i<=c2;++i)
{
printf("\n");
for(j=0;j<3;++j)
{
printf("%d ",sp2[i][j]);
}
}
for(j=0;j<=c1;++j)
{
if((sp1[j][0]==sp2[j][0])&&(sp1[j][1]==sp2[j]
[1]))
{
res[j][0]=sp1[j][0];
res[j][1]=sp1[j][1];
res[j][2]=sp1[j][2]+sp2[j][2];
sp1[j][0]=sp2[j][0]=sp1[j][1]=sp2[j]
[1]=sp1[j][2]=sp2[j][2]=0;
c=j;
}
}
c++;
for(i=0;i<=c1;++i)
{
if(sp1[i][0]!=0)
{
res[c][0]=sp1[i][0];
res[c][1]=sp1[i][1];
res[c][2]=sp1[i][2];
c++;
}
}
for(i=0;i<=c2;++i)
{
if(sp2[i][0]!=0)
{
res[c][0]=sp2[i][0];
res[c][1]=sp2[i][1];
res[c][2]=sp2[i][2];
c++;
}
}
printf("\nResultant matrix is") ;
for(i=0;i<=c2+1;++i)
{
printf("\n");
for(j=0;j<3;++j)
{
printf("%d ",res[i][j]);
}
}
getch();
}
OUTPUT
PROGRAM 9
/*SPARSE MATRIX MULTIPLICATION*/
#include <stdio.h>
#include <conio.h>
void main()
{
int c=0,m,n,mat1[20][20],mat2[20]
[20],i,j,c1=0,c2=0,sp1[20][3],k,sp2[20][3],res[20][20],sp3[20]
[3];
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tSPARSE MATRIX MULTIPLICATION");
printf("\n\t----------------------");
printf("\nEnter the order of square matrix:");
scanf("%d",&m);
printf("\nEnter the sparse matrix 1:");
for(i=0;i<m;++i)
{
for(j=0;j<m;++j)
{
scanf("%d",&mat1[i][j]);
if(mat1[i][j]!=0)
c1++;
}
}
printf("\nEnter the order of square matrix:");
scanf("%d",&n);
printf("\nEnter the sparse matrix 2:");
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&mat2[i][j]);
if(mat2[i][j]!=0)
c2++;
}
}
k=1;
sp1[0][0]=m;
sp1[0][1]=m;
sp1[0][2]=c1;
for(i=0;i<m;++i)
{
for(j=0;j<m;++j)
{
if(mat1[i][j]!=0)
{
sp1[k][0]=i+1;
sp1[k][1]=j+1;
sp1[k][2]=mat1[i][j];
k++;
}
else
continue;
}
}
k=1;
sp2[0][0]=n;
sp2[0][1]=n;
sp2[0][2]=c2;
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
if(mat2[i][j]!=0)
{
sp2[k][0]=i+1;
sp2[k][1]=j+1;
sp2[k][2]=mat2[i][j];
k++;
}
else
continue;
}
}
printf("\n3 tuple representation of Sparse matrix 1") ;
for(i=0;i<=c1;++i)
{
printf("\n");
for(j=0;j<3;++j)
{
printf("%d ",sp1[i][j]);
}
}
printf("\n3 tuple representation of Sparse matrix 2") ;
for(i=0;i<=c2;++i)
{
printf("\n");
for(j=0;j<3;++j)
{
printf("%d ",sp2[i][j]);
}
}
if(m!=n)
{
printf("\nMatrices cannot be multiplied");
getch();
}
else
{
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
res[i][j]=0;
for(k=0;k<n;++k)
res[i][j]=res[i][j]+mat1[i][k]*mat2[k][j];
}
}
printf("\nResultant matrix is");
for(i=0;i<m;++i)
{
printf("\n");
for(j=0;j<n;++j)
{
if(res[i][j]!=0)
c++;
printf("%d ",res[i][j]);
}
}
printf("\n3-tuple representation of resultant matrix is");
k=1;
sp3[0][0]=m;
sp3[0][1]=n;
sp3[0][2]=c;
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
if(res[i][j]!=0)
{
sp3[k][0]=i+1;
sp3[k][1]=j+1;
sp3[k][2]=res[i][j];
k++;
}
else
continue;
}
}
for(i=0;i<m;++i)
{
printf("\n");
for(j=0;j<3;++j)
printf("%d ",sp3[i][j]);
}
}
getch();
}
OUTPUT
PROGRAM 10
/*STACK USING ARRAY*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
# define max 15
int push(int a[],int);
void display(int a[],int);
int pop(int a[],int);
void main()
{
int c,top=1,a[max],n;
char ch;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tIMPLEMENTATION OF STACK USING
ARRAY\n");
printf("\t\------------------------------------\n");
printf("1.push");
printf("\n 2.pop");
printf("\n 3.display");
printf("\n 4.exit");
do{
printf("\n enter your choice");
scanf("%d",&c);
switch(c)
{
case 1:
top=push(a,++top);
break;
case 2:
top=pop(a,top);
break;
case 3:
display(a,top);
break;
case 4:
exit(0);
default:
printf("invalid option");
}
printf("\n continue");
flushall();
scanf("%c",&ch);
}while(ch=='y' || ch=='Y');
getch();
}
int push(int a[],int i)
{
char ch;
//i=0;
do
{
if(i<max)
{
printf("enter the elements");
scanf("%d",&a[i]);
i++;
}
else {
printf("stack overflow");
return --i;
}
printf("do you wnat to push another element");
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
return --i;
}
int pop(int a[],int top)
{
int i;
int n=top;
char ch;
do
{
if(top<=0)
{ printf("stack underflow");
break;
}
else
{
top=top-1;
printf("\n after pop");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return top;
}
printf("\n do you want to pop another
element");
scanf("%d",&ch);
}while(ch=='y'||ch=='Y');
return --i;
}
void display(int a[],int j)
{
int i=0;
if(i<0){
printf("stack underflow");
}
else{
for(i=j;i>1;i--)
printf("%d\n",a[i]);
}
}
OUTPUT
PROGRAM 11
/*REVERSE A STRING USING STACK*/
#include <stdio.h>
#include <conio.h>
void main()
{
char str[20];
int top=0,n;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tREVERSE OF STRING USING ARRAY\n");
printf("\t\------------------------------\n");
printf("\nEnter number of charcters:");
scanf("%d",&n);
printf("\nEnter the string:");
while(top<=n)
{
scanf("%c",&str[top]);
top++;
}
printf("\nReverse of the string\n");
while(top>0)
{
printf("%c",str[top-1]);
top--;
}
getch();
}
OUTPUT
PROGRAM 12
/*INFIX TO POSTFIX CONVERSION*/
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#define SIZE 50
char s[SIZE];
int top=-1;
push(char elem)
{
s[++top]=elem;
}
char pop()
{
return(s[top--]);
}
int pr(char elem)
{
switch(elem)
{
case '#':return 0;
case '(':return 1;
case '+':
case '-':return 2;
case '*':
case '/':return 3;
}
}
main()
{
char infx[50],pofx[50],ch,elem;
int i=0,k=0;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tINFIX TO POSTFIX CONVERSION");
printf("\n\t---------------------------");
printf("\nenter the infix expression:");
scanf("%s",infx);
push('#');
while((ch=infx[i++])!='\0')
{
if(ch=='(')
push(ch);
else if(isalnum(ch))
pofx[k++]=ch;
else if(ch==')')
{
while(s[top]!='(')
pofx[k++]=pop();
elem=pop();
}
else
{
while(pr(s[top])>=pr(ch))
pofx[k++]=pop();
push(ch);
}
}
while(s[top]!='#')
pofx[k++]=pop();
pofx[k]='\0';
printf("\n given infix expression:%s\n \npostfix expression:%s\n",infx,pofx);
getch();
}
OUTPUT
PROGRAM 13
/*ARITHMETIC EXPRESSION EVALUATION*/
OUTPUT
/*GRAPH USING ARRAY*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void undirgraph();
void dirgraph();
int read(int[10][10],int);
void main()
{
int c;
char ch;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tGRAPH USING ARRAY");
printf("\n\t-----------------");
printf("\n 1.undirected graph");
printf("\n2.directed graph");
printf("\n 3.exit");
printf("\n enter ur choice");
do
{
scanf("%d",&c);
switch(c)
{
case 1:
undirgraph();
break;
case 2:
dirgraph();
break;
case 3:
exit(0);
break;
default:printf("invalid option");
}
printf("\ncontinue");
flushall();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
getch();
}
void undirgraph()
{
int n,adjmat[10][10],i,j,deg=0;
printf("\n enter the no of vertices");
scanf("%d",&n);
read(adjmat,n);
printf("\n vertex \tdegree");
for(i=1;i<=n;i++)
{
deg=0;
for(j=1;j<=n;j++)
{
if(adjmat[i][j]==1)
deg++;
}
printf("\n %d\t\t%d",i,deg);
}
}
void dirgraph()
{
int n,adjmat[10][10],i,j,indeg=0,outdeg=0;
printf("\n enter the no of vertices");
scanf("%d",&n);
read(adjmat,n);
printf("\n vertex \tindegree \toutdegree \ttotaldegree");
for(i=1;i<=n;i++)
{
indeg=0;
outdeg=0;
for(j=1;j<=n;j++)
{
if(adjmat[j][i]==1)
indeg++;
}
for(j=1;j<=n;j++)
{
if(adjmat[i][j]==1)
outdeg++;
}
printf("\n%d \t\t%d \t\t%d
\t\t%d",i,indeg,outdeg,indeg+outdeg);
}
}
int read(int adjmat[10][10],int n)
{
int i,j;
char reply;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
{
adjmat[i][j]=0;
continue;
}
printf("\n vertex %d is adjacent to vertex %d(y,n)?",i,j);
flushall();
scanf("%c",&reply);
if(reply=='y'||reply=='Y')
adjmat[i][j]=1;
else
adjmat[i][j]=0;
}
}
return 0;
}
OUTPUT
/*SPARSE MATRIX MULTIPLICATION*/
#include<stdio.h>
#include<conio.h>
void main()
{
int c[10][10],a[10][10],b[10][10],i,j,m1,n1,m2,n2,k;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tSPARSE MATRIX MULTIPLICATION");
printf("\n\t----------------------------");
printf("\nenter the order of first matrix");
scanf("%d%d",&m1,&n1);
printf("\nenter the elements");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the order of second matrix");
scanf("%d%d",&m2,&n2);
printf("\nenter the elements");
int c1=0,c2=0;
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
scanf("%d",&b[i][j]);
if(b[i][j]==0)
c1++;
else
c2++;
}
}
if(c1<c2)
{
printf("not sparse");
}
printf("\n first matrix is:");
printf("\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("\n second matrix is:");
printf("\n");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
c[i][j]=0;
for(k=0;k<m1;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("\nproduct of matrix is");
printf("\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT
/*REALLOC*/
#include<conio.h>
#include<alloc.h>
void main()
{
int *a,i,j,m,n;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\tIMPLEMENTATION OF REALLOC");
printf("\n\t-------------------------");
printf("\n enter the size of array");
scanf("%d",&m);
a=(int*)calloc(sizeof(int),m);
printf("\n enter %d elements",m);
for(i=0;i<m;i++)
{
printf("\n enter a[%d]",i);
scanf("%d",a+i);
}
printf("\n elements are");
for(i=0;i<m;i++)
printf("\t%d",*(a+i));
printf("\nenter new size of array");
scanf("%d",&n);
a=(int*)realloc(a,n);
printf("\n enter %d more elements",n-m);
for(i=m;i<n;i++)
{
printf("\n enter a[%d]",i);
scanf("%d",a+i);
}
printf("\nelements are");
for(i=0;i<n;i++)
printf("\t%d",*(a+i));
getch();
}
OUTPUT
/*STACK USING LINKED LIST*/
#include<stdio.h>
#include<conio.h>
struct stack
{
int info;
struct stack *next;
};
typedef struct stack node;
node *push();
node *pop(node *);
void display(node *);
node *top=NULL;
void main()
{
int ch;
char c;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\nSTACH USING LINKED LIST ");
printf("\n------------------------");
printf("\n1.Push Operation");
printf("\n2.Pop Operation");
printf("\n3.Display");
printf("\n4.Exit");
do
{
printf("\nEnter the Choice");
scanf("%d",&ch);
switch(ch)
{
case 1:top=(node*)malloc(sizeof(node));
top=push();
break;
case 2:if(top!=NULL)
top=pop(top);
else
printf("\nStack Underflow");
break;
case 3:if(top!=NULL)
display(top);
else
printf("\nStack Overflow");
break;
case 4:exit(0);
break;
default:printf("Default choice");
}
printf("Continue(Y/N)");
flushall();
scanf("%c",&c);
}while(c=='y'||c=='Y');
getch();
}
node *push(node *t)
{
node *t1;
char ch,c;
printf("\nEnter the element");
scanf("%d",&t->info);
t->next=NULL;
do{
printf("Do u want to add another element");
flushall();
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{
t1=(node *)malloc(sizeof(node));
printf("\nEnter the element");
scanf("%d",&t1->info);
t1->next=t;
t=t1;
}
printf("continue(y/n)");
flushall();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
return(t);
}
node *pop(node *t)
{
char c;
node *temp;
do
{
if(t!=NULL)
{
temp=t;
t=t->next;
free(temp);
}
else
{
printf("\nStack is empty");
return(t);
}
printf("\nPop again?");
flushall();
scanf("%c",&c);
}
while(c=='y'||c=='Y');
return(t);
}
void display(node *t)
{
printf("\nThe Stack is");
while(t!=NULL)
{
printf("\n|%d|\n",t->info);
t=t->next;
}
}
OUTPUT
/*DOUBLY LINKED LIST*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct linklist
{
int info;
struct linklist *next;
};
typedef struct linklist node;
void insertlinlist();
void display();
void search(node*,int);
int main()
{
int c,key;
char ch;
node *head;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\nDOUBLY LINKED LIST");
printf("\n------------------");
printf("\n1.Insert to linklist\n 2.Display linklist\n3.Search
linklist\n 4.exit");
do
{
printf("\n enter your choice");
scanf("%d",&c);
switch(c)
{
case 1: head=(node*)malloc(sizeof(node));
insertlinlist(head);
break;
case 2: if(head!=NULL)
display(head);
else
printf("\nThe list is empty.");
break;
case 3: printf("\nEnter the element to search:");
scanf("%d",&key);
search(head,key);
break;
case 4:exit(0);
break;
default:printf("\nWrong Selection..\n");
}
printf("\nDo you want to continue:(y/n)");
scanf("%*c%c",&ch);
}while(ch=='y'||ch=='Y');
return 0;
}
void insertlinlist(node *temp)
{
char ch;
printf("\nEnter the element:");
scanf("%d",&temp->info);
printf("\nDo you want to continue(y/n):");
scanf("%*c%c",&ch);
if(ch=='Y'||ch=='y')
{
temp->next=(node*)malloc(sizeof(node));
insertlinlist(temp->next);
}
else
{
temp->next=NULL;
}
}
void display(node *temp)
{
printf("The elements in linked list are:");
while(temp!=NULL)
{
printf("%d->",temp->info);
temp=temp->next;
}
printf("NULL");
}
void search(node *temp,int key)
{
while(temp!=NULL)
{
if(temp->info==key)
{
printf("\nElement present");
return;
}
else
{
temp=temp->next;
}
}
printf("\nElement not found");
}
OUTPUT
/*QUEUE USING ARRAY*/
#include <conio.h>
#include <stdio.h>
#define max 10
int insert(int[],int,int);
int delet(int);
void display(int[],int,int);
void main()
{
int q[max],rear=0,front=0,c;
char ch;
clrscr();
printf("\n\t\t\t\t\t\t NAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\t REGISTER NO:1624028");
printf("\n\QUEUE IMPLEMENTATION");
printf("\n--------------------");
printf("\n1-INSERTION\n2-DELETION\n3-
DISPLAY\n4-EXIT");
do
{
printf("\nEnter your choice");
scanf("%d",&c);
switch(c)
{
case 1:rear=insert(q,rear,front);break;
case 2:front=delet(front);break;
case 3:display(q,rear,front);break;
case 4:exit(0);
default:printf("\nInvalid entry");
}
printf("\nDo you want to continue(y/n):");
flushall();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
getch();
}
int insert(int a[],int r,int f)
{
char ch;
do
{
if((r<max)&&(f>=0))
{
printf("\nEnter element:");
scanf("%d",&a[r]);
r++;
}
else
{
printf("\nQueue overflow");
return --r;
}
printf("\nDo you want to add element(y/n):");
flushall();
scanf("%c",&ch);
} while(ch=='y'||ch=='Y');
return r--;
}
void display(int a[],int r,int f)
{
int i;
for(i=f;i<r;++i)
printf("%d\n",a[i]);
}
int delet(int f)
{
char ch;
do
{
if(f=='0')
{
printf("\nNo elements");
return f;
}
else
{
printf("\nAn element is deleted");
f++;
}
printf("\nDo you want to delete again(y/n)");
flushall();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
return f;
}
OUTPUT
/*CIRCULAR QUEUE USING ARRAY*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 15
int queue_array[max];
int rear = - 1;
int front = 0;
void insert();
void deletes();
void display();
void main()
{ int c;
char ch;
clrscr();
printf("\n\t\t\t\t\t\tNAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\tREGNO:1624028");
printf("\n\t CIRCULAR QUEUE USING ARRAY");
printf("\n\t--------------------");
printf("\n1.INSERT");
printf("\n2.DELETE");
printf("\n3.DISPLAY");
printf("\n4.EXIT");
do
{
printf("\nyour choice:");
scanf("%d",&c);
switch(c)
{
case 1:insert();
break;
case 2:deletes();
break;
case 3:display();
break;
case 4:exit(0);
default:
printf("\ninvalid option");
}
printf("continue");
flushall();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
getch();
}
void insert()
{
int c;
char ch;
do
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
queue_array[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
queue_array[++rear]=x;
}
}
printf("continue(y/n):");
flushall();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
}
void deletes()
{ int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
exit(0);
}
if(front==rear)
{
a=queue_array[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=queue_array[front];
front=0;
}
else a=queue_array[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch();
//exit(0);
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",queue_array[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",queue_array[j]);
printf("\nrear is at %d\n",queue_array[rear]);
printf("\nfront is at %d\n",queue_array[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%d",queue_array[i]);
}
printf("\nrear is at %d\n",queue_array[rear]);
printf("\nfront is at %d\n",queue_array[front]);
}
printf("\n");
}
OUTPUT
/*DOUBLE ENDED QUEUE*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 10
int a[max],f=-1,r=-1;
void display()
{
int i;
for(i=f;i<=r;i++)
{
printf("-%d-",a[i]);
}
}
void add1()
{
char c;
do{
if(f==-1 && r==-1)
{
f++;
r++;
printf("Enter the element : ");
scanf("%d",&a[r]);
}
else if(r<max)
{
r++;
printf("Enter the element : ");
scanf("%d",&a[r]);
}
else
{
printf("Queue Full");
}
printf("Do you want to continue??");
scanf("%*c");
scanf("%c",&c);
}while(c=='y'||c=='Y');
}
void del1()
{
if(f<=r)
{
f++;
}
else
printf("No elements..");
}
void del2()
{
if(f<=r)
{
r--;
}
else
printf("No elements...");
}
void delopr()
{
int ch;
do{
printf("1.Deletion at front\n2.Deletion at
rear\n3.Display\n4.End\nEnter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1 : del1();
break;
case 2 :del2();
break;
case 3 :display();
break;
case 4 :
exit(0);
break;
}
}while(ch!=4);
}
void queue1()
{
int ch;
char c;
//clrscr();
printf("\nINPUT RESTRICTED QUEUE");
printf("\n----------------------");
printf("\n1.ADD\n2.DELETE\n3.DISPLAY4.exit");
do
{
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: add1();
break;
case 2: delopr();
break;
case 3: display();
break;
case 4:exit(0);
break;
}
printf("Do you want to continue ?");
scanf("%*c");
scanf("%c",&c);
}while(c=='y'||c=='Y');
}
void add2()
{
if(f>=0)
{
f--;
printf("Enter the element : ");
scanf("%d",&a[f]);
}
else
printf("Queue Full");
}
void addopr()
{
int ch;
printf("Enter the elements :");
add1();
printf("\n1.Insertion at front\n2.Insertion at
rear\n3.Display\n4.End");
do
{
printf("\nEnter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1 :add2();
break;
case 2 :add1();
break;
case 3 :display();
break;
case 4 :
exit(0);break;
}
}while(ch!=4);
}
void queue2()
{
int ch;
char c;
//clrscr();
printf("\nOUTPUT RESTRICTED QUEUE");
printf("\n-----------------------");
printf("\n1.ADD\n2.DELETE\n3.DISPLAY");
do
{
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: addopr();
break;
case 2: del1();
break;
case 3: display();
break;
}
printf("Do you want to continue ?");
scanf("%*c");
scanf("%c",&c);
}while(c=='y'||c=='Y');
}
void main()
{
int ch;
char c;
do{
clrscr();
printf("\n\t\t\t\t\t\tNAME:NEENA NAVAS");
printf("\n\t\t\t\t\t\tREGISTER NO:1624028");
printf("\nDOUBLE ENDED QUEUE");
printf("\n------------------");
printf("\n1.INPUT RESTRICTED QUEUE\n2.OUTPUT
RESTRICTED QUEUE\nEnter Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: queue1();
break;
case 2: queue2();
break;
}
printf("Do you want to continue?");
scanf("%c",&c);
}while(c=='y'||c=='Y');
getch();
}
OUTPUT