c - 遍历列表并打印项目

标签 c list

我有一个这样的列表

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

struct ListItem 
{
    int x;
    int y;
    struct ListItem *next;
};

int main()
{   
    int x1 =0; 
    int y1 = 0;

    printf("Please enter the x coordinate: ");
    scanf("%d", &x1); 
    printf("Please enter the y coordinate: ");
    scanf("%d", &y1); 

    struct ListItem root;
    if( root.next == NULL )
    {
        root.x = x1;
        root.y = y1;
        //I dont know what should I assign here but I want to have about 30 locations
        //root.next = struct ListItem next;
    }


    //WHAT SHOULD I DO HERE?
    {
        printf("Your location is : (%d,%d)\n", root.x, root.y); 
    }
}

现在我想编写一个循环来迭代它,以便我可以打印列表中的每个元素:) 基本上我想做的是,我想从用户那里获取位置,然后打印它们。 请帮忙。

最佳答案

链接列表。输入坐标直到输入零。

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

struct ListItem
{
    int x;
    int y;
    struct ListItem *next;
};

int main()
{
    int x1 =0;
    int y1 = 0;
    int iCount = 0; // keep count of the structures allocated
    int iEach = 0;
    struct ListItem root = { 0, 0, NULL};// declare and initialize the first structure
    struct ListItem* pFreeListItem = NULL;// declare a pointer and initialize it null. use for freeing memory later
    struct ListItem* pListItem = &root;// declare a pointer and initialize it to point to the first structure

    while ( 1) { // the main loop

        printf("Please enter the x coordinate: ");
        scanf(" %d", &x1);
        printf("Please enter the y coordinate: ");
        scanf(" %d", &y1);

        pListItem->x = x1; // use the pointer to assign the coordinate
        pListItem->y = y1;

        iCount++; // keep track of the number of structures
        printf("Input complete for location number %d\n", iCount);


        printf("Enter 0 to exit or any other number to continue: ");
        scanf(" %d", &y1);
        if ( y1 == 0) { // exit the loop if zero is entered
            break;
        }
        else { // if zero was not entered
            pListItem->next = malloc ( sizeof ( struct ListItem));// allocate memory for the next structure
            if ( pListItem->next == NULL) {
                //allocation failed
                exit (1);
            }

            pListItem = pListItem->next; // set the pointer to point to the new 'next' structure
            pListItem->next = NULL; // set this to null as no memory has yet been allocated
        }
    }

    pListItem = &root; // set the pointer to the original structure root
    for ( iEach = 0; iEach < iCount; iEach++) // loop through each structure. icount holds the number of structures
    {
        printf("Location number %d is : (%d,%d)\n", iEach + 1, pListItem->x, pListItem->y);
        pListItem = pListItem->next; // set the pointer to the next structure

    }

    pListItem = root.next; // set the pointer to the first allocated structure
    for ( iEach = 1; iEach < iCount; iEach++) // loop through each structure
    //start with 1 as the first structure was not allocate and does not need to be freed. icount holds the number of structures
    {
        pFreeListItem = pListItem->next; // set the free pointer to the next structure
        free ( pListItem); // free the memory for the structure
        pListItem = pFreeListItem; // point to the free pointer
    }
}

编辑:此代码将显示指针的地址,也许这将有助于澄清正在发生的事情

        else { // if zero was not entered
            pListItem->next = malloc ( sizeof ( struct ListItem));// allocate memory for the next structure
            if ( pListItem->next == NULL) {
                //allocation failed
                exit (1);
            }
            printf ( "pListItem points to %p and pListItem->next points to %p\n", pListItem, pListItem->next);
            pListItem = pListItem->next; // set the pointer to point to the new 'next' structure
            pListItem->next = NULL; // set this to null as no memory has yet been allocated
            printf ( "NOW pListItem points to %p and pListItem->next points to %p\n", pListItem, pListItem->next);
        }

关于c - 遍历列表并打印项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24101310/

相关文章:

c - 也将大写转换为小写以进行计数

c - 为什么 lex 不能识别我的正则表达式定义

python - 空列表、元组、字典如何分配内存?

c - 我如何在 C 中取消引用指向结构的指针?

c - GTK 不呈现所有用户界面

c - 使用 scanf 将字符串存储到字符串数组中

python - 计算嵌套列表中的元素出现次数

list - NetLogo 按特定值对代理列表进行排序

r - 删除R中多级列表上data.frames中列的属性

Python - 使用 boolean 值进行安全索引