simple linked list implementation logical error? - c

I have implemented a linked list code, but it seems that there is somewhere a logic error in my code, can anyone help me to fix this problem?
struct Node{
int val;
struct Node *next;
};
void add(struct Node *new_node, struct Node *head){
struct Node *new_n;
if( head == NULL){
head = new_node;
}
else{
new_n = head;
while(new_n){
new_n = new_n->next;
}
new_n = new_node;
}
}
void print(struct Node*n){
while(n != NULL){
fprintf(stderr, "val:%d addr%p \t next%p\n",n->val, n, n->next);
n=n->next;
}
}
void main (){
struct Node *head;
struct Node *node;
int i ;
for( i =1; i< 5; i++){
struct Node *node = malloc(sizeof(*node));
bzero(node,sizeof(*node));
node->val = i;
node->next =NULL;
add(node, head);
}
print(head);
}
This code doesn't print any values? What are the problems with this code?
[ I used: $gcc filename.c -o filename.o]

I see three problems:
1) head is never initialized
Use:
struct Node *head = NULL;
2) Changes to head inside the add function does not change head in main
Try
void add(struct Node *new_node, struct Node **head){
^^
and use *head in the function and call it like add(node, &head);
3) New elements are not added to the list.
Try:
void add(struct Node *new_node, struct Node **head) {
struct Node *new_n;
if (*head == NULL) {
*head = new_node;
}
else {
new_n = *head;
while (new_n->next) { // Notice - iterate until the next pointer is NULL
new_n = new_n->next;
}
new_n->next = new_node; // Notice
}
}

in doing this you return a pointer to the new head node and thus can iterate the new list containing your newer element at the end of the list.
struct Node{
int val;
struct Node *next;
};
*Node add(struct Node *new_node, struct Node *head)
{
struct Node *new_n;
if( head == NULL)
{
head = new_node;
}
else
{
new_n = head;
while(new_n)
{
new_n = new_n->next;
}
new_n = new_node;
}
return new_n;
}
void print(struct Node*n){
while(n != NULL){
fprintf(stderr, "val:%d addr%p \t next%p\n",n->val, n, n->next);
n=n->next;
}
}
void main (){
struct Node *head;
struct Node *node;
int i ;
for( i =1; i< 5; i++){
struct Node *node = malloc(sizeof(*node));
bzero(node,sizeof(*node));
node->val = i;
node->next =NULL;
*head = add(node, head);
}
print(head);
}

Related

Copying elements of a linked list to another linked list in reverse order in C

