程序不打印我需要打印的内容

时间:2021-06-21 20:19:53

I want to create a sorted linked list, so everytime we insert an element, it's should be sorted (in ascending order). Now I have made a sorted linked list. The program is working fine, but there's an issue. It's not printing the middle and last inserted element in the sorted linked list, even though it's inserted.

我想创建一个有序链表,所以每次插入一个元素时,都应该按照升序排序。现在我已经制作了一个有序的链表。该计划工作正常,但有一个问题。它不会在已排序的链表中打印中间和最后插入的元素,即使它已插入。

My code is

我的代码是

 //Libraries
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>


struct Node{

    int val;
    struct Node  *next;
};

struct LLADT{

    struct Node *head;
};


// Initializing Linked List
void init(struct LLADT *LL){
    LL->head = 0;
}

// Sorted Inserted Linked List Function
void sortInsert(struct LLADT *LL, int num){
    struct Node *temp;
    struct Node *temp2;
    temp = (struct Node*)malloc(sizeof(struct Node));



// If list is Empty
    if(LL->head == 0){
        temp-> val = num;
        temp->next = NULL;
        LL->head = temp;
        return;
    }

// When list is not empty and key is smallest

    if(num < LL->head->val){
        temp-> val = num;
        temp->next = LL->head;
        LL->head = temp;
        return;

    }
// When list is not empty and we have to insert in middle
    temp2 = LL->head;
    temp-> val = num;
    temp->next = NULL;
    while(temp2->next !=0){
        if(num > temp2->next->val){
            temp2 = temp2->next;
        }
        else{

            temp2->next = temp->next;
            temp->next = temp2;
            return;
        }

    }


// When list is not empty and we have to insert at the end
    if(num > temp->val){

        temp->val = num;
        temp->next = NULL;
        temp2->next = temp;
        return;


    }
}

//Printing Linked List
void print_list(struct LLADT *LL){
    struct Node *temp;
    temp = LL-> head;
    while(temp !=0){
        printf("%d\n", temp->val);
        temp = temp -> next;
    }
}

// Main Function
int main(){

    struct LLADT LL;
    init(&LL);

    // inserting
    sortInsert(&LL,17);
    sortInsert(&LL,3);
    sortInsert(&LL,5);
    sortInsert(&LL,2);
    sortInsert(&LL,1);
    sortInsert(&LL,20);

    //Printing
    print_list(&LL);
    getch();
    return 0;
}

Output of this program is:

该计划的输出是:

1
2
3

And I am using Visual Studio 2012 Ultimate.

我正在使用Visual Studio 2012 Ultimate。

2 个解决方案

#1


1  

This is a simplified version. It checks if the new node belongs at the head of the list as a special case at the beginning. The it loops through the list until either the correct position or the end of the list is found.

这是一个简化版本。它检查新节点是否属于列表的开头,作为开头的特殊情况。它循环遍历列表,直到找到正确的位置或列表的结尾。

// Sorted Inserted Linked List Function
void sortInsert(struct LLADT *LL, int num)
{
    struct Node *newNode;
    struct Node *currentNode;
    struct Node *previousNode;

    // Initialise a new node for the new value.
    newNode = malloc(sizeof(struct Node));
    newNode->val = num;

    // If list is Empty or the new node is first in the list.
    if((NULL == LL->head) || (num < LL->head->val))
    {
        newNode->next = LL->head;
        LL->head = newNode;
        return;
    }

    // Iterate until last element or found position
    currentNode = LL->head;
    while((NULL != currentNode)&&(num >= currentNode->val))
    {
        // Move on to the next element
        previousNode = currentNode;
        currentNode = currentNode->next;
    }

    // Insert the new element between the previous and current
    previousNode->next = newNode;
    newNode->next = currentNode;
}

#2


1  

Your logic when inserting the middle of the list is faulty:

插入列表中间时的逻辑是错误的:

temp2->next = temp->next;
temp->next = temp2;

At this point, you want to insert the new node temp right after temp2. But instead, you have temp2 followed by the initial value of temp->next, which is NULL, then you have temp followed by temp2 (the opposite of what you want.

此时,您希望在temp2之后插入新节点temp。但相反,你有temp2后跟temp-> next的初始值,它是NULL,然后你有temp后跟temp2(与你想要的相反)。

So you have this:

所以你有这个:

           ----------------
temp -->   | num  |  NULL |
           ----------------

           ----------------     ----------------
temp2 -->  | v1   |   . --|---> | v2   |   . --|--> ...
           ----------------     ----------------

And you want this:

你想要这个:

                                temp --|
                                       v
           ----------------     ----------------    ----------------
temp2 -->  | v1   |   . --|---> | num   |   . --|--> | v2   |   . --|--> ...
           ----------------     ----------------    ----------------

So you do it like this:

所以你这样做:

temp->next = temp2->next;
temp2->next = temp;

On a side note, always use NULL instead of 0 when assigning or checking a NULL pointer. In most cases they're the same, but the standard doesn't say that they have to be.

另外,在分配或检查NULL指针时,始终使用NULL而不是0。在大多数情况下,它们是相同的,但标准并没有说它们必须是。

#1


1  

This is a simplified version. It checks if the new node belongs at the head of the list as a special case at the beginning. The it loops through the list until either the correct position or the end of the list is found.

这是一个简化版本。它检查新节点是否属于列表的开头,作为开头的特殊情况。它循环遍历列表,直到找到正确的位置或列表的结尾。

// Sorted Inserted Linked List Function
void sortInsert(struct LLADT *LL, int num)
{
    struct Node *newNode;
    struct Node *currentNode;
    struct Node *previousNode;

    // Initialise a new node for the new value.
    newNode = malloc(sizeof(struct Node));
    newNode->val = num;

    // If list is Empty or the new node is first in the list.
    if((NULL == LL->head) || (num < LL->head->val))
    {
        newNode->next = LL->head;
        LL->head = newNode;
        return;
    }

    // Iterate until last element or found position
    currentNode = LL->head;
    while((NULL != currentNode)&&(num >= currentNode->val))
    {
        // Move on to the next element
        previousNode = currentNode;
        currentNode = currentNode->next;
    }

    // Insert the new element between the previous and current
    previousNode->next = newNode;
    newNode->next = currentNode;
}

#2


1  

Your logic when inserting the middle of the list is faulty:

插入列表中间时的逻辑是错误的:

temp2->next = temp->next;
temp->next = temp2;

At this point, you want to insert the new node temp right after temp2. But instead, you have temp2 followed by the initial value of temp->next, which is NULL, then you have temp followed by temp2 (the opposite of what you want.

此时,您希望在temp2之后插入新节点temp。但相反,你有temp2后跟temp-> next的初始值,它是NULL,然后你有temp后跟temp2(与你想要的相反)。

So you have this:

所以你有这个:

           ----------------
temp -->   | num  |  NULL |
           ----------------

           ----------------     ----------------
temp2 -->  | v1   |   . --|---> | v2   |   . --|--> ...
           ----------------     ----------------

And you want this:

你想要这个:

                                temp --|
                                       v
           ----------------     ----------------    ----------------
temp2 -->  | v1   |   . --|---> | num   |   . --|--> | v2   |   . --|--> ...
           ----------------     ----------------    ----------------

So you do it like this:

所以你这样做:

temp->next = temp2->next;
temp2->next = temp;

On a side note, always use NULL instead of 0 when assigning or checking a NULL pointer. In most cases they're the same, but the standard doesn't say that they have to be.

另外,在分配或检查NULL指针时,始终使用NULL而不是0。在大多数情况下,它们是相同的,但标准并没有说它们必须是。