Why Use a Helper Variable When Inserting an Element in an Array?

  • Context: MHB 
  • Thread starter Thread starter mathmari
  • Start date Start date
  • Tags Tags
    Array
Click For Summary

Discussion Overview

The discussion revolves around the insertion of elements into an array, specifically focusing on achieving O(1) time complexity. Participants explore the use of helper variables and data structures, such as linked lists and arrays, in the context of implementing a star system program.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants inquire about achieving O(1) insertion time in arrays and linked lists, questioning the necessary variables for this operation.
  • One participant describes a structure for a star system and proposes a function for its constitution, seeking validation of their approach.
  • Another participant suggests tracking the number of star systems separately to facilitate insertion, proposing the use of a variable to manage the current count.
  • There is a discussion about whether to use a specific field in the structure to find the first free position or to implement a linked list instead.
  • Participants express confusion about the implications of using an array versus a linked list for managing star systems, debating the correct approach for insertion.
  • Concerns are raised regarding the consistency of the code provided, particularly in how structures and arrays are defined and used.
  • One participant questions the syntactical correctness of a proposed code snippet, highlighting potential compilation issues.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach for inserting elements into the array or linked list. Multiple competing views remain regarding the use of helper variables and the structure of the data.

Contextual Notes

There are unresolved questions about the definitions and roles of certain variables and structures, as well as the implications of using arrays versus linked lists. Some code snippets are noted to be syntactically correct, but their functionality remains uncertain.

Who May Find This Useful

This discussion may be useful for programmers and computer science students interested in data structures, particularly those exploring efficient insertion methods in arrays and linked lists.

  • #91
I like Serena said:
The first is correct! (Smile)

But the second is not. (Worried)
Sentinal is a data member of StarS[k], and not of last->next.
You should set StarS[k].Sentinal to last->next, which is by now the same as plansystem.Alternatively, you could do at the end: [m]StarS[k].Sentinal = plansystem[/m].
That works because [m]plansystem[/m] is always added to the end, wherever that is. So afterwards it is the last node in the list, which is where [m]Sentinal[/m] should point. (Nerd)

Do you mean that we could write it as followed??

Code:
int Beginning(int solid, int ss){
        int i, k=-1;
        /* Add a planetary system to the list of the planetary system of the star system */
        plansys_t *plansystem = calloc(1, sizeof(plansys_t));
        
        plansystem->solid=solid;
        plansystem->asteroids=NULL;
        plansystem->next=NULL;
	for(i=0; i<Sfreep; i++){
		if(StarS[i].ss==ss){
			k=i;
			if (StarS[k].plasy == NULL) {
                                   /* List is empty: replace with new plansystem */
                                   StarS[k].plasy = plansystem;
                        } else {
                                    /* Find last node */
                                    plansys_t *last = StarS[k].plasy;
                                    while (last->next != NULL) {
                                             last = last->next;
                                    }
                                    /* Append new plansystem to the last node */
                                    last->next = plansystem;
                        }
                        StarS[k].Sentinal = plansystem;
                }
        }
	return k;
}

(Wondering)
I like Serena said:
The way the code is indented, it is hard to follow the flow of the program, and understand if and where something is wrong and should be changed. (Sweating)

Better is:
Code:
int Beginning(int solid, int ss){
    int i, k = -1;
    /* Add a planetary system to the list of the planetary system of the star system */
    plansys_t *plansystem = calloc(1, sizeof(plansys_t));
    
    plansystem->solid = solid;
    plansystem->asteroids = NULL;
    plansystem->next = NULL;
    for(i=0; i<Sfreep; i++){
        if(StarS[i].ss == ss){
            k=i;
            if (StarS[k].plasy == NULL) {
                /* List is empty: replace with new plansystem */
                StarS[k].plasy = plansystem;
                StarS[k].Sentinel = StarS[k].plasy;
            } else {
                /* Find last node */
                plansys_t *last = StarS[k].plasy;
                while (last->next != NULL) {
                    last = last->next;
                }
                /* Append new plansystem to the last node */
                last->next = plansystem;
                last->next->Sentinel = last;
            }
        }
    }
    return k;
}

Like this it becomes clear where the for-loop starts and ends, where the first if-statement is, and where the nested if-else statement is (and that there is one). (Whew)

Ok! (Blush)
 
Technology news on Phys.org
  • #92
mathmari said:
Do you mean that we could write it as followed?? (Wondering)

Yep! (Nod)
 
  • #93
mathmari said:
The structures have the following form:

View attachment 3540

I am looking again at the program and I got stuck...

Code:
asteroid_t *p=StarS[k].plasy->asteroids;
while(p != NULL && p->as != as){
		p=p->next;
}
if(p != NULL && p->as == as){
		k=i;				
}
After [m]p=p->next;[/m] where does [m]p[/m] point?? At the next node of the planetary system, plasy, or the next asteroid?? (Wondering)
 
  • #94
