C - 打印功能中的链接列表数组仅打印最后一个值&得到分段错误

时间:2017-03-30 21:00:55

标签: c arrays linked-list singly-linked-list

我的代码应该做什么?

  • 我试图创建一个数组,在每个索引存储一个指向链表的指针。
  • 用户输入不同的号码,这些号码将根据其最后一位数字存储在链接列表中。例如。如果用户输入为10,那么10将存储在数组[0]的链表中,因为10的最后一位是0.如果输入它是72,那么它将被存储在数组[2]的链表,因为最后一位是2,依此类推。
  • 代码应迭代数组,并以这种方式打印链接列表:10-> 60-> 50-> Null(数字不必按顺序排列)。

有什么问题?

  • 输出只是一个地址,后跟输入的最后一个数字,它在非停止循环中打印相同的内容。仅当数字以0结尾时才会发生这种情况。

  • 当我尝试输入以0以外的数字结尾的数字时,我得到"分段错误:11"。用户应该能够输入随机整数,而不一定是那些以0结尾的整数。

这是我的代码:

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

typedef struct mylist {       // This structure defines linked list nodes; structure
    int x; // data
    struct mylist * nextnode;
}Node;

void userData(Node* head){ //prompts the user for data input and this data will then be assigned to the node.
    int data;
    printf("Enter new data: ");
    scanf("%d", &data);
    head -> x = data;
}

void printer(Node* arraylinks[]){       //function prints the linked lists in the array.
    printf("entering linked lists printer\n\n");
    int i = 0;
    Node* current;
    for(i = 0; i < 10; i++){       // iterates the array
        current = arraylinks[i]; //assign the first pointer of the linked list to current
        while( current != NULL){   //traverse the linked list
            printf("%d -> ", current -> x);
            current = current -> nextnode;
        }
        printf("null\n");
    }
    printf("\n");

}

void linkedlists(Node* arraylinks[], Node* pointer, Node* tempPointer){
    int x = 10;
    int lastdigit;

    do{

        userData(pointer); //calls linkedlists() to get the data from the user input

        lastdigit = pointer-> x % 10; //checks the last digit of the number entered by the user. if the number ends in 0 it will be stored in the linked list at arraylinks[0]

        //tempPointer = arraylinks[lastdigit];
        //tempPointer -> nextnode = pointer;

        if(arraylinks[lastdigit] == NULL){  // if the array[lastdigit] is empty then the new node is added but with next node as NULL
            arraylinks[lastdigit] -> nextnode = pointer;
            pointer -> nextnode = NULL;
        }

        else{
            tempPointer = arraylinks[lastdigit]; // tempPointer gets assigned temporarily the existing linked list that's stored in that array[lastdigit]
            arraylinks[lastdigit] -> nextnode = pointer;//arraylinks[lastdigit] now points to the the newnode entered.
            pointer -> nextnode = tempPointer; // the new added node points to the rest of the linked list, that was temporarly saved in tempPointer
        }

        x--;
    } while(x > 0);

    printf("\ngoing out of the while\n\n");
    printer(arraylinks); // printer function to display the linked lists that are in the array.
}

int main(){

    Node* arraylinks[10];     //array of linked list. Each pointer points to the first item in a linked list
    Node* pointer = (Node*)malloc(sizeof(Node));
    Node* tempPointer =(Node*)malloc(sizeof(Node)) ;

    linkedlists(arraylinks, pointer, tempPointer);
 }

感谢任何帮助。提前致谢!

1 个答案:

答案 0 :(得分:0)

我们初学者应该互相帮助。:)

注意列表数组未初始化

Node* arraylinks[10];     

这些节点分配和函数调用及其相应的定义没有意义。

Node* pointer = (Node*)malloc(sizeof(Node));
Node* tempPointer =(Node*)malloc(sizeof(Node)) ;

linkedlists(arraylinks, pointer, tempPointer);

程序可以按以下方式显示,如下所示。

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

// This structure defines linked list nodes;
typedef struct mylist 
{
    int data;
    struct mylist *next;
} Node;

#define N   10

void display( Node * arrayLinks[] )
{
    for ( size_t i = 0; i < N; i++ )
    {
        for ( Node *current = arrayLinks[i]; current; current = current->next )
        {
            printf( "%d -> ", current->data );
        }
        puts( "null" );
    }

    putchar( '\n' );
}

void push( Node * arrayLinks[], int data )
{
    int lastDigit = data % N;
    if ( lastDigit < 0 ) lastDigit = -lastDigit;

    Node *tmp = malloc( sizeof( Node ) );

    if ( tmp )
    {
        tmp->data = data;
        tmp->next = arrayLinks[lastDigit];
        arrayLinks[lastDigit] = tmp;
    }
}

void clear( Node * arrayLinks[] )
{
    for ( size_t i = 0; i < N; i++ )
    {
        while ( arrayLinks[i] )
        {
            Node *tmp = arrayLinks[i];
            arrayLinks[i] = arrayLinks[i]->next;
            free( tmp );
        }
    }
}

int main(void) 
{
    //array of linked list. Each pointer points to the first item in a linked list
    Node * arrayLinks[N] = { 0 };

    size_t n = 0;

    printf( "Enter number of items you are going to add to the lists (0 - exit): " );
    scanf( "%zu", &n );

    for ( size_t i = 0; i < n; i++ )
    {
        int data;

        printf("Enter new data: ");
        if ( scanf( "%d", &data ) != 1 ) break;

        push( arrayLinks, data );
    }

    display( arrayLinks );

    clear( arrayLinks );

    return 0;
}

程序输出可能看起来像

Enter number of items you are going to add to the lists (0 - exit): 10
Enter new data: 10 
Enter new data: 20 
Enter new data: 31 
Enter new data: 42 
Enter new data: 52 
Enter new data: 65 
Enter new data: 77 
Enter new data: 85 
Enter new data: 99 
Enter new data: 9
20 -> 10 -> null
31 -> null
52 -> 42 -> null
null
null
85 -> 65 -> null
null
77 -> null
null
9 -> 99 -> null