Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:27:03+00:00 2026-06-14T04:27:03+00:00

When designing a dynamic array which will shrink, what is a method to keep

  • 0

When designing a dynamic array which will shrink, what is a method to keep track of the highest used index, and when that index’s held object is deleted, find the new highest used index.

Right now I can think up having a a simple int last_used and then charging the cost of maintaining this variable to the func_delete which must check if it deletes the highest, and if so, check each smaller value looking for a non null. (array will always be null initialized)

if(last_index == deleted_index){

    while(last_index >0 && array[--last_index] != NULL)

    // if the array is now only half full, I realloc 
} 

Is there any other smart ways?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:27:05+00:00Added an answer on June 14, 2026 at 4:27 am

    It looks okay to me, but logic wise, if the function func_delete always deletes the highest item, then there shouldn’t be any NULL values at smaller indices. So this should do:

    if(last_index == deleted_index && array[last_index] != NULL) {
        //delete last item
        free(array[last_index]);
        //set to NULL and decrement
        array[last_index--] = NULL;
    } 
    

    Edit:

    based on your comments, I understand what you’re trying to do, I think you could just keep track of the highest used index in the insertion function instead:

    void array_insert(array , element e, int index) 
    {
        if (index > arr->highest_index) {
           arr->highest_index = index;
        }
        //insert element
    }
    

    And when you delete you check if the index is the highest or not. like you’re doing, don’t think there’s a better way to do that, without complicating things further, for example, you could keep another sorted list of indices, this way you when you delete the highest index you find the next one in constant time, but like I said, it complicates things. However, I think another data structure might be more useful, a linked list for example, is more efficient when randomly deleting nodes, but not when randomly inserting nodes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently designing an webpage which will be used to display dynamic content
I'm designing a dynamic SQL-query that will check a source-table against a target table.
I'm designing an ASP.NET Web Forms application that will be used to query the
I am designing a dynamic site for some form of weather data, which I
I'm designing a dynamic web page that loads contents by AJAX, like the wall
Background I am designing a system which enables the development of dynamic authentication schemes
I'm designing a simple website with no dynamic content that I want to be
Supposing you were designing an image gallery website that uses dynamic images. By dynamic,
I am having some problems designing a schema for dynamic properties with versioning. Assume
I'm designing a game object in F#. In C++, I would make classes to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.