繁体   English   中英

如何在这个链表中使用正确的指针

[英]How to use correct pointer in this linked list

这是我从 codedope.com 获得的代码,我进行了一些修改以了解带有链表的队列 ADT 是如何工作的。

但是,我在此代码上使用指针卡住了。 我只是尝试打印出队列中的所有元素,尝试更改参数。 而且我无法从队列中转到下一个节点,我是否尝试了此代码无法实现的操作? 因为它是这样设计的或这里的任何解决方案?

我需要您的帮助或提示如何使用正确的指针跟随指针。

'''

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define FULL 10


typedef struct node{
    int data;
    struct node *next;
} NodeT;


typedef struct QueueRep{
    int length;
    NodeT *head;
    NodeT *tail;
} QueueRep;

typedef struct QueueRep *queue;


void initialize(queue q){
    q->length = 0;
    q->head = NULL;
    q->tail = NULL;
}


int isempty(queue q) {
    return(q->tail == NULL);
}

void enqueue(QueueRep *q, int value) {
    if(q->length < FULL) {
        NodeT *tmp;
        tmp = malloc(sizeof(NodeT));
        tmp->data = value;
        tmp->next = NULL;
        if(!isempty(q)) {
            q->tail->next = tmp;
            q->tail = tmp;
        } else {
            q->head = q->tail = tmp;
        } 
        q->length++;
    } else {
        printf("List is full\n");
    }
}

int dequeue(QueueRep *q) {
    NodeT *tmp;
    int n = q->head->data;
    tmp = q->head;
    q->head = q->head->next;
    q->length--;
    free(tmp);
    return(n);
}

/* original display function
void display(NodeT *head)
{
    if(head == NULL)
    {
        printf("NULL\n");
    }
    else
    {
        printf("%d\n", head -> data);
        display(head->next);
    }
}
*/

/* I would like to print like this !!!! */
void display(queue q) {
    NodeT *p = q->head;
    if(p->data == NULL) {
        printf("NULL\n");
    } else {
        printf("%d ", p->data);
        display(q->head); //wrong pointer? how can your fix here?
        
    }
    
}



int main() {
    QueueRep *q;
    q = malloc(sizeof(QueueRep));
    initialize(q);
    enqueue(q,10);
    enqueue(q,20);
    enqueue(q,30);
    printf("Queue before dequeue\n");
    //display(q->head); 
    display(q); //this is What I am trying to use
    dequeue(q);
    printf("\nQueue after dequeue\n");
    //display(q->head);
    display(q); //this is What I am trying to use
    return 0;
}

'''

这个函数定义

/* I would like to print like this !!!! */
void display(queue q) {
    NodeT *p = q->head;
    if(p->data == NULL) {
        printf("NULL\n");
    } else {
        printf("%d ", p->data);
        display(q->head); //wrong pointer? how can your fix here?
    }
}

是错误的,不得编译。

例如结构node的数据成员data不是指针。 所以这个 if 语句

    if(p->data == NULL) {

无效,因为将int ( p->data ) 类型的对象与空指针NULL

在这份声明中

display(q->head); //wrong pointer? how can your fix here?

该函数使用类型struct node的参数调用,而相应的函数参数具有类型queue 因此,编译器将再次为该语句发出错误。

您可以将该功能拆分为两个功能。 例如

void display_list( const NodeT *head )
{
    if ( !head )
    {
        puts( "NULL" );
    }
    else
    {
        printf( "%d ", head->data );
        display_list( head->next );
    }
} 

void display( const QueueRep *q ) 
{
    display_list( q->head );
}

函数dequeue

int dequeue(QueueRep *q) {
    NodeT *tmp;
    int n = q->head->data;
    tmp = q->head;
    q->head = q->head->next;
    q->length--;
    free(tmp);
    return(n);
}

可以调用未定义的行为。 首先它不检查队列是否为空/。 其次,当队列中的单个节点被删除时,它不会将指针tail设置为NULL

例如,可以通过以下方式定义该函数

int dequeue( QueueRep *q, int *data ) 
{
    int success = q->head != NULL;

    if ( success )
    {
        NodeT *tmp = q->head;
        *data = tmp->data;

        q->head = q->head->next;
        if ( !q->head ) q->tail = NULL;
        --q->length;
 
        free( tmp );
    }

    return success;
}

注意,在 main 中动态声明QueueRep类型的对象没有QueueRep 你可以写

QueueRep q;
initialize( &q );

而这个 typedef

typedef struct QueueRep *queue;

只会让代码的读者感到困惑。 在程序的某些地方您使用QueueRep *类型,而在其他地方您使用类型queue

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM