【问题标题】:Linked list: Insert function points to itself after second insertion链表:第二次插入后插入函数指向自身
【发布时间】:2016-12-19 09:17:40
【问题描述】:

我正在处理链表,我要创建一个插入函数。该列表是从包含学生姓名和分数的文件创建的,并且以排序方式创建,第一次尝试我插入一个新节点没问题,但第二次尝试使新节点指向自身而不是指向 null 或节点之前插入的位置。我似乎找不到导致节点指向自身的线在哪里,而它在第一次尝试中没有发生!

typedef struct student
{
    char name[20];  
    int score;    
    struct student *next;
} Student_Data_Type;

Student_Data_Type *insert(Student_Data_Type *head, Student_Data_Type *p)
{
    if(head == NULL)//if the head is empty then create list 
    {
        head = Readfromfile(head);
    }
    Student_Data_Type *bufferStack = head;
    Student_Data_Type *prev;
    prev = malloc(sizeof(Student_Data_Type));
    bool inserted = false;
    while(bufferStack->next != NULL && 
      strcmp(bufferStack->next->name, p->name) < 0)

    {
        bufferStack = bufferStack->next;
    }
    p->next = bufferStack->next;
    bufferStack->next = p;
    printf("[####] ADDED %s   %d\n",bufferStack->next->name, bufferStack->next->score);//Second try says pointing to the same node
    prev = bufferStack->next;
    printf("[##] AND IS POINTING TO %s %d\n", prev->next->name, prev->next->score);
    inserted = true;
    return head;
}

这是第一个和第二个插入的输出:-

//This is the initial list created from the file
[###] DISPLAYING NAMES AND SCORE OF STUDENTS:- 
[###] ChenZhiheng <-----> 67
[###] GaoSuxiang <-----> 89
[###] MaQianli <-----> 90
[###] ZhangCheng <-----> 95
1.create list(read from file)
2.display all records
3.insert a record
4.delete a record
5.query
0.exit

//INSERT ONE
[###]ENTER NAME PLZ: Noor
[###] ENTER SCORE: 88
[####] ADDED Noor   88
[##] AND IS POINTING TO ZhangCheng 95
1.create list(read from file)
2.display all records
3.insert a record
.......

 //NOW DISPLAYING THE LIST AFTER INSERTING:-
[###] DISPLAYING NAMES AND SCORE OF STUDENTS:- 
[###] ChenZhiheng <-----> 67
[###] GaoSuxiang <-----> 89
[###] MaQianli <-----> 90
[###] Noor <-----> 88
[###] ZhangCheng <-----> 95
1.create list(read from file)
 ......
//THEN THE SECOND INSERT TRY
[###]ENTER NAME PLZ: Layla
[###] ENTER SCORE: 90
[####] ADDED Layla   90
[##] AND IS POINTING TO MaQianli 90
1.create list(read from file)
......
//THEN I CALL MY DISLAY FUNCTION AGAIN AND THIS IS THE OUTPUT:
[###] DISPLAYING NAMES AND SCORE OF STUDENTS:- 
[###] ChenZhiheng <-----> 67
[###] GaoSuxiang <-----> 89
[###] Layla <-----> 90
[###] MaQianli <-----> 90
[###] Layla <-----> 90
[###] MaQianli <-----> 90
[###] Layla <-----> 90
[###] MaQianli <-----> 90
[###] Layla <-----> 90
[###] MaQianli <-----> 90
 ....AND FOREVER LOOP,...

//HERE IS MY DISPLAY FUNCTION
void DisplayAll(Student_Data_Type *head)
{
 Student_Data_Type *stackbuffer = head;
 printf("[###] DISPLAYING NAMES AND SCORE OF STUDENTS:- \n");
 while(stackbuffer != NULL)
  {
   printf("[###] %s <-----> %d\n", stackbuffer->name, stackbuffer->score);
   stackbuffer = stackbuffer->next;
 }

}

【问题讨论】:

    标签: c data-structures struct linked-list insert


    【解决方案1】:

    您的整个prev 未使用,使用malloc 会浪费内存。在您的printf 语句中,您使用prev,这实际上是您原来的p,所以prev-&gt;next 是您原来的bufferStack-&gt;next,它可能是空的,也可能是您不想要的其他东西。

    就目前的代码而言,删除任何对 prev 的使用,它应该可以工作。您的插入代码似乎正确。

    p.s.:也删除inserted,因为它没有被使用。


    Silly Noor ...错误在于您没有向我们展示的部分,您的案例3:
    int main()
    {
    Student_Data_Type *head,*p;
        ...
        case 3:     
                ...
                strcpy(p->name, Student_Insert);  /// <-- copy where????
                p->score = Score_Insert;
                head = insert(head, p);
                break;
    

    现在,p 的内存分配在哪里???你很幸运你没有遇到分段错误,因为p 没有被初始化(它被初始化了,但那是因为你首先做了一个查询左右)。

    【讨论】:

    • 我已经删除了prev和insert,仍然没有改变@paul-ogilvie
    • 你的其余代码一定有问题,因为他插入的代码似乎正确。
    • 这里是github中的完整代码...github.com/farooqy/data_structure/tree/master
    • 你说得对,我忘了分配空间,我的编译器没有给我 seg 错误,但现在可以正常工作了,谢谢。
    猜你喜欢
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 2023-03-27
    • 2012-02-18
    • 1970-01-01
    相关资源
    最近更新 更多