Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 不带结构但仅使用数组的链表 #包括 int main() { int n,i; int头=0; 打印F(“学生人数:\n”); scanf(“%d”和“&n”); int数据[n]; _数据[n]的int地址_; int*next; printf(“输入标记:\n”); 对于(i=0;i信息); temp=当前;/*此项将被删除*/ 当前=当前->下一步;/*将当前指针设置为当前元素的后续元素*/ 免费(临时); } 返回0; }_Arrays_C_Algorithm_Data Structures_Linked List - Fatal编程技术网

Arrays 不带结构但仅使用数组的链表 #包括 int main() { int n,i; int头=0; 打印F(“学生人数:\n”); scanf(“%d”和“&n”); int数据[n]; _数据[n]的int地址_; int*next; printf(“输入标记:\n”); 对于(i=0;i信息); temp=当前;/*此项将被删除*/ 当前=当前->下一步;/*将当前指针设置为当前元素的后续元素*/ 免费(临时); } 返回0; }

Arrays 不带结构但仅使用数组的链表 #包括 int main() { int n,i; int头=0; 打印F(“学生人数:\n”); scanf(“%d”和“&n”); int数据[n]; _数据[n]的int地址_; int*next; printf(“输入标记:\n”); 对于(i=0;i信息); temp=当前;/*此项将被删除*/ 当前=当前->下一步;/*将当前指针设置为当前元素的后续元素*/ 免费(临时); } 返回0; },arrays,c,algorithm,data-structures,linked-list,Arrays,C,Algorithm,Data Structures,Linked List,在上面的代码中,我使用指针打印数据值。使用两个数组,一个用于数据,另一个用于数据地址。但是我不知道如何真正实现链表,我真的很困惑,有人能告诉我如何在不使用结构的情况下实现链表吗。这里是一个简单链表的完整示例,在纯C中更容易。请注意结构中的“下一个”成员-这是指向列表中后续成员的指针 #include <stdio.h> int main() { int n, i; int head = 0; printf("No of Students:\n&quo

在上面的代码中,我使用指针打印数据值。使用两个数组,一个用于数据,另一个用于数据地址。但是我不知道如何真正实现链表,我真的很困惑,有人能告诉我如何在不使用结构的情况下实现链表吗。

这里是一个简单链表的完整示例,在纯C中更容易。请注意结构中的“下一个”成员-这是指向列表中后续成员的指针

