Segmentation fault in alphabetical linked list in C - c

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;
}

Related

How to add a new node at a specific index in doubly linked lists

How to add a new node at a specific index in doubly linked list. I wrote a function that is meant to add a new node at a specific index, but after i tested my function in the main program i got an infinite loop.
dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n)
{
dlistint_t *temp;
dlistint_t *new_node;
unsigned int count;
temp = *h;
new_node = malloc(sizeof(dlistint_t));
if (new_node == NULL)
return (NULL);
new_node->prev = NULL;
new_node->next = NULL;
new_node->n = n;
if (idx == 0)
{
*h = new_node;
return (new_node);
}
else
{
count = 0;
while (temp != NULL && count != idx - 1)
{
temp = temp->next;
count++;
}
if (count != idx - 1)
{
return (NULL);
}
temp->next = new_node;
new_node->next = temp;
new_node->prev = temp;
return (new_node);
}
}

Multiple data inside a single linked list node

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");
}

Generalized Linked List: Adding Child Link whenever an opening bracket occurs

Here is my code for generating a GLL for the string input: a,(b,c),d where (b,c) will be linked as a child at the next link of a.
GLL* generateList(char poly[])
{
GLL* newNode = NULL, *first = NULL, *ptr = NULL;
while (poly[i] != '\0')
{
if (poly[i] == ')')
{
return first;
}
else
{
if (poly[i] != ',')
{
if (poly[i] != '(')
{
newNode = createNode(poly[i], 0);
}
else
{
++i;
newNode = createNode('#', 1);
newNode->dlink = generateList(poly);
}
}
}
if (first != NULL)
{
ptr = first;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = newNode;
}
else
{
first = newNode;
}
i++;
}
return first;
}
And here is the structure I used for each node.
typedef struct gll
{
int tag;
struct gll* next;
char data;
struct gll* dlink;
} GLL;
I am not finding a way to add that child link to the parent link whenever the bracket opens. The programs runs in a loop.
Note: I have declared i=0 as a global variable to hold the position of character.
Edit: Here is the createNode function
GLL* createNode(char value, int flag)
{
GLL* newNode;
newNode = (GLL *) malloc(sizeof(GLL)*1);
newNode->data = value;
newNode->dlink = NULL;
newNode->tag = flag;
newNode->next = NULL;
return newNode;
}
How do I do it then?
You could do something like that:
#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct gll
{
int tag;
struct gll* next;
char data;
struct gll* dlink;
} GLL;
GLL* createNode(char value, int flag)
{
GLL* newNode = calloc(1, sizeof(*newNode));
if (!newNode)
return NULL;
newNode->tag = flag;
newNode->data = value;
return newNode;
}
void freeList(GLL *list)
{
for (GLL *current_node = list, *temp; current_node; current_node = temp) {
temp = current_node->next;
freeList(current_node->dlink);
free(current_node);
}
}
GLL* generateList(char *poly, size_t *pos)
{
size_t const length = strlen(poly);
GLL *head = NULL;
GLL *tail = NULL;
for (; *pos < length; ++*pos) {
if (poly[*pos] == '(') {
++*pos; // don't have the next called generateList() read '(' again
tail->dlink = generateList(poly, pos);
if (!tail->dlink) {
freeList(head);
return NULL;
}
continue;
}
else if (poly[*pos] == ')') {
return head;
}
else if (isalpha((char unsigned)poly[*pos])) {
if (!head) {
head = tail = createNode(poly[*pos], 0);
}
else {
tail->next = createNode(poly[*pos], 0);
tail = tail->next;
}
continue;
}
else if (poly[*pos] == ',')
continue;
fputs("Format error :(\n\n", stderr);
freeList(head);
return NULL;
}
return head;
}
void printList(GLL *list)
{
for (GLL *node = list; node; node = node->next) {
printf("%c ", node->data);
if (node->dlink) {
putchar('(');
printList(node->dlink);
printf("\b) ");
}
}
}
int main(void)
{
size_t pos = 0;
GLL *list = generateList("a,(b,(c,d,e(f)),g,h),i,j,k", &pos);
printList(list);
putchar('\n');
freeList(list);
}
Output
a (b (c d e (f)) g h) i j k
Also, if flag is true then it means that the data is not to be considered but there is a child list to be linked.
Sorry, but I don't get how there could be a child list if there is no data for the node.

Doubly linked list segmentation fault printing backwards