I'm new to programming in C and taking a course. I'm having trouble with one of the tasks I'm practicing. I'm supposed to Write a program that creates a linked list of 10 characters, then creates a copy of the list in reverse order. I have written (mostly copied) a code, but it only reverses the contents of my linked list, doesn't copy them to a new linked list in reverse order. It's also not working with letters even though I'm using char data type. works fine with numbers.
Here's my code:
#include <stdio.h>
#include <malloc.h>
struct Node
{
char data;
struct Node *next;
};
static void reverse(struct Node **head_ref)
{
struct Node *previous = NULL;
struct Node *current = *head_ref;
struct Node *next;
while (current != NULL)
{
next = current->next;
current->next = previous;
previous = current;
current = next;
}
*head_ref = previous;
}
void push(struct Node **head_ref, char new_data)
{
struct Node *new_node =
(struct Node *)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node *head)
{
struct Node *temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
int main()
{
struct Node *head = NULL;
char element = NULL;
printf("Enter 10 characters:\n");
for (int i = 0; i <= 9; i++)
{
scanf_s("%d", &element);
push(&head, element);
}
printf("Given linked list\n");
printList(head);
reverse(&head);
printf("\nReversed Linked list \n");
printList(head);
getchar();
}
This for loop
for (int i = 0; i <= 9; i++)
{
scanf_s("%d", &element);
push(&head, element);
}
invokes undefined behavior because there is used an incorrect conversion specifier %d with an object of the type char,
You need to write
for (int i = 0; i <= 9; i++)
{
scanf_s( " %c", &element, 1 );
push(&head, element);
}
Pay attention to the blank before the conversion specifier %c in the format string. This allows to skip white space characters in the input stream.
As for the function then it can be declared and defined the following simple way using the function push that you already defined
struct Node * reverse_copy( const struct Node *head )
{
struct Node *new_head = NULL;
for ( ; head != NULL; head = head->next )
{
push( &new_head, head->data );
}
return new_head;
}
And in main you can write something like
struct Node *second_head = reverse_copy( head );
Take into account that the function push would be more safer if it would process the situation when memory allocation for a node failed.
To create a copy in reverse order, create a new list with the same values as the original list but prepend the new nodes using the push function.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
struct Node *next;
};
void prepend(struct Node **head_ref, char new_data) {
struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void append(struct Node **head_ref, char new_data) {
struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
struct Node *node = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (!node) {
*head_ref = new_node;
} else {
while (node->next)
node = node->next;
node->next = new_node;
}
}
void printList(const struct Node *head) {
const struct Node *temp = head;
while (temp != NULL) {
printf("%c ", temp->data);
temp = temp->next;
}
printf("\n");
}
struct Node *copy_reverse(struct Node *list) {
struct Node *new_list = NULL;
while (list) {
prepend(&new_list, list->data);
list = list->next;
}
return new_list;
}
void freeList(struct Node *list) {
while (list) {
struct Node *node = list;
list = list->next;
free(node);
}
}
int main() {
struct Node *head = NULL;
char element;
printf("Enter 10 characters:\n");
for (int i = 0; i < 10; i++) {
scanf_s("%c", &element);
push(&head, element);
}
printf("Given linked list\n");
printList(head);
struct Node *copy = copy_reverse(head);
printf("\nReversed Linked list \n");
printList(copy);
freeList(head);
freeList(copy);
getchar();
}
You're almost there. All it needs is one tweak. In reverse, you need to create a new copy of the current node and use that instead. Also, since you'll be ending up with a second list and not altering the original, you should return the new list from reverse.
static struct Node* reverse(const struct Node* head_ref)
{
struct Node* previous = NULL;
const struct Node* current = head_ref;
struct Node* copy;
while (current != NULL) {
copy = malloc(sizeof(*copy));
if (copy == NULL) {
// handle error
}
copy->data = current->data;
copy->next = previous;
previous = copy;
current = current->next;
}
return previous;
}
You can also make the loop prettier by converting it to a for loop.
for (current = head_ref; current != NULL; current = current->next) {
Finally, when you print out the list, you're using %d in the printf format string. %d will print the char as an integer. To print out the actual character, use %c instead.

Linked list implementation in C(printing only last two nodes)

#include <stdlib.h>
#include <stdio.h>
struct node {
int data;
struct node *next;
};
void addLast(struct node **head, int value);
void printAll(struct node *head);
struct node *head1 = NULL;
int main() {
addLast(&head1, 10);
addLast(&head1, 20);
addLast(&head1, 30);
addLast(&head1, 40);
printAll(head1);
return 0;
}
void addLast(struct node **head, int value) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = value;
if (*head == NULL) {
*head = newNode;
(*head)->next = NULL;
} else {
struct node **temp = head;
while ((*temp)->next != NULL) {
*temp = (*temp)->next;
}
(*temp)->next = newNode;
newNode->next = NULL;
}
}
void printAll(struct node *head) {
struct node *temp = head;
while (temp != NULL) {
printf("%d->", temp->data);
temp = temp->next;
}
printf("\n");
}
addLast() will append the new node at the end of the list, with printAll(), I am printing entire list.
Every time when I am printing the list, I can only see the last two nodes.
Can anyone please help, why loop is not iterating over entire list ?
The function addLast is too complicated and as result is wrong due to this statement
*temp = (*temp)->next;
in the while loop. It always changes the head node.
Define the function the following way
int addLast( struct node **head, int value )
{
struct node *newNode = malloc( sizeof( struct node ) );
int success = newNode != NULL;
if ( success )
{
newNode->data = value;
newNode->next = NULL:
while( *head ) head = &( *head )->next;
*head = newNode;
}
return success;
}
Take into account that there is no need to declare the variable head1 as global. It is better to declare it inside the function main.
Also all the allocated memory should be freed before exiting the program.

How to obtain a pointer to pointer in C?

I'm supposed to write a function to remove the first node in a linked list.
List is defined like so:
struct ListNode{
int nInfo;
struct ListNode *next;
};
struct ListNode *createNode(int nInfo) {
ListNode *node;
node->nInfo = nInfo;
node->next = NULL;
return node;
}
void insertNode(struct ListNode **list, struct ListNode *node) {
//Sorting list after nInfo
struct ListNode *temp;
struct ListNode *tmpList = *list;
if(*list != NULL) { //List exists
while((tmpList->next != NULL)) {
if((tmpList->nInfo >= node->nInfo) && (tmpList->next->nInfo < node->nInfo)) {
break;
}
tmpList = tmpList->next;
}
//Found where to insert the node
temp = tmpList->next; //Saving old nextnode
tmpList->next = node; //Assigning new nextnode
node->next = temp; //Re-inserting old node
}
else{
*list = node;
}
}
The function for removing the first node looks like this:
void deleteFirst(struct ListNode **list) {
//Delete first node
struct ListNode *temppointer = *list;
if(temppointer == NULL)
return; //List is NULL
*list = temppointer->next;
}
I use the functions like so:
struct ListNode *list = createNode(100);
struct ListNode *node1 = createNode(50);
insertNode(list, node1); //Gives error, cannot convert ListNode* to ListNode**
deleteFirst(list); //Same error
I can't figure out how to obtain a pointer to the list pointer.
As we suspected, you forget to allocate memory for your nodes:
struct ListNode *createNode(int nInfo) {
ListNode *node;
node->nInfo = nInfo;
node->next = NULL;
return node;
}
Your *node is a pointer, but it still points to nothing. You must ask the heap for memory for your node:
ListNode *node = malloc(sizeof(ListNode));
Then in DeleteNode, you must return the memory to the heap, as you no longer need it:
void deleteFirst(struct ListNode **list) {
//Delete first node
struct ListNode *temppointer = *list;
if(temppointer == NULL)
return; //List is NULL
*list = temppointer->next;
free(temppointer); // release the memory.
}
Pay attention! The function you wrote for creating a node cannot work as it is: the node is allocated on the stack, in the context of the function, and it is invalid after the function has exited.
You have to allocate the memory for the node on the heap, using (p.e.) malloc. The function that remove the node from the list is responsable for its deallocation, usually using free.
[posting as an answer to get the formatting right]
Note: your insert() function is overly complex. It can be reduced to
void insertNode(struct ListNode **list, struct ListNode *node) {
for( ; *list ; list = &(*list)->next ) { //List exists
if(*(list)->nInfo >= node->nInfo) break;
}
//Found where to insert the node
node->next = *list;
*list = node;
}
#include <stdlib.h>
struct ListNode{
int nInfo;
struct ListNode *next;
};
struct ListNode *createNode(int nInfo) {
struct ListNode *node=malloc(sizeof(*node));
if(node){
node->nInfo = nInfo;
node->next = NULL;
}
return node;
}
void insertNode(struct ListNode **list, struct ListNode *node) {
// for safety
if(!list) return;
if(!node) return;
//Sorting list after nInfo
struct ListNode *temp;
struct ListNode *tmpList = *list;
if(tmpList!= NULL) { //List exists
while(tmpList->next) {
if(
((tmpList->nInfo)>= (node->nInfo))
&&
((tmpList->next->nInfo) < (node->nInfo))
) {
break;
}
tmpList = tmpList->next;
}
//Found where to insert the node
temp = tmpList->next; //Saving old nextnode
tmpList->next = node; //Assigning new nextnode
node->next = temp; //Re-inserting old node
}
else{
*list = node;
}
}
void deleteFirst(struct ListNode **plist) {
if(!plist) return;
struct ListNode *list=*plist;
if(!list) return;
*plist=list->next;
free(list);
return ;
}
void printNodes(char *title,struct ListNode *list){
printf("\n== %s\n",title);
while(list){
printf("\t%d\n",list->nInfo);
list=list->next;
}
printf("\n");
}
int main(){
struct ListNode *list = createNode(100);
struct ListNode *node1 = createNode(50);
insertNode(&list, node1);
printNodes("on start",list);
insertNode(&list, createNode(70));
printNodes("after add 70",list);
deleteFirst(&list);
printNodes("after del first",list);
}

Expected expression before 'Node'

I've been messing aroung with the linked list implementation in C and got stuck at this problem: why is it giving me expected expression before 'Node' in the following lines?:
Node *newNode1= malloc(sizeOf(Node));
Node *newNode2= malloc(sizeOf(Node));
Node *newNode3= malloc(sizeOf(Node));
Node *newNode4= malloc(sizeOf(Node));
I've never seen such problems in C before. What went wrong ?
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node_{
int data;
struct Node_ *next;
}Node;
void insertNodeBegin(Node **head, Node *newNode){
if((*head)==NULL){
(*head)->next = NULL;
*head = newNode;
}
else{
newNode->next = (*head)->next;
(*head)->next = newNode;
}
}
void printList(Node *head){
Node *current = head;
while(current != NULL){
printf("%d ", current->data);
current = current->next;
}
}
int main()
{
Node *head = NULL;
Node *newNode1= malloc(sizeOf(Node));
newNode1->data = 12;
Node *newNode2 = malloc(sizeOf(Node));
newNode2->data = 16;
Node *newNode3 = malloc(sizeOf(Node));
newNode3->data = 55;
Node *newNode4 = malloc(sizeOf(Node));
newNode4->data = 8;
insertNodeBegin(&head, newNode1);
insertNodeBegin(&head, newNode2);
insertNodeBegin(&head, newNode3);
insertNodeBegin(&head, newNode4);
printList(head);
return 0;
}
sizeOf(Node) should be sizeof(Node), note the small o. After all this is C, not Java. :)
In short reorder if commands inside insertNodeBegin()
if(*head == NULL){
*head = newNode;
(*head)->next = NULL;
}
Because in first execution of program head points to NULL and for it we haven't any allocated memory. As head in first execution not points to a Node location using head -> next not valid and cause segmentation fault in run time.
In node creation with malloc() we must check is memory allocation successful or not. for that i use createNode() function.
We check if memory allocation for Node is OK then add that Node to list. And finally Use void as main(void) argument.
After these change your code becomes
Code
#include <stdio.h>
#include <stdlib.h>
typedef struct Node_{
int data;
struct Node_ *next;
}Node;
void insertNodeBegin(Node **head, Node *newNode){
if(*head == NULL){
*head = newNode;
(*head)->next = NULL;
}
else{
newNode->next = (*head)->next;
(*head)->next = newNode;
}
}
void printList(Node *head){
Node *current = head;
while(current != NULL){
printf("%d ", current->data);
current = current->next;
}
}
int createNode(Node **node, int value){
*node = malloc(sizeof(Node));
if(*node){
(*node)->data = value;
return 1;
}else{
fprintf(stdout, "%s", "can't allocate ...\n");
return 0;
}
}
int main(void)
{
Node *head = NULL;
Node *newNode1;
Node *newNode2;
Node *newNode3;
Node *newNode4;
if(createNode(&newNode1, 12)) {
insertNodeBegin(&head, newNode1);
}
if(createNode(&newNode2, 16)) {
insertNodeBegin(&head, newNode2);
}
if(createNode(&newNode3, 55)) {
insertNodeBegin(&head, newNode3);
}
if(createNode(&newNode4, 8)) {
insertNodeBegin(&head, newNode4);
}
printList(head);
return 0;
}

