链接列表:第二次插入后插入功能点自身

时间:2016-12-19 09:17:40

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

我正在处理链表,我正在制作插入功能。该列表是从包含学生姓名和分数的文件创建的,并且按照排序的方式,第一次尝试我插入新节点就可以,但第二次尝试使新节点指向自身而不是指向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;
 }

}

1 个答案:

答案 0 :(得分:2)

您的整个prev未使用,使用malloc会浪费内存。在您的printf声明中,您使用prev,这实际上是您的原始p,因此prev->next是您原来的bufferStack->next,可能是空的,或者可能是您不喜欢的其他内容不打算。

正如您的代码现在所示,请删除对prev的任何使用,它应该有效。您的插入代码似乎是正确的。

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

<小时/> 愚蠢的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没有初始化(它被初始化,但那是因为你第一次做了一个查询)。