I am trying to delete a node at a specific location but I keep getting a segmentation fault 11.
I am reading the position and value from a file.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
float val;
struct node *prev;
struct node *next;
}node_t;
void printForward(node_t *head) {
node_t *current = head;
while (current != NULL) {
printf("%.2f\n", current->val);
current = current->next;
}
}
void printBackward(node_t *head) {
node_t *current = head;
while (current->next != NULL) {
current = current->next;
}
while (current != NULL) {
printf("%.2f\n", current->val);
current = current->prev;
}
}
void deleteAtPos(node_t **head, int pos) {
int i;
node_t *current = *head;
node_t *temp = NULL;
if (pos == 0) {
temp = (*head)->next;
free(*head);
(*head) = temp;
(*head)->prev = NULL;
return;
}
for (i = 0; i < (pos - 1); i++) {
if (current->next != NULL) {
current = current->next;
}
}
temp = current->next;
current->next = temp->next;
free(temp);
}
// Fix insert at position
void insertAtPos(node_t **head, int pos, float val) {
int i;
node_t *newNode = malloc(sizeof(node_t));
node_t *current = *head;
newNode->val = val;
if (pos == 0) {
newNode->next = (*head);
newNode->prev = NULL;
(*head)->prev = newNode;
(*head) = newNode;
return;
}
for (i = 0; i < pos; i++) {
if (current->next != NULL) {
current = current->next;
}
else {
printf("Node does not exist\n");
break;
}
}
current->prev->next = newNode;
newNode->prev = current->prev;
newNode->next = current;
current->prev = newNode;
}
void addEnd(node_t **head, float val) {
node_t *current = *head;
node_t *newNode = malloc(sizeof(node_t));
newNode->next = NULL;
newNode->val = val;
if (*head == NULL) {
*head = newNode;
newNode->prev = NULL;
return;
}
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Error");
}
node_t *head = NULL;
FILE *fp;
int i = 0, x;
float valLine1, valLine2, valLine3;
char buffer[200], *token, *del = ",";
float posVals[200], delPos[200];
fp = fopen(argv[1], "r");
fgets(buffer, sizeof(buffer), fp);
token = strtok(buffer, del);
while (token != NULL) {
valLine1 = atof(token);
addEnd(&head, valLine1);
token = strtok(NULL, del);
}
printForward(head);
printf("\n");
del = ":,";
fgets(buffer, sizeof(buffer), fp);
token = strtok(buffer, del);
while (token != NULL) {
valLine2 = atof(token);
posVals[i] = valLine2;
token = strtok(NULL, del);
i++;
}
for (x = 0; x < i; x += 2) {
insertAtPos(&head, posVals[x + 1], posVals[x]);
}
printForward(head);
fgets(buffer, sizeof(buffer), fp);
i = 0;
token = strtok(buffer, del);
while (token != NULL) {
valLine3 = atof(token);
delPos[i] = valLine3;
token = strtok(NULL, del);
i++;
}
printf("\n");
for (x = 0; x < i; x++) {
deleteAtPos(&head, delPos[x]);
}
printForward(head);
printf("\n");
printBackward(head);
fclose(fp);
}
The trouble is with my deleteAtPos function but I can't figure out why.
Here is the output:
24.00
0.04
17.00
-200.10
34.60
0.00
Segmentation fault: 11
And here is the contents of the file:
17,32.5,12,0,34.6,-200.1,17,0.04,24
1:2,4.1:5,-12:4
3,5,0
Please help!
Thank you
I have modified your deleteAtPos() function now you can delete at any position and your backward() function will print correct values.
void deleteAtPos(node_t **head, int pos) {
int i;
node_t *current = *head;
node_t *temp = NULL;
if (pos == 0) {
temp = (*head)->next;
free(*head);
(*head) = temp;
(*head)->prev = NULL;
return;
}
for (i = 0; i < (pos - 1); i++) {
if (current->next != NULL) {
current = current->next;
}
}
temp = current;
if(current->next==NULL)
{
current->prev->next = current->next;
}
else
{
current->prev->next = current->next;
current->next->prev = current->prev;
}
free(temp);
}
You are saying the trouble is with deleteAtPos. But your code is very far from an SSCCE, so I don't want to isolate the problem for you - you should do that, and most of the time, during the process of isolation, you will find the answer. If not, you can then post the question here.
Therefore I will just list the things I can see are wrong with deleteAtPos and maybe if you fix all of them, the problem will disappear.
You are not handling the situation where *head is NULL, the list has 0 elements. This would surely segfault.
You are not checking that pos >= 0.
You are not correctly handling the case where your list has one element, either. Another segfault.
You are not correctly handling the case where pos is the end of your list. Another segfault.

