print the elements of a linked list c++ code example
Example 1: how to print the elements of a linked list in c
typedef struct node{
int value;
struct node *next;
}node;
void printList(node *head){
node *tmp = head;
while(tmp != NULL){
if(tmp->next == NULL){
printf("%d", tmp->value);
}
else{
printf("%d, ", tmp->value);
}
tmp = tmp->next;
}
}
Example 2: print elements of linked list
void printLinkedList(SinglyLinkedListNode* head) {
while(head!=NULL)
{
cout<<head->data<<endl;
head=head->next;
}
}