Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My linked list code seems to print out the value of data in an infinite loop and i believe it's because the head is never set to NULL. I am not sure how to do this.

What I have tried:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
	int data;
	struct Node* next;
}Node;

typedef struct 
{
	Node* head;
}Linked_List;

/*------------------------------------------------------------------*/

Linked_List* create_empty_list(void)
{
	Linked_List* new_head; /*create list pointer*/
	new_head = (Linked_List*)malloc(sizeof(Linked_List)); /*assign it memory of head struct*/
	new_head->head = NULL;
	
	return new_head;
}

Node* insert_start(Linked_List* list, int data)
{
	Node* new_node;
	new_node = (Node*)malloc(sizeof(Node));
	new_node->data = data;
	new_node->next = list->head;
	list->head = new_node;
	
	return new_node;
}
	
void display(Linked_List* list)
{
	Node* temp;
	temp = list->head;
	while(temp != NULL)
	{
		printf("%d ", temp->data);
	}
	temp = temp->next;
}
	
int main(void)
{
	
	Linked_List* the_list = create_empty_list();
	
	the_list->head = insert_start(the_list, 5);
	
	display(the_list);
	
	
	return(0);
	
}
Posted
Updated 18-Oct-17 8:44am
Comments
k5054 18-Oct-17 12:14pm    
Check your while loop in display(), and see if you can figure out why it does not terminate.

Jochen already fixed your bug.
Additionally, please note you need neither the Linked_List struct nor the create_empty_list function, while you do need to release memory.
Try
C
#include <stdlib.h>
#include <stdio.h>

typedef struct Node
{
  int data;
  struct Node* next;
} Node;

Node * insert(Node* head, int data)
{
  Node* new_node;
  new_node = (Node*)malloc(sizeof(Node));
  new_node->data = data;
  new_node->next = head;
  return new_node;
}

void display(Node * head)
{
  Node * temp = head;
  while(temp != NULL)
  {
    printf("%d ", temp->data);
    temp = temp->next;
  }
}

void cleanup(Node * head)
{
  Node * temp = head;
  while ( temp != NULL)
  {
    Node * cur = temp;
    temp = temp->next;
    free(cur);
  }
}


int main()
{
  Node * head = NULL;

  head = insert(head, 1);
  head = insert(head, 3);
  head = insert(head, 5);
  head = insert(head, 12);

  display(head);
  printf("\n");
  cleanup(head);

  return 0;
}
 
Share this answer
 
Quote:
i believe it's because the head is never set to NULL.

Stop believing, learn the debugger and ensure.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
The update of temp->next in the display function must be inside the while loop:
void display(Linked_List* list)
{
    Node* temp;
    temp = list->head;
    while(temp != NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900