In: Computer Science
C PROGRAMMING: SHIFT TYPEDEFS INSIDE OF AN ARRAY.
I have an array of NODES,
NODE nodes[513];
typedef struct node {
int weight;
} NODE;
I have a value called int num_nodes which gives the number of positions in that array which are filled.
What I need to do:
there is a value inside of nodes[0], but there are other values in the node array which can be found at index nodes[256] to nodes[256+num_nodes]. I need to shift all of these nodes that are in the far part of the array so that they appear after node[0]. the nodes which are unused have their weight set to -1. i need to shift till there are no -1 between root and the used nodes
How?
Also, I am NOT ALLOWED TO USE ARRAY BRACKETS [ ] ONLY POINTERS to navigate the array.
//C program
#include<stdio.h>
typedef struct node {
int weight;
} NODE;
NODE nodes[513];
int main(){
int i;
(nodes+0)->weight = 100;
int num_nodes = 20;;
for(i=1;i<513;i++)(nodes+i)->weight = -1;
for(i =256;i<256+num_nodes;i++)(nodes+i)->weight
= i+num_nodes;
printf("array before shifting\n ");
for(i=0;i<513;i++){
printf("%d
",(nodes+i)->weight);
if(i!=0&&i%10==0)printf("\n");
}
for(i=1;i<=num_nodes;i++){
(nodes+i)->weight =
(nodes+255+i)->weight;
(nodes+255+i)->weight= -1;
}
printf("\n\narray after shifting\n ");
for(i=0;i<513;i++){
printf("%d
",(nodes+i)->weight);
if(i!=0&&i%10==0)printf("\n");
}
return 0;
}