Home > Software engineering >  my linked list won't allow me to print my string using index value
my linked list won't allow me to print my string using index value

Time:04-16

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

// Create a node
struct Node {
  char data[100];
  struct Node* next;
};


//function to search element
int search(char key[100],struct Node *head) {
   int index;
   struct Node *newnode;
    index = 0;
   newnode = head;
   while (newnode != NULL && strcmp(newnode->data, key) != 0) {
      index  ;
      newnode = newnode->next;
   }
   return (newnode != NULL) ? index : -1;
}

// Insert at the beginning
void insert(struct Node** head_ref, char new_data[]) {
  // Allocate memory to a node
  struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

  // insert the data
    strcpy(new_node->data,new_data);

  new_node->next = (*head_ref);

  // Move head to new node
  (*head_ref) = new_node;
}


int main(void) {
    //sets structure
    struct Node* names = NULL;
    
    insert(&names, "bob");
    insert(&names, "sam");
    insert(&names, "tom");
    insert(&names, "bill");
    insert(&names, "chris");
    insert(&names, "nick");
    
    char nameCheck[100];
    
    //takes the user input of the name and checks that name if its in the data structure
    printf("Please enter your name to check database: ");
    scanf("%s",nameCheck);
    
    int index = search(nameCheck,names);
    if (index >= 0){
        printf("%s found at position %d\n", nameCheck, index);
    }
    printf("name is %s \n", names[index].data);
    return 0;
}

I'm trying to output the names from a linked list, however whenever I try and output a name using the index value for it, it does not work for any index value except 0.

I want it to the user to be able to enter the name, get the index value position for the name and then output the value using the linked list and index values.

CodePudding user response:

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

// Create a node
struct Node {
  char data[100];
  struct Node* next;
};


//function to search element
int search(char key[100],struct Node *head) {
   int index;
   struct Node *newnode;
    index = 0;
   newnode = head;
   while (newnode != NULL && strcmp(newnode->data, key) != 0) {
      index  ;
      newnode = newnode->next;
   }
   return (newnode != NULL) ? index : -1;
}

// Insert at the beginning
void insert(struct Node** head_ref, char new_data[]) {
  // Allocate memory to a node
  struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

  // insert the data
    strcpy(new_node->data,new_data);

  new_node->next = (*head_ref);

  // Move head to new node
  (*head_ref) = new_node;
}


int main(void) {
    //sets structure
    struct Node* names = NULL;
    
    insert(&names, "bob");
    insert(&names, "sam");
    insert(&names, "tom");
    insert(&names, "bill");
    insert(&names, "chris");
    insert(&names, "nick");
    
    char nameCheck[100];
    
    //takes the user input of the name and checks that name if its in the data structure
    printf("Please enter your name to check database: ");
    scanf("%s",nameCheck);
    
    int index = search(nameCheck,names);
    if (index >= 0){
        printf("%s found at position %d\n", nameCheck, index);
    }
    
    while (names != NULL) {
        if (strcmp(names->data,nameCheck) == 0) {
            printf("name is %s", names->data);
        }
    names = names->next;
    }
    
    printf("\n");
    
    return 0;
}

Above I have changed the code so that I work directly with the pointers to the nodes, meaning capturing the index value would be useless in this case.

CodePudding user response:

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

// Create a node
struct Node {
  char data[100];
  struct Node* next;
};


//function to search element
int search(char key[100],struct Node *head) {
   int index;
   struct Node *newnode;
    index = 0;
   newnode = head;
   while (newnode != NULL && strcmp(newnode->data, key) != 0) {
      index  ;
      newnode = newnode->next;
   }
   return (newnode != NULL) ? index : -1;
}

// Insert the the end
void insert(struct Node** head_ref, char new_data[]) {
    // Allocate memory to a node
  struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
  struct Node* last = *head_ref; /* used in step 5*/

    // insert the data
    strcpy(new_node->data,new_data);
  new_node->next = NULL;

  if (*head_ref == NULL) {
      // Move head to new node
  *head_ref = new_node;
  return;
  }

  while (last->next != NULL) last = last->next;

  last->next = new_node;
  return;
}

int main(void) {
    //sets structure
    struct Node* names = NULL;
    
    insert(&names, "bob");
    insert(&names, "sam");
    insert(&names, "tom");
    insert(&names, "bill");
    insert(&names, "chris");
    insert(&names, "nick");
    
    char nameCheck[100];
    
    //takes the user input of the name and checks that name if its in the data structure
    printf("Please enter your name to check database: ");
    scanf("%s",nameCheck);
    
    int index = search(nameCheck,names);
    if (index >= 0){
        printf("%s found at position %d\n", nameCheck, index);
    }
    
    printf("name is %s \n", names[index].data);
    
    return 0;
}

I could have also done it this way changing the insert function so that it only inserts the answer at the end of the list.

  • Related