简体   繁体   中英

C singly linked list using Queue

#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;
}

This is what I have so far and the contents for "locker.h" is:

#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

Everything works fine except for the attachEnqueue part. The purpose is, when I create locker 1 and locker 2 and input ATTACH 20, locker 1's owner should be 20 and again if I input ATTACH 30, locker 2`s owner should be 30.

However, when I create 2 lockers and firstly ATTACH 20 and then again input ATTACH 30, the locker 1's owner`s value only changes from 20 to 30, not assigning the 30 owner to locker 2.

I am 100% sure that the attachEnqueue function involves the wrong contents but I am really not sure how to modify it..

Also, I need to include a "LOCK" command to make the locker whether to be locked or unlocked, but the problem is, school wants me to do this by using signal SIGUSR. How should I use the signal function to lock or unlock the locker? Would pthread.mutex.lock and unlock work?

Any help or advice would be very thankful!

The comment is right. The last line of attachEnqueue is temp = temp->next; Maybe you assume temp keeps that information in mind next time you call the function, but right now, absolutely not. This line is of no use, the next time, temp will be assigned to the front of the queue.
To go around that, you may create a flag in your struct as stated above, add a counter argument to your function to keep track of which element in your queue to attach, or make temp a static argument, so its state persists between calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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