0% found this document useful (0 votes)
76 views99 pages

DS PGMS

The document provides an algorithm and program to create a sparse matrix from a given dense matrix. The algorithm counts the number of non-zero elements, stores the row and column indices and element value of non-zeros in a sparse matrix array, and displays the sparse matrix. The program implements the algorithm by taking matrix size as input, reading the dense matrix, counting non-zeros, initializing the sparse matrix with row and column counts and non-zero count, storing non-zero elements row and column indices and values, and displaying the sparse matrix.

Uploaded by

anoopkumar.m
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views99 pages

DS PGMS

The document provides an algorithm and program to create a sparse matrix from a given dense matrix. The algorithm counts the number of non-zero elements, stores the row and column indices and element value of non-zeros in a sparse matrix array, and displays the sparse matrix. The program implements the algorithm by taking matrix size as input, reading the dense matrix, counting non-zeros, initializing the sparse matrix with row and column counts and non-zero count, storing non-zero elements row and column indices and values, and displaying the sparse matrix.

Uploaded by

anoopkumar.m
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 99

1. Write a program to insert an element in to a sorted array?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

We have to declare an array with name a [size]


Step 1: Start
Step 2: Read n, item
Step 3: Set i=0, ctr=0, u=n-1
Step 4: Read array element in ascending order
Step 5: If (a [ctr]>item), then, if not go to step 6
Set pos=0 go to step 10
Step 6: If (ctr<u) then repeat step 7 to step 8
Step 7: If (a [ctr] <=item and item<=a [ctr+1]) then
Set pos=ctr+1, then go to step 10
Step 8: ctr=ctr+1 end of repeat
Step 9: If ctr=u then if not go to step 10
Pos=u+1
Step 10: Set ctr=u
Step 11: If (ctr>=pos) then repeat steps 12, 13
If not go to step 14
Step 12: a [ctr+1] =a [ctr]
Step 13: ctr=ctr-1
Step 14: a [pos] =item
Step 15: Display array a [size]
Step 16: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

