【问题标题】:C singly linked list using QueueC使用Queue的单链表
【发布时间】:2017-05-21 08:33:38
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "locker.h"


void QueueInit(Queue* p)
{
    p->front = NULL;
    p->rear = NULL;
}

int QIsEmpty(Queue* p)
{
    if(p->front == NULL)
    {
        return 1;
    }
    return 0;
}

void Enqueue(Queue* p, int data)
{
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->next = NULL;
    newNode->id = data;

    if(QIsEmpty(p))
    {
        p->front = newNode;
        p->rear = newNode;
    } else {
        p->rear->next = newNode;
        p->rear = newNode;
    }
}

void attachEnqueue(Queue* p, int user_id)
{
    Node* temp = p->front;
    temp->user_id = user_id;    
    p->front = temp;

    printf("Locker %d Owned By %d\n", temp->id, temp->user_id);

    temp->owned = 1;

    temp = temp->next;

}

int Dequeue(Queue* p)
{
    Node* temp = p->front;
    uint16_t item;

    if(QIsEmpty(p))
    {
        printf("No element exists!");
        exit(0);
    } else {
        item = temp->id;
        p->front = temp->next;
        free(temp);

        if(temp == NULL)
        {
            p->rear = NULL;
        }
        return (item);
    }
}

void printList(Queue* p) 
{
    Node* v = p->front;
    while(v != NULL){ 
        printf("Locker: %d\n", v->id);
        v = v->next;
    }
}

int count (Queue p)
{
    int c = 0 ;
    Node* temp = p.front ;

    while ( temp != NULL )
    {
        temp = temp->next;
        c++ ;
    }

    return c ;
}

void SearchQueue(Queue* p, int val1)
{
    Node* v = p->front;
    int sw = 0;

    while( v != NULL)
    {
        if(v->id == val1)
        {
            printf("Locker ID: %d\n", val1);
            printf("Lock Status: locked\n");

            if(v->owned == 0){
            printf("unowned\n");
            } else if(v->owned == 1)
            {
            printf("owned by %d\n", v->user_id);
        }
            sw = 1;
        }
        v = v->next;
    }
    if(!sw)
    {
        printf("locker %d does not exists\n", val1);
    }
}

int main(int argc, char* argv[])
{

    Queue queue;
    QueueInit(&queue);

    char input[50];
    char command[20];
    int val1;
    uint16_t id = 1;


    while(1)
    {
        scanf(" %49[^\n]s", input);
        sscanf(input, "%s %d", &command, &val1);

        if(strcmp(command, "CREATE") == 0)
        {
            printf("New Locker created: %d\n", id);
            Enqueue(&queue, id);
            id++;

        } else if(strcmp(command, "DISPLAY") == 0)
        {
            SearchQueue(&queue, val1);

        } else if(strcmp(command, "ATTACH") == 0)
        {
            attachEnqueue(&queue, val1);    

        } else if(strcmp(command, "DISPLAYALL") == 0)
        {
            printList(&queue);

        }else if(strcmp(command, "DELETE") == 0)
        {
            printf("Deleted the locker, %d\n",Dequeue(&queue)); 

        }else if(strcmp(command, "QUIT") == 0)
        {
            printf("Good Bye!\n");
            exit(0);
        }
        continue;
    }
    return 0;
}

这是我目前所拥有的,“locker.h”的内容是:

#ifndef LOCKER_H
#define LOCKER_H
#include <stdint.h>

typedef struct locker_t {
  uint16_t id;
  uint16_t user_id;
  uint8_t locked;
  uint8_t owned;
  int write_fd;
  int read_fd;
      struct locker_t* next;
    }Node;

typedef struct queue_t {
  Node* front;
  Node* rear;
  size_t size;
}Queue;

#endif

除了 attachEnqueue 部分外,一切正常。 目的是,当我创建储物柜 1 和储物柜 2 并输入 ATTACH 20 时, 储物柜 1 的主人应该是 20,如果我输入 ATTACH 30,储物柜 2 的主人应该是 30。

但是,当我创建 2 个储物柜并首先 ATTACH 20 然后再次输入 ATTACH 30 时,储物柜 1 的所有者的值仅从 20 变为 30,而不是将 30 的所有者分配给储物柜 2。

我 100% 确定 attachEnqueue 函数涉及错误的内容,但我真的不知道如何修改它..

另外,我需要包含一个“LOCK”命令来使储物柜被锁定或解锁,但问题是,学校希望我通过使用信号 SIGUSR 来做到这一点。我应该如何使用信号功能来锁定或解锁储物柜? pthread.mutex.lock 和 unlock 会起作用吗?

任何帮助或建议将不胜感激!

【问题讨论】:

  • 您的attachEnqueue 总是更新列表的头部。您没有尝试实现您所描述的逻辑。您需要使用无效/未使用的值初始化 user_id 字段(或使用单独的“未附加”标志字段)。那么attachEnqueue需要遍历列表,直到找到这样的值。

标签: c queue computer-science singly-linked-list


【解决方案1】:

评论是对的。 attachEnqueue 的最后一行是temp = temp-&gt;next; 也许您认为temp 在您下次调用该函数时会记住该信息,但现在绝对不是。这条线没用,下一次,temp 将被分配到队列的最前面。
为了解决这个问题,您可以如上所述在您的结构中创建一个标志,向您的函数添加一个计数器参数以跟踪要附加的队列中的哪个元素,或者使 temp 成为静态参数,因此它的状态在来电。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多