Linked list reverse function leads to infinite printing loop

I was writing a C code to reverse a link list. I got into one problem.
If I do not make my next pointer NULL my reverse function works fine, but if I make it null the linked list always keeps printing in the while loop.
Below is the correct program, which works fine.
But if I make *next = NULL, the display function will keep printing in the while loop.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
} *head;
/*************************************************************/
/* */
/* create - Function to create Nodes and add them at last */
/* */
/*************************************************************/
int create(int data)
{
struct node *temp,*ptr = NULL;
//int data = 0;
ptr = head;
//Printf(" Enter the Data for Node : ");
//scanf(" %d ", data);
temp = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL) {
// this is the first node
temp->next = NULL;
temp->data = data;
head = temp;
} else {
// this is not the first node
while (ptr != NULL) {
if (ptr->next == NULL) {
temp->next = NULL;
temp->data = data;
ptr->next = temp;
break;
}
ptr = ptr->next;
}
}
return 0;
}
/*************************************************************/
/* */
/* create_in_front - Function to add Node in Front */
/* */
/*************************************************************/
int create_in_front(int data)
{
struct node *temp,*ptr = NULL;
ptr = head;
temp = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL) {
// this is the first node
temp->next = NULL;
temp->data = data;
head = temp;
} else {
// this is not the first node
temp->next = ptr->next;
temp->data = data;
head = temp;
}
return 0;
}
/*************************************************************/
/* */
/* create_in_between - Function to add Node in between nodes*/
/* */
/*************************************************************/
int create_in_between(int data,int pos)
{
struct node *temp, *ptr = NULL;
int i = 0;
ptr = head;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
for (i = 0; i < pos; i++) {
if (i == pos-1) {
temp->next = ptr->next;
ptr->next = temp;
}
ptr = ptr->next;
}
return 0;
}
/*************************************************************/
/* */
/* delete_in_between - Function to add Node in between nodes*/
/* */
/*************************************************************/
delete_in_between(int pos)
{
struct node *ptr, *prev = NULL;
ptr = head;
int i = 0;
for (i = 0; i < pos; i++) {
if (i == pos-1) {
prev = ptr->next;
free(ptr);
break;
}
prev = ptr;
ptr = ptr->next;
}
return 0;
}
/*************************************************************/
/* */
/* reverse - Function to reverse link list */
/* */
/*************************************************************/
int reverse()
{
struct node *prev = NULL;
struct node *curr = head;
struct node *next = NULL;
curr = head;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
return 0;
}
/*************************************************************/
/* */
/* display - Function to diplay link list */
/* */
/*************************************************************/
// Function to display Link List
int display()
{
struct node *temp = head;
while (temp != NULL) {
printf("%d->",temp->data);
temp = temp->next;
}
return 0;
}
int main()
{
create(10);
create(20);
create(30);
create(40);
create(50);
create_in_front(34);
create_in_between(55,2);
//delete_in_between(4);
reverse();
display();
return 0;
}
Let me know the logic behind this.
Function create_in_front() is bogus: temp->next = ptr->next; should be changed to temp->next = ptr;
create_in_between() does not handle the case of pos==0.
delete_in_between() is completely dysfunctional: the node is frees but its predecessor still points to it.
reverse() seems correct to me, it could be simplified this way:
int reverse() {
struct node *prev = NULL;
struct node *curr = head;
while (curr != NULL) {
struct node *next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
return 0;
}
The problem seems unrelated to your modifying the reverse() function, maybe a side effect of bugs in the other functions.
Your reverse() function seems correct, but the rest of the code is somewhat overcomplicated. Try something like this:
void create(int data) {
struct node *temp = malloc(sizeof(struct node));
if (temp != NULL) {
temp->next = NULL;
temp->data = data;
if (head == NULL) { // this is the first node
head = temp;
} else {
// this is not the first node
struct node *last = head;
while (last->next)
last = last->next;
last->next = temp;
}
}
}
void create_in_front(int data) {
struct node *temp = malloc(sizeof(struct node));
if (temp != NULL) {
temp->next = head;
temp->data = data;
head = temp;
}
}

Resources