具有特定功能的列表中的最小元素

时间:2018-01-14 02:42:57

标签: c function arraylist linked-list

我必须使用带有数组和索引的链表上的函数运行程序,该函数将列表的最小元素写入指针指向的变量。 我还必须使用此函数,如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>



/*
     * defining my list
     */

struct record {
    int value;
    int next;
};

struct list {
    int first;
    int free;
    int size;
    struct record* buffer;
};


void init(struct list* ptr, int dimensione){
    ptr->buffer = (struct record *) malloc(dimensione*sizeof(struct record));
    ptr->size = dimensione;
    ptr->first = ptr -> size; //valore illegale

    /*
     * order I want
     * 1 -> 3 -> 0 -> 4 -> 2
     */

    ptr->free = 1;
    ptr->buffer[0].next = 3;
    ptr->buffer[1].next = 0;
    ptr->buffer[2].next = 4;
    ptr->buffer[3].next = 2;
    ptr->buffer[4].next = ptr->size; 

}

/*
 * bottom inserting
 */
bool insert( struct list* ptr, int value ){
    int moved;
    int * position_ptr;

    if(ptr->free != ptr->size){
        moved = ptr -> free;
        ptr->free = ((ptr->buffer)[ptr->free]).next;
        position_ptr = &ptr->first;
        while(*position_ptr != ptr->size){
            position_ptr = &(((ptr->buffer)[*position_ptr]).next);
        }
        *position_ptr = moved;
        ptr->buffer[moved].value = value;
        ptr->buffer[moved].next = ptr -> size;

        return true;
    }
    else{
        return false;
    }
}
/*
 * I MUST use EXACTLY this function 
 */
void minimum(struct list* ptr, unsigned int * varPtr){
        unsigned int min, position;

        for(position=ptr->buffer[ptr->first].next,
            min=ptr->buffer[ptr->first].value; 
            position != ptr->size;
            position=ptr->buffer[position].next) {
           if (min > ptr->buffer[position].value);
              min=ptr->buffer[position].value;
              printf("%d\n",min);
            }
        *varPtr = min;
    }

void visit (struct list * ptr){
    int position;
    position = ptr->first;
    while(position!=ptr->size){
        printf("%d\n", ptr->buffer[position].value);
        position=ptr->buffer[position].next;
    }
}
int main(void){
    struct list mylist;
    init(&mylist, 5);

    insert(&mylist, 10);
    insert(&mylist, 20);
    insert(&mylist, 50);
    insert(&mylist, 35);

    int min_element;
     minimum(&mylist,&min_element);
     visit(&mylist);

    printf("min value: %d\n", min_element);
    fflush(stdout);

    return EXIT_SUCCESS;
}

不幸的是,每次运行代码时,最后一个printf总是返回列表中的最小值但不是最后一个元素,无论是小于还是大于其他元素。 我非常确定inizializing和插入功能正常工作,但无法找到错误。 谢谢你的帮助。

以下是该计划:

Phase:Establishment
Input: Lookup Token
Options: control = appelt

Rule: EstablishmentNonNumeric
Priority: 100
//do not tag establishment names which contain only numbers as this is just weird
(
({Lookup.majorType == "establishment", Lookup.kind == number})
): temp
-->
{ }

Rule: Establishment
(
{Lookup.majorType == "establishment"}
): temp
-->
:temp.Establishment ={rule= "EstablishmentRule" } 

1 个答案:

答案 0 :(得分:0)

如果您将代码格式化程序应用于'unchangeable'函数minimum(),则会发现其中存在(大)错误:

static void minimum(struct list *ptr, unsigned int *varPtr)
{
    unsigned int min, position;

    for (position = ptr->buffer[ptr->first].next,
         min = ptr->buffer[ptr->first].value;
         position != ptr->size;
         position = ptr->buffer[position].next)
    {
        if (min > ptr->buffer[position].value)
            ;
        min = ptr->buffer[position].value;
        printf("%d\n", min);
    }
    *varPtr = min;
}

if末尾的分号是空语句,它使if条件无意义。

因此,您需要仔细查看从哪里获得“不可更改”的代码。如果代码被正确转录,则存在无法修复的错误,因为代码是不可更改的。更有可能的是,你有一个转录错误,删除那个迷路分号将解决你的问题。好吧,循环内的printf()也存在问题。它可能应该在函数的末尾,在循环之外。

该功能设计也很差;它应该返回最小值,而不是打印它。调用代码可以做到这一点。打印使功能非常专业。 (好的:它通过指针参数返回最小值。最好将其作为值返回。)

函数中使用的类型(unsigned int)与结构中使用的类型(int)之间存在差异。当我编译时,我会因为在比较中混合有符号和无符号整数而大喊大叫。修正intunsigned int {}},您可以最终得到:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct record
{
    unsigned value;
    unsigned next;
};

struct list
{
    unsigned first;
    unsigned free;
    unsigned size;
    struct record *buffer;
};

static void init(struct list *ptr, int dimensione)
{
    ptr->buffer = (struct record *)malloc(dimensione * sizeof(struct record));
    ptr->size = dimensione;
    ptr->first = ptr->size;

    ptr->free = 1;
    ptr->buffer[0].next = 3;
    ptr->buffer[1].next = 0;
    ptr->buffer[2].next = 4;
    ptr->buffer[3].next = 2;
    ptr->buffer[4].next = ptr->size;
}

static bool insert(struct list *ptr, int value)
{
    unsigned moved;
    unsigned *position_ptr;

    if (ptr->free != ptr->size)
    {
        moved = ptr->free;
        ptr->free = ((ptr->buffer)[ptr->free]).next;
        position_ptr = &ptr->first;
        while (*position_ptr != ptr->size)
        {
            position_ptr = &(((ptr->buffer)[*position_ptr]).next);
        }
        *position_ptr = moved;
        ptr->buffer[moved].value = value;
        ptr->buffer[moved].next = ptr->size;

        return true;
    }
    else
    {
        return false;
    }
}

static void minimum(struct list *ptr, unsigned int *varPtr)
{
    unsigned int min, position;

    for (position = ptr->buffer[ptr->first].next,
         min = ptr->buffer[ptr->first].value;
         position != ptr->size;
         position = ptr->buffer[position].next)
    {
        if (min > ptr->buffer[position].value)
            min = ptr->buffer[position].value;
        printf("%d\n", min);
    }
    *varPtr = min;
}

static void visit(struct list *ptr)
{
    unsigned position;
    position = ptr->first;
    while (position != ptr->size)
    {
        printf("%d\n", ptr->buffer[position].value);
        position = ptr->buffer[position].next;
    }
}

int main(void)
{
    struct list mylist;
    init(&mylist, 5);

    insert(&mylist, 10);
    insert(&mylist, 20);
    insert(&mylist, 50);
    insert(&mylist, 35);

    unsigned min_element;
    minimum(&mylist, &min_element);
    printf("List:\n");
    visit(&mylist);

    printf("min value: %u\n", min_element);
    fflush(stdout);

    return EXIT_SUCCESS;
}

然后输出:

10
10
10
List:
10
20
50
35
min value: 10

各种替代值集也显示正确的最小值。

树形结构非常奇特 - 至少可以说是不寻常的。