main()
{
int a[10],item,n,i,u,ctr=0,pos;
clrscr();
printf("\nEnter size:\t");
scanf("%d",&n);
printf("\nEnter elements in ascending order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter item for insert:\t");
scanf("%d",&item);
u=n-1;
if(a[ctr]>item)
pos=0;
else
{
while(ctr<u)
{
if((a[ctr]<=item)&&(item<=a[ctr+1]))
{
pos=ctr+1;
break;
}
ctr=ctr+1;
}
if(ctr==u)
pos=u+1;
}
ctr=u;
while(ctr>=pos)
{
a[ctr+1]=a[ctr];
ctr=ctr-1;
}
a[pos]=item;
printf("\nAfter insertion\n");
for(i=0;i<=n;i++)
printf("\n%d",a[i]);
getch();
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT

Enter size: 4
Enter elements in ascending order
2
4
7
9
Enter item for insert: 5
After insertion
2
4
5
7
9

DEPART MENT OF COMPUTER APPLICATION, SNGCE


2. Program to delete an element from sorted array?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

We have to declare an array with name a [size]


Step 1: Start
Step 2: Read n, pos
Step 3: Set i=0
Step 4: Read array element in ascending order
Step 5: Repeats step 6 and 7 until pos<=n-1
Step 6: a [pos] =a [pos+1]
Step 7: pos=pos+1 end of repeat
Step 8: Display array
Step 9: stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

main()
{
int a[10],n,pos,i;
clrscr();
printf("\nEnter size:\t");
scanf("%d",&n);
printf("\nEnter elements in ascending order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter position for delete:\t");
scanf("%d",&pos);
while(pos<=n-1)
{
a[pos]=a[pos+1];
pos=pos+1;
}
printf("\nAfter deletion\n");
for(i=0;i<n-1;i++)
printf("\n%d",a[i]);
getch();
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT

Enter size: 4
Enter elements in ascending order
2
5
6
9
Enter position for delete: 2
After deletion
2
5
9

DEPART MENT OF COMPUTER APPLICATION, SNGCE


3. Program to search a value using linear search?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

We have to declare an array with name a [size]

Step 1: Start
Step 2: Read n, item
Step 3: Set i=0
Step 4: Read array element
Step 5: Repeats steps 6 until i<n
Step 6: If a[i] =item, go to step 7
Step 7: Display value present, go to step 10
Step 8: If i=n, go to step 9
Step 9: Display unsuccessful search
Step 10: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

main()
{
int a[10],i,n,item;
clrscr();
printf("\nEnter size:\t");
scanf("%d",&n);
printf("\nEnter element\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter item for search:\t");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(a[i]==item)
{
printf("\nSearch is success");
break;
}
}
if(i==n)
printf("\nValue not present");
getch();
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT

Enter size: 4
Enter element
1
5
2
6
Enter item for search: 5
Search is success

DEPART MENT OF COMPUTER APPLICATION, SNGCE


4. Program to search a value using binary search?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

We have to create an array with name a [size]

Step 1: Start
Step 2: Read array elements, n, no
Step 3: Set upp=n-1, low=i= 0
Step 4: Repeats steps 5 to 7 until i<n
Step 5: Set j=i+1
Step 6: Repeat steps 7 until j<n
Step 7: If a[i]>a[j] then
t=a[i]
a[i]=a[j]
a[j]=t end of repeat
Step 8: Repeats steps 9 to 14 until low<=upp
Step 9: mid = (low+upp)/2
Step 10: If a[mid]=no go to step 11,if not go to step 12
Step 11: Display value present go to step 18
Step 12: If a[mid]<no go to step 13,if not go to step 14
Step 13: Set low =mid+1
Step 14: If a[mid]>no go to step 15,if not go to step 16
Step 15: Set upp=mid-1 end of repeats
Step 16: If low>upp
Step 17: Display value cannot present
Step 18: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

main()
{
int a[10],n,i,low=0,upper,mid,no,j,t;
clrscr();
printf("\nEnter the size:\t");
scanf("%d",&n);
printf("\nEnter the numbers\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the search no's:\t");
scanf("%d",&no);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
upper=n-1;
while(low<=upper)
{
mid=(low+upper)/2;
If(a[mid]==no)
{
printf("\nValue is present");
break;
}
if(a[mid]<no)
low=mid+1;
if(a[mid]>no)
upper=mid-1;
if(low>upper)
printf("\nUnsuccessful search");
}
getch();
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT

Enter the size: 4


Enter the numbers
2
4
5
7
Enter the search no's: 2
Value is present

DEPART MENT OF COMPUTER APPLICATION, SNGCE


5. Program to sort an array using bubble sort?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

We have to declare an array with name a [size]

Step 1: Start
Step 2: Read n
Step 3: Set i=j=0
Step 4: Read array element
Step 5: Repeat steps 6 until i<n
Step 6: Repeat step 7 until j<n-1-i
Step 7: If a[j]>a [j+1] then,
c=a[j]
a[j]=a[j+1]
a[j+1]=c
Step 8: j=j+1 end of repeat
Step 9: i=i+1 end of repeat
Step 10: Display array
Step 11: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM
main()
{
int a[10],i,j,n,c;
clrscr();
printf("\nEnter the size:\t");
scanf("%d",&n);
printf("Enter the number\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
If(a[j]>a[j+1])
{
c=a[j];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
printf("\nAfter sorting\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT

Enter the size: 5


Enter the number
10
5
7
3
1
After sorting
1
3
5
7
10

DEPART MENT OF COMPUTER APPLICATION, SNGCE


6. Write a program to create a sparse matrix?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

Input: A matrix mat[ ][ ] with size m and n


Output: Sparse matrix spar [ ] [ ] of matrix mat [ ] [ ], with nonzero element
Nzero

Step 1: Start
Step 2: Set i=0,j=0,s=0
Step 3: 1: Spar[s][0]=m
2: Spar[s][1]=n
3: Spar[s][2]=Nzero
4: Set s=1
Step 4: Repeat while i<m and j<n go to step 5
Step 5: if mat[i][j]!=0 then, Otherwise go to step 6
Spar[s][0]=i+1
Spar[s][1]=j+1
Spar[s][2]=mat[i][j]
Set i=i+1,j=j+1,s=s+1
Step 6: Set i=i+1,j=j+1
Step 7: Display Spar[ ][ ]
Step 8: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

main()
{
int m,n,i,j,mat[10][10],spar[20][20],s,nzero=0;
clrscr();
printf("\nEnter value of row:\t");
scanf("%d",&m);
printf("\nEnter value for column:\t");
scanf("%d",&n);
printf("\nEnter matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&mat[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(mat[i][j]!=0)
nzero=nzero+1;
}
}
s=0;
spar[s][0]=m;
spar[s][1]=n;
spar[s][2]=nzero;
s=1;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(mat[i][j]!=0)
{
spar[s][0]=i+1;
spar[s][1]=j+1;
spar[s][2]=mat[i][j];
s=s+1;
}
}
}
printf("\nSparse matrix is\n");
for(i=0;i<=nzero;i++)
{
for(j=0;j<3;j++)

DEPART MENT OF COMPUTER APPLICATION, SNGCE


printf("%d\t",spar[i][j]);
printf("\n");
}
getch();
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT

Enter value of row: 2

Enter value for column: 2

Enter matrix
1
0
0
5

Sparse matrix is
2 2 2
1 1 1
2 2 5

DEPART MENT OF COMPUTER APPLICATION, SNGCE


7. Write a program to add two sparse matrixes?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

Sparse matrix add(sp1,sp2,res)


Input: Two sparse matrix sp1[ ][ ] and sp2[ ][ ] which are two dimensional
Matrix
Output: Sparse matrix res[ ][ ] which is the result addition of sparse matrix
sp1[ ][ ] and sp2[ ][ ]

Data structure
Two dimensional array structure with each row representing
the nonzero elements of original matrixes.
Step 1: Start
Step 2: If sp1[0][0]=sp2[0][0] AND
sp1[0][1]=sp2[0][1] then go to step 3
Else
Output: Matrix are not of same order addition not possible .
Step 3: 1: Set I=J=K=1
2: Repeat while I<=sp1[0][2] AND j<=sp2[0][2]
1: If sp1[I][0]=sp2[J][0] then, elements in same row
I: If spi[I][1]=sp2[J][1] then, elements in same column
a: Set res[K][0]= sp1[I][0]
b: Set res[K][1]=sp1[J][1]
c: Set res[K][2]=sp1[I][2]+sp2[J][2]
d: Set I=I+1,J=J+1,K=K+1
II: Else
a: Ifsp1[I][1]< sp2[J][1] then if it elements of matrix is
Proceeding column
1: Set res[K][0]=sp1[I][0]
2: Set res[K][1]=sp1[I][1]
3: Set res[K][2]=sp1[I][2]
4: Set I=I+1,K=K+1
b: Else
1: Set res[K][0]=sp2[J][0]
2: Set res[K][1]=Sp2[J][1]
3: Set res[K][2]=Sp2[J][2]
4: Set J=J+1,K=K+1

2: Else
a: If sp1[I][0]<sp2[J][0] then
1: Set res[K][0]=sp1[I][0]
2: Set res[K][1]=sp1[I][1]
3: Set res[K][2]=sp1[a][2]

DEPART MENT OF COMPUTER APPLICATION, SNGCE


4: Set K=K+1,I=i+1

b: Else
1: Set res[K][0]=sp2[J][0]
2: Set res[K][1]=sp2[J][1]
3: Set res[K][2]=sp2[J][2]
4: Set K=K+1,J=J+1
Step 4: Repeat while I<=sp1[0][2]
1: Set res[K][0]=sp1[I][0]
2: Set res[K][1]=sp1[I][1]
3: Set res[k][2]=sp1[I][2]
4: Set K=K+1,I=I+1

Step 5: Repeat while J<=sp2[0][2]


1: Set res[K][0]=sp2[J][0]
2: Set res[K][1]=sp2[J][1]
3: Set res[k][2]=sp2[J][2]
4: Set K=K+1,J=J+1
Step 6: 1: Set res[K][0]= sp1[0][0]
2: Set res[K][1]=sp1[0][1]
3: Set res[K][2]=k-1
Step 7: Display res[ ][ ]
Step 8: Stop

PROGRAM

DEPART MENT OF COMPUTER APPLICATION, SNGCE


main()
{
int m,n,i,j,fm[10][10],sm[10][10],sp1[10][10],sp2[10][10];
int res[10][10],noz=0,s=1,nz=0,p=1,a=1,b=1,k=0,c=0;
clrscr();
printf("\nOUTPUT");
printf("\n..................");
printf("\nEnter value for row and column\n");
scanf("%d%d",&m,&n);
printf("\nEnter first matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&fm[i][j]);
if(fm[i][j]!=0)
noz=noz+1;
}
}
sp1[0][0]=m;
sp1[0][1]=n;
sp1[0][2]=noz;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(fm[i][j]!=0)
{
sp1[s][0]=i+1;
sp1[s][1]=j+1;
sp1[s][2]=fm[i][j];
s=s+1;
}
}
}
printf("\nFirst sparse matrix is\n");
for(i=0;i<=noz;i++)
{
for(j=0;j<3;j++)
printf("%d\t",sp1[i][j]);
printf("\n");
}
printf("\nEnter second matrix\n");
for(i=0;i<m;i++)
{

DEPART MENT OF COMPUTER APPLICATION, SNGCE


for(j=0;j<n;j++)
{
scanf("%d",&sm[i][j]);
if(sm[i][j]!=0)
nz=nz+1;
}
}
sp2[0][0]=m;
sp2[0][1]=n;
sp2[0][2]=nz;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(sm[i][j]!=0)
{
sp2[p][0]=i+1;
sp2[p][1]=j+1;
sp2[p][2]=sm[i][j];
p=p+1;
}
}
}
printf("\nSecond sparse matrix is\n");
for(i=0;i<=nz;i++)
{
for(j=0;j<3;j++)
printf("%d\t",sp2[i][j]);
printf("\n");
}
res[k][0]=m;
res[k][1]=n;
k=1;
while((a<=sp1[0][2])&&(b<=sp2[0][2]))
{
if(sp1[a][0]==sp2[b][0])
{
if(sp1[a][1]==sp2[b][1])
{
res[k][0]=sp1[a][0];
res[k][1]=sp1[a][1];
res[k][2]=(sp1[a][2]+sp2[b][2]);
k=k+1,a=a+1,b=b+1,c=c+1;
}
else if(sp1[a][1]<sp2[b][1])
{

DEPART MENT OF COMPUTER APPLICATION, SNGCE


res[k][0]=sp1[a][0];
res[k][1]=sp1[a][1];
res[k][2]=sp1[a][2];
k=k+1,a=a+1,c=c+1;
}
else
{
res[k][0]=sp2[b][0];
res[k][1]=sp2[b][1];
res[k][2]=sp2[b][2];
k=k+1,b=b+1,c=c+1;
}
}
else if(sp1[a][0]<sp2[b][0])
{
res[k][0]=sp1[a][0];
res[k][1]=sp1[a][1];
res[k][2]=sp1[a][2];
k=k+1,a=a+1,c=c+1;
}
else
{
res[k][0]=sp2[b][0];
res[k][1]=sp2[b][1];
res[k][2]=sp2[b][2];
k=k+1,b=b+1,c=c+1;
}
}
while(a<=sp1[0][2])
{
res[k][0]=sp1[a][0];
res[k][1]=sp1[a][1];
res[k][2]=sp1[a][2];
k=k+1,a=a+1,c=c+1;
}
while(b<=sp2[0][2])
{
res[k][0]=sp2[b][0];
res[k][1]=sp2[b][1];
res[k][2]=sp2[b][2];
k=k+1,b=b+1,c=c+1;
}
res[0][2]=c;
printf("\nSum of sparse matrix\n");
for(i=0;i<=c;i++)
{

DEPART MENT OF COMPUTER APPLICATION, SNGCE


for(j=0;j<3;j++)
printf("\t%d",res[i][j]);
printf("\n");
}
getch();
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT
..................

Enter value for row and column


2
2
Enter first matrix
1
0
0
3

First sparse matrix is


2 2 2
1 1 1
2 2 3

Enter second matrix


2
0
3
0

Second sparse matrix is


2 2 2
1 1 2
2 1 3

Sum of sparse matrix


2 2 3
1 1 3
2 1 3
2 2 3

DEPART MENT OF COMPUTER APPLICATION, SNGCE


8.Write a program to add two polynomial?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

POLYNOMIAL ADD ARRAY


( P 1,P 2,P 3,M,N)

Input
Two polynomial P1 and p2 which are array of structure. n
and m are maximum number of term in p1 and p 2.
Output
A resultant polynomial p 3 which is the sum of p 1 and p 2.
Datastructure in use
Array of structure is use to store a polynomial. The
structure will contain the fields coefficient and exponent.

Step 1: Start
Step 2: Set i=j=k=1
Step 3: Repeat while i<=m AND j<=n
3.1: if p1[i].exp=p2[j].exp then
a: Set p3[k].exp=p1[i].exp
b: Set p3[k].c=p1[i].c+p2[j].c
c: Set k=k+1,i=i+1,j=j+1
3.2: Else
a: if p1[i].e<p2[j].e then
1: Set p3[k].e=p2[j].e
2: Set p3[k].c=p2[j].c
3: Set k=k+1,j=j+1
b: Else
1: p3[k].e=p1[i].e
2: p3[k].c=p1[i].c
3: k=k+1,i=i+1
Step 4: Repeat while i<=n
1: p3[k].e=p1[i].e
2: p3[k].c=p1[i].c
3: k=k+1,i=i+1
Step 5: Repeat while j<=m
1: Set p3[k].e=p2[j].e
2: Set p3[k].c=p2[j].c
3: Set k=k+1,j=j+1
Step 6: Display polynomial p3
Step 7: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

struct poly1
{
int c,e;
}p1[10];
struct poly2
{
int c,e;
}p2[10];
struct poly3
{
int c,e;
}p3[20];
main()
{
int m,n,i,j,k,s;
clrscr();
printf("\n*************OUTPUT**************");
printf("\n******POLINOMIAL ADDITION********\n");
printf("\n.................................\n");
printf("\nEnter value for m&n\n");
scanf("%d%d",&n,&m);
printf("\nEnter first polynomil\n");
for(i=0;i<n;i++)
{
printf("Coefficent:");
scanf("%d",&p1[i].c);
printf("\tExponential:");
scanf("%d",&p1[i].e);
}
printf("\nEnter second polynomil\n");
for(i=0;i<m;i++)
{
printf("\nCoefficent:");
scanf("%d",&p2[i].c);
printf("\tExponential:");
scanf("%d",&p2[i].e);
}
i=0;k=0;j=0;s=0;
while((i<=n)&&(j<=m))
{
if(p1[i].e==p2[j].e)
{
p3[k].e=p1[i].e;
p3[k].c=(p1[i].c+p2[j].c);

DEPART MENT OF COMPUTER APPLICATION, SNGCE


k=k+1;i=i+1;j=j+1;s=s+1;
}
else if(p1[i].e<p2[j].e)
{
p3[k].e=p2[j].e;
p3[k].c=p2[j].c;
k=k+1;j=j+1;s=s+1;
}
else
{
p3[k].e=p1[i].e;
p3[k].c=p1[i].c;
k=k+1;i=i+1;s=s+1;
}
}
while(i<n)
{
p3[k].e=p1[i].e;
p3[k].c=p1[i].c;
k=k+1;i=i+1;s=s+1;
}
while(j<m)
{
p3[k].e=p2[j].e;
p3[k].c=p2[j].c;
k=k+1;j=j+1;s=s+1;
}
printf("\nNew polynomil is\n");
for(i=0;i<s-1;i++)
{
printf("\nExponent\t:%d",p3[i].e);
printf("\tCoefficent\t:%d",p3[i].c);
}
getch();
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


*************OUTPUT**************
******POLINOMIAL ADDITION********
.............................................

Enter value for m&n


2
2

Enter first polynomil


Coefficent:3
Exponential:2
Coefficent:1
Exponential:1

Enter second polynomil

Coefficent:2
Exponential:3

Coefficent:4
Exponential:2

New polynomil is

Exponent :3 Coefficent :2
Exponent :2 Coefficent :7
Exponent :1 Coefficent :1

DEPART MENT OF COMPUTER APPLICATION, SNGCE


9. Write a program for stack operation?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

We have an array staarr[ ],with top pointer top=-1,and max=20

Step 1: Start
Step 2: Read choice to cho
Step 3: Repeat while 1
a: if cho=1 push() function call
b: if cho=2 pop() function call
c: if cho=3 display() function call
d: if cho=4 stop program
d: other wise wrong number
End of while
Step 4: Stop

Sub function push()

Step 1: Start
Step 2: if top=max-1 then
Display “Over flow”
Step 3: Else
a: Read n
b: Repeat while i<n
c: read item to it
d: Set top=top+1
e: Set Staarr[top]=it
End of while and else
Step 4: Stop

Sub function pop()

Step 1: Start
Step 2: if top=-1 then
Display “Stack empty”
Step 3: Else
a: Deleated item is staarr[top]
b: Set top=top-1
End of else
Step 4: Stop

Sub function Display()

DEPART MENT OF COMPUTER APPLICATION, SNGCE


Step 1: Start
Step 2: if top=-1 then
Display “Stack empty”
Step 3: Else
a: Set i=top
b: Repeat while i>-1
c: Display staarr[i]
b: Set i=i+1
End of while and else
Step 4: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

#define max20
int staarr[33],top=-1;
main()
{
int cho;
clrscr();
while(1)
{
printf("\nChoices are\n");
printf("\n1 Insert");
printf("\n2 Delete");
printf("\n3 Display");
printf("\n4 Exit");
printf("\nEnter your choice:\t");
scanf("%d",&cho);
switch(cho)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4:exit(0);
default:printf("\nWrong number");break;
}
}
getch();
}

push ()
{
int it,i,n,max;
if(top==(max-1))
printf("\nStack over flow");
else
{
printf("\nHow many number enter in to the stack:\t");
scanf("%d",&n);
printf("\nEnter inserted item\n");
for(i=0;i<n;i++)
{
scanf("%d",&it);
top=top+1;
staarr[top]=it;

DEPART MENT OF COMPUTER APPLICATION, SNGCE


}
}
}

pop()
{
if(top==-1)
printf("\nStack is empty");
else
{
printf("\nDeleted item is \t%d",staarr[top]);
top=top-1;
}
}

display()
{
int i;
if(top==-1)
printf("\nStack is empty");
else
{
for(i=top;i>-1;i--)
printf("%d\n",staarr[i]);
} /*end of else*/
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT

Choices are

1 Insert
2 Delete
3 Display
4 Exit
Enter your choice: 1

How many number enter in to the stack: 2

Enter inserted item


1
2

Choices are

1 Insert
2 Delete
3 Display
4 Exit
Enter your choice: 3
2
1

Choices are

1 Insert
2 Delete
3 Display
4 Exit
Enter your choice: 4

DEPART MENT OF COMPUTER APPLICATION, SNGCE


10. Write a program for queue operation?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

We have an array qu[ ],with front=rear=-1,and size=50

Step 1: Start
Step 2: Read choice to cho
Step 3: Repeat while 1
a: if cho=1 push() function call
b: if cho=2 pop() function call
c: if cho=3 display() function call
d: if cho=4 stop program
d: other wise wrong number
End of while
Step 4: Stop

Sub function push()

Step 1: Start
Step 2: if rear=max-1 then
Display “Over flow”
Step 3: Else
a: Read n
b: If front=-1 then
Set front=0
c: Repeat while i<n
c: read item to it
d: Set rear=rear+1
e: Set qu[rear]=it
End of while and else
Step 4: Stop

Sub function pop()


Step 1: Start
Step 2: if front=-1 OR front>rear then
Display “Queue empty”
Step 3: Else
a: Deleated item is qu[front]
b: Set front=front-1
End of else
Step 4: Stop

Sub function Display()

DEPART MENT OF COMPUTER APPLICATION, SNGCE


Step 1: Start
Step 2: if front=-1 then
Display “Queue empty”
Step 3: Else
a: Set i=front
b: Repeat while i>=rear
c: Display qu[i]
b: Set i=i+1
End of while and else
Step 4: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

#include<stdio.h>
#define size50;
int qu[10],rear=-1,front=-1;
main()
{
char c,cho;
clrscr();
while(1)
{
printf("\nChoice are\n");
printf("\n1 Insert");
printf("\n2 Deleate");
printf("\n3 Display");
printf("\n4 Exit");
printf("\nEnter your choice:\t");
scanf("%d",&cho);
switch(cho)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
default:exit(0);
}
}
getch();
}

push ()
{
int it,i,n,size;
printf("\nHow many elements enter to the queue:\t");
scanf("%d",&n);
if(rear==(size-1))
printf("\nQueue over flow");
else
{
if(front==-1)
front=0;
printf("\nEnter inserted item\n");
for(i=0;i<n;i++)
{
scanf("%d",&it);
rear=rear+1;

DEPART MENT OF COMPUTER APPLICATION, SNGCE


qu[rear]=it;
}
}
}

pop()
{
if(front==-1||front>rear)
printf("\nQueue is empty");
else
{
printf("\nDeleted item is \t%d",qu[front]);
front=front+1;
}
}

display()
{
int i;
if(front==-1)
printf("\nQueue is empty");
else
{
for(i=front;i<=rear;i++)
printf("%d\n",qu[i]);
}
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT

Choice are
1 Insert
2 Deleate
3 Display
4 Exit
Enter your choice: 1

How many elements enter to the queue: 2

Enter inserted item


1
2

Choice are

1 Insert
2 Deleate
3 Display
4 Exit
Enter your choice: 3
1
2

Choice are

1 Insert
2 Deleate
3 Display
4 Exit
Enter your choice: 4

DEPART MENT OF COMPUTER APPLICATION, SNGCE


11.Program to convert infix to postfix ?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

Convert ion of infix to postfix

Step 1: Start
Step 2: Add the unique expression # in to the
stack and at the end of array infix
Step 3: Scan the symbol of array infix
one by one from left to right
Step 4: If the symbol is left parentheses (‘
then add to the stack
Step 5: The symbol is operant then add into array postfix
Step 6:
a: If the symbol is operator then pop the operator which have same
Precedence or higher precedence then the operator which occur
b: Add the pop operator to array postfix
c: Add the scan symbol in to the stack
Step 7:
a: If the symbol is right parentheses ‘)’ then pop all operator from
stack until the left parentheses ‘(‘ in the stack.
b: Remove the left parentheses from the stack
Step 8: If the symbol is # pop the entire symbol from stack and add them to
array postfix expect #
Step 9: Do the same process until # come in scanning array infix.
Step 10: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


Evaluation of postfix expression

Step 1: Start
Step 2: Add symbol # at the end of array postfix.
Step 3: Scan the symbol of array postfix one by one from left to right
Step 4: If the symbol is operant push in to stack.
Step 5: If the symbol is operator then pop the last two element of stack
And evaluate it us [top-1] and [top]
Step 6: Do the same process until the # comes in scanning
Step 7: Pop the element of stack which will be the value of evaluation of
postfix arithmetic expression
Step 8: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

#include<stdio.h>
#include<string.h>
#include<math.h>
#define max 200
long int top,stack[max],fox(),pop();
char choice='y',infix[max],postfix[max];
main()
{
long int h,a;
clrscr();
do
{
top=0;
printf("\nEnter infix expression\n");
fflush(stdin);
gets(infix);
printf("\nPostix expression is\n");
post();
printf("%s",postfix);
h=fox();
printf("\nResult of an expression\n");
printf("\nResult:%d",h);
printf("\n Do you want to continue(y/n):\t");
scanf("%c",&choice);
}while(choice=='y');
}

post()
{
long int len,p=0,precedence,i,e;
char a;
stack[top]='#';
len=strlen(infix);
infix[len]='#';
for(i=0;infix[i]!='#';i++)
{
e=space(infix[i]);
if(e==0)
{
switch(infix[i])
{
case '(':push(infix[i]);break;
case ')':while((a=pop())!='(')

DEPART MENT OF COMPUTER APPLICATION, SNGCE


postfix[p++]=a;
break;
case '+':
case '-':
case '*':
case '/':
case '^':
precedence=pre(infix[i]);
while(stack[top]!='#'&&precedence<=pre(stack[top]))
postfix[p++]=pop();
push(infix[i]);
break;
default:postfix[p++]=infix[i];
}
}
}
while(stack[top]!='#')
postfix[p++]=pop();
postfix[p]='\0';
}

push(int s)
{
if(top>max)
{
printf("\n Stack over flow");
exit(0);
}
else
{
top=top+1;
stack[top]=s;
}
}

long int pop()


{
if(top==-1)
{
printf("\nStack is empty");
exit(0);
}
else
return(stack[top--]);
}
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


pre(char sy)
{
switch(sy)
{
case '(':return(0);
case '+':case '-':return(1);
case '*':case '/':return(2);
case '^':return(3);
}
}

space(char s)
{
if(s==' '||s=='\t'||s=='\0')
return(1);
else
return(0);
}

/*EVALUATION OF EXPRESSION*/

long int fox()


{
long int a,b,temp,re,i,len;
len=strlen(postfix);
postfix[len]='#';
for(i=0;postfix[i]!='#';i++)
{
if(postfix[i]<='9'&&postfix[i]>='0')
push(postfix[i]-48);
else
{
a=pop();
b=pop();
switch(postfix[i])
{
case '+':temp=(b+a);break;
case '-':temp=(b-a);break;
case '*':temp=(b*a);break;
case '/':temp=(b/a);break;
case '^':temp=(pow(b,a));break;
}
push(temp);
}
}
re=pop();

DEPART MENT OF COMPUTER APPLICATION, SNGCE


return(re);
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT

Enter infix expression


4*(5+4^2)-2^2*(9/3)

Postix expression is
4542^+*22^93/*-
Result of an expression

Result:72
Do you want to continue(y/n): n

DEPART MENT OF COMPUTER APPLICATION, SNGCE


12. Program for circular queue?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

We have an array cq[ ],with front=rear=-1,and max=30

Step 1: Start
Step 2: Read choice to c
Step 3: Repeat while 1
a: if cho=1 insert() function call
b: if cho=2 delete() function call
c: if cho=3 display() function call
d: if cho=4 stop program
d: other wise wrong number
End of while
Step 4: Stop

Sub function insert()

Step 1: Start
Step 2: Read item to it
Step 3: if front=0 AND rear=max-1 OR front=rear+1 then
Display “Queue over flow” go to step 7
Step 4: If front=-1 then
Set front=rear=0
Step 5: else
a: if rear=max-1 then
rear=0
b: else
rear=rear+1
Step 6: set cq[rear]=it
Step 7: Stop

Sub function delete()

Step 1: Start
Step 2: if front=-1 then
Display “Queue empty” go to step 6
Step 3: Deleted item is cq[front]
Step 4: If front=rear then
Set front=rear=-1
Step 5: else

DEPART MENT OF COMPUTER APPLICATION, SNGCE


a: if front=max-1 then
front=0
b: else
front=front+1
Step 6: Stop

Sub function of display()

Step 1: Start
Step 2: Set f=front and r=rear
Step 3: If f=-1 then
Display “Queue empty” go to step
Step 4: display “queue element are
Step 5: If f<=r then
a: repeat while f<=r
1: display cq[f]
2: Set f=f+1
End while and if
Step 6: else
1: Repeat while f<max-1
a: Display cq[f]
b: Set f=f+1
End of while
2: Set f=0
3: Repeat while f<,=r
a: Display cq[f]
b: f=f+1
End of while and else
Step 7: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

#include<stdio.h>
#define max 30
int cq[max];
int rear=-1;
int front=-1;
main()
{
int c;
clrscr();
while(1)
{
printf("\n1 Insert");
printf("\n2 Delete");
printf("\n3 Display");
printf("\n4 Exit");
printf("\nEnter your choice : ");
scanf("%d",&c);
switch(c)
{
case 1:insert();break; /*insert function call*/
case 2:delete();break; /*delete function call*/
case 3:display();break; /*display functio call*/
case 4:exit(0);
default:printf("\nwrong number");
}
}
getch();
}

/*insert function declaration declaration*/


insert()
{
int it;
if((front==0 && rear==max-1) || (front==rear+1))
{
printf("\nQueue over flow");
return;
}
if(front==-1)
{
front=0;
rear=0;
}
else

DEPART MENT OF COMPUTER APPLICATION, SNGCE


if(rear==(max-1))
rear=0;
else
rear=rear+1;
printf("\nInput element for insertion: ");
scanf("%d",&it);
cq[rear]=it;
}

/*delete function declaration*/


delete()
{
if(front==-1)
{
printf("\nQueue empty");
return(0);
}
printf("Deleted item:\t%d",cq[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else
if(front==(max-1))
front=0;
else
front=front+1;
}

/*display function declaration*/


display()
{
int f=front,r=rear;
if(f==-1)
{
printf("\nQueue is empty");
return;
}
printf("\nQueue elements are\n");
if(f<=r)
{
while(f<=r)
{
printf("\n%d",cq[f]);
f=f+1;
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


}
else
{
while(f<=(max-1))
{
printf("\n%d",cq[f]);
f=f+1;
}
f=0;
while(f<=r)
{
printf("\n%d",cq[f]);
f=f+1;
}
}
printf("\n");
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT
1 Insert
2 Delete
3 Display
4 Exit
Enter your choice : 1

Input element for insertion: 1

1 Insert
2 Delete
3 Display
4 Exit
Enter your choice : 1

Input element for insertion: 2

1 Insert
2 Delete
3 Display
4 Exit
Enter your choice : 1
Input element for insertion: 3

1 Insert
2 Delete
3 Display
4 Exit
Enter your choice : 3

Queue elements are

1
2
3

1 Insert
2 Delete
3 Display
4 Exit
Enter your choice : 4

DEPART MENT OF COMPUTER APPLICATION, SNGCE


13.program for linked list?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

We have a structure that contain two part number part and pointer part
There are three pointer variable named first,temp,head .

Step 1: Start
Step 2: Set first=null,choice=y,read choice to c
Step 3: If choice=c then
Create() function call
Step 4: Repeat while
1: read a
2: If a=1 then
insert() function call
3: If a=2 then
delete() function call
4: If a=3 then
display() function call
5: If a=4 stop
Step 5: Stop

Sub function insert()

Step 1: Start
Step 2: Read a
Step 3:
1: if a=1 begin() call
2: if a=2 endin() call
3: if a=3 appin() call
4: IF a=4 break
5: if a=5 stop
Step 4: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


Sub function delete()

Step 1: Start
Step 2: Read a
Step 3:
1: if a=1 begde() call
2: if a=2 endde() call
3: if a=3 appde() call
4: if a=4 break
5: if a=5 stop
Step 4: Stop

Sub function of create()

Step 1:
Start
Step 2:
Read c
Step 3:
Repeat while c=1
Step 4:
Allocate memory and read data in data field
Step 5:
If first!=null then
Set temp→ptr and temp=head
Step 6: else set first=temp=head end of while
Step 7: temp→ptr=null and temp=first
Step 8: stop

Sub function of display()

Step 1: start
Step 2: Set temp=first
Step 3: repeat while temp!=null
Step 4: Display temp→num
Step 5: Set temp=temp→ptr
Step 6: Stop

Subfunction of begin()

Step 1: Start
Step 2: Allocate memory for new node
Step 3: Assign the value to the data field of new node

DEPART MENT OF COMPUTER APPLICATION, SNGCE


Step 4: Make the link field of new node point to the
Starting node of the link list
Step 5: Set external pointer to point to new node
Step 6: Stop

Sub function of endin()

Step 1: Start
Step 2: Allocate memory for new node
Step 3: Assign the value to the data field of new node
Step 4: Assign the null value to the pointer field of new node
Step 5: Make the link field of second last node to point to the last node
Step 6: Stop

Sub function of appin()

Step 1: Start
Step 2: Allocate memory for new node
Step 3: Assign the value to the data field of new node
Step 4: Make the link field of new node to point to node B
Step 5: Make the link field of node A to point to new node
Step 6: Stop

Sub function of begde()

Step 1: Start
Step 2: If the first is not empty then check whether
the element belong to first node of the list
Step 3: Move the start pointer to the second node
Step 4: free the first node
Step 5: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


Sub function of ended()

Step 1: Start
Step 2: If the first is not empty go on traversing till the last node
Step 3: Set the link field of second last node to null
Step 4: Free the last node
Step 5: Stop

Sub function of appde()

Step 1: Start
Step 2: If the first is not empty then make the link field of
Node A point to the node B
Step 3: Free the node between A and B
Step 4: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM
#include<stdio.h>
#include<ctype.h>
#define null'\0'
#define choice 'y'
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *temp,*first,*head;
int create(),insert(),delete(),display();
int begin(),endin(),appin();
int begde(),endde(),appde();
char cho='y';
main()
{
int a;
char c;
clrscr();
first=null;
printf("\nYou want to create a linked list(y/n)");
scanf("%c",&c);
if(choice==c)
create();
while(1)
{
printf("\nChoices are");
printf("\n1 Insert");
printf("\n2 Delete");
printf("\n3 Display");
printf("\n4 Exit");
printf("\nEnter your choice:\t");
scanf("%d",&a);
switch(a)
{
case 1:insert();break; /*insert function call*/
case 2:delete();break; /*delete function call*/
case 3:display();break; /*display function call*/
case 4:exit(0);
default:printf("\nWrong number");
}
}
getch();
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


/*insert function declaration*/
insert()
{
int a;
printf("\nWhere you want to insert");
printf("\n1 Insert at begining");
printf("\n2 Insert at end");
printf("\n3 Insert at specific location");
printf("\n4 Break");
printf("\n5 Exit");
printf("\nEnter your choice:\t");
scanf("%d",&a);
switch(a)
{
case 1:begin();break; /*begin function call*/
case 2:endin();break; /*endin function call*/
case 3:appin();break; /*appin function call*/
case 4:break;
case 5:exit(0);
default:printf("\nWrong number");
}
}

/*delete functon declaration*/


delete()
{
int a;
printf("\n1 Delete at begining");
printf("\n2 Delete at end");
printf("\n3 Delete at specific position");
printf("\n4 Break");
printf("\n5 Exit");
printf("\nPrintf enter your choice");
scanf("%d",&a);
switch(a)
{
case 1:begde();break; /*begde function call*/
case 2:endde();break; /*endde function call*/
case 3:appde();break; /*appde function call*/
case 4:break;
case 5:exit(0);
default:printf("\nWrong number");
}
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


/*create function declaration*/
create()
{

do
{
head=(NODE*)malloc(sizeof(NODE));
printf("\nEnter the data item:\t");
scanf("%d",&head->num);
if(first!=null)
{
temp->ptr=head;
temp=head;
}
else
first=temp=head;
printf("\nDo you want to continue (y/n)");
scanf("%s",&cho);
}while(cho=='y');
temp->ptr=null;
temp=first;
}

/*display function declaration*/


display()
{
printf("Elements in linked list are\n");
temp=first;
while(temp!=null)
{
printf("%d\n",temp->num);
temp=temp->ptr;
}
}

/*begin function declaration*/


begin()
{
temp=first;
head=(NODE*)malloc(sizeof(NODE));
printf("\nEnter inserted item:\t");
scanf("%d",&head->num);
if(head==null)
head->ptr=null;
else
head->ptr=temp;

DEPART MENT OF COMPUTER APPLICATION, SNGCE


temp=first=head;
}

/*endin function declaration*/


endin()
{
temp=first;
head=(NODE*)malloc(sizeof(NODE));
printf("\nEnter inserted item:\t");
scanf("%d",&head->num);
head->ptr=null;
if(first==null)
first=head;
else
{
temp=first;
while(temp->ptr!=null)
temp=temp->ptr;
temp->ptr=head;
}
}

/*declaration of function appin*/


appin()
{
int k,loc,it;
temp=first;
printf("\nEnter location for insert:\t");
scanf("%d",&loc);
printf("\nEnter inserted item\t");
scanf("%d",&it);
for(k=0;k<loc-1;k++)
{
temp=temp->ptr;
if(temp==null)
{
printf("\nNode is lesthan one");
return;
}
}
head=(NODE*)malloc(sizeof(NODE));
head->num=it;
head->ptr=temp->ptr;
temp->ptr=head;
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


/*begde function declaration*/
begde()
{
if(first==null)
{
printf("\nNo node");
return;
}
else
{
head=first;
first=first->ptr;
free(head);
}
}

/*endde function declaration*/


endde()
{
if(first==null)
eturn;
else if(first->ptr==null)
{
emp=first;
ree(temp);
}
else
{
ead=first;
emp=first->ptr;
}
while(temp->ptr!=null)
{
head=temp;
temp=temp->ptr;
}
head->ptr=null;
free(temp);
}

/*declaration of appde function*/


appde()
{
int item;

DEPART MENT OF COMPUTER APPLICATION, SNGCE


printf("\nEnter the item for delete:\t");
scanf("%d",&item);
head=first;
if(first==null)
{
printf("\nList is empty");
return;
}
else
{
emp=head;
hile(temp!=null)
if(head->num==item)
{
temp->ptr=head->ptr;
free(head);
return;
}
temp=head;
head=head->ptr;
}
}
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


output
You want to create a linked list(y/n)y

Enter the data item: 1

Do you want to continue (y/n)y

Enter the data item: 2

Do you want to continue (y/n)y

Enter the data item: 3

Do you want to continue (y/n)n


Choices are
1 Insert
2 Delete
3 Display
4 Exit
Enter your choice: 3
Elements in linked list are
1
2
3

Choices are
1 Insert
2 Delete
3 Display
4 Exit
Enter your choice: 4

DEPART MENT OF COMPUTER APPLICATION, SNGCE


14.Stack implement using linke list?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

We have a structure that contain two part number part and pointer part
There are three pointer variable named top,temp,head .

Step 1: Start
Step 2: Set top=null, read choice to cho
Step 3: Repeat while
1: Ifcho=1 then
push() function call
2: If cho=2 then
pop() function call
3: If cho=3 then
display() function call
4: If cho=4 stop
Step 4: Sotp

Subfunction of push()

Step 1: Start
Step 2: Allocate memory for new node
Step 3: Assign the value to the data field of new node
Step 4: Assign the value to the top to the pointer part of new node
Step 5: Assign head to top
Steo 6: Stop
Subfunction of pop()

Step 1: Start
Step 2: if the stack is not empty ,then,
Step 3: Set temp=top
Step 4: Set top=pointer part of top
Step 5: Free the temp
Step 6: Stop

Subfunction of display()

Step 1: Start
Step 2: Set temp=top
Step 3: If temp!=null then
Step 4: Display number part of temp
Step 5: Set temp=pointer part of temp
Step 6: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

#define null'\0'
#include<stdio.h>
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head,*temp,*top=null;
main()
{
int cho;
clrscr();
while(1)
{
printf("\nChoice are");
printf("\n1 Insert number");
printf("\n2 Delete number");
printf("\n3 Display number");
printf("\n4 Out of program");
printf("\nEnter your choice:\t");
scanf("%d",&cho);
switch(cho)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4:exit(0);
default:printf("\nWrong number");
} /*end switch*/
/*end of main*/
} /*end main*/

/*push function declare*/


push()
{
int i,n;
printf("\nHow many elements enter in to the stack:\t");
scanf("%d",&n);
printf("\nEnter item\n");
for(i=0;i<n;i++)
{
head=(struct node*)malloc(sizeof(struct node));
scanf("%d",&head->num);

DEPART MENT OF COMPUTER APPLICATION, SNGCE


head->ptr=top;
top=head;
} /*end of for*/
} /*end of push function*/

/*pop function declaration*/


pop()
{
if(top==NULL)
printf("\nList is empty");
else
{
temp=top;
printf("\nPoped item:\t%d",temp->num);
top=top->ptr;
free(temp);
} /*end of else*/
} /*end of pop function*/

/*display function declaration*/


display()
{
temp=top;
if(top==NULL)
printf("\nStack is empty");
else
{
printf("\nElements in stack");
while(temp!=NULL)
{
printf("\n%d",temp->num);
temp=temp->ptr;
} /*end of while*/
} /*end of else*/
} /*end of display function*/

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT

Choice are
1 Insert number
2 Delete number
3 Display number
4 Out of program
Enter your choice: 1

How many elements enter in to the stack: 3

Enter item
1
2
3

Choice are
1 Insert number
2 Delete number
3 Display number
4 Out of program
Enter your choice: 3

Elements in stack
3
2
1
Choice are
1 Insert number
2 Delete number
3 Display number
4 Out of program
Enter your choice: 4

DEPART MENT OF COMPUTER APPLICATION, SNGCE


15. Program for queue implement using link list?

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

We have a structure that contain two part number part and pointer part
There are three pointer variable named front,temp,rear.

Step 1: Start
Step 2: Set front=rear=null, read choice to cho
Step 3: Repeat while
1: Ifcho=1 then
insert() function call
2: If cho=2 then
delete() function call
3: If cho=3 then
display() function call
4: If cho=4 stop
Step 4: Sotp

Sub function of insert()

Step 1: Start
Step 2: Allocate memory for new node
Step 3: Assign the value to the data field of new node
Step 4: If rear=null then,
Set rear=temp
Assign value of rear to link field of new node
Else
Step 5: Assign the link value of rear to link field of new node
Step 6: Assign value of new node to the pointer part of rear
Step 7: Set rear=temp
Step 8: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


16.Tree implementation using array.

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM

We have create a node that contain three part they are lchild,
rchild, number part . Temp, root, current nod are the three
structure variable. Set top=-1, max represent the maximum size.

Step 1:
Start
Step 2:
Read choice to n
Step 3:
Create tree, create ()
Step 4:
Repeat while
1: if n=1, preorder (root)
2: if n=2, inorder (root)
3: if n=3, postorder (root)
4: if n=4, stop
Step 5: Stop

Sub function of create ()

Step 1: Start
Step 2: Set ch=y
Step 3: Allocate memory for new node, assign value to number
Part and assign null value to the pointer parts of node.
Step 4: Set root=temp, current node=temp, and push the root
Step 5: Repeat while ch! =n
1: If left pointer part of new node is null, then
2: Read choice to ch ,if ch=!=n ,then go to step 3
3: Allocate memory for new node, assign value to
number Part ,and assign null value to the pointer
parts of node.
4: Set current node=temp, and push the root
Else,currentnode=pop()
Step 6: if we want right child then

1: Allocate memory for new node, assign value to


number Part ,and assign null value to the pointer
parts of node.
2: Set currentnode=temp, and push the root
Step 7: Currentnode=pop()
Step 8: Stop

Sub function of push ()

Step 1: Start

DEPART MENT OF COMPUTER APPLICATION, SNGCE


Step 2: if top= max, stop, otherwise go to step 3
Step 3: top=top+1, stack [top] =current node
Step 4: Stop

Sub function of pop()


Step 1: Start
Step 2: Assign value of stack of top to current node
Step 3: Decrees top value one by one, return current node
Step 4: Stop

Sub function of preorder ()

Step 1: Start
Step 2: if current node! = null, then, Display current node data
Step 3: Call preorder (left pointer part of current node)
Step 4: Call preorder (right pointer part of current node)
Step 5: Stop

Sub function of preorder ()


Step 1: Start
Step 2: if current node! = null, then
Step 3: Call inorder (left pointer part of current node)
Step 4: Display current node data
Step 5: Call inorder (right pointer part of current node)
Step 6: Stop

Sub function of postorder ()


Step 1: Start
Step 2: if current node! = null, then
Step 3: Call post order (left pointer part of current node)
Step 4: Call post order (right pointer part of current node)
Step 5: Display current node data
Step 6: Stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
#define MAX 10
struct node
{
int data;
struct node *lchild,*rchild;
}*temp,*root,*cn;
struct node *stack[MAX];
int top=-1;
struct node *pop();
void push(struct node*cn);
void inorder(struct node*cn);
void postorder(struct node*cn);
void preorder(struct node*cn);
void create();
void main()
{
clrscr();
create();
puts(" Preorder traversal");
preorder(root);
puts("\n Inorder traversal");
inorder(root);
puts("\n Postorder traversal");
postorder(root);
getch();
}
void create()
{
int num;
char ch='y';
printf("Enter the data of root");
scanf("%d",&num);
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->lchild=temp->rchild=NULL;
root=temp;
push(root);
cn=temp;
do
{
while(ch!='n')
{
if(cn->lchild==NULL)

DEPART MENT OF COMPUTER APPLICATION, SNGCE


{
printf("Do you want left child for %d",cn->data);
scanf(" %c",&ch);
if(ch!='n')
{
printf("Enter the left child");
scanf("%d",&num);
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->lchild=temp->rchild=NULL;
cn->lchild=temp;
push(cn);
cn=temp;

}
}
else
cn=pop();
}
printf("Do you want right clild for %d",cn->data);
scanf(" %c",&ch);
if(ch!='n')
{
printf("Enter the right child");
scanf(" %d",&num);
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->lchild=temp->rchild=NULL;
cn->rchild=temp;
cn=temp;
push(cn);
}
cn=pop();
}while(top!=-1);
}
void push(struct node* cn)
{
if(top==MAX)
{
getch();
exit(0);
}
else
{
top++;
stack[top]=cn;
}
}
struct node*pop()
{
struct node*cn;
cn=stack[top];

DEPART MENT OF COMPUTER APPLICATION, SNGCE


top--;
return(cn);
}
void preorder(struct node*cn)
{
if(cn!=NULL)
{
printf("\t%d",cn->data);
preorder(cn->lchild);
preorder(cn->rchild);
}
}
void inorder(struct node*cn)
{
if(cn!=NULL)
{
inorder(cn->lchild);
printf("\t%d",cn->data);
inorder(cn->rchild);
}

}
void postorder(struct node*cn)
{
if(cn!=NULL)
{
postorder(cn->lchild);
postorder(cn->rchild);
printf("\t%d",cn->data);
}
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT

Enter the data of root 50


Do you want left child for 50 y
Enter the left child 20
Do you want left child for 20 y
Enter the left child 30
Do you want left child for 30 n
Do you want right clild for 30 n
Do you want right clild for 20 y
Enter the right child 10
Do you want left child for 10 n
Do you want right clild for 10 n
Do you want right clild for 50 y
Enter the right child 100
Do you want left child for 100 y
Enter the left child 20
Do you want left child for 20 y
Enter the left child 30
Do you want left child for 30 n
Do you want right clild for 30
Do you want right clild for 20 n
Do you want right clild for 100 n

Preorder traversal
50 20 30 10 100 20 30

Inorder traversal
30 20 10 50 30 20 100

Postorder traversal
30 10 20 30 20 100 50

DEPART MENT OF COMPUTER APPLICATION, SNGCE


17.Write a program to implement sequential file access.

DEPART MENT OF COMPUTER APPLICATION, SNGCE


Algorithm
Step1:start
Step2:open a text file in write mode
Step3:read name and no
Step4:write name and no into the opened file
Step5:close the file
Step6:open the file in read mode
Step7:print name and no from the file
Step8:close the file
Step9:stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


Program
#include<stdio.h>
main()
{
FILE *fp;
char name[10];
int age;
clrscr();
fp=fopen("text.txt","w+");
printf("Enter Name & Age");
scanf("%s %d",name,&age);
fprintf(fp,"%s %d",name,age);
fscanf(fp,"%s %d",name,&age);
printf("Name: %s",name);
printf("\nAge: %d",age);
fclose(fp);
getch();
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT
Enter Name & Age : shyma 19
Name: shyma
Age: 19

DEPART MENT OF COMPUTER APPLICATION, SNGCE


18.Write a program to implement random file access.

DEPART MENT OF COMPUTER APPLICATION, SNGCE


ALGORITHM
Step 1: start
Step 2: Read no
Step 3: open a file in read mode
Step 4: use the function fseek
Step 5: display the content of the file after using fseek
Step 6: stop

DEPART MENT OF COMPUTER APPLICATION, SNGCE


PROGRAM
#include<stdio.h>
main()
{
FILE *fp;
int no;
char c;
clrscr();
fp=fopen("text.txt","r");
while((c=getc(fp))!=EOF)
printf("%c",c);
printf("How many positions");
scanf("%d",&no);
fseek(fp,no,0);
while((c=getc(fp))!=EOF)

printf("%c",c);

fclose(fp);
getch();
}

DEPART MENT OF COMPUTER APPLICATION, SNGCE


OUTPUT
shyma 19
How many positions 3
ma 19

DEPART MENT OF COMPUTER APPLICATION, SNGCE

You might also like