在C中从LinkedList创建队列

在C中从LinkedList创建队列,c,linked-list,queue,C,Linked List,Queue,目前我正在编写一个代码,它要求我从linkedlist“复制”一系列整数(用户输入),并将它们放入队列中 我知道我需要一个名为enqueue的函数才能继续,但是,有人告诉我,只要使用insertNode()函数,我就可以创建一个基本的enqueue函数。e、 g void enqueue(Queue *q, int item) { insertNode(&(q->ll),q->ll.size, item); removeNode(ll,

目前我正在编写一个代码,它要求我从linkedlist“复制”一系列整数(用户输入),并将它们放入队列中

我知道我需要一个名为enqueue的函数才能继续,但是,有人告诉我,只要使用insertNode()函数,我就可以创建一个基本的enqueue函数。e、 g

   void enqueue(Queue *q, int item)
     {
      insertNode(&(q->ll),q->ll.size, item);
      removeNode(ll,0);
     }
给定insertNode():

我面临的问题是,我不确定我的enqueue()是否正常工作,或者是否有更好的方法来执行它? 提前谢谢

队列、Linkedlist和ListNode的定义;假设不允许我更改它们:

typedef struct _listnode
{
    int item;
    struct _listnode *next;
} ListNode; 

typedef struct _linkedlist
{
    int size;
    ListNode *head;
} LinkedList;   

typedef struct _queue
{
    LinkedList ll;
} Queue;

这就是我应该做的-有两个指针,
head
tail
。因此,您的
enqueue
只需使用指向链表末尾的
tail
指针来使用
insertNode
dequeue
将使用
head
指针弹出元素。因此,插入和删除操作都有
O(1)
。请显示
队列
链接列表
列表节点
@Bill谢谢!我已经更新了我的代码,部分要求是不允许在我的定义中添加尾部Linkedlist@JoachimPileborg刚刚更新了我的代码!我的怀疑得到了证实,考虑到您将指向
ListNode
的指针传递给一个需要指向
LinkedList
的指针的函数,我很惊讶您竟然可以构建。您确定要在
排队
中调用
插入节点
    void createQueueFromLinkedList(LinkedList *ll, Queue * q)
    {

    if (isEmptyQueue) {
        removeAllItemsFromQueue(q);
    }// empty the queue if it is not empty
    while (ll->size)
    {
    enqueue(q, ll->head->item);     
    //code to remove ll->size by 1 each time 
        }
    }


    int isEmptyQueue(Queue *q)
     {
       if ((q->ll).size == 0){
         return 0;
      }
         return 1;
      }
typedef struct _listnode
{
    int item;
    struct _listnode *next;
} ListNode; 

typedef struct _linkedlist
{
    int size;
    ListNode *head;
} LinkedList;   

typedef struct _queue
{
    LinkedList ll;
} Queue;