我的代码是关于实现堆栈和队列的功能。如果你想知道为什么我不在程序中使用struct,这是因为我们的教师只允许我们使用指针,并且我使用指针有点困惑。它还没有完成,尽可能多地尝试一次完成一个功能,而不是跳到下一个功能。如何使用C中的指针增加一个变量?
#include <stdio.h>
int container[5];
int len=0;
据我了解我的功能插件的这一部分,“LEN”是假设改变,因为它的每递增一次的功能是使用
int insert(int container[],int len)
{
int i,j,content,*incre;
incre=&len;
for(i=*incre;i<*incre+1;i++)
{
printf("Enter number:\n");
scanf("%d",&content);
container[i]=content;
}
(*incre)++;
}
int printcontent(int container[])
{
int i;
printf("Content of container:\n");
for(i=0;i<len+1;i++)
{
printf("%d ",container[i]);
}
printf("\n");
}
这部分询问用户是否添加容器中的内容或只是将其删除。他们一次只能添加或删除一个。
int stack(int container[])
{
while(1)
{
int choice;
printf("What do you wanna do?\n");
printf("1.Insert\n2.Pop\n3.isEmpty\n4.Exit\n");
scanf("%d", &choice);
我检查了len的值,当我第一次使用insert时,它仍然是0而不是1。
if (choice==1)
{
insert(container,len);
printf("Value of len:%d\n",len);
printcontent(container);
}
else if(choice==4)
{
break;
}
}
}
main()
{
while(1)
{
int choice;
printf("What do you want to do?\n");
printf("1.Stack\n2.Queue\n3.Exit\n");
scanf("%d",&choice);
if(choice==1)
{.
stack(container);
}
else if(choice==3)
{
break;
}
}
}
非常感谢您的帮助。
对不起 - 我误解了你的问题首先。在下面编辑我的答案,实际给你一些可能对你有帮助的事情。 :) – mxscho