Following the tutorial, I wrote down this function to find the key in a linked list. However, it doesn't seem to work for me, curious about the reason. Here's part of the find function:
node_t *find_node(node_t *head, int number_to_find)
{
node_t *tmp = head;
while (tmp != NULL)
{
if(tmp->value == number_to_find)
{
return tmp;
tmp = tmp->next;
}
}
return NULL;
}
I embedded this function into my program. It successfully compiled but as it runs, the terminal did not show anything.
#define MAX_LIST 25
typedef struct node
{
int value;
struct node *next;
}node_t;
node_t *create_new_node(int value)
{
node_t *new = malloc(sizeof(node_t));
new->value = value;
new->next = NULL;
return new;
}
node_t *add_to_list(node_t *head, node_t *next_node)
{
next_node->next = head;
return next_node;
}
/*
TO FIND VALUE IN THE LINKED LIST
*/
node_t *find_link_list(node_t *head, int number_to_find)
{
node_t *tmp = head;
while (tmp != NULL)
{
if(tmp->value == number_to_find)
{
return tmp;
tmp = tmp->next;
}
}
return NULL;
}
bool print_linked_list(node_t *head)
{
if(head == NULL)
{
return false;
}
else
{
node_t *tmp = head;
while(tmp != NULL)
{
printf("%d ", tmp->value);
tmp = tmp->next;
}
return true;
}
}
int main()
{
node_t *head = NULL;
node_t *tmp;
for(int i = 0; i < MAX_LIST; i++)
{
tmp = create_new_node(i);
head = add_to_list(head, tmp);
}
tmp = find_link_list(head, 9);
printf("%d \n", tmp->value);
print_linked_list(head);
}
Anyone has a clue why this happen? It would be much appreciated : )
tmp = tmp->next;
should be outside of the if statement. Currently, you're saying, get to the next node in the list only if the value is equal to the target value. So instead you can have
while (tmp != NULL)
{
if(tmp->value == number_to_find)
{
return tmp;
}
tmp = tmp->next;
}
Is it possible to have multiple data inside a single linked list node in C? And how do you input and access data with this?
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
char name[30];
struct node *next;
};
struct node *head, *tail = NULL;
void addNode(int data, char string) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->name[30] = string;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
}
void sortList() {
struct node *current = head, *index = NULL;
int temp;
if(head == NULL) {
return;
}
else {
while(current != NULL) {
index = current->next;
while(index != NULL) {
if(current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty \n");
return;
}
while(current != NULL) {
printf("%d - %s", current->data, current->name);
current = current->next;
}
printf("\n");
}
int main()
{
char string1[10] = "Aaron";
char string2[10] = "Baron";
char string3[10] = "Carla";
addNode(9, string1);
addNode(7, string2);
addNode(2, string3);
printf("Original list: \n");
display();
sortList();
printf("Sorted list: \n");
display();
return 0;
}
I don't understand why my code didn't work. I was trying to make use of single linked list where it can accept/input and print/output the number and the name at the same time.
What I want it to happen is to print the number and the name.
The output should be:
Carla - 2
Baron - 7
Aaron - 9
Please read my comments marked as // CHANGE HERE.
// CHANGE HERE: accept a character array as argument
void addNode(int data, char string[]) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
// CHANGE HERE: copy char array argument to name
strncpy(newNode->name, string, 30);
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
}
void sortList() {
struct node *current = head, *index = NULL;
int temp;
char temp1[30];
if(head == NULL) {
return;
}
else {
while(current != NULL) {
index = current->next;
while(index != NULL) {
if(current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
// CHANGE HERE: swap the name along with data
strncpy(temp1, current->name, 30);
strncpy(current->name, index->name, 30);
strncpy(index->name, temp1, 30);
}
index = index->next;
}
current = current->next;
}
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty \n");
return;
}
while(current != NULL) {
printf("%d - %s\n", current->data, current->name);
current = current->next;
}
printf("\n");
}
I have a segmentation fault occurring in my alphabetical linked list and I pinned it down to the prev in the last else statement.
else
{
prev->next = new_node;
new_node->next = ptr;
return 0;
}
How do I fix this issue?
int add(char *str, char *file) {
struct word_node *new_node = malloc(sizeof(struct word_node));
unsigned len = strlen(str) + 1;
char *s = malloc(len);
memcpy(s, str, len);
new_node->data = s;
new_node->count = 1;
new_node->filename = file;
new_node->next = NULL;
// struct word_node
if (head == NULL)
{
head = new_node;
return 0;
}
struct word_node* ptr = head;
struct word_node* prev = NULL;
while (ptr != NULL)
{
if (strcmp(ptr->data, new_node->data) > 0)
{
break;
}
ptr = ptr->next;
}
if (ptr == head)
{
new_node->next = head;
head = new_node;
return 0;
}
else
{
prev->next = new_node;
new_node->next = ptr;
return 0;
}
}
Your code simplifies to:
struct word_node* prev = NULL;
prev->next = new_node;
See the problem now?
To run that second line you must initialize prev to something valid before using it.
In this while loop
while (ptr != NULL)
{
if (strcmp(ptr->data, new_node->data) > 0)
{
break;
}
ptr = ptr->next;
}
you forgot to update the pointer prev as for example
while (ptr != NULL)
{
if (strcmp(ptr->data, new_node->data) > 0)
{
break;
}
prev = ptr;
ptr = ptr->next;
}
My assignment was to read int input from two files. One contained a poem with misspelled words and the other contained a key with the misspelled word and the correct replacement right after.
I'm supposed to populate two linked lists with the information from each file and create a function that decodes the first file. I'm required to use pointers instead of char arrays in the linked list and at the end the program needs to print the first file with all corrections made.
I'm all good up until the decoder function needs to compare words with punctuation in them. How would i ignore punctuation without losing it in the final format.
Here's my decoder function:
LINK *decoder(TRANS *codet, LINK *head)
{
LINK *currentt;
currentt = head;
TRANS *current;
current = codet;
printf("Decoding...\n");
while (currentt != NULL)
{
current = codet;
while (1)
{
if ()
printf("Comparing %s with %s: \n", currentt->words, current->word1);
if (!strcmp(currentt->words, current->word1))
{
printf("Replacing...\n");
currentt->words = (char*)calloc(strlen(current->word2), sizeof(char));
strcpy(currentt->words, current->word2);
break;
}
current = current->next;
}
currentt = currentt->next;
}
return head;
}
Here's the rest of my code:
//Tristan Shepherd
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct node
{
char *words;
struct node *next;
};
struct codex
{
char *word1;
char *word2;
struct codex *next;
};
typedef struct node LINK;
typedef struct codex TRANS;
void printInsert(LINK *head)
{
printf("\n\nPrinting list: \n\n");
LINK *current;
current = head;
while (current != NULL) {
printf("%s ", current->words);
current = current->next;
}
}
void printCodex(TRANS *codet)
{
printf("\n\nPrinting code: \n\n");
TRANS *current;
current = codet;
while (current != NULL) {
printf("%s %s\n", current->word1, current->word2);
current = current->next;
}
}
void reverse(LINK **head)
{
struct node *prev = NULL;
struct node *current = *head;
struct node *next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
LINK *insertList(char *wordt, LINK *head)
{
LINK *current, *temp;
temp = (LINK *)malloc(sizeof(LINK));
temp->words = (char*)calloc(strlen(wordt)+1, sizeof(char));
strcpy(temp->words, wordt);
if (head == NULL)
{
head = (LINK *)malloc(sizeof(LINK));
head = temp;
temp->next = NULL;
return head;
}
current = head;
if (strcmp(current->words, wordt))
{
temp->next = current;
head = temp;
return head;
}
current = head;
while (current != NULL)
{
if (current->next == NULL || strcmp(current->next->words, wordt))
{
temp->next = current->next;
current->next = temp;
return head;
}
current = current->next;
}
}
TRANS *insertCodex(char *codeword, char *replace, TRANS *codet)
{
TRANS *current, *temp;
temp = (TRANS *)malloc(sizeof(TRANS));
temp->word1 = (char*)calloc(strlen(codeword)+1, sizeof(char));
temp->word2 = (char*)calloc(strlen(replace)+1, sizeof(char));
strcpy(temp->word1, codeword);
strcpy(temp->word2, replace);
if (codet == NULL)
{
codet = (TRANS *)malloc(sizeof(TRANS));
codet = temp;
temp->next = NULL;
return codet;
}
current = codet;
if (strcmp(current->word1, codeword) && strcmp(current->word2, replace))
{
temp->next = current;
codet = temp;
return codet;
}
current = codet;
while (current != NULL)
{
if (current->next == NULL || strcmp(current->next->word1, codeword) && strcmp(current->next->word2, replace))
{
temp->next = current->next;
current->next = temp;
return codet;
}
current = current->next;
}
}
TRANS *scanCodex(FILE *code, TRANS *codet)
{
char *codeword = (char*)malloc(13*sizeof(char));
char *replace = (char*)malloc(13*sizeof(char));
while(1)
{
fscanf(code, "%s %s", codeword, replace);
if (feof(code)) break;
codet = insertCodex(codeword, replace, codet);
}
fclose(code);
return codet;
}
LINK *scanInsert(FILE *stream, LINK *head)
{
char *input = (char*)malloc(13*sizeof(char));
while (1)
{
fscanf(stream, "%s", input);
if(feof(stream)) break;
head = insertList(input, head);
}
fclose(stream);
return head;
}
LINK *decoder(TRANS *codet, LINK *head)
{
LINK *currentt;
currentt = head;
TRANS *current;
current = codet;
printf("Decoding...\n");
while (currentt != NULL)
{
current = codet;
while (1)
{
if ()
printf("Comparing %s with %s: \n", currentt->words, current->word1);
if (!strcmp(currentt->words, current->word1))
{
printf("Replacing...\n");
currentt->words = (char*)calloc(strlen(current->word2), sizeof(char));
strcpy(currentt->words, current->word2);
break;
}
current = current->next;
}
currentt = currentt->next;
}
return head;
}
int main (void)
{
FILE *stream = fopen("hw10data.txt", "r");
FILE *code = fopen("hw10codex.txt", "r");
LINK *head;
TRANS *codet;
head = NULL;
codet = NULL;
head = scanInsert(stream, head);
reverse(&head);
printInsert(head);
codet = scanCodex(code, codet);
printCodex(codet);
head = decoder(codet, head);
printInsert(head);
exit(0);
}
#David C. Rankin
Contents of the Files:
File 1:
Eye I
eye I
chequer checker
Pea P
Sea C
plane plainly
lee skip
four for
revue review
Miss Mistakes
Steaks skip
knot not
sea see
quays keys
whirred word
weight wait
Two To
two to
Weather Whether
write right
oar or
aweigh away
threw through
Your You're
shore sure
two to
no know
Its It's
vary very
weigh way
tolled told
sew so
bless blessed
freeze frees
yew you
lodes loads
thyme time
right write
stiles styles
righting writing
aides aids
rime rhyme
frays phrase
come composed
posed skip
trussed trusted
too to
bee be
joule jewel
cheque check
sum some
File 2:
Eye have a spelling chequer,
It came with my Pea Sea.
It plane lee marks four my revue,
Miss Steaks I can knot sea.
Eye strike the quays and type a whirred,
And weight four it two say,
Weather eye am write oar wrong,
It tells me straight aweigh.
Eye ran this poem threw it,
Your shore real glad two no.
Its vary polished in its weigh.
My chequer tolled me sew.
A chequer is a bless thing,
It freeze yew lodes of thyme.
It helps me right all stiles of righting,
And aides me when eye rime.
Each frays come posed up on my screen,
Eye trussed too bee a joule.
The chequer pours over every word,
Two cheque sum spelling rule.
Here's my final code if you need it:
//Tristan Shepherd
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct node
{
char *words;
struct node *next;
};
struct codex
{
char *word1;
char *word2;
struct codex *next;
};
typedef struct node LINK;
typedef struct codex TRANS;
void delete(LINK **head, char *key)
{
LINK *temp = *head, *prev;
if (temp != NULL && !strcmp(temp->words, key))
{
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && strcmp(temp->words, key))
{
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
void printInsert(LINK *head, int aftersort)
{
printf("\n\nPrinting list: \n\n");
LINK *current;
current = head;
while (current != NULL)
{
if (aftersort)
{
printf("%s", current->words);
}
else
{
printf("%s ", current->words);
}
current = current->next;
}
}
void printCodex(TRANS *codet)
{
printf("\n\nPrinting codex: \n\n");
TRANS *current;
current = codet;
while (current != NULL)
{
printf("%s %s\n", current->word1, current->word2);
current = current->next;
}
}
void reverse(LINK **head)
{
struct node *prev = NULL;
struct node *current = *head;
struct node *next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
LINK *insertList(char *wordt, LINK *head)
{
LINK *current, *temp;
temp = (LINK *)malloc(sizeof(LINK));
temp->words = (char*)calloc(strlen(wordt)+1, sizeof(char));
strcpy(temp->words, wordt);
if (head == NULL)
{
head = (LINK *)malloc(sizeof(LINK));
head = temp;
temp->next = NULL;
return head;
}
current = head;
if (strcmp(current->words, wordt))
{
temp->next = current;
head = temp;
return head;
}
current = head;
while (current != NULL)
{
if (current->next == NULL || strcmp(current->next->words, wordt))
{
temp->next = current->next;
current->next = temp;
return head;
}
current = current->next;
}
}
TRANS *insertCodex(char *codeword, char *replace, TRANS *codet)
{
TRANS *current, *temp;
temp = (TRANS *)malloc(sizeof(TRANS));
temp->word1 = (char*)calloc(strlen(codeword)+1, sizeof(char));
temp->word2 = (char*)calloc(strlen(replace)+1, sizeof(char));
strcpy(temp->word1, codeword);
strcpy(temp->word2, replace);
if (codet == NULL)
{
codet = (TRANS *)malloc(sizeof(TRANS));
codet = temp;
temp->next = NULL;
return codet;
}
current = codet;
if (strcmp(current->word1, codeword) && strcmp(current->word2, replace))
{
temp->next = current;
codet = temp;
return codet;
}
current = codet;
while (current != NULL)
{
if (current->next == NULL || strcmp(current->next->word1, codeword) && strcmp(current->next->word2, replace))
{
temp->next = current->next;
current->next = temp;
return codet;
}
current = current->next;
}
}
TRANS *scanCodex(FILE *code, TRANS *codet)
{
char *codeword = (char*)malloc(13*sizeof(char));
char *replace = (char*)malloc(13*sizeof(char));
while(1)
{
fscanf(code, "%s %s", codeword, replace);
if (feof(code)) break;
codet = insertCodex(codeword, replace, codet);
}
fclose(code);
return codet;
}
LINK *scanInsert(FILE *stream, LINK *head)
{
char *input = (char*)malloc(13*sizeof(char));
while (1)
{
fscanf(stream, "%s", input);
if(feof(stream)) break;
head = insertList(input, head);
}
fclose(stream);
return head;
}
LINK *decoder(TRANS *codet, LINK *head)
{
LINK *currentt;
currentt = head;
TRANS *current;
current = codet;
char *temp = (char*)malloc(33*sizeof(char));
while (currentt != NULL)
{
int CorP = 0;
int punct = 0;
int t = 0;
current = codet;
while (1)
{
if (!strcmp(currentt->words, current->word1))
{
currentt->words = (char*)calloc(strlen(current->word2)+1, sizeof(char));
strcpy(currentt->words, current->word2);
strcat(currentt->words, " ");
if (punct == 1)
{
strtok(currentt->words, " ");
strcat(currentt->words, ".\n");
}
if (punct == 2)
{
strtok(currentt->words, " ");
strcat(currentt->words, ",\n");
}
if (!strcmp(currentt->words, "skip "))
{
delete(&head, currentt->words);
}
break;
}
current = current->next;
if (current == NULL)
{
strcpy(temp, currentt->words);
if (!strcmp(currentt->words, strtok(temp, ".")))
{
if(!strcmp(currentt->words, strtok(temp, ",")))
{
if(t == 1)
{
strcat(currentt->words, " ");
if (punct == 1)
{
strtok(currentt->words, " ");
strcat(currentt->words, ".\n");
}
if (punct == 2)
{
strtok(currentt->words, " ");
strcat(currentt->words, ",\n");
}
break;
}
t++;
}
else
{
strcpy(currentt->words, strtok(currentt->words, ","));
current = codet;
punct = 2;
}
}
else
{
strcpy(currentt->words, strtok(currentt->words, "."));
current = codet;
punct = 1;
}
current = codet;
}
}
currentt = currentt->next;
}
return head;
}
int main (void)
{
FILE *stream = fopen("hw10data.txt", "r");
FILE *code = fopen("hw10codex.txt", "r");
LINK *head;
TRANS *codet;
head = NULL;
codet = NULL;
head = scanInsert(stream, head);
reverse(&head);
printInsert(head, 0);
codet = scanCodex(code, codet);
printCodex(codet);
head = decoder(codet, head);
printInsert(head, 1);
exit(0);
}
Why does the last append call not work? I have to add some garbage here because it is complaining that my post is mostly code, I hope it is enough details by now.
typedef struct node {
int val;
struct node * next;
} node_t;
void append_node(node_t * head, int val) {
node_t * current = head;
while(current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof(node_t));
if(current->next == NULL)
printf("err");
current = current->next;
current->val = val;
current->next = NULL; //malloc(sizeof(node_t));
}
void print_list(node_t * head) {
node_t * current = head;
while(current->next != NULL) {
printf("%d ", current->val);
current = current->next;
}
printf("\n");
}
int main() {
node_t * list = malloc(sizeof(node_t));
list->val = 1;
list->next = NULL;
append_node(list,12);
append_node(list,14);
append_node(list,17);
print_list(list);
return 0;
}
Output:
1 12 14
The problem is in your print function. You don't print the last element.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node * next;
} node_t;
void append_node(node_t * head, int val) {
node_t * current = head;
while(current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof(node_t));
if(current->next == NULL)
printf("err");
current = current->next;
current->val = val;
current->next = NULL; //malloc(sizeof(node_t));
}
void print_list(node_t * head) {
node_t * current = head;
while(current!= NULL) {
printf("%d ", current->val);
current = current->next;
}
printf("\n");
}
int main() {
node_t * list = malloc(sizeof(node_t));
list->val = 1;
list->next = NULL;
append_node(list,12);
append_node(list,14);
append_node(list,17);
print_list(list);
return 0;
}