My Profile

I, Das ShrikKrishna J. MCA III IMSCD&R, Ahmednagar.

Monday 15 August 2011

Program of sparse matrix for 3-tuple method using array


/* Program of sparse matrix for 3-tuple method using array*/
#include<stdio.h>
#define srow 50
#define mrow 20
#define mcolumn 20
main()
{
int mat[mrow][mcolumn],sparse[srow][3];
int i,j,nzero=0,mr,mc,sr,s;
printf("Enter number of rows : ");
scanf("%d",&mr);
printf("Enter number of columns : ");
scanf("%d",&mc);
for(i=0;i<mr;i++)
for(j=0;j<mc;j++)
{
printf("Enter element for row %d,column %d : ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
printf("Entered matrix is : \n");
for(i=0;i<mr;i++)
{
for(j=0;j<mc;j++)
{
printf("%6d",mat[i][j]);
if(mat[i][j]!=0)
nzero++;
}
printf("\n");
}
sr=nzero+1;
sparse[0][0]=mr;
sparse[0][1]=mc;
sparse[0][2]=nzero;
s=1;
for(i=0;i<mr;i++)
for(j=0;j<mc;j++)
{
if(mat[i][j]!=0)
{
sparse[s][0]=i+1;
sparse[s][1]=j+1;
sparse[s][2]=mat [i][j];
s++;
}
}
printf("Sparse matrix is :\n");
for(i=0;i<sr;i++)
{
for(j=0;j<3;j++)
printf("%5d",sparse[i][j]);
printf("\n");
}
}/*End of                main()*/

Program of stack using array


/* Program of stack using array*/
#include<stdio.h>
#define MAX 5
int top = -1;
int stack_arr[MAX];
main()
{
int choice;
while(1)
{
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
push()
{
int pushed_item;
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
printf("Enter the item to be pushed in stack : ");
scanf("%d",&pushed_item);
top=top+1;
stack_arr[top] = pushed_item;
}
}/*End of push()*/
pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
{
printf("Popped element is : %d\n",stack_arr[top]);
top=top-1;
}
}/*End of pop()*/
display()
{
int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
for(i = top; i >=0; i--)
printf("%d\n", stack_arr[i] );
}
}/*End of    display()*/

Program of stack using linked list

 
/* Program of stack using linked list*/
# include<stdio.h>
# include<malloc.h>
struct node
{
int info;
struct node *link;
} *top=NULL;
main()
{
int choice;
while(1)
{ printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ") ;
scanf("%d", &choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main() */
push()
{
struct node *tmp;
int pushed_item;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the new value to be pushed on the stack : ");
scanf("%d",&pushed_item);
tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}/*End of push()*/
pop()
{
struct node *tmp;
if(top == NULL)
printf("Stack is empty\n");
else
{ tmp=top;
printf("Popped item is %d\n",tmp->info);
top=top->link;
free(tmp);
}
}/*End of pop()*/
display()
{ struct node *ptr;
ptr=top;
if(top==NULL)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
while(ptr!= NULL)
{
printf("%d\n",ptr->info);
ptr = ptr->link;
}/*End of         while */
}/*End of           else*/
}/*End of      display()*/

Addition Of two Polynomial

#include<stdio.h>
struct node {
int coef,pow;
struct node *next;
};
typedef struct node *POLY;
main()
{
POLY p1=NULL,p2=NULL,p3=NULL;
POLY create(POLY,int);
void display (POLY);
POLY add (POLY p1,POLY p2,POLY p3);
int n;
clrscr();
printf("/********* Addition of polynomials *************/\n");
printf("\n How many terms in the first polynomial ?");
scanf("%d",&n);
p1=create(p1,n);
display(p1);
printf("\n How many terms in the second polynomial ?");
scanf("%d",&n);
p2=create(p2,n);
display(p2);
p3=add(p1,p2,p3);
printf("\n the addition is : ");
display(p3);
getch();
return(0);
}
POLY create(POLY p1,int n)
{
POLY temp=p1,newnode;
int i;
printf("\n enter the termsin descending order of power :\n");
for(i=0;i<n;i++)
{
newnode=(POLY) malloc(sizeof(struct node));
newnode->next=NULL;
printf("\n coeff= :");
scanf("%d",&newnode->coef);
printf("\n power= : ");
scanf("%d",&newnode->pow);
if (p1==NULL)
{
temp=p1=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
return p1;
}
void display (POLY p1)
{
POLY temp=p1;
while(temp)
{
printf("+%dX^%d ",temp->coef,temp->pow);
temp=temp->next;
}
}
POLY add(POLY p1,POLY p2,POLY p3)
{
POLY t1=p1,t2=p2,t3=p3,newnode;
int i;
while (t1 && t2)
{
newnode=(POLY) malloc(sizeof (struct node));
newnode->coef=NULL;
if(t1->pow>t2->pow)
{
newnode->pow=t1->pow;
newnode->coef=t1->coef;
t1=t1->next;
}
else
if (t1->pow<t2->pow)
{
newnode->pow=t2->pow;
newnode->coef=t2->coef;
t2=t2->next;
}
else
{
newnode->pow=t1->pow;
newnode->coef=t1->coef+t2->coef;
t1=t1->next;
t2=t2->next;
}
/********* link newnode *****/
if (t3==NULL)
{
p3=t3=newnode;
}
else
{
t3->next=newnode;
t3=newnode;
}
}
while(t1)
{
newnode=(POLY) malloc(sizeof (struct node));
newnode->next=NULL;
newnode->pow=t1->pow;
newnode->coef=t1->coef;
t3->next=newnode;
t3=newnode;
t1=t1->next;
}
while(t2)
{
newnode=(POLY) malloc(sizeof(struct node));
newnode->next=NULL;
newnode->coef=t2->coef;
t3->next=newnode;
t3=newnode;
t2=t2->next;
}
return p3;
}
 

Infix to Prefix Conversion

/* Infix to Prefix Conversion */
# include<stdio.h>
# include<conio.h>
# include<string.h>
# include<stdlib.h>
# define MAX 30
int rear=-1,top=-1;
char stk[30],que[30],ch,chout;
int stkempty (int top)
{
if(top==-1)
{
return(1);
}
else
{
return(0);
}
}
int stkfull (int top)
{
if(top==MAX-1)
{
return(1);
}
else
{
return(0);
}
}
int push(char ch)
{
if(stkfull(top))
{
printf("\nstack is full.cannot push into stack");
return(top);
}
else
if(top==-1)
{
top++;
stk[top]='~';
}
top++;
stk[top]=ch;
return(top);
}
int pop()
{
if(stkempty(top))
{
printf("");
}
else
if(stk[top]!='~')
{
chout=stk[top];
top--;
}
return(chout);
}
int qempty (int rear)
{
if(rear==-1)
{
return(1);
}
else
{
return(0);
}
}
int qfull (int rear)
{
if(rear==MAX-1)
{
return(1);
}
else
{
return(0);
}
}
int addq(char ch)
{
if(qfull(rear))
{
printf("\nqueue is full.cannot add in queue");
return(rear);
}
else
{
rear++;
que[rear]=ch;
}
return(rear);
}
int isp(char ch)
{
switch(ch)
{
case '+' :
case '-' : return(1);
case '*' :
case '/' : return(2);
case '^' : return(3);
case '(' : return(0);
case '~' : return(-99);
}
return 0;
}
int icp(char ch)
{
switch(ch)
{
case '+' :
case '-' : return(1);
case '*' :
case '/' : return(2);
case '^' : return(4);
case '(' : return(4);
}
return 0;
}
int display(int len)
{
int i;
for(i=len;i>=0;)
{
if(que[i]=='('||que[i]==')')
{
i--;
}
else
{
printf(" %c",que[i]);
i--;
}
}
return;
}
void prefix()
{
char str[10],ch;
int i,len;
printf("\nEnter the expression:");
scanf("%s",str);
len=strlen(str);
printf("\n");
for(i=len;i>=0;i--)
{
ch=str[i];
if(ch>='a'&&ch<='z')
{
addq(ch);
}
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^'||ch=='('||ch==')')
{
if(ch=='(')
{
while(chout!=')')
{
chout=pop();
addq(chout);
}
}
if(icp(ch)>isp(stk[top]))
{
push(ch);
}
else
{
chout=pop();
addq(chout);
push(ch);
}
}
}
while(stk[top]!='~')
{
chout=pop();
addq(chout);
}
display(len);
}
void main()
{
clrscr();
prefix();
getch();
}

Infix To Postfix Conversion

# include<stdio.h>
# include<conio.h>
# include<string.h>
# include<stdlib.h>
# define MAX 30
int rear=-1,top=-1;
char stk[30],que[30],ch,chout;
int stkempty (int top)
{
if(top==-1)
{
return(1);
}
else
{
return(0);
}
}
int stkfull (int top)
{
if(top==MAX-1)
{
return(1);
}
else
{
return(0);
}
}
int push(char ch)
{
if(stkfull(top))
{
printf("\nstack is full cannot push into stack ");
return(top);
}
else
if(top==-1)
{
top++;
stk[top]='~';
}
top++;
stk[top]=ch;
return(top);
}
int pop()
{
if(stkempty(top))
{
printf("\nstack is empty cannot pop from stack ");
}
else
if(stk[top]!='~')
{
chout=stk[top];
top--;
}
return(chout);
}
int qempty (int rear)
{
if(rear==-1)
{
return(1);
}
else
{
return(0);
}
}
int qfull (int rear)
{
if(rear==MAX-1)
{
return(1);
}
else
{
return(0);
}
}
int addq(char ch)
{
if(qfull(rear))
{
printf("\nqueue is full cannot add in queue ");
return(rear);
}
else
{
rear++;
que[rear]=ch;
}
return(rear);
}
int isp(char ch)
{
switch(ch)
{
case '+' :
case '-' : return(1);
case '*' :
case '/' : return(2);
case '^' : return(3);
case '(' : return(0);
case '~' : return(-99);
}
return 0;
}
int icp(char ch)
{
switch(ch)
{
case '+' :
case '-' : return(1);
case '*' :
case '/' : return(2);
case '^' : return(4);
case '(' : return(4);
}
return 0;
}
 
int display(int len)
{
int i;
printf("\n Postfix conversion is \n");
for(i=0;i<=len;i++)
{
if(que[i]!='('&&que[i]!=')')
printf(" %c",que[i]);
}
return;
}
 
void postfix()
{
char str[10],ch;
int i,len;
printf("\nEnter the expression: ");
scanf("%s",str);
len=strlen(str);
printf("\n");
for(i=0;i<=len;i++)
{
ch=str[i];
if(ch>='a'&&ch<='z')
{
addq(ch);
}
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^'||ch=='('||ch==')')
{
if(ch==')')
{
while(chout!='(')
{
chout=pop();
addq(chout);
}
}
if(icp(ch)>isp(stk[top]))
{
push(ch);
}
else
{
chout=pop();
addq(chout);
push(ch);
}
}
}
while(stk[top]!='~')
{
chout=pop();
addq(chout);
}
display(len);
}
void main()
{
clrscr();
postfix();
getch();
}

Decision Of Graph

/* Graph */
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
# include <process.h>
# include <alloc.h>
# define MAX 50
static int mat[MAX][MAX],que[MAX],visit[MAX];
int stk[MAX];
int top=-1,rear=-1,front=-1;
int vert;
void push(int ele)
{ if(top==MAX)
{ printf("\nstack is full cannot push into stack"); }
else { top++; stk[top]=ele; }
}
int pop()
{ int ele;
if(top<=-1)
{ printf("\nstack is empty cannot pop from stack"); }
else { ele=stk[top]; top--; }
return(ele);
}
void addq(int ele)
{ if(rear==MAX)
{ printf("\nqueue is full.cannot add in queue"); }
else
{ que[++rear]=ele; }
}
int delq()
{ int ele;
if(front==rear)
{ printf("\nqueue is empty cannot delete from stack");
return(rear);
}
else
{ ele=que[++front]; }
return(ele);
}
void print()
{ int i,j;
printf("\nThe adjacency matrix is::\n");
for(i=0;i<vert;i++)
{ for(j=0;j<vert;j++)
{ printf(" %d",mat[i][j]); }
}
}
 
 
 
 
 
 
void create()
{ int i,j,ans,direct;
printf("\nEnter:: 1: for directed matrix\n\t0:for non-directed:: ");
scanf("%d",&direct);
printf("\nEnter the no. of vertices::");
scanf("%d",&vert);
for(i=0;i<vert;i++)
{ while(1)
{ printf("\n\nVERTEX %d",i);
printf("\nEnter 1 if there is any adjacent vertex for %d:: ",i);
scanf("%d",&ans);
if(ans!=1)
{ break; }
else { printf("\nEnter the adjacent vertex:: ");
scanf("%d",&j);
mat[i][j]=1;
if(direct==0)
{ mat[j][i]=1; }
}
}
}
}
void bfs()
{ int start,ele,j,flag=-1;
printf("\nEnter the vertex from which you want to start:: ");
scanf("%d",&start);
addq(start);
visit[start]=1;
while(front!=rear)
{ ele=delq();
printf("\n%d ",ele);
for(j=0;j<vert;j++)
{ if(mat[ele][j]==1 && visit[j]!=1)
{ addq(j); visit[j]=1; }
}
}
for(j=0;j<vert;j++)
{ if(visit[j]!=1) { flag=j;}
}
if(flag!=-1)
{ printf("\nThe graph is not a connected graph!"); }
}
void main()
{ create();
print();
bfs();
getch();
}

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls