0% found this document useful (0 votes)
25 views

Module 5-4

Uploaded by

pruthviraj001r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Module 5-4

Uploaded by

pruthviraj001r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Department of Computer Science and

Engineering
Preparing Better Computer Professionals for a Real World

Lecture Notes on

DATA STRUCTURES & APPLICATIONS


BCS304

Module 5

Compiled by :
Dr. Bhavanishankar K
Professor
Dept. of CSE
// C program to accept, print and add 2 complex numbers using automatic memory allocation
#include<stdio.h>
typedef struct Complex
{
float real;
int imag;
} CMP;
CMP Read_Cmp();
void Print_Cmp(CMP);
CMP Add_Cmp(CMP, CMP);
int main()
{
CMP C1, C2,C3;
C1=Read_Cmp();
C2=Read_Cmp();
Print_Cmp(C1);
Print_Cmp(C2);
C3=Add_Cmp(C1, C2);
Print_Cmp(C3);
return 0;
}

CMP Read_Cmp(){
CMP C;
printf("Ener real and imaginary parts for the complex number\n");
scanf("%f%d", &C.real, &C.imag);
return C;
}
void Print_Cmp(CMP C)
{
printf("\nThe complex number is \n");
printf("%.2f+i%d", C.real, C.imag);
}
CMP Add_Cmp(CMP C1, CMP C2)
{
CMP C3;
C3.real=C1.real+C2.real;
C3.imag=C1.imag+C2.imag;
return C3;
}
#include<stdio.h>
int main()
{
enum days{sun,mon,tue=5,wed,thur};
printf("%d \t %d\t%d",mon,tue,wed);
enum days dow;
dow=mon;
dow=dow+5;
//mon=mon+2;
printf(" %d ",mon);
}
Stack Implementation
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
void show(int , int []);
int is_full(int );
void push(int,int *,int []);
int is_empty(int );
int pop(int *, int[]);
int main()
{
int stack[MAX],top=-1,ch,ele,ele_del;
for (;;)
{
printf("1: push, 2:pop, 3:show, 4:exit \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the element \n");
scanf("%d",&ele);
if(is_full(top)==0)
{
printf("stack is full \n");
}
else
{
push(ele,&top,stack);
}
break;
case 2:
if(is_empty(top))
{
printf("stack is empty \n");
break;
}
ele_del=pop(&top,stack);
printf("%d \t",ele_del);
break;
case 3:
if(is_empty(top))
{
printf("stack is empty \n");
break;
}
show(top,stack);
break;
case 4:
exit(0);
}
}
return (0);
}
int is_full(int tp)
{
if (tp==(MAX-1))
{
return 0;
}
return 1;
}
void push(int ele,int *tp,int st[])
{
*tp =*tp+1;
st[(*tp)]=ele; //st[++(*tp)];
}
int is_empty(int tp)
{
if(tp==-1)
{
return 1;
}
return 0;
}
int pop(int *tp,int stack[])
{
int del=stack[(*tp)]; // int del=stack[(*tp)--]; return (stack[(*tp)--])
*tp=*tp-1;
return (del);
}
void show(int tp,int stack[])
{
for(int i=tp;i>=0;i--)
{
printf("%d \t",stack[i]);
}
}
Sparse Matrix implementation
#include<stdio.h>
typedef struct sparsematrix
{
int row,col,val;
}smp;

int main()
{
smp mat[10];
int i,j,r,c;
int inmat[10][10],ind=1,nval=0;
printf("enter the rows and column \n");
scanf("%d%d",&r,&c);
printf("enter the values \n");
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&inmat[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(inmat[i][j]==0)
{
continue;
}
mat[ind].row=i;
mat[ind].col=j;
mat[ind++].val=inmat[i][j];
nval++;
}
}
mat[0].row=r;
mat[0].col=c;
mat[0].val=nval;
printf("\t row \tcolumn\t value\n");
printf("[0]\t%d\t%d\t %d\n",mat[0].row,mat[0].col,mat[0].val);
for(i=1;i<=nval;i++)
{
printf("[%d]\t%d\t%d\t %d\n",i,mat[i].row,mat[i].col,mat[i].val);
}
return 0;
}

You might also like