Hey! :o

mathmari said:
After [m]p=p->next;[/m] where does [m]p[/m] point?? At the next node of the planetary system, plasy, or the next asteroid?? (Wondering)

Code:
asteroid_t *p=StarS[k].plasy->asteroids;

After the initial assignment, [m]p[/m] points to the first asteroid of the first planetary system of star system [m]k[/m].

Code:
while(p != NULL && p->as != as){
		p=p->next;
}

After p=p->next, p points to the next asteroid of the first planetary system. (Nerd)
 
  • #95
Ok! (Malthe)

To find the asteroid with identifier "as" in the star system I had written the following part:

Code:
	int i, k=-1;
	asteroid_t *p=NULL;
	asteroid_t *ast=NULL;
	for(i=0; i<Sfreep; i++){
			ast=StarS[i].plasy->asteroids;
			while(ast != NULL && ast->as != as){
				ast=ast->next;
			}
			if(ast != NULL && ast->as == as){
				k=i;				
			}
	}

	if(k == -1){
		return -1;
	}
		
	p=StarS[k].plasy->asteroids;
	
	if(p == NULL){
		return -1;
	}

But in this way do we not look only the asteroids of the first planetary system?? (Wondering)

Should it be as followed??

Code:
	for(i=0; i<Sfreep; i++){
                        Plan_Syst=StarS[i].plasy;
                        while(Plan_Syst != NULL){ 
                                ast=Plan_Syst->asteroids;
			        while(ast != NULL && ast->as != as){
				        ast=ast->next;
			        }
			        if(ast != NULL && ast->as == as){
				        k=i;	
                                        A=ast;			
			       }
                               Plan_Syst=Plan_Syst->next; 
	                }
        }
	if(k == -1){
		return -1;
	}
		
	p=A;
	
	if(p == NULL){
		return -1;
	}

(Wondering)
 
  • #96
mathmari said:
Ok! (Malthe)

But in this way do we not look only the asteroids of the first planetary system?? (Wondering)

Should it be as followed??

(Wondering)

Yep. To search all asteroids it should be like that. (Wasntme)
 
  • #97
I like Serena said:
Yep. To search all asteroids it should be like that. (Wasntme)

Ok! (Malthe) I have also an other question...
I like Serena said:
Code:
int nrStarS = 0;

int Constitution(int ss) {
        int i;

        // Add a star system to the the array of stars
        starsy_t *starsystem = &StarS[nrStarS];
        nrStarS++;

        // Initialize the new star system
        starsystem->ss = ss;
        starsystem->plasy=StarS[i].Sentinel;
	for(i=0; i<max; i++){
		starsystem->ffplan[i].fp=INT_MAX;
		starsystem->ffplan[i].ff=NULL;
	}

        // Return the index of the star system that was added
	return (nrStarS - 1);
}

int main(){
	Constitution(12);
	for(i=0; i<nrStarS; i++){
		printf("The elements are: %d \n", StarS[i].ss);
	}
	return 0;
}

Could you explain to me why we write [m]starsystem = &StarS[nrStarS];[/m] and not [m]&StarS[nrStarS]=starsystem[/m] ?? (Wondering)
 
  • #98
mathmari said:
Could you explain to me why we write [m]starsystem = &StarS[nrStarS];[/m] and not [m]&StarS[nrStarS]=starsystem[/m] ?? (Wondering)

The structure [m]StarS[nrStarS][/m] is an entry in a global array. Its address is fixed and cannot change. That means it makes no sense to try and assign anything to [m]&StarS[nrStarS][/m].

The pointer [m]starsystem[/m] is a helper variable. We let it point to the location of [m]StarS[nrStarS][/m], so that we can manipulate it more easily, and so that the code reads better. That's why we assign the address [m]&StarS[nrStarS][/m] to it. (Nerd)
 
  • #99
I like Serena said:
The structure [m]StarS[nrStarS][/m] is an entry in a global array. Its address is fixed and cannot change. That means it makes no sense to try and assign anything to [m]&StarS[nrStarS][/m].

The pointer [m]starsystem[/m] is a helper variable. We let it point to the location of [m]StarS[nrStarS][/m], so that we can manipulate it more easily, and so that the code reads better. That's why we assign the address [m]&StarS[nrStarS][/m] to it. (Nerd)

How can the array [m]StarS[/m] have the value of the new starsystem at the position nrStarS, when we assign the [m]&StarS[nrStarS][/m] to [m]starsystem[/m] ?? (Wondering)

[m]nrStarS[/m] is the first free position of the array, so if we assign [m]&StarS[nrStarS][/m] to the new [m]starsystem[/m] doesn't it get the value [m]NULL[/m] ?? (Wondering)
 

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
7
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
235
Views
14K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K