Inputting Values in the members of structure through linked list

Viewed 71

I am working in C. Here, I have a structure Node with data members passengers and station. I have a function which inputs the passenger number and stop Name from the user.

struct Node
{
    int stopNo;
    int passenger;
    char station[50];
    struct node* next;
};

I have a function which takes the input as the name of station and passengers no and automatically increase the stopNo by 1.

I am trying to Use a linked list to store the stations.

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

struct Node
{   
    int stopNo;
    int passenger;
    char station[50];
    struct node* next;
};  

void takeInput(struct Node* head)
{
    head->stopNo = head->stopNo +1; 
    printf("Station Name");
    scanf(" %[^\n]s",head->station);
    printf("New Passengers ");
    scanf("%d",&head->passenger);
}

void printStops(struct Node* head)
{
    for(int i=0;i<=head->stopNo;i++)
    {
        printf("Stop %d. %s with %d passenger",head->stopNo,head->station,head->passenger);
    }
}

int main()
{  int n;
    struct Node* head = NULL;
    struct Node* second = NULL;
    //Memory Allocation into heap
    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    while(1){
        //Menu
        printf("1.Log Stops\n");
        printf("2.Print Stop");
        printf("3.Exit");
        scanf("%d",&n);

        switch(n){
          case 1:
            takeInput(struct Node*);
            break;
          case 2:
            printStops(struct Node*);
            break;
          case 3:
            exit(0);
          default:
            printf("Enter the numbers from 1 to 2");
        }
    }
}   

I am confused what to pass as an argument through the function.

1 Answers

You are mis-using the linked list concept.

The principle is to dynamically add new nodes at the beginning (simpler) or at the end (slightly more complex). Here, as you already have a function dedicated to feeding the list, you could just allocate the nodes there. A possible way is to pass the address of the current head, and let the function change it. But you have to define a sentinel value to tell the function that you do not want to input more nodes.

In the following code "STOP" is that sentinel:

void takeInput(struct Node **head)
{
    int stopNo = 0;
    for (;;) {
        struct Node *node = malloc(sizeof (*node));
        node->stopNo = stopNo++;
        node->next = *head;
        printf("Station Name");
        scanf(" %[^\n]",node->station);
        if (strcmp(node->station, "STOP") == 0) {      // no more station
            free(node);
            break;
        }
        printf("New Passengers ");
        scanf("%d",&node->passenger);
        *head = node;
    }
}

That way, you can iterate the list to print its content:

void printStops(struct Node* head)
{
    while (head != NULL)
    {
        printf("Stop %d. %s with %d passenger\n",head->stopNo,head->station,head->passenger);
        head = head->next;
    }
}

Finally, you main would become:

int main()
{  int n;
    struct Node* head = NULL;
    while(1){
        //Menu
        printf("1.Log Stops\n");
        printf("2.Print Stop\n");
        printf("3.Exit\n");
        scanf("%d",&n);

        switch(n){
          case 1:
            takeInput(&head);
            break;
        ...
Related