キューを作成し、ユーザーが値を追加できるようにするプログラムをCで作成しようとしています。キューは配列を使用して設定されます。どういうわけか私のコードが動作していないと誰かが私のトラブルシューティングを手伝ってくれるのだろうかと思っていました。Cでキューをプログラミングしていますが、コードが間違っていました
#include <stdio.h>
#define CAP 10
//define a struct for our queue
typedef struct _que
{
int arr[CAP];
int front;
int size;
}
que;
void enqueue (que* ptr, int add);
int main(void)
{
int i;
que q1;
q1.front = 0;
q1.size = 0;
char yn = 'n';
//while loop for adding elements
do
{
printf("Enter the value you wish to add\n");
scanf("%d",&i);
enqueue(&q1, i);
printf("Would you like to add any more elements?\n");
scanf("%c",&yn);
}
while (yn == 'y' && q1.size <= CAP);
printf("The current element(s) in the queue are:");
//TODO: print out elements in the queue
for(int start = 0; start <= q1.size; start++)
{
printf("%d",q1.arr[start]);
}
printf("\n");
}
void enqueue(que* ptr, int add)
{
ptr->arr[((ptr->front)+(ptr->size))] = add;
ptr->size += 1;
}
プログラムは、それは「あなたが任意のより多くの要素を追加したいと思います」印刷し、それはちょうどDO-whileループの外にジャンプし、それはまた、キュー内の要素を出力する部分まで正常に実行されます217836276のようなガベージ値が吐き出され、メモリの問題が発生している可能性があります。
解決済み、ありがとう! –