Impliment the stack using structure

Wednesday, 11 September 2013

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define STACK_SIZE 100
struct stack
{
int top; int items[STACK_SIZE];
};
struct stack s;
int empty(struct stack *ps)
{
if(ps->top==-1)
{
printf("Empty");
return 1;
}
else
return 0;
}
int pop(struct stack *ps)
{
int x;
if(empty(ps))
{
printf("underflow");
return 0;
}
x=ps->items[ps->top];
ps->top--;
return x;
}
void push(struct stack *ps,int x)
{
if(ps->top==STACK_SIZE-1)
{
printf("overflow");
return;
}
ps->top++;
ps->items[ps->top]=x;
}
void display(struct stack *ps)
{
int t=ps->top;
if(empty(ps))
{
printf("stack empty");
return;
}
while(t!=-1)
{
printf("\n %d",ps->items[t]);
t--;
}}
void main()
{
int choice,x;
struct stack s;
clrscr();
s.top=-1;
do
{
printf("\n Insert 1:PUSH");
printf("\n Insert 2:POP");
printf("\n Insert 3:DISPLAY");
printf("\n Insert 4:EXIT");
printf("\n Enter ur Choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter element to insert");
scanf("%d",&x);
push(&s,x);
break;
case 2:
x=pop(&s);
if(x)
printf("\n Deleted element is: %d",x);
break;
case 3:
display(&s);
break;
case 4:
exit(0);
}}
while(choice!=4);
getch();
}


OUTPUT-



C Program to find union of two sets

Monday, 9 September 2013

#include<stdio.h>
#include<conio.h>
void main()
{
 int A[10],B[10],C[10],i,j,k=0,n,m,flag=0;
 clrscr();
 printf("Enter the size of array A");
 scanf("%d",&n);
 printf("Enter the element of First array A");
   for(i=0;i<n;i++)
     {
       scanf("%d",&A[i]);
     }
     printf("Enter the size of array B");
 scanf("%d",&m);
 printf("Enter the elements of array B");
   for(j=0;j<m;j++)
     {
       scanf("%d",&B[j]);
     }
for(i=0;i<n;i++)
 {
  C[k]=A[i];
  k++; }
for(i=0;i<n;i++)
 {
 flag=0;
  for(j=0;j<m;j++)
   {
    if(B[i]==C[j])
     {
     flag==1;
     break;
     }
   }
if(flag==0)
 {
  C[k]=B[i];
  k++;
 }}
printf("\n union\n");
for(i=0;i<k;i++)
{
printf("%d",C[i]);
}
getch();}
 
OUTPUT:-
Related Posts Plugin for WordPress, Blogger...
Related Posts Plugin for WordPress, Blogger...
 

Most Reading

Labels