Sorting characters in an ADT List

If the given list is 'a' 'c' 'd' 'e' then the user inputs 'b' @ position 3 (assume position-1) then it should sort to 'a' 'b' 'c' 'd' 'e' ignoring the position. The problem is that the character is only placed at the last index. Same happens if there is the same character existed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void InsertSorted_VER1(LIST *L, Position *p, Elem x)
{
    int i,j,k,a;
    Position newP = *p; //unlink pointer so that it can go through the Insert() function

    Insert(x,newP,L);
    if((*L).A[(*L).Last] != x) //checks if last value is same in input
    {
        for(i=0;(*L).A[i] == x;i++);
        for(j=0,++(*L).Last;j<=(*L).Last;j++)
        {
            if((*L).A[j] < (*L).A[i]) //if position was placed by the user in the middel
            {
                for(k=j;k<=(*L).Last;(*L).A[k] = (*L).A[k+1],k++);
                (*L).A[j] = (*L).A[i];
                break;
            }
            else if((*L).A[j] == (*L).A[i]) //checks if there is a same value
            {
                for(a=(*L).Last;a>j;(*L).A[a] = (*L).A[a-1],a--); //transfer it next to the same value
                (*L).A[a] = (*L).A[i];
                for(a=i;a<=(*L).Last;(*L).A[a] = (*L).A[a+1], a++);
                break;
            }
        }
        (*L).Last--;
        *p = j; //doggy style
    }
}


God I need to sleep.
Last edited on
God I need to sleep.

You also need to improve your otherwise atrocious style. Nothing personal.

It's up to you to format your code so that it's readable.
It's up to you to use L->A[j] and L->Last instead of (*L).A[j] and (*L).Last, which only clutter your code with parentheses.

I could go on about why sensible usage of whitespaces and empty lines increases readability, but that's not what you're here for.

So. What does Insert(x,newP,L); do?
And how do you know that's not causing the problem instead?

You also did not give your LIST structure's code.
Knowing how your list looks on the inside helps people intuit your "plan".
Topic archived. No new replies allowed.