Showing posts with label stack operations. Show all posts
Showing posts with label stack operations. Show all posts
Friday, November 11, 2011
Matrix Addition in C laguage
#include<stdio.h>
int main(){
int a[3][3],b[3][3],c[3][3],i,j;
printf("Please Enter values for first Matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Please Enter values for second Matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
// Addition operations
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\n Result after Addition of two matrix is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
Monday, October 4, 2010
Advance Stack Using C language
Stack using C language with advance options.
#include <stdio.h>
#include<stdlib.h>
#define MAX 9
void push(int stack[],int *top,int item)
{
if(*top==MAX)
{
printf("\nOver Flow.....");
return;
}
else
{
*top=*top+1;
stack[*top]=item;
}
}
int pop(int stack[],int *top)
{
int a;
if(*top==-1)
{
printf("\nUnder flow......");
return -1;
}
else
{
a=stack[*top];
*top=*top-1;
return a;
}
}
void display(int stack[],int top)
{
int i;
for(i=0;i<=top;i++)
{
printf("\nPosition: %d\tVaue: %d",i,stack[i]);
}
}
void main()
{
int stack[10];
int top=-1;
int c,item;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter item to insert: ");
scanf("%d",&item);
push(stack,&top,item);
break;
case 2:
item=pop(stack,&top);
if(item!=-1)
printf("\n%d is deleted",item);
break;
case 3:
display(stack,top);
break;
case 4:
exit(0);
default:
printf("\nWrong choice");
}
}
}
#include <stdio.h>
#include<stdlib.h>
#define MAX 9
void push(int stack[],int *top,int item)
{
if(*top==MAX)
{
printf("\nOver Flow.....");
return;
}
else
{
*top=*top+1;
stack[*top]=item;
}
}
int pop(int stack[],int *top)
{
int a;
if(*top==-1)
{
printf("\nUnder flow......");
return -1;
}
else
{
a=stack[*top];
*top=*top-1;
return a;
}
}
void display(int stack[],int top)
{
int i;
for(i=0;i<=top;i++)
{
printf("\nPosition: %d\tVaue: %d",i,stack[i]);
}
}
void main()
{
int stack[10];
int top=-1;
int c,item;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter item to insert: ");
scanf("%d",&item);
push(stack,&top,item);
break;
case 2:
item=pop(stack,&top);
if(item!=-1)
printf("\n%d is deleted",item);
break;
case 3:
display(stack,top);
break;
case 4:
exit(0);
default:
printf("\nWrong choice");
}
}
}
Stack using C Language
This is a very simple stack progam using C language
#include <stdio.h>
#include<stdlib.h>
#define MAX 9
int stack[10];
int top=-1;
void push(int item)
{
if(top==MAX)
{
printf("\nOver Flow.....");
return;
}
else
{
top=top+1;
stack[top]=item;
}
}
int pop()
{
int a;
if(top==-1)
{
printf("\nUnder flow......");
return -1;
}
else
{
a=stack[top];
top=top-1;
return a;
}
}
void display()
{
int i;
for(i=0;i<=top;i++)
{
printf("\nPosition: %d\tVaue: %d",i,stack[i]);
}
}
void main()
{
int c,item;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter item to insert: ");
scanf("%d",&item);
push(item);
break;
case 2:
item=pop();
if(item!=-1)
printf("\n%d is deleted",item);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong choice");
}
}
}
#include <stdio.h>
#include<stdlib.h>
#define MAX 9
int stack[10];
int top=-1;
void push(int item)
{
if(top==MAX)
{
printf("\nOver Flow.....");
return;
}
else
{
top=top+1;
stack[top]=item;
}
}
int pop()
{
int a;
if(top==-1)
{
printf("\nUnder flow......");
return -1;
}
else
{
a=stack[top];
top=top-1;
return a;
}
}
void display()
{
int i;
for(i=0;i<=top;i++)
{
printf("\nPosition: %d\tVaue: %d",i,stack[i]);
}
}
void main()
{
int c,item;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter item to insert: ");
scanf("%d",&item);
push(item);
break;
case 2:
item=pop();
if(item!=-1)
printf("\n%d is deleted",item);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong choice");
}
}
}











