logo
down
shadow

Confusion in adding an element to a sorted list of integers


Confusion in adding an element to a sorted list of integers

By : shazu143
Date : September 23 2020, 09:00 AM
Hope this helps The variable F is a pointer to a pointer to a Node.
Things might be clearer with this figure:
code :
      Node1 {
        V
 F ---> Next ----> Node2 {
      }              V
                     Next
                   }


Share : facebook icon twitter icon
Issue when adding element into sorted linked list C++

Issue when adding element into sorted linked list C++


By : user3049905
Date : March 29 2020, 07:55 AM
it fixes the issue That's because cur is a reference in add_ordered_i and you have cur = cur->next which also modify l.
I made a few changes and it works for me.
code :
template<class T>
void add_ordered_i (LN<T>*& l, T value) {
    LN<T>* cur = l;//just a normal pointer will be fine
    if(cur == nullptr)  {
        l = new LN<T>(value);//a empty list
        return;
    }
    while(cur->next != nullptr)
    {
        if(value < cur->next->value)
            break;
        cur = cur->next;
    }
    if(cur == l) {
        l = new LN<T>(value, cur);//add to head of list
    } else {
        cur->next = new LN<T>(value,cur->next);
    }
}
Adding element to a sorted list

Adding element to a sorted list


By : JAMES
Date : March 29 2020, 07:55 AM
Hope that helps The error only occurs when your second call for ListEl has a value for key larger than your first call. That's why the code was failing ~%50 of the time, because it all depends on which numbers were randomly called. As for "sometimes doesn't add 20 numbers and stop at 19", I'm not sure why this was happening, as I cannot reproduce this.
To fix your problem, from this answer, "If you need a pointer of a typedeffed type, declare an instance of one, but keep your typedefs to types so an not to mask levels of indirection."
code :
int i;
srand(time(NULL));
ListEl a = {0, NULL};
ListEl* head = &a; 
head = PushFront(head, rand()%100);
for (i = 0; i < 20; i++)
    head = PushFrontsort(head, rand()%100);
typedef struct _ListEl {
    int key;
    struct _ListEl *next;
} ListEl;
Haskell, own datatype, adding an Element to an sorted list

Haskell, own datatype, adding an Element to an sorted list


By : Данил Сидоренко
Date : March 29 2020, 07:55 AM
it should still fix some issue working version with minimalistic changes, thx @Matt, Petr and Landei!!! , I see two major problems with ins:
Interpreting "takes as input a sorted list of N integers output will be sorted squared integer list"

Interpreting "takes as input a sorted list of N integers output will be sorted squared integer list"


By : zsigmonda
Date : March 29 2020, 07:55 AM
Hope that helps I think the requirement is very clear - you need to write a function that receives a sorted list of integers, and returns a list of their squares. E.g., in Java:
Insert a node into a sorted linked list of integers such that the list still remains sorted with final members for next

Insert a node into a sorted linked list of integers such that the list still remains sorted with final members for next


By : Rodrive
Date : September 07 2020, 12:00 AM
it fixes the issue When the next link is final:
The only theoretical way would be to add a new node in front of the list and shift the data:
code :
void insertSorted(MyList list, int data) {
    list.head = new Node(0, list.head); // Insert in front;
    Node prior = list.head;
    // Invariant condition: prior points to a node (not null) and soon data >= prior.data
    Node current = prior.next;
    while (current != null) {
        if (data < current.data) {
            break;
        }
        prior.data = current.data; // Shift smaller
        prior = current;
        current = current.next;
    }
    prior.data = data;
}

insert:      d
list.head:   a ; b ; c ; e ; f ; g
--------------------------------------
list.head:   X ; a ; b ; c ; e ; f ; g
             a <-´   |   |
                 b <-´   |
                     c <-´
                         d
Related Posts Related Posts :
  • Avoid Overflow when Calculating π by Evaluating a Series Using 16-bit Arithmetic?
  • How to handle data or char -1 when reading from file, since EOF is also -1
  • Compare every 1kb of contents of two files instead of character by character
  • Understanding functions in stm8s disassembly?
  • Recursive and no-recursive get different answer
  • Can anyone explain the following output of the code?
  • How to resync time from NTP server in esp-idf?
  • Output for the following C code is confusing me
  • Determine if window is visible with High CPU
  • Writing a file by taking input from user only saves value for "x" only
  • How should I fill an array of ints from a file using command line args? The size of the file and the no. of elements may
  • Segmentation Fault While Reading File in C
  • Fill an array at index n with m times data without bit-fields
  • Why don't the values I type in the terminal match the values I've already stored in the array?
  • MPLABX XC16 unable to resolve built-in identifier __builtin_dmaoffset
  • How call and compile function from elf to my binary?
  • Modifying non-const char array that is referred to by const char array
  • Does anyone see any mistakes here? I am trying to get a message via a pipe, from my parent to child
  • Memory Leak - Singly linked list in C
  • Trouble with translation of typedef between C to D
  • Why does SIGINT stop sleep more than one time?
  • How to pause a timer created with CreateTimerQueueTimer
  • How can I read data from file ? I can't fix it
  • Scanf in visual studio not accepting multiple cases of characters
  • How can I determine if a char* is a windows line ending?
  • I am trying to store value in long long int type but giving wrong return
  • How to manage different kinds of data in a linked list?
  • uint8_t not rollover to 0 after reaching 255 not working properly
  • Why do these two execvp produce different results?
  • Pass uintmax_t or size_t to custom printf conversion specifier
  • Why does free() leaves stuff in memory?
  • Why pointers can't be used to index arrays?
  • memory allocation eror in C
  • C custom datatypes mapped to C datatypes grouped under a single struct
  • pipe() data is not transferred to child process
  • Getting a core dump from a simple C program
  • Fatal error on makefile, need to understand the problem
  • How can I add a delay of 90 minutes when a port has gone from 0 to 1?
  • To use strcpy or not
  • the usage of strtok() in c shows warnings and returns segmentation fault(core dumped)
  • Trouble allocating array of structs
  • Only first char of user input used in array
  • Why does "int x = 5; printf("%d %d %d", x==5, x=10, x==5);" in C print "0 10 0"?
  • How to scan specific string format in C?
  • sscanf skipping the final value when reading multiple values from a line
  • How can I access full memory space in FreeDOS with C application
  • Semantics of sem_getvalue() in POSIX
  • What does the [x,y] symbol mean in a multidimensional array access?
  • compilation error: cast from pointer to integer of different size
  • why am i getting compilation error "error: conflicting types for ‘ptr’ " for the following code? static int va
  • Why does getw return -1 when trying to read a character?
  • Why C program in whch two functions call each other recursively gives segmentation fault on linux?
  • ssize_t is undefined
  • Passing a generic argument in a C function
  • Restoring stdout after redirecting it via freopen()
  • Tuples "cannot be marshalled in a foreign call" when trying to export a Haskell function to C
  • Dereferencing double pointer to pass to recursive function
  • Why is the output different in these two scenarios
  • what will happen if we dont use free() for allocated memory
  • counting words from user input
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk