-1

What is the issue with the logic I used? I was trying to take a linked list and then sort and then perform searching but the sorting is not happening here. Is there any logic which I have used is wrong. I have compared one element to other and tried to change the node

I tried to take a linked list and sort it and then would have done the searching. But it is not sorting the linked list.

#include <stdio.h>
#include <conio.h>

int INFO[20];
int LINK[20];
int START;
int SEARCH(int);

void main()
{
    int PTR, ITEM, LOC;

    INFO[0] = 22;
    INFO[2] = 5;
    INFO[3] = 19;
    INFO[5] = 87;
    INFO[7] = 29;
    INFO[8] = 79;
    INFO[9] = 33;
    INFO[11] = 2;
    INFO[13] = 50;
    INFO[14] = 8;
    INFO[16] = 55;
    INFO[18] = 99;

    LINK[0] = 3;
    LINK[2] = 13;
    LINK[3] = 2;
    LINK[5] = 8;
    LINK[7] = 14;
    LINK[8] = 9;
    LINK[9] = 18;
    LINK[11] = 16;
    LINK[13] = 5;
    LINK[14] = -1;
    LINK[16] = 0;
    LINK[18] = 7;
    START = 11;
    PTR = START;
    printf("LIST: \n");
    while (PTR != -1)
    {
        printf("%d\t", INFO[PTR]);
        PTR = LINK[PTR];
    }
    int temp, index;
    printf("\n-------------------------\n");
    printf("\nSorted\n");

    //sorting begins
    // ISSUE IS HERE
    while (PTR != -1)
    {
        index = LINK[PTR];
        while (PTR != -1)
        {
            if (INFO[PTR] > INFO[index])
            {
                temp = INFO[PTR];
                INFO[PTR] = INFO[index];
                INFO[index] = temp;
            }
            index = LINK[index];
        }
        PTR = LINK[PTR];
    }
    PTR = START;
    while (PTR != -1)
    {
        printf("%d\t", INFO[PTR]);
        PTR = LINK[PTR];
    }

    printf("\n\nEnter the ITEM to be searched: ");
    scanf("%d", &ITEM);
    LOC = SEARCH(ITEM);

    if (LOC == -1)
        printf("\nITEM is not present in the list");
    else
        printf("\nITEM %d present at the INDEX location %d in the list", ITEM, LOC);
    getch();
}

int SEARCH(int I)
{
    int P = START;
    int L = -1;
    while (P != -1)
    {
        if (I == INFO[P])
        {
            L = P;
            break;
        }
        else
            P = LINK[P];
    }
    return (L);
}

I tried to take a linked list and sort it and then would have done the searching. But it is not sorting the linked list

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Mar 26 '22 at 14:36

1 Answers1

0

Your code never enters the sorting loop because you only initialize PTR before printing, but not before sorting:

PTR = START;
printf("LIST: \n");
while (PTR != -1)
{
    printf("%d\t", INFO[PTR]);
    PTR = LINK[PTR];
}

Once the printing is dome, PTR is -1 (that is when the print loop exits).

You never reset it to START or any other value, so the condition in the sort loop is always false, and the loop is never executed.

Start by resetting the PTR value like so:

printf("\nSorted\n");

PTR = START; //add this line

//sorting begins
// ISSUE IS HERE
while (PTR != -1)
{
    ....
}

There may still be an error in your sort logic, but it is very difficult to tell because you have an unusual implementation of a linked list, you did not specify what sorting algorithm you are trying to implement or in what order you want the list sorted, and also whether you want to sort the list order or the list values.

Lev M.
  • 6,088
  • 1
  • 10
  • 23
  • Sorry if I didn't able to clearly mention the doubt as it is the first time using stackoverflow I wanted to arrange the values in the ascending order using bubble sort. Here i used one list to store values and other to store the indexes. Also adding that line did not solve the issue – new programmer Mar 26 '22 at 14:00
  • @newprogrammer You are not really using a "list". You are using 2 static arrays to try and simulate a linked list, and this makes implementing anything much harder. Is there any special reason you are doing it this way? Any way, the sorting algorithm you wrote is not that of a bubble sort. Look here: https://en.wikipedia.org/wiki/Bubble_sort – Lev M. Mar 26 '22 at 14:57
  • @newprogrammer, your inner loop goes `while (PTR != -1)`, but you never update `PTR`. You probably want `while (index)` here. – M Oehm Mar 26 '22 at 14:59
  • @LevM.: That's a perfectly idiomatic way to write a linked list -- in Fortran 77. `:)` – M Oehm Mar 26 '22 at 15:01