Reputation: 33
I am learning singly-linked lists in C and trying to link components of an array. I have written a few lines of code but it is not giving required results i-e making and printing linked list from array. Can someone please explain why it is not working? Thanks
#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
int data;
struct list *next;
}
list;
list *createlinkedlist(int *arr);
void printlist(list * head);
int main(void)
{
int arr[]={1,2,3,4,5};
list *head=NULL;
head = createlinkedlist(arr);
printlist(head);
}
list *createlinkedlist(int *arr)
{
list *head=NULL;
list *temp=NULL;
list *p=NULL;
for (int j=0, O=sizeof(arr); j<O; j++)
{
temp= (list*)malloc(sizeof(list));
temp->next = NULL;
if(head == NULL)
head=temp;
else
{
p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next = temp;
}
}
return head;
}
void printlist(list *head)
{
list * p = head;
while (p !=NULL)
{
printf("%d\n", p->data);
p = p->next;
}
}
Upvotes: 2
Views: 151
Reputation: 73
#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
int data;
struct list* next;
}
list;
list* createlinkedlist(int* arr, int length);
void printlist(list* head);
void deleteList(list* _list, int length);
int main(void)
{
int arr[] = { 1,2,3,4,5 };
list* head = NULL;
head = createlinkedlist(arr, 5);
// Result is '1\n,2\n,3\n,4\n,5\n'
printlist(head);
deleteList(head, 5);
// Below value is garbage
printf("%p\n", head->next);
}
void deleteList(list* p, int length)
{
list** nodeArr = (list **)malloc(sizeof(list*) * length);
for (int i = 0; p != NULL; i++, p = p->next)
{
nodeArr[i] = p;
}
for (int i = 0; i <= sizeof(nodeArr); ++i)
{
free(nodeArr[i]);
}
free(nodeArr);
}
list* createlinkedlist(int* arr, int length)
{
list* head = NULL;
list* temp = NULL;
list* p = NULL;
// repeat until j == length
for (int j = 0; j < length; j++)
{
temp = (list*) malloc(sizeof(list));
// assign value of arr[j]
temp->data = arr[j];
temp->next = NULL;
if (head == NULL)
head = temp;
else
{
p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = temp;
}
}
return head;
}
void printlist(list* head)
{
list* p = head;
while (p != NULL)
{
printf("%d\n", p->data);
p = p->next;
}
}
First, You have not assigned the value of the array into the list.
Second, While Statement must repeat until length of arr, and you have not passed the length of arr.
('arr' is int pointer pointing to the first element (address of 1) of an array. and, sizeof(arr) is 'int pointer size', not length of arr.)
So, you need to put the length in the second argument of 'createdlinkedlist'.
If you fix it like the code above, it works.
To add that, You must call 'free' function to return used memory after using heap memory.
(Namely after using malloc)
In this case, you could define 'deleteList' function to free all the 'next' in the list.
Or, If you can use c++, you can define destructor in the list
typedef struct list
{
int data;
struct list* next;
~list (){
delete next;
}
}
list;
Calling delete of the head of the list calls a series of delete call chain.
In this case, you must use 'delete' keyword instead of 'free' function
Upvotes: 0
Reputation: 12742
Two problems,
Array passed to function decays to pointer.
for (int j=0, O=sizeof(arr); j<O; j++)
Here you are looping till sizeof pointer not till size of actual array.
Pass the array size from main
itself.
list *createlinkedlist(int *arr, int length); //prototype becomes
head = createlinkedlist(arr, sizeof (arr)); // function call becomes
for (int j=0; j<length; j++) //for loop becomes
You are not storing array contents into linked list.
temp= (list*)malloc(sizeof(list));
temp->data = arr[j]; //store the content into list
temp->next = NULL;
Upvotes: 1