C ++无法将新节点插入链表的中间

时间:2014-02-09 00:53:08

标签: c++

我已经被困了好几天了。我试图将一个节点插入一个链表。我能够将节点插入到空列表的开头和列表的开头,但是我遇到了中间和中间的问题。结束插入。任何帮助将不胜感激&先感谢您! 8)

我目前收到以下错误消息,但我不确定如何修复它:

$ g++ a1.cpp
a1.cpp: In function ‘int allocate_memory(int, int)’:
a1.cpp:165:46: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if (t->next != NULL && p->start_byte < t->next) // inserting p into middle of alloclist
                                          ^

我的计划如下:

#include <iostream>

using namespace std;

typedef struct FREE_NODE * FREEPTR;
typedef struct ALLOCNODE * ALLOCPTR;

struct FREE_NODE    // FREE LIST NODES
{
    int start_byte;
    int end_byte;
    int size;

    FREEPTR next;
};

struct ALLOCNODE    // ALLOCATED LIST NODES
{
    int start_byte;
    int end_byte;
    int size;
    int id;

    ALLOCPTR next;
};

FREEPTR freelist = NULL;    // the FREE link list
ALLOCPTR alloclist = NULL;  // the ALLOCATED link list
int total_memory_managed = 0;   // the amount of memory managed

void init_memory_manager(const int amount)
{
    total_memory_managed = amount;

    freelist = new FREE_NODE;   // set up the freelist linked list
    freelist -> size = amount;
    freelist -> start_byte = 0;
    freelist -> end_byte = amount - 1;
    freelist -> next = NULL;

    alloclist = NULL;   // set up the alloclist linked list
}

//--------------------
int largest_free(void)
{
    FREEPTR temp = freelist;    //temp pointer to freelist
    int largest_free = 0;   //initialize largest_free

    while (temp != NULL)
    {
        if (temp->size > largest_free)  //compares size field & remembers largest as it cycles through freelist
        largest_free = temp->size;
        else
            temp = temp->next;
    }
    return largest_free;    //return largest free node available
}

//--------------------
int allocate_memory(const int job, const int amount)
{
    if (amount <=0 || amount > total_memory_managed || amount > largest_free()) //Excludes special cases
    return 0;

    FREEPTR temp = freelist;    // temp pointer points to freelist 
    int num = 0;
    while (temp != NULL)
    {
        if (temp->size < amount)
            temp->next;
        else
        {   num = temp->end_byte;
            temp->size = temp->size - amount;
            temp->end_byte = temp->end_byte - amount;
            break;
        }
    }

    if (num == 0)
       return -99;

    ALLOCPTR p = new ALLOCNODE;
    p->next = NULL;
    p->end_byte = num;
    p->size = amount;
    p->start_byte = num - amount +1;
    p->id = job;

    ALLOCPTR t = alloclist;

    if (alloclist == NULL)  // inserting p into empty alloclist
    alloclist = p;
    else
        if (p->start_byte < alloclist->start_byte)  //inserting p into beginning of alloclist
         {
            p->next = alloclist;
            alloclist = p;                  
         }   
        else
             if (t->next != NULL && p->start_byte < t->next) // inserting p into middle of alloclist
             {
                 p->next = t->next;
                 t->next = p;
             }
             else
             {
                 while (t->next != NULL) // inserting p at end of alloclist
                 {
                     t = t->next;
                 }
                 t->next = p;                
             }

//insert function to remove zero size nodes

// return amount of memory allocated
}

// MAIN ===================================================================

int main(void)
{
    init_memory_manager(256);
    allocate_memory(1,20);

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的代码可能需要很长时间才能进行深入分析,但是根据您提供的行进行猜测,您可能只需要在比较之前将t->next转换为int。

reinterpret_cast<int>(t->next);

如果您想了解更多细节 - 请提供尽可能短的代码并重现问题。