Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

print linked list recursively c++

/*
  follow me: https://codejudge.blogspot.com/
*/

#include<iostream>
using namespace std;

struct node
{
	int data;
	node* next;
};
node* insert(node* head, int value)
{
	node* temp1 = (node*)malloc(sizeof(node));
	temp1->data = value;
	temp1->next = NULL;
	if (head == NULL)
	{
		head = temp1;
	}
	else
	{
		node* temp2 = head;
		while (temp2->next!=NULL)
		{
			temp2 = temp2->next;
		}
		temp2->next = temp1;
	}
	return head;
}
void print(node* p)
{
	if (p == NULL)
		return;
	cout << p->data << " ";
	print(p->next);
}
int main()
{
	node* head = NULL;
	head = insert(head, 1);
	head = insert(head, 2);
	head = insert(head, 3);
	print(head);

	return 0;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #print #linked #list #recursively
ADD COMMENT
Topic
Name
3+3 =