Skip to content

Commit e625727

Browse files
Printing linkedlist in forward and backward manner
Printing elements in forward and reverse manner with "-" between all data. #hactoberfest
1 parent 1979373 commit e625727

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* struct Node
2+
{
3+
int data;
4+
struct Node* next;
5+
};
6+
7+
*/
8+
9+
//This function prints ll in forward direction
10+
void forwardPrint(struct Node* head)
11+
{
12+
while(head != NULL) {
13+
cout<<head->data<<"-";
14+
head = head->next;
15+
}
16+
}
17+
18+
//This function prints ll in reverse direction
19+
void backwardPrint(struct Node* head)
20+
{
21+
if(head == NULL) return;
22+
backwardPrint(head->next);
23+
cout<<head->data<<"-";
24+
}
25+
26+
/*
27+
Sample Input:
28+
1
29+
3
30+
1
31+
2
32+
3
33+
34+
Sample Output:
35+
1-2-3-
36+
3-2-1-
37+
*/

0 commit comments

Comments
 (0)