将数据从链接列表转移到队列[C]



所以我是一个自我学习者,我被困在这里。我要完成的是创建一个带有一些数据的链接列表,然后从链接列表中选择一个随机节点,更改一些数据,然后将其移至队列(也使用链接列表也实现了队列)

我不确定如何编写我的结构。这是我的结构。

struct Apps{
    int id;
    int size;
    char name;
    float xronos_eiswdou;
    char condition;
    int xronos_ekteleshs;
    struct Apps *next, *next1;
}*rear, *front;

我确定这是错误的,我对如何修复它没有线索。

这是我现在的功能。

void push(struct Apps *head)
{
     struct Apps *temp; 
     struct Apps *temp1;
     temp1=(struct Apps *)malloc(sizeof(struct Apps));
     temp1->id = temp->id;
     temp1->size = temp->size;
     temp1->name = temp->name;
     temp1->xronos_eiswdou = temp->xronos_eiswdou;
     temp1->condition = temp->condition;
     temp1->xronos_ekteleshs = temp->xronos_ekteleshs;
     temp1->next = head;
     if (front == NULL)
     {
           front=temp1;
           front->next1=NULL;
           rear=front;
     }
     else
     {
           front->next1=temp1;
           front=temp1;
           front->next1=NULL;
     }
}

这是我将数据添加到我的链接列表的主要代码:

for(step = 0; step < 100; step++)
      {
        int r = rand() % 100+1;
        if(r < 31){
            int r1 = rand() % 100+1;
            aa = r1;
            int r2 = rand() % 100+1;
            bb = r2;
            int r3 = rand() % 3;
            if(r3 == 0){ 
                 cc = 'l';
            }
            else if(r3 == 1 ){ 
                 cc = 'b';
            }
            else if( r3 == 2 ){ 
                 cc = 'c';
            }
            dd = (double)seconds/CLOCKS_PER_SEC;
            ee = 'a';
            ff = -1;
            insert_new(aa, bb, cc, dd, ee, ff);
        }
        else if(r > 30 && r < 51){
            pickRandom(head); /*picking a random node and changing some data*/
            /*Here I need a function which moves the data of the above node to a queue*/
        }
    }

长话短说,我需要一个将数据从链接列表移至队列的函数!任何帮助都会受到赞赏,并为我的问题辩解,我自己学习东西:/

编辑

我尝试了一些似乎有效的东西,善良...这是我的结构:

    struct Apps{
    int id;
    int size;
    char name;
    float xronos_eiswdou;
    char condition;
    int xronos_ekteleshs;
    struct Apps *next;
};
struct Queue
{
    int id1;
    int size1;
    char name1;
    float xronos_eiswdou1;
    char condition1;
    int xronos_ekteleshs1;
    struct Queue *next;
    struct Queue *front;
    struct Queue *rear;
};

这是我的功能:

struct Queue *start = NULL;
struct Queue *front = NULL;
struct Queue *rear = NULL;
    void push()
    {
         struct Apps *temp; 
         struct Queue *temp1;
         temp1=(struct Queue *)malloc(sizeof(struct Queue));
         temp1->id1 = temp->id; 
         temp1->size1 = temp->size;
         temp1->name1 = temp->name;
         temp1->xronos_eiswdou1 = temp->xronos_eiswdou;
         temp1->condition1 = temp->condition;
         temp1->xronos_ekteleshs1 = temp->xronos_ekteleshs;
         temp1->next = start;
         if (front == NULL)
         {
               front=temp1;
               front->next=NULL;
               rear=front;
         }
         else
         {
               front->next=temp1;
               front=temp1;
               front->next=NULL;
         }
}

程序运行没有错误,但崩溃了,所以再次出现了问题!

,所以您的问题是

1.a列表包含一些随机数的节点

2.收集队列中列表中修改的元素(这是另一个列表)

要解决此问题,请修改您的结构为

 struct Apps{
    int id;
    int size;
    char name;
    float xronos_eiswdou;
    char condition;
    int xronos_ekteleshs;
    struct Apps *next;
};
struct Queue
{
    Struct Apps *node;
    struct Queue * next;
};

因此,现在您将拥有列表中(应用程序)中的原始元素列表和在队列(queue_struct)中收集的修改后的节点(apps列表的),该节点(queue_struct)用LinkedList实施。

最新更新