I'm trying to make an array of nodes,and enter values from S[] array to it. But I keep getting a segmentation fault.
My struct looks like this:
typedef struct Node {
int num;
struct Node *next;
}ListNode;
Initialized array of S[10] with random numbers:
printf("list is:");
for(i=0;i<10; i++)
{
RN= (random()+1);
S[i]=RN;
printf("%d ",S[i]);
}
printf("\n");
Here is how I initialized my array of nodes:
ListNode *bucket[Radix];
for(j=0; j<Radix; j++)
{
bucket[i]=(ListNode*)malloc(sizeof(ListNode));
bucket[i]->next=NULL;
}
And this is the function I use to read array numbers from S[] into the array of linked list, bucket[]:
for(y=0;y<(sizeof(S)/sizeof(int));y++) // S is size of a normal array
{
digit=bucketNumber(S[y],1);// returns the first digit values
pointer= bucket[digit];
number= S[y];
insert_tail(pointer, number);
}
my insert_tail function look like this:
ListNode *insert_tail(ListNode *tail, int data)
// insert and element at tail
{
if (tail==NULL)
{ tail=(ListNode *)malloc(sizeof(ListNode));}
else
{
while(tail->next !=NULL)
tail->next = (ListNode *)malloc(sizeof(ListNode));
tail= tail->next;
}
tail->num= data;
tail->next=NULL;
}
this is the bucketNumber function:
int bucketNumber(int num,int digit)
{
int x, y;
y= 10*digit;
x= num%y;
if(digit>=2)
{
num= num%y;
x= num/(y/10);
}
return (x);
}
I think the reason for segmentation fault is that my function is not creating the links in the array properly. I'm not sure thou, there could be something else wrong!
I think the problem is in this section of insert_tail():
else
{
while(tail->next !=NULL)
tail->next = (ListNode *)malloc(sizeof(ListNode));
tail= tail->next;
}
tail->num= data;
tail->next=NULL;
Since the while() loop doesn't have curly braces following it, the below line gets executed if and only if tail->next != NULL:
tail->next = (ListNode *)malloc(sizeof(ListNode));
...which is sort of the opposite of what you want; you want to allocate a new node for next if next is NULL. As is, next is likely NULL, and so tail gets moved forward to next, but next is not allocated--it's NULL. The bottom two lines above would each cause a segmentation fault in that case, since you can't dereference a NULL pointer, which is what -> is doing.
Related
I have written a linked list program and I ought to program a bubble sorting function, but the last node seems to not participate in the bubble sort. I guess that's because the pointer moves to NULL and rejects the last node data. Can you please suggest me a way to sort the linked list in any other way, that would also help.
My code is:
#include<stdio.h>
#include<stdlib.h>
typedef struct node_type{
int data;
struct node_type *next;
}node;
typedef node *list;
list head;
void traverse()
{
list temp;
temp=head;
printf("\nThe Data is: \n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
printf("\n\n");
}
void sort_list()
{
list new,ptr;
int temp;
for(new=head;new->next!=NULL;new=new->next)
{
for(ptr=new->next;ptr->next!=NULL;ptr=ptr->next)
{
if(new->data > ptr->data)
{
temp=new->data;
new->data=ptr->data;
ptr->data=temp;
}
}
}
traverse();
}
void main()
{
list temp,new;
head=NULL;
int n;
char ch;
temp=(list) malloc(sizeof(node));
printf("\nGive data: ");
scanf("%d",&temp->data);
head=temp;
printf("\n");
printf("Enter more data(y/n): ");
scanf("\n%c",&ch);
while(ch=='y')
{
new=(list) malloc(sizeof(node));
printf("Give data: ");
scanf("%d",&new->data);
temp->next=new;
temp=new;
printf("\nEnter more data(y/n): ");
scanf("\n%c",&ch);
}
temp->next=NULL;
traverse(head);
printf("\n\nDo you want to sort the Link-List(y/n): ");
scanf("\n%c",&ch);
if(ch=='y')
{
sort_list();
}
}
Input: 2 3 1 5 4
Output: 1 2 3 5 4
The inner loop of sort_list() is not considering the last node during sorting because of the condition ptr->next!=NULL in for loop. You should check it ptr with NULL:
for(ptr = new->next; ptr != NULL; ptr = ptr->next)
^^^^
There are several things that needs to be fixed.
The main function needs to have a return value of int not void.
There are calls to traverse that pass head as an argument, which will give an error. Either update the traverse function to except a list argument, or fix those calls.
The sort_list function's inner for loop's condition should end when ptr == NULL i.e.
for(ptr=new->next; ptr != NULL; ptr=ptr->next)
The function could be more defensive, if head happens to be NULL, the first for loop would cause a segmentation fault when new->next != NULL becomes evaluated. You could have an if statement at the beginning of the function to guard against this.
The last thing is since malloc is being used, make sure that memory is freed. You will need to iterate through the list and free each node.
I am trying to implement linked list in C and I have two different functions that are supposed to insert elements in the beginning and at nth position. My program crashes when it goes to the part where it starts executing the function to insert at nth position. Can someone please point out my mistake. Also the compiler tends to skip the last scanf statement (marked in the comments).
/****************************************************************
*Date: 12/30/2016
*This program adds an element to the beginning and nth position
*of a linked list and then displays the elements in the list
*****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int data;//Refers to the data part of the linked list
struct node* next;//Refers to the pointer which points to the next element
};
/*************************************************************************************
Description : This function is used to print the elements in the linked list
Input : Pointer to the beginning of the linked list
Output : N/A
*************************************************************************************/
void PrintElements(struct node *start)
{
if(start==NULL)//If the list is empty
printf("List is empty");
else
{
while(start!=NULL)
{
printf("%d ",start->data);
start=start->next;
}
}
}
/*************************************************************************************
Description : This function is used to insert elements in the beginning of the
linked list
Input : Element to be inserted
Output : N/A
*************************************************************************************/
struct node* InsertElementInTheBeginning(struct node *start, int x)
{
struct node *new_node=(struct node*)malloc(sizeof(struct node));
if(new_node==NULL)
{
printf("Memory allocation failed");
return start;
}
new_node->data=x;
new_node->next=start;
start=new_node;
return new_node;
}
/*************************************************************************************
Description : This function is used to insert elements in the nth position of the
linked list
Input : Element and position to be inserted, pointer to beginning of linked
list
Output : N/A
*************************************************************************************/
struct node* InsertElementAtN(struct node *start,int x, int n)//Here the starting position for the linked list is assumed to be 1
{
int i;
struct node* new_node=(struct node*)malloc(sizeof(struct node));
if(new_node==NULL)
{
printf("Memory allocation failed");
return start;
}
new_node->data=x;
new_node->next=NULL;
if(n==1)
{
new_node->next=start;
start=new_node;
return start;
}
struct node *ptr;
ptr=start;
for(i=0;i<n-2;i++)
{
ptr=ptr->next;
}
new_node->next=ptr->next;
ptr->next=new_node;
return start;
}
int main()
{
int x, n;
struct node *HEAD;
struct node *ptr;
HEAD=NULL; //Assigning HEAD to null when there are no elements in the list
ptr=NULL;
printf("\n\rEnter numbers to be inserted into the list\n Press q to quit\n");
while(scanf("%d",&x)==1)
{
HEAD=InsertElementInTheBeginning(HEAD,x);
PrintElements(HEAD);
printf("\n\rEnter numbers to be inserted into the list\n Press q to quit\n");
}
printf("\n\rEnter the number and position to be inserted");
scanf("%d %d",&x,&n);//The compiler always skips this scanf no clue why
HEAD=InsertElementAtN(HEAD, x, n);
PrintElements(HEAD);
//Freeing dynamic memory
while(HEAD!=NULL)
{
ptr=HEAD;
HEAD=ptr->next;
free(ptr);
}
return 0;
}
I've always found something like this to be easier for me to understand (I'm assuming your positions must be 1 or more based on your code, unlike the usual C convention of 0 or more):
struct node *insertValueAt(struct node *head, int value, int position) {
struct node *current;
if (head == NULL || position == 1)
return insertBefore(head, value);
for (current = head; position > 1 && current->next != NULL; position--)
current = current->next;
current->next = insertBefore(current->next, value);
return head;
}
Note that I used the name insertBefore, but your InsertElementAtTheBeginning does the same thing. The key to inserting a node N between to existing nodes A and B is to make A->next point to N that is returned from InsertElementAtTheBeginning(B, value).
Another thing to note is the unconditional insertion before a NULL node (i.e. at the beginning of an empty list or at the end of the list), regardless of the value of position because you can't insert an item at position 10 if you only have fewer than 4 items.
Add a getchar() after the first while loop in the main(). For explanation please check C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf.
you should check that ptr->next is not NULL.
for(i=0;i<n-2;i++)
{
if (ptr->next == NULL) return NULL;
ptr=ptr->next;
}
I'm working on linked lists and pointers. Here is a simple code including a push function. After pushing my elements, and trying to print the first member, the executed code crashes at run time. However, when passing the same pointer to the print_list function and applying the printf function inside print_list it works fine. But when using it directly in the main function and applying the printf function it crashes.
#include<stdio.h>
#include<stdlib.h>
typedef struct list{
int order;
struct list *next;
}list;
void push(struct list **arg,int i);
int main()
{
struct list **ptr=NULL;
for(int i=0;i<10;++i){
push(&ptr,i);
}
print_list(&ptr); // Here works fine
printf("%d\n", (*ptr)->order); //Here run time error
return 0;
}
void push(struct list **arg,int i){
struct list *temp;
temp= malloc(sizeof(list));
temp->order=i;
temp->next=*arg;
*arg=temp;
}
void print_list(list ** head) {
while ((*head) != NULL) {
printf("%d\n", (*head)->order); //Here works fine too !
*head = (*head)->next;
}
}
There are a couple pointer management errors in this piece of code.
void print_list(list ** head) {
while ((*head) != NULL) {
printf("%d\n", (*head)->order);
*head = (*head)->next; // print_list is clearly a readonly function, you don't want to modify head of the list
}
}
Use iterator instead:
void print_list(list *head) { // Passing a copy of head pointer here, so real head can't be changed.
struct list *iter = head;
while (iter != NULL) {
printf("%d\n", iter->order);
iter = iter->next;
}
}
struct list **ptr=NULL; - Your want to declare pointer to head of the list here, but you are declaring pointer to this pointer.
Change it to: struct list *ptr = NULL;
And after this changes you don't need to either take an address of head to pass it to print_list:
print_list(&ptr) --> print_list(ptr)
or dereference it in printf:
printf("%d\n", (*ptr)->order) --> printf("%d\n", ptr->order)
I am learning pointers in c and wrote a program which inserts elements into linked list and prints them in the end.
// this is exercise 2 in chapter 11 on pointers
#include <stdio.h>
#include <stdbool.h>
typedef struct node
{
int value;
struct node * next;
}node;
/**
Insert into linked list
**/
bool insert(node * list, int n);
void printList(node *startNode);
int main(void)
{
node n1,n2;
n1.value = 0;
n2.value = 1;
n1.next = &n2;
n2.next = NULL;
// insert 2 into list
insert(&n1, 2);
// print the updated list
printList(&n1);
printf("Program Executed Successfully \n");
return 0;
}
bool insert(node * list, int n)
{
while(list->next != NULL)
{
if (list->value < n)
{
list = list->next;
}
else
{
node tempNode;
tempNode.value = n;
tempNode.next = list->next;
list->next = &tempNode ;
return true;
}
}
node tempNode;
tempNode.value = n;
tempNode.next = list->next;
list->next = &tempNode ;
return false;
}
void printList(node * startNode)
{
while(startNode->next != NULL)
{
printf("%i\n", startNode->value);
startNode = startNode->next;
}
}
Insertion seems to be fine. I have initially two nodes and then I added one more with a value of 2 but when I print, it just prints the first two elements correctly.
I used GDB debugger and tried to trace where the problem occurs, i saw that when it has printer first and second node, the address of the third node has automatically changed to
0x7ffff7dea560 <_dl_fini>
before at the start of the print function it was
0x7ffffe018
and the output of the complete program is
0
1
-777224576
-443987883
Segmentation fault
The insert function just looks wrong, but the worst offenders are these lines from the function:
else
{
node tempNode;
...
list->next = &tempNode ;
}
Here you declare a local variable `tempNode, and save a pointer to it in the list. The variable will go out of scope and cease to exist once the closing curly brace is reached, leaving you with a stray pointer. Attempting to dereference that stray pointer will lead to undefined behavior.
A little further down you do the same mistake again, saving a pointer to a local variable.
I am trying to implement a linked list in C. I believe I am creating and inserting elements correctly, but there is a segmentation fault every time I try to loop through. Here is my code for the linked list:
struct node {
char **data;
struct node *next;
};
Two global variables to store pointers to head and tail:
struct node *head;
struct node *tail;
Code to insert an element:
void insert(char **args)
{
struct node* pointer = (struct node*)malloc(sizeof(struct node));
pointer -> data = args;
pointer -> next = NULL;
if( head == NULL ) {
head = pointer;
tail = pointer;
}
else {
tail -> next = pointer;
tail = pointer;
}
}
I then try to go through the list and print the data contents (this successfully prints the elements in the list, but then there is a segmentation fault):
int print_list(char **args)
{
struct node *curPointer = head;
if(curPointer == NULL) {
printf("%s", "List is empty\n");
}
else {
printf("%s", "List: ");
do {
int i;
while(curPointer->data[i] != NULL) {
printf("%s", tail->data[i]);
i++;
}
printf("%s", "\n");
curPointer = curPointer->next;
}while(curPointer->next != NULL);
}
return 1;
}
Other functions in my program that rely on looping through the list have a similar segmentation fault issue.
The value of local variable i having automatic storage duration is used without initializing, so it will invoke undefined behavior. Initialize i by replacing int i; to int i = 0;
When curPointer becomes NULL in curPointer = curPointer->next;, dereferencing curPointer in the condition curPointer->next != NULL have a big chance to cause Segmentation Fault.
Try using curPointer != NULL instead of curPointer->next != NULL for the condition.