微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

链表printf“%d \ n”,tasks_head-> head-> tid隔离错误

如何解决链表printf“%d \ n”,tasks_head-> head-> tid隔离错误

我有代码,我想打印每个节点的tid。我在此for循环中存在隔离错误 @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int width = getWidth(); int height = getHeight(); int shadowGap = this.shadowGap; Color shadowColorA = new Color(shadowColor.getRed(),shadowColor.getGreen(),shadowColor.getBlue(),shadowAlpha); Graphics2D graphics = (Graphics2D) g; //Sets antialiasing if HQ. if (highQuality) { graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); } //Draws shadow borders if any. if (shady) { graphics.setColor(shadowColorA); graphics.fillRoundRect( shadowOffset,// X position shadowOffset,// Y position width - strokeSize - shadowOffset,// width height - strokeSize - shadowOffset,// height arcs.width,arcs.height);// arc Dimension } else { shadowGap = 1; } //Draws the rounded opaque panel with borders. graphics.setColor(getBackground()); graphics.fillRoundRect(0,width - shadowGap,height - shadowGap,arcs.width,arcs.height); graphics.setColor(getForeground()); graphics.setstroke(new Basicstroke(strokeSize)); graphics.drawRoundRect(0,arcs.height); //Sets strokes to default,is better. graphics.setstroke(new Basicstroke()); } 而且我不确定printf("%d\n",tasks_head->head->tid);是否按我的意愿工作。我想在数组的每个位置保存一个task_count[]

counter++

解决方法

此代码存在一些问题,但最重要的是您如何影响task_head。

目前,您每次输入insert_task时都会分配一个新的task_head,并使用以下两行来重新创建head元素:-

tasks_head=(struct Head_GL*)malloc(sizeof(struct Head_GL));
tasks_head->head=(struct Tasks*)malloc(sizeof(struct Tasks));

通常,您可以在单独的初始化函数调用中执行一次此操作,也可以在insert_task中通过检查是否已执行此操作来完成此操作。

您实际上并不需要单独创建“ head”,因为在初始化task_head全局变量时,head将是第一个“新”的。因此,如果将这段代码移到创建“新”任务的下方,则可以直接将其分配给头部。

如果我们根据您的难度查看将项目插入链接列表的代码,那么您会发现您已经在执行此操作:-

if(tasks_head==NULL){
    tasks_head->head = new;
    return 1;
}

因此完全不需要创建单独的“头部”。

顺便说一句,这里的检查恰好是您要一次性执行“ tasks_head”初始化的检查,因此,如果我们从上方获取您的代码并将其移至此部分,则您将不会继续重置您的全球客户。

但是,进行初始化的不利之处在于,您已经尝试过调整任务难度计数,因此也需要在下面进行调整,我们需要为init情况添加一个等效项。

如果将所有这些更改放在一起,则会得到以下信息:-

int insert_task(int tid,int difficulty){
    num++;
    struct Tasks *prev=NULL;
    struct Tasks *temp=NULL;
    struct Tasks *new=(struct Tasks*)malloc(sizeof(struct Tasks));

    if(new==NULL)
        return 0;

    new->tid = tid;
    new->difficulty = difficulty;
    new->next = NULL;

    if(tasks_head==NULL){
        tasks_head=(struct Head_GL*)malloc(sizeof(struct Head_GL));
        tasks_head->head=(struct Tasks*)malloc(sizeof(struct Tasks));
        tasks_head->tasks_count[(difficulty != 1 && difficulty != 2)?2:difficulty-1]=1;
        tasks_head->head = new;
        return 1;
    }

    if(difficulty==1)
        tasks_head->tasks_count[0]++;
    else if(difficulty==2)
        tasks_head->tasks_count[1]++;
    else
        tasks_head->tasks_count[2]++;

    if( tasks_head->head->difficulty > difficulty){
        new->next = tasks_head->head;
        tasks_head->head = new;
        return 1;
    }
    else{
        prev = tasks_head->head;
        temp = tasks_head->head->next;
        while(temp != NULL && temp->difficulty < difficulty){
            prev = temp;
            temp = temp->next;
        }
        if(temp==NULL){
            prev->next = new;
            return 1;
        }
        else{
            new->next = temp;
            prev->next = new;
            return 1;
        }
    }
}

task_count难度映射看起来有些奇怪,但是我试图复制您当前的行为,其中难度1的任务设置为0,2的任务设置为1,其他所有条件都设置为2。使用Tenery运算符的这条时髦的线:

tasks_head->tasks_count[(difficulty != 1 && difficulty != 2)?2:difficulty-1]=1;

但这可能只是您想要的:

tasks_head->tasks_count[difficulty-1]=1;

如果您的任务难度仅在1-3范围内。

最后,我们需要查看您的main方法中的循环,这些循环当前在尝试遍历链接列表时正在修改全局变量。

for(int i=0; i<num; i++){
    printf("%d\n",tasks_head->head->tid);
    tasks_head->head=tasks_head->head->next;
}

我认为您想考虑使用一个指针来遍历此对象,如下所示:-

for(struct Tasks *p = tasks_head->head; p != NULL; p = p->next){
    printf("%d\n",p->tid);
}

我会让您考虑第二个循环,以弄清楚您可能希望做些类似的事情。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。