1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
# include<stdio.h>
# include<string.h>
# include<ctype.h>
# define size 5
int count0=0;
int top = -1;
int top0 =-1;
int flag = 0;
int flag0 = 0;
int BItem[size][size];
int stackItem[size];
int stackBin[size];
void push0(int *, int ,int *,int);
void push(int *, int ,int *,int);
int pop0(int * ,int *);
int pop(int * ,int *);
void display(int *);
void display0(int *);
void push0(int ItemID0[], int Item0, int BinID0[],int Bin0 )
{
if(top0 ==(size-1))
flag0 = 0;
else
{
flag0 = 1;
++top0;
ItemID0[top0] = Item0;
BinID0[top0]=0;
}
}
void push(int ItemID[], int Item, int BinID[],int Bin )
{
if(top ==(size-1))
flag = 0;
else
{
flag = 1;
++top;
ItemID[top] = Item;
BinID[top]=Bin;
}
}
int pop(int ItemID[],int BinID[])
{
int popped_element,popped_element1;
if(top == -1)
{
popped_element = 0;
popped_element1 = 0;
flag = 0;
}
else
{
flag = 1;
popped_element = ItemID[top];
popped_element1 = BinID[top];
--top;
}
return (popped_element,popped_element1);
}
void display(int ItemID[],int BinID[])
{
int i;
if(top == -1)
{
printf("\n Stack is empty");
}
else
{
printf("\n ItemID\t\tBinID");
for(i = top; i >= 0; --i)
printf("\n %d \t\t %d", ItemID[i], BinID[i]);
}
}
void display0(int ItemID0[],int BinID0[])
{
int i;
if(top0 == -1)
{
printf("\n Stack is empty");
}
else
{
printf("\n ItemID\t\tBinID");
for(i = top0; i >= 0; --i)
printf("\n %d \t\t %d", ItemID0[i], BinID0[i]);
}
}
void main()
{
int item,bin;
char choice;
int q = 0;
do
{
printf("\nEnter ID->i ; Pop ID->p ;Quit->q:");
printf("\nInput the choice : ");
do
{
choice = getchar();
choice =tolower(choice);
}while(strchr("ipq",choice)==NULL);
printf("Your choice is: %c",choice);
switch(choice)
{
case 'i' :
printf("\n Input the element for the ItemID:");
scanf("%d", &item);
do{
printf("\n Input the element for the binID:");
scanf("%d", &bin);
}while (bin!=0 && bin!=1 && bin!=2 && bin!=3 && bin!=4 );
switch(bin)
{
case 0:
push0(stackItem, item,stackBin, bin);
if((flag0)&& (bin==0))
{
printf("\n After inserting ");
display0(stackItem,stackBin);
count0=count0+1;
if(top0 == (size-1))
printf("\n Stack is full");
}
else
printf("\n Stack overflow after pushing");
break;
case 1:
push(stackItem, item,stackBin, bin);
if((flag)&& (bin==1))
{
printf("\n After inserting ");
display(stackItem,stackBin);
if(top == (size-1))
printf("\n Stack is full");
}
else if (count0==5)
printf("\n Stack overflow after pushing==5");
else
printf("\n Stack overflow after pushing");
break;
}
break;
case 'p' :
item = pop(stackItem,stackBin);
bin = pop(stackItem,stackBin);
if(flag)
{
printf("\n Item is popped: %d", item);
printf("\n Rest Item in stack is as follows:\n");
display(stackItem,stackBin);
}
else
printf("\n Stack underflow" );
break;
case 'q':
q = 1;
}
} while(!q);
system("pause");
return 0;
} |