0

Hi this is the code I wrote for create as many nodes as he needs (the m variable), but I noticed that using this method I'm creating one more node. What's the best way of fcreating as many nodes as the user say us?

#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
    int val;
    int rip;
    struct Node *next;
} node;

node *modify(node *head);

void print(node *head2);

int main(){

    int m, i;

    printf("How many nodes: \n");
    scanf("%d", &m);

    node *head = NULL;
    head = (node *)malloc(sizeof(node));
    node *temp = head;
    node *head2 = NULL;

    printf("Write the value in HEAD position : \n");
    scanf("%d", &temp->val);
    temp->rip=0;
    temp->next = NULL;

    for(i=0; i < m-1; i++)
    {
        temp->next = (node *)malloc(sizeof(node));
        printf("Write the value in position %d: \n", i);
        temp = temp->next;
        scanf("%d", &temp->val);
        temp->rip=0;
        temp->next = NULL;  
    }

    head2 = modify(head);

    print(head2);

    return 0;
}

node *modify(node *head){

    int counter, pass, m;

    node *curr = head;
    node *track = head;
    node *precNode;

    while (track != NULL){
        counter = 0;
        pass = 0;
        m = track->val;
        while( curr != NULL){
            if(m == (curr)->val){
                pass++;
                counter++;
                if(pass > 1){
                    node *removed = curr;

                    precNode->next = (curr)->next;

                    curr = (curr)->next;
                    free(removed);

                }
                if(pass == 1)
                {
                    precNode = curr;
                    curr = curr->next;
                }
            }
            else{
                precNode = curr;
                curr = (curr)->next;
            }    
        }
        track->rip = counter;
        track = track->next;
        curr = track;
    }

    return head;
}

void print(node *head2){

    while(head2 != NULL){
        printf("[%d, %d]  ->   ", head2->val, head2->rip);
        head2 = head2->next;
    }
    printf("\n");
} 
```
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 3
    Well, it would be nice to see more code and a run, but seems to me this code does allocate m+1 nodes. You allocate the head and then m more. – Frank Merrow Feb 14 '20 at 01:16
  • 1
    ...and to elaborate on Frank's comment: If you want to allocate only `m` nodes, instead of `m+1`, then you should do _exactly what you've done, except, you should allocate one fewer node than that._ Ta-da! – Quuxplusone Feb 14 '20 at 03:12
  • Yes I'm allocating m+1 nodes but i do not know how to fix it since the last loop is necessary to take the last input, how can I fix it? Do you need all the code? –  Feb 14 '20 at 08:26
  • `for(i=0; i < m - 1; i++)` should do it, no? – Graipher Feb 14 '20 at 09:01
  • no because imagine m=1 it wouldn't ask for the input of the single node and in the other cases if m=3 for example it wouldn't ask the input for the third node –  Feb 14 '20 at 09:13
  • This question should be closed because the code does not work as intended. –  Feb 14 '20 at 13:21
  • Why read `m` from input instead of taking it as a command line parameter? Values like that should come from `argv`, not from stdin. – William Pursell Feb 14 '20 at 17:00

0 Answers0