#include <stdio.h>
int main()
{
    int n, i;
    int head = 0;
    printf("No of Students:\n");
    scanf("%d", &n);
    int data[n];
    int address_of_data[n];
    int *next;
    printf("Enter Marks:\n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &data[i]);
    }
    for (i = 0; i < n; i++)
    {
        if (data[head] != -1)
        {
            address_of_data[head] = &data[i];
        }
        else
        {
            break;
        }
        head++;
    }
    next = address_of_data[0];
    for (i = 0; i < n; i++)
    {
        printf("%d ", *(next + i));
    }
    return 0;
}
#包括
#包括
#包括
结构我的_数据{
整数;
字符信息[30];
结构我的_数据*下一步;
};
#定义最大元素5
int main(int argc,字符**argv)
{
struct MY_DATA*root=NULL;/*列表的根*/
struct MY_DATA*prev=NULL;/*以前创建的元素*/
对于(int i=0;i<(MAX_ELEMS);i++)
{
/*分配内存*/
struct MY_DATA*new_DATA=(struct MY_DATA*)malloc(sizeof(MY_DATA));
/*…并初始化新数据集*/
memset(新的_数据,0x00,sizeof(我的_数据));
新建数据->编号=i*i;
sprintf((char*)&(new_data->info),“这是数据记录%d”,i);
if(root==NULL)
{
root=prev=new_data;/*记住第一个元素*/
}
其他的
{
prev->next=new_data;/*上一个元素现在有一个后续元素*/
prev=new_data;/*在下一次迭代中记住这一点*/
}
}
struct MY_DATA*current=root;/*获取列表中的第一个元素*/
struct MY_DATA*temp=NULL;/*仅用于清理内容*/
对于(int i=0;i<(MAX_ELEMS);i++)
{
/*显示数据*/
printf(“数据集#%d:%s\n”,当前->编号,当前->信息);
temp=当前;/*此项将被删除*/
当前=当前->下一步;/*将当前指针设置为当前元素的后续元素*/
免费(临时);
}
返回0;
}
另一个例子-使用索引或指针访问数组:

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

struct MY_DATA {
    int number;
    char info[30];
    struct MY_DATA *next;
};

#define MAX_ELEMS   5

int main(int argc, char **argv)
{
    struct MY_DATA *root = NULL;        /* Root of list */
    struct MY_DATA *prev = NULL;        /* Previously created element */

    for(int i = 0; i < (MAX_ELEMS); i++)
    {
        /* Allocate memory ... */
        struct MY_DATA *new_data = (struct MY_DATA *)malloc(sizeof(MY_DATA));

        /* ...and inialize new data set */
        memset(new_data, 0x00, sizeof(MY_DATA));
        new_data->number = i * i;
        sprintf((char *)&(new_data->info), "This is data record %d", i);


        if (root == NULL)
        {
            root = prev = new_data;      /* Remember the first element */
        }
        else
        {
            prev->next = new_data;      /* The previous element has now a successor */
            prev = new_data;            /* Remember this for next iteration */
        }
    }

    struct MY_DATA *current = root;     /* Get the 1st element in the list */
    struct MY_DATA *temp = NULL;        /* Just for clean up stuff */

    for(int i = 0; i < (MAX_ELEMS); i++)
    {
        /* Display data */
        printf("Data set #%d: %s\n", current->number, current->info);

        temp = current;             /* This becomes deleted */
        current = current->next;    /* Set current pointer to successor of current element */
        free(temp);
    }

    return 0;
}
#包括
#定义最大元素5
int my_数组[MAX_ELEMS]={5,18,42,31,10};
int main(int argc,字符**argv)
{
int*current=(int*)my_数组;/*获取列表中的第一个元素*/
对于(int i=0;i<(MAX_ELEMS);i++)
{
/*使用数组索引*/
printf(“数据集#%d\n”\
“索引访问:%d\n”,i,我的_数组[i]);
/*使用指针*/
printf(“指针访问:%d\n”,*当前++);
}
返回0;
}

链表的主要优点是,插入或追加元素时,可以将该元素存储在内存中的任何位置。列表中的其他元素可以指向它,如果您在列表上迭代,您可以沿着元素形成的链沿着指针在内存中的任意位置来回跳转,直到到达尾部,尾部有一个空指针,因为它是最后一个元素

实现“仅使用数组”的链表并没有多大意义。即使你可以,你为什么要这么做?数组很好,因为你可以在固定的时间内直接索引到它们中——你不能用链表来做这件事,你只能对它们进行迭代。但是数组也有它们的缺点——它们的大小是固定的,当它们填满时,它们就填满了!大多数共享库列表,如java中的ARARYLIST或C++中的向量类,都将基础数据存储在固定大小的数组中,然后如果插入了太多的数组,它们会在后台创建一个新的更大的数组,并为您复制元素。当阵列中的空间不足时,确实没有神奇的解决方案


既然如此,为什么只使用数组实现链表呢?您删除了它们唯一的优势——可以任意追加,而无需昂贵的重新分配。我甚至不确定这是否是一个定义明确的问题。也许如果你真的很绝望,你可以创建一个大数组,把它当作你自己的虚拟内存,分配和释放其中的插槽,然后把一个两元素数组当作一个条目(条目[0]=数据,条目[1]=下一个的“地址”,即大数组的索引)。但是这有点可怕的代码,而且确实缺少了链表的意义。

提示:数组索引与指针非常相似。@Henry感谢您的提示,但我仍然感到困惑感谢您的响应,但我想知道是否可以只使用数组,而不使用结构。请参见第二个示例。就已知的数据类型而言(此处为int),您只需创建一个指向第一个元素的指针,并使用“++”运算符遍历数据。@WillyK。你好,我想联系你,你能帮忙吗?我只是想弄明白你的解决方案是如何运作的?您提到了小窗口0x0,但作为一个孩子,您在哪里添加了它?我正在尝试对我的应用程序执行相同的操作,但不了解您的解决方案。
#include <stdio.h>

#define MAX_ELEMS   5
int my_array[MAX_ELEMS] = { 5, 18, 42, 31, 10 };

int main(int argc, char **argv)
{
    int *current = (int *)my_array;     /* Get the 1st element in the list */

    for(int i = 0; i < (MAX_ELEMS); i++)
    {
        /* Using array index */
        printf("Data set #%d \n" \
               "  Indexed access: %d\n", i, my_array[i]);

        /* Using the pointer*/
        printf("  Pointer access: %d\n", *current++);
    }

    return 0;
}