Reputation: 201
I'm a Noob, trying to build a singly linked linked list in C, based upon an array of strings (where the array can have different lengths and items are of different size). In the code I have modified, I seem to be getting the right number of pointers, but none of the Node values are displaying. Desired output to console is head --> one --> two --> etc. (right now, I'm just getting head --> --> -->)
// starter code from https://www.tutorialspoint.com/learn_c_by_examples/simple_linked_list_program_in_c.htm
#include <stdio.h>
#include <stdlib.h>
struct node {
const char *data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//display the list
void printList() {
struct node *ptr = head;
printf("\n[head] =>");
//start from the beginning
while(ptr != NULL) {
printf(" %s =>", ptr -> data);
ptr = ptr -> next;
}
printf(" [null]\n");
}
//insert link at the first location
void insert(const char data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
//link->key = key;
link -> data = &data;
//point it to old first node
link -> next = head;
//point first to new first node
head = link;
}
int main() {
// create an array of strings, to be used to dynamically populate linked list
const char *strings[] = {"one" ,"two","three", "four", "somewhereselse", "onemore"};
// below line to find number of items in an array of strings, assign to n
int n = sizeof(strings) / (2 * sizeof(int));
for (int j = 0; j < n; j++){
const char *temp = strings[j];
insert(*temp);
//printf("%s\n", temp);
}
printList();
return 0;
}
Upvotes: 1
Views: 121
Reputation: 831
try this code instead
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char *data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//display the list
void printList() {
struct node *ptr = head;
printf("\n[head] =>");
//start from the beginning
while(ptr != NULL) {
printf(" %s =>", ptr -> data);
ptr = ptr -> next;
}
printf(" [null]\n");
}
//insert link at the first location
void insert(char *data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->data=malloc(strlen(data)*sizeof(char)); //don't forget malloc for string : link->data
strcpy(link->data,data); //copy parameter named data to link->data
//point it to old first node
link -> next = head;
//point first to new first node
head = link;
}
int main() {
// create an array of strings, to be used to dynamically populate linked list
const char *strings[] = {"one" ,"two","three", "four", "somewhereselse", "onemore"};
// below line to find number of items in an array of strings, assign to n
int n = sizeof(strings) / (2 * sizeof(int));
char temp[20]={0};
for (int j =n-1; j >=0; j--){
//const char *temp = strings[j];
strcpy(temp,strings[j]);
insert(temp);
//printf("%s\n", temp);
}
printList();
return 0;
}
Upvotes: 1
Reputation: 201
My code did not work due to 3 pieces of syntax, which I have identified below
// starter code from https://www.tutorialspoint.com/learn_c_by_examples/simple_linked_list_program_in_c.htm
#include <stdio.h>
#include <stdlib.h>
struct node {
const char *data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//display the list
void printList() {
struct node *ptr = head;
printf("\n[head] =>");
//start from the beginning
while(ptr != NULL) {
printf(" %s =>", ptr -> data);
ptr = ptr -> next;
}
printf(" [null]\n");
}
//insert link at the first location
// first change insert *
void insert(const char *data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
//link->key = key;
// 2nd change remove &
link -> data = data;
//point it to old first node
link -> next = head;
//point first to new first node
head = link;
}
int main() {
// create an array of strings, to be used to dynamically populate linked list
const char *strings[] = {"one" ,"two","three", "four", "somewhereselse", "onemore"};
// below line to find number of items in an array of strings, assign to n
int n = sizeof(strings) / (2 * sizeof(int));
for (int j = 0; j < n; j++){
const char *temp = strings[j];
// 3rd change remove * before temp
insert(temp);
//printf("%s\n", temp);
}
printList();
return 0;
}
Upvotes: 0
Reputation: 91825
void insert(const char data)
This is wrong.
You're inserting a single character. Well, specifically, you're inserting a pointer to this single character:
link -> data = &data;
...but (a) that character's on the stack, so the pointer's not going to stay valid for very long, and (b) it's not the string that you think it is.
This:
insert(*temp);
Should be:
insert(temp);
...and you should change the prototypes and implementation to match.
Note also that you might want to take a copy of the string (use strdup
, but be aware of memory leaks), rather than point to the original one.
Upvotes: 1