更正它現在的作用,謝謝大家。用C填寫隊列C
好了,所以我現在已經更新了我的主文件到
的main.c
#include "queue.h"
int main(){
int i;
int* dataPtr;
int number;
QUEUE* numbers;
numbers = createQueue();
printf("Please enter the 10 numbers you want to know the sum and average of.\n");
for (i = 0; i < 10; i++){
printf("Number %d: ", i+1);
scanf("%d", &number);
*dataPtr = number;
enqueue(numbers, dataPtr);
}
system("Pause");
return 0;
}
但我的程序崩潰時,它跑了
有誰知道爲什麼發生這種情況?
原貼
我有麻煩搞清楚如何填補與C.用戶輸入的隊列任何人都可以點我在正確的方向。
queue.h:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//Queue ADT Type Definitions
typedef struct node{
void* dataPtr;
struct node* next;
} QUEUE_NODE;
typedef struct{
QUEUE_NODE* front;
QUEUE_NODE* rear;
int count;
} QUEUE;
//Prototype Declarations
QUEUE* createQueue (void);
QUEUE* destroyQueue (QUEUE* queue);
bool dequeue (QUEUE* queue, void** itemPtr);
bool enqueue (QUEUE* queue, void* itemPtr);
bool queueFront (QUEUE* queue, void** itemPtr);
bool queueRear (QUEUE* queue, void** itemPtr);
int queueCount (QUEUE* queue);
bool emptyQueue (QUEUE* queue);
bool fullQueue (QUEUE* queue);
//End of Queue ADT definitions
Queue.c
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
QUEUE* createQueue (void){
QUEUE* queue;
queue = (QUEUE*) malloc (sizeof (queue));
if (queue){
queue->front = NULL;
queue->rear = NULL;
queue->count = 0;
}
return queue;
}
bool enqueue (QUEUE* queue, void* itemPtr){
QUEUE_NODE* newPtr;
if(!(newPtr = (QUEUE_NODE*)malloc(sizeof(QUEUE_NODE))))
return false;
newPtr->dataPtr = itemPtr;
newPtr->next = NULL;
if (queue->count == 0)
queue->front = newPtr;
else
queue->rear->next = newPtr;
(queue->count)++;
queue->rear = newPtr;
return true;
}
bool dequeue (QUEUE* queue, void** itemPtr){
QUEUE_NODE* deleteLoc;
if (!queue->count)
return false;
*itemPtr = queue->front->dataPtr;
deleteLoc = queue->front;
if(queue->count == 1)
queue->rear = queue->front = NULL;
else
queue->front = queue->front->next;
(queue->count)--;
free (deleteLoc);
return true;
}
bool queueFront (QUEUE* queue, void** itemPtr){
if (!queue->count)
return false;
else{
*itemPtr = queue->front->dataPtr;
return true;
}
}
bool queueRear (QUEUE* queue, void** itemPtr){
if (!queue->count)
return true;
else{
*itemPtr = queue->rear->dataPtr;
return false;
}
}
bool emptyQueue (QUEUE* queue){
return (queue->count == 0);
}
bool fullQueue(QUEUE* queue){
QUEUE_NODE* temp;
temp = (QUEUE_NODE*)malloc(sizeof(*(queue->rear)));
if (temp){
free (temp);
return true;
}
return false;
}
int queueCount(QUEUE* queue){
return queue->count;
}
QUEUE * destroyQueue (QUEUE* queue){
QUEUE_NODE* deletePtr;
if (queue){
while (queue->front != NULL){
free (queue->front->dataPtr);
deletePtr = queue->front;
queue->front = queue->front->next;
free(deletePtr);
}
free (queue);
}
return NULL;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
int main(){
int number;
int *dataPtr;
QUEUE* numbers;
numbers = createQueue();
printf("Please enter the 10 numbers you want to know the sum and average of.\n");
for (int i = 0, i < 10; i++){
if (!(dataPtr = (int*) malloc (sizeof (int))))
printf("Overflow trying to fill queues.\n"), exit(100);
system("Pause");
return 0;
}
你有任何代碼示例將是有用的:) – Jedidja
你如何發佈代碼標籤? – shinjuo
我的意思是你如何發佈代碼塊? – shinjuo