尝试运行链表c程序时出错

时间:2015-04-02 13:31:31

标签: c error-handling

我是编程的新手。所以这里的问题是,当我想执行以下程序时,它只是显示程序已停止工作。我不知道代码有什么问题,因为没有编译错误。任何帮助将不胜感激。

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

struct ppl
{
    char name[30];
    struct ppl *next;
};

main()
{

    struct ppl *student1, *student2, *student3, *student4, *temp, *ptr, *x;
    struct ppl *head = NULL;
    int no;

    head = (struct ppl *)malloc(sizeof(struct ppl));
    student1 = (struct ppl *)malloc(sizeof(struct ppl));
    student2 = (struct ppl *)malloc(sizeof(struct ppl));
    student3 = (struct ppl *)malloc(sizeof(struct ppl));
    student4 = (struct ppl *)malloc(sizeof(struct ppl));

    head->next = student1;
    strcpy(student1->name, "Aizar");

    student1->next = student2;
    strcpy(student2->name, "Chandi");

    student2->next = student3;
    strcpy(student3->name, "Faizul");

    student3->next = student4;
    strcpy(student4->name, "Joshua");
    student4->next = NULL;

    ptr = head;
    while (ptr->next != NULL)
    {
        ptr = (struct ppl *)malloc(sizeof(struct ppl));
        ptr = ptr->next;
        printf("Name list : %s \n", ptr);

    };

    return 0;
}

3 个答案:

答案 0 :(得分:0)

问题出在while循环中。试试这个:

while(ptr->next != NULL)
{
 ptr = ptr->next; 
    printf("Name list : %s \n", ptr );

};

PS:ptr不需要单独的内存分配。它将指向分配给它的指针指向的内存位置(在while循环中)。

答案 1 :(得分:0)

注意 don't give me fish teach me how to fish

一些评论:

 ptr = head; // here ptr has the value of head
    while (ptr->next != NULL)
    {
        ptr = (struct ppl *)malloc(sizeof(struct ppl)); // ptr loses the value of head and takes new value given by malloc
        ptr = ptr->next; // ptr -> next is null so ptr becomes null
        printf("Name list : %s \n", ptr); // generally if you want to print pointers use %p not %s 

    };

一些迹象:

  1. 我想你要打印学生名字而不是指针。
  2. 不需要为ptr分配内存,因为在while循环之前它需要head的值。
  3. 现在轮到您根据需要更正代码了。

    快乐编码:D

答案 2 :(得分:0)

我认为你的意思如下:。)

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

struct ppl
{
    char name[30];
    struct ppl *next;
};

int main( void )
{

    struct ppl *student1, *student2, *student3, *student4, *temp;
    struct ppl *head = NULL;

    student1 = (struct ppl *)malloc( sizeof( struct ppl ) );
    student2 = (struct ppl *)malloc( sizeof( struct ppl ) );
    student3 = (struct ppl *)malloc( sizeof( struct ppl ) );
    student4 = (struct ppl *)malloc( sizeof( struct ppl ) );

    strcpy( student1->name, "Aizar" );
    student1->next = student2;

    strcpy( student2->name, "Chandi" );
    student2->next = student3;

    strcpy( student3->name, "Faizul" );
    student3->next = student4;

    strcpy( student4->name, "Joshua" );
    student4->next = NULL;

    head = student1;

    for ( temp = head; temp != NULL; temp = temp->next )
    {
        printf( "Student Name: %s \n", temp->name );
    }

    while ( head )
    {
        temp = head;
        head = head->next;
        free( temp );
    }

    return 0;
}

程序输出

Student Name: Aizar 
Student Name: Chandi 
Student Name: Faizul 
Student Name: Joshua 

至于你的代码然后这个循环

while (ptr->next != NULL)
{
    ptr = (struct ppl *)malloc(sizeof(struct ppl));
    ptr = ptr->next;
    printf("Name list : %s \n", ptr);

};

没有意义。

此外,您无需为头部分配单独的节点。头部应该是指向第一个分配的“学生”的指针。

考虑到C中的函数main应具有返回类型int