迭代列表并打印项目

时间:2014-06-07 20:30:07

标签: 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); 
    }
}

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

4 个答案:

答案 0 :(得分:1)

这是一个有效的玩具程序,用于演示存储在数组中的列表。 实际编译没有警告 - gcc -Wall -Werror -std = c99 l.c

使用calloc动态分配的版本,如果你想要动态内存,可以使用,所以你可以稍后添加到列表中。

ladm@ash:~/src/scratch> ./a.out|head | sed 's/^/    /'
array[ 0] : (1,1)
array[ 1] : (2,2)
array[ 2] : (3,3)
Your location is : (3,3)
Your location is : (2,2)
Your location is : (1,1)
array[ 2] : (3,3)
array[ 1] : (2,2)
array[ 0] : (1,1)

ladm@ash:~/src/scratch> cat l.c | sed 's/^/    /'
#include <stdio.h>
#include <assert.h>

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

void getCoord(int *x, int *y) {
    static int num = 1;
    *x = *y = num++;
}

int main( int argc, char **argv) {

    const int N = 3;
    ListItem listN[ N];
    /* ListItem *listN = calloc( N, sizeof( ListItem));   */  /* Dynamic allocation method */

    /* First Coordinate */
    listN[ 0].next = NULL;                     /* Add item at front of list */
    getCoord( &listN[ 0].x, &listN[ 0].y);            /* Does the scanf stuff */

    /* Add new coords to the list */
    for (int i=1; i < N; i++) {
         getCoord( &listN[ i].x, &listN[ i].y);            /* Does the scanf stuff */

         listN[ i].next = &listN[ i-1];                     /* Add item at front of list */
    }

    /* List built */
    {
    ListItem *first = &listN[ N-1];

    /* Dump all the coords in backing store */
    for (int i = 0; i < N; i++) {
        printf("array[ %d] : (%d,%d)\n", i, listN[ i].x, listN[ i].y);
    }

    /* Print list following pointers - should be reversed */
    for (ListItem *l = first; l != NULL; l = l->next) {
        printf("Your location is : (%d,%d)\n", l->x, l->y);
    }
    /* Dump all the coords in backing store reversed */
    for (int i = N-1; i >= 0; --i) {
        printf("array[ %d] : (%d,%d)\n", i, listN[ i].x, listN[ i].y);
    }
    }
}

答案 1 :(得分:1)

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

#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);
        }

答案 2 :(得分:0)

如果您知道将拥有的位置数量,最好使用数组或矩阵,例如:

int x [30]; int y [30];

int location [60];

int location [2] [30];

并使用for循环迭代元素。

如果您不知道位置的数量,那么您希望在C中搜索LinkedList(或ArrayList)实现。互联网上有大量材料教授如何使用这些数据结构。

答案 3 :(得分:0)

迭代列表的好方法是创建give_successoris_after_last等函数,然后在for循环中使用它,就像这样:

struct ListItem *l;
struct ListItem *head;
for (l = head; !(is_after_last(l)); l = give_successor(l)) {
    do_stuff(l);
}

is_after_last的实施可能会因列表的实施而异:您可能需要检查l == NULLl是否指向列表末尾的人工元素。 函数give_successor看起来像这样:

struct ListItem *give_successor(l) {
    if (l == NULL)
        return NULL;
    return l->next
}

但是,我建议使用those等表格和库。