C 堆垛:推,然后弹出,再推到另一个堆垛机
我有一辆“卡车”和一辆“堆垛机”,两者都使用stack。用户可以分配卡车的大小(我使用了malloc)和堆垛机的大小,固定大小为5。 我可以将值推送到卡车上,但如何弹出卡车顶部并将其推到堆垛机中C 堆垛:推,然后弹出,再推到另一个堆垛机,c,stack,C,Stack,我有一辆“卡车”和一辆“堆垛机”,两者都使用stack。用户可以分配卡车的大小(我使用了malloc)和堆垛机的大小,固定大小为5。 我可以将值推送到卡车上,但如何弹出卡车顶部并将其推到堆垛机中 struct box { int item; int bin; int top; }*boxs,*stacker; int i; int num; int topt = -1; int tops = -1; void main() { void truck();
struct box
{
int item;
int bin;
int top;
}*boxs,*stacker;
int i;
int num;
int topt = -1;
int tops = -1;
void main()
{
void truck();
void display();
int op;//option
char ch;//choice
do{
system("cls");
printf("\n Stack:-");
printf("\n\n 1.<Insert>");
printf("\n 2.<Display>");
printf("\n\n Option: ");
scanf("%d", &op);fflush(stdin);
switch(op)
{
case 1:truck();break;
case 2:display();break;
default:printf("\n Invalid Choice!!!");break;
} printf("\n\n\n Continue?(Y/N): ");
scanf("%c", &ch); fflush(stdin);
}while(ch=='y' || ch=='Y');
return;
}
void truck()
{
struct box e;
int a,temp;
printf("Enter Number of Boxes to unload from the Truck: ");
scanf("%d", &num);
boxs= (struct box*)malloc(sizeof (struct box));
temp=num;
/*tempt->tops=copy-1;*/
for(a=0; a<num;a++)
{
topt++;
temp--;
/*tempt[copy-a].tops--;*/
printf("\n1. Enter Item ID: ");
scanf("%d", &e.item);
boxs[a].item=e.item;fflush(stdin);
/*tempt[copy-a].item=e.item;*/
printf("\n2. Enter Bin ID: ");
scanf("%d", &e.bin);
boxs[a].bin=e.bin;fflush(stdin);
//tempt[copy-a].bin=e.bin;
}
stackers();
}
void display()
{
for(i=0;i<=topt;i++)
{
printf("\n%d",boxs[i].item);
printf("\n%d",boxs[i].bin);
}
}
void stackers()
{
}
struct-box
{
国际项目;
国际银行;
int top;
}*箱子,*堆垛机;
int i;
int-num;
int-topt=-1;
int tops=-1;
void main()
{
空卡车();
void display();
int op;//选项
char ch;//选择
做{
系统(“cls”);
printf(“\n堆栈:-”);
printf(“\n\n 1.”);
printf(“\n 2.”);
printf(“\n\n选项:”);
scanf(“%d”,&op);fflush(标准输入法);
开关(op)
{
案例1:卡车();断裂;
案例2:显示();中断;
默认值:printf(“\n无效选项!!!”);中断;
}printf(“\n\n\n是否继续?”);
scanf(“%c”和&ch);fflush(标准输入法);
}而(ch='y'| ch='y');
返回;
}
空卡车()
{
结构盒e;
INTA,温度;
printf(“输入要从卡车上卸下的箱子数量:”);
scanf(“%d”和&num);
boxs=(结构框*)malloc(sizeof(结构框));
温度=数值;
/*诱惑->顶部=复制-1*/
对于(a=0;通过填写{
和}之间的内容开始)
。您已经将物品推到卡车上。请将其从卡车向后推到堆栈。我强烈建议您将其推到,因为此代码中有很多错误,但没有添加更多内容。这是否仍然有效?代码到处都有内存泄漏。truck()
每次调用它都会泄漏内存,因为它总是分配给boxs
。您只为一个项目分配空间,但您访问boxs[a]
使用a>0
。首先修复您的代码。将malloc
移动到main中,使其分配一次。它无法完全工作。目前,我只能将其添加到卡车中,但无法添加到堆垛机中