A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Curry

  • one year ago

AM i starting the first of this code correctly?

  • This Question is Closed
  1. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @dan815

  2. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    before when i made a linked list, we used nodes to long the values. which made it easier to traverse the list. but without it, i'm not quite sure how to traverse the list.

  3. e.mccormick
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Well, I would start with checking if the list is full. If it is, you can't insert without doubling the size first.

  4. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    you traverse this list like you would any array: list *ls = ...; for (int i = 0; i < ls->size; ++i) { int val = ls->sortedList[i]; ... }

  5. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    anyways, first you want to check if it's full (size == maxSize); if so, you want to reallocate it with double its previous capacity (maxSize) if (ls->size == ls->maxSize) { ls->maxSize *= 2; ls->sortedList = (int *) realloc(ls->sortedList, ls->maxSize); }

  6. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    now to find where to insert it, you need to loop through until you find the point where the elements become bigger than that which we want to insert; basically, if we had 1 3 4 8 10, and we wanted to insert 7, we'd loop through 1, 3, 4, and then once we got to 8 we would notice that 7 needs to be inserted in between 4 and 8

  7. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so: int i; for (i = 0; i < ls->size; ++i) { if (ls->sortedList[i] <= val) { /* val should be inserted between the elements at i-1, i */ break; } } /* shift over by 1 the elements from i to the end to make room for val */ for (int j = ls->size - 1; j >= i; --j) { ls->sortedList[j+1] = ls->sortedList[j]; } /* insert val at i */ ls->sortedList[i] = val;

  8. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.