logo
down
shadow

Linked List Sorting with Strings In C


Linked List Sorting with Strings In C

By : SpiderXC
Date : November 21 2020, 07:01 PM
wish helps you When you enter AAA, BBB and then CCC namebox is always NULL when while loop finishes.
And then you are doing:
code :


Share : facebook icon twitter icon
Sorting linked list by sorting the actual nodes and not just by swapping node values

Sorting linked list by sorting the actual nodes and not just by swapping node values


By : Bart93
Date : March 29 2020, 07:55 AM
wish of those help This is the traditional and fastest way to merge sort a linked list. It's a bottom up style merge sort that uses an array of pointers to nodes to hold intermediate lists, where array[i] is either null or points to a list with 2 to the power i nodes. Nodes from the original list are merged into the array, then the lists in the array are merged to form a single sorted list. HP / Microsoft C++ STL std::list::sort uses the same algorithm.
code :
/* prototype */
NODE * MergeLists(NODE *pSrc1, NODE *pSrc2);

/* sort list using array of pointers to first nodes of list   */
/* aList[i] = NULL or ptr to list with 2 to the power i nodes */

#define NUMLISTS 32                 /* size of array */
NODE * SortList(NODE *pList)
{
NODE * aList[NUMLISTS];             /* array of lists */
NODE * pNode;
NODE * pNext;
int i;
    if(pList == NULL)               /* check for empty list */
        return NULL;
    for(i = 0; i < NUMLISTS; i++)   /* zero array */
        aList[i] = NULL;
    pNode = pList;                  /* merge nodes into array */
    while(pNode != NULL){
        pNext = pNode->next;
        pNode->next = NULL;
        for(i = 0; (i < NUMLISTS) && (aList[i] != NULL); i++){
            pNode = MergeLists(aList[i], pNode);
            aList[i] = NULL;
        }
        if(i == NUMLISTS)           /* don't go past end of array */
            i--;
        aList[i] = pNode;
        pNode = pNext;
    }
    pNode = NULL;                   /* merge array into one list */
    for(i = 0; i < NUMLISTS; i++)
        pNode = MergeLists(aList[i], pNode);
    return pNode;
}

/* mergelists -  compare uses src2 < src1           */
/* instead of src1 <= src2 to be similar to C++ STL */

NODE * MergeLists(NODE *pSrc1, NODE *pSrc2)
{
NODE *pDst = NULL;                  /* destination head ptr */
NODE **ppDst = &pDst;               /* ptr to head or prev->next */
    if(pSrc1 == NULL)
        return pSrc2;
    if(pSrc2 == NULL)
        return pSrc1;
    while(1){
        if(pSrc2->data < pSrc1->data){  /* if src2 < src1 */
            *ppDst = pSrc2;
            pSrc2 = *(ppDst = &(pSrc2->next));
            if(pSrc2 == NULL){
                *ppDst = pSrc1;
                break;
            }
        } else {                        /* src1 <= src2 */
            *ppDst = pSrc1;
            pSrc1 = *(ppDst = &(pSrc1->next));
            if(pSrc1 == NULL){
                *ppDst = pSrc2;
                break;
            }
        }
    }
    return pDst;
}
Runtime error sorting a linked list of strings

Runtime error sorting a linked list of strings


By : art.sanderson
Date : March 29 2020, 07:55 AM
hop of those help? char *str; ... strcpy(str,x->name); copies data pointed to by name to an someplace (it is undefined behavior) as str is an uninitialized pointer.
Just swap pointers.
code :
       if (strcmp(x->name,y->name) > 0) {
         char *temp = x->name;
         x->name = y->name;
         y->name = temp;
       }
sorting a linked list of strings by first letter

sorting a linked list of strings by first letter


By : Joseph M. Durnal
Date : March 29 2020, 07:55 AM
I wish did fix the issue. This solution would require three O(N) traversings of the list, so it will still be O(N). The problem with it is that it creates a new list, and the requirements seem to imply inplace sorting.
An inplace approach could be to go over the list, and move any items starting with A to the beginning and any items starting with C to the end. E.g.:
code :
for item in list:
    if item.data[0] == 'A'
        item.remove()
        list.prepend(item.data)
    elif item.data[0] == 'C'
        item.remove()
        list.append(item.data)
Sorting strings in a linked list

Sorting strings in a linked list


By : Sephirothphoenix
Date : March 29 2020, 07:55 AM
Hope this helps The function works quite ok but the problem comes in when I insert the same surname more than once. Please help, it's my school project. , You have a simple bug in the swapping part of your code:
Is sorting a Linked List using Quicksort really slower than Mergesort because of no random access in Linked List?

Is sorting a Linked List using Quicksort really slower than Mergesort because of no random access in Linked List?


By : user5588445
Date : March 29 2020, 07:55 AM
To fix the issue you can do I'm updating this answer to provide a better comparison. In my original answer below, I include an example of bottom up merge sort, using a small array of pointers to lists. The merge function merges two lists into a destination list. As an alternative, the merge function could merge one list into the other via splice operations, which would mean only updating links about half the time for pseudo random data. For arrays, merge sort does more moves but fewer compares than quicksort, but if the linked list merge is merging one list into the other, the number of "moves" is cut in half.
For quicksort, the first node could be used as a pivot, and only nodes less than pivot would be moved, forming a list prior to the pivot (in reverse order), which would also mean only updating links about half of the time for pseudo random data.
Related Posts Related Posts :
  • C error conflicting types
  • How do I convert text to XML in C?
  • Call by Reference Function
  • C programming XML
  • Reading a directory
  • C error Array: array type has incomplete element type.
  • Compiling C code in Windows XP
  • Is there any char to hexadecimal function for C?
  • c compilation error
  • 5x5 matrix multiplication in C
  • parent process, and a child process
  • Linked list recursive reverse
  • C warning conflicting types
  • how to print a char from struct
  • dynamic memory for 2D char array
  • C compiler producing lightweight executeables
  • Cannot Open Shared Object cygmpfr-1.dll
  • How to access a structure member in a function that get it as void* type?
  • Replace for loop with formula
  • how to find if stack increases upwards or downwards?
  • Why does my homespun sizeof operator need a char* cast?
  • Setting exit status when creating core dump
  • Signal Handling in C
  • How to implement B+ Tree for file systems?
  • Stack storage at function call
  • Manipulate the Clipboard in C on Mac OS X
  • Problem with writing a hexadecimal string
  • Why does this code read all ' ' for the anything after the 4th character?
  • GUI for linux c
  • Where do I put all these function-like #defines, in C?
  • Padding in 24-bits rgb bitmap
  • how to acess and change variable of kernel space from user space
  • Can a conforming C implementation #define NULL to be something wacky
  • send() always interrupted by EPIPE
  • Socket Select with empty fd set
  • Coding self similar fractals in computer graphics using C
  • Forking with Pipes
  • sscanf + c99 not working on some platforms?
  • Problem with C function of type char pointer, can someone explain?
  • Macros in C.... please give the solution
  • Unix Piping using Fork and Dup
  • Any difference between these two while loops?
  • Hashtable implementation for C
  • Problems with Linked List in C
  • Stack Allocation in C
  • Random function stuck on one number
  • printf following zeros to a certain degree
  • Measuring CPU clocks consumed by a process
  • How is a CRC32 checksum calculated?
  • Tips on redefining a register bitfield in C
  • Converting a program from c to bash script
  • Static library not included in resulting LLVM executable
  • What is the meaning of "wild pointer" in C?
  • Reading DWORD from binary file
  • Efficient way to find divisibility
  • Whats the fastest, portable way to hash pointers we know are pointer aligned to a fixed size int?
  • Why does my summation program behaves weird?
  • How to determine values saved on the stack?
  • Thread scheduling C
  • C - converting to 2s complement
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk