如何将新元素添加到双链表

时间:2018-04-07 15:55:53

标签: c function struct

我想创建一个可以在C中为双链表添加新元素的函数,但我无法做到。这是代码。 新元素应该有名字,小组等。只要解释我如何制作名字和其余部分,我将自己做。

select deneme.ogr_id,
deneme.ad,
sum(case deneme.tur_id WHEN 1 THEN YUZDE else 0 END) sabah ,
sum(case deneme.tur_id WHEN 2 THEN YUZDE else 0 END) ogle,
sum(case deneme.tur_id WHEN 4 THEN YUZDE else 0 END) aksam,
sum(case deneme.tur_id WHEN 5 THEN YUZDE else 0 END) yatsi,
sum(case deneme.tur_id WHEN 6 THEN YUZDE else 0 END) sohbet,
sum(case deneme.tur_id WHEN 7 THEN YUZDE else 0 END) muhtelif 
FROM (
      SELECT s_kontrol.ogr_id, 
      s_ogr.ad,tur_id, 
      CEILING(((SUM(case s_kontrol.durum_id WHEN 1 THEN 1 else 0 end)
      +SUM(case s_kontrol.durum_id WHEN 2 THEN 1 else 0 end)
      +SUM(case s_kontrol.durum_id WHEN 3 THEN 1 else 0 end)
      +SUM(case s_kontrol.durum_id WHEN 4 THEN 1 else 0 end)
      +SUM(case s_kontrol.durum_id WHEN 5 THEN 1 else 0 end) )
      -(SUM(case s_kontrol.durum_id WHEN 3 THEN 1 else 0 end)))
      /(SUM(case s_kontrol.durum_id WHEN 1 THEN 1 else 0 end)
      +SUM(case s_kontrol.durum_id WHEN 2 THEN 1 else 0 end)
      +SUM(case s_kontrol.durum_id WHEN 3 THEN 1 else 0 end)
      +SUM(case s_kontrol.durum_id WHEN 4 THEN 1 else 0 end)
      +SUM(case s_kontrol.durum_id WHEN 5 THEN 1 else 0 end) ) 
      * 100) YUZDE 
      FROM s_kontrol, s_ogr 
      WHERE s_kontrol.ogr_id=s_ogr.numara 
      and s_ogr.yurt_id=? 
      and tarih BETWEEN ? and ? 
      and tur_id IN(1,2,3,4,5,6,7) 
      GROUP BY s_kontrol.ogr_id,s_ogr.ad, tur_id 
     ) deneme 
group by ogr_id,ad order by 1

1 个答案:

答案 0 :(得分:1)

我尝试实现一个基于给定索引插入元素的函数。请注意,我稍微更改了 list_item struct ,以便它包含指向数据元素的指针。

以下是实施:

/**************************************************************
* Function:     insert_index
* Parameters:   APTEKA* head, INF_BLOK* data, int index
* Return value: Returns NULL on failure, a pointer to the head
*               on success
* Description:  Inserts a APTEKA* element based on an given 
*               index
***************************************************************/
APTEKA* insert_index(APTEKA* head, INF_BLOK* data, int index) {

// Local variable for index
int ind = 1;

APTEKA* new_node = (APTEKA*)malloc(sizeof(APTEKA));
new_node->inf = data;

// Check if head exists, the malloc call was successfull and the index is
// in allowed range
// NOTE: Index for head starts at position 1 
if(head && new_node && index) {

// If index is one, set a new head
if(index == 1) {


    // The previous node is of course NULL
    new_node->prev = NULL;
    new_node->next = head->next;

    if(head->next)
        head->next->prev = new_node;

    head->next = new_node;

    // In a full implementation you need to free the memory for head and the data field in the          
    // structure. free(...)

    // Return a pointer to the new head of the list
    return new_node;
} else {

    APTEKA* current_node = head->next;

    // Loop through all positions before the desired index      
    for(; ind < (index - 1); ++ind)
        current_node = current_node->next;

    new_node->prev = current_node;
    new_node->next = current_node->next;

    if(current_node->next)
        current_node->next->prev = new_node;

    current_node->next = new_node;
    } 
}
else {
    // Return NULL on failure
    return NULL;
}

// Return an pointer to the head
return head;
}

<强>解释: 首先,该函数创建一个名为 new_node 的新节点,并将 inf 数据字段的指针设置为给定参数。在实际插入之前我基本上检查一切是否正确。

然后我分为两种情况:第一种是替换头(索引== 1),第二种是任何其他索引。

如果应该更换头部,我会更改相关性并返回指向新创建节点的指针。对于任何其他情况,我迭代到之前索引,然后尝试插入它。

当我使用这个主要功能进行测试时,似乎有效:

int main()
{      

/* Only used for testing purposes */

APTEKA* head = (APTEKA*)malloc(sizeof(APTEKA));
APTEKA* first = (APTEKA*)malloc(sizeof(APTEKA));
APTEKA* tail = (APTEKA*)malloc(sizeof(APTEKA));

head->next = first, head->prev = NULL;
first->next = tail, first->prev = head;
tail->next = NULL, tail->prev = first;

/* Information for head node */
INF_BLOK* block_head = (INF_BLOK*)malloc(sizeof(INF_BLOK));
memcpy(block_head->name, "Head", 5); 

/* Information for tail node */
INF_BLOK* block_tail = (INF_BLOK*)malloc(sizeof(INF_BLOK));
memcpy(block_tail->name, "Tail", 5); 

/* Information for first block */
INF_BLOK* block_first = (INF_BLOK*)malloc(sizeof(INF_BLOK));
memcpy(block_first->name, "First", 6);

/* Information for block to add */
INF_BLOK* block_sec = (INF_BLOK*)malloc(sizeof(INF_BLOK));
memcpy(block_sec->name, "Second", 7); 

head->inf = block_head, first->inf = block_first, tail->inf = block_tail;

if(!insert_index(head, block_sec, 2))
    fprintf(stderr, "Error inserting element\n");

APTEKA* element = head;

/* Print out name-data of nodes */
while(element) {
    puts(element->inf->name);
    element = element->next;

}

element = head;

// Freeing everything   
while (element) {
    APTEKA* next = element->next;
    free(element->inf), free(element);
    element = next;
}

return 0;
}

希望我的回答可以为您提供所需的见解。如果我做错了,请纠正我:)

注意:对于这个答案,我只使用了数据项结构的name属性。要存储组,表单等,您需要另一个设置这些值的过程。