从队列中删除偶数

时间:2015-10-31 11:50:23

标签: c pointers linked-list queue nodes

我试图在C编程中从队列中删除偶数时遇到困难。这是代码:

我的队列数据结构:

typedef struct _listnode
{
    int data;
    struct _listnode *next;
} ListNode; // You should not change the definition of ListNode

typedef struct _linkedlist
{
    int size;
    ListNode *first;
} LinkedList;   // You should not change the definition of LinkedList

typedef struct _queue
{
    LinkedList list;
} Queue;

我正从我的主人那里打来电话:

case 3:
        deleteEven(&q); // You need to code this function
        printf("Result:\n");
        printList(&q.list);
        break;

我要做的是首先,我试图检查队列是否为空。然后我将获得链表头和模数为2,如果得到0意味着它的偶数,然后我将它从队列中出列。

但是,使用这些代码,当我尝试从队列中出列偶数时,它就会停止工作并且不显示任何错误消息。

有谁知道如何解决这个问题?提前谢谢。

1 个答案:

答案 0 :(得分:1)

在您的函数func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { if annotation.isMemberOfClass(MKUserLocation.self) { return nil } let reuseId = "MKAnnotationView" var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKAnnotationView if pinView == nil { pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)} pinView!.canShowCallout = true pinView!.animatesDrop = false // i am not sure about this line so must try this by commenting pinView!.image = UIImage(named: "marker.png") // make sure this name is the name of image return pinView } ' deleteEven循环中执行此操作 -

while

问题1 - 但您还没有为free(odd->list.first->data); //problem 1 odd->list.first->data= odd->list.first->next; //problem 2- assigning pointer type to int 分配内存,它是一个整数变量。如果您尝试odd->list.first->data这样的事情可能会导致您的程序出错(您不应 free 内存未由 free分配或类似功能

您将内存分配给malloc,因此odd指针free

即使您不应在odd循环内free,因为您只分配了一次内存。

问题2 - 您将指针类型指定给while。这可能也是导致此错误的原因之一。

你应该写这样的循环 -

int

3。在调用函数while (!isEmptyQueue(odd)) { enqueue(que, odd->list.first->data); odd->list.first= odd->list.first->next; // increment pointer } free(odd); -

dequeue

dequeue(que->list.first->data); 已通过,但预计会int

其类型为Queue *,但您从中返回int(返回NULL或其他内容以显示失败)。

相关问题