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

error when trying to run linked list c program

我是新手 programming.So 这里的问题是当我想执行下面的程序时,它只是显示程序已经停止 working.I 不知道有什么问题代码因为没有编译error.Any帮助将不胜感激。

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

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

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

};

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

: 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 的值。

现在轮到您根据需要更正您的代码了。

快乐编码:D

我想你的意思是:)

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

};

没有意义。

另外你不需要为头部分配一个单独的节点。头部应该是指向第一个分配的 "student".

的指针

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