C: How to free nodes in the linked list?

How will I free the nodes allocated in another function?
struct node {
int data;
struct node* next;
};
struct node* buildList()
{
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = malloc(sizeof(struct node));
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
return head;
}
I call the buildList function in the main()
int main()
{
struct node* h = buildList();
printf("The second element is %d\n", h->next->data);
return 0;
}
I want to free head, second and third variables.
Thanks.
Update:
int main()
{
struct node* h = buildList();
printf("The element is %d\n", h->next->data); //prints 2
//free(h->next->next);
//free(h->next);
free(h);
// struct node* h1 = buildList();
printf("The element is %d\n", h->next->data); //print 2 ?? why?
return 0;
}
Both prints 2. Shouldn't calling free(h) remove h. If so why is that h->next->data available, if h is free. Ofcourse the 'second' node is not freed. But since head is removed, it should be able to reference the next element. What's the mistake here?
An iterative function to free your list:
void freeList(struct node* head)
{
struct node* tmp;
while (head != NULL)
{
tmp = head;
head = head->next;
free(tmp);
}
}
What the function is doing is the follow:
check if head is NULL, if yes the list is empty and we just return
Save the head in a tmp variable, and make head point to the next node on your list (this is done in head = head->next
Now we can safely free(tmp) variable, and head just points to the rest of the list, go back to step 1
Simply by iterating over the list:
struct node *n = head;
while(n){
struct node *n1 = n;
n = n->next;
free(n1);
}
One function can do the job,
void free_list(node *pHead)
{
node *pNode = pHead, *pNext;
while (NULL != pNode)
{
pNext = pNode->next;
free(pNode);
pNode = pNext;
}
}
struct node{
int position;
char name[30];
struct node * next;
};
void free_list(node * list){
node* next_node;
printf("\n\n Freeing List: \n");
while(list != NULL)
{
next_node = list->next;
printf("clear mem for: %s",list->name);
free(list);
list = next_node;
printf("->");
}
}
You could always do it recursively like so:
void freeList(struct node* currentNode)
{
if(currentNode->next) freeList(currentNode->next);
free(currentNode);
}

Resources