Skip to content

Commit c722f88

Browse files
committed
Adding print elements in cpp
1 parent 37fa27c commit c722f88

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class SinglyLinkedListNode {
6+
public:
7+
int data;
8+
SinglyLinkedListNode *next;
9+
10+
SinglyLinkedListNode(int node_data) {
11+
this->data = node_data;
12+
this->next = nullptr;
13+
}
14+
};
15+
16+
class SinglyLinkedList {
17+
public:
18+
SinglyLinkedListNode *head;
19+
SinglyLinkedListNode *tail;
20+
21+
SinglyLinkedList() {
22+
this->head = nullptr;
23+
this->tail = nullptr;
24+
}
25+
26+
void insert_node(int node_data) {
27+
SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data);
28+
29+
if (!this->head) {
30+
this->head = node;
31+
} else {
32+
this->tail->next = node;
33+
}
34+
35+
this->tail = node;
36+
}
37+
};
38+
39+
40+
void free_singly_linked_list(SinglyLinkedListNode* node) {
41+
while (node) {
42+
SinglyLinkedListNode* temp = node;
43+
node = node->next;
44+
45+
free(temp);
46+
}
47+
}
48+
49+
// Complete the printLinkedList function below.
50+
51+
/*
52+
* For your reference:
53+
*
54+
* SinglyLinkedListNode {
55+
* int data;
56+
* SinglyLinkedListNode* next;
57+
* };
58+
*
59+
*/
60+
void printLinkedList(SinglyLinkedListNode* head) {
61+
while(head != NULL){
62+
cout<<head->data<<endl;
63+
head = head->next;
64+
}
65+
66+
}
67+
68+
int main()
69+
{
70+
SinglyLinkedList* llist = new SinglyLinkedList();
71+
72+
int llist_count;
73+
cin >> llist_count;
74+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
75+
76+
for (int i = 0; i < llist_count; i++) {
77+
int llist_item;
78+
cin >> llist_item;
79+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
80+
81+
llist->insert_node(llist_item);
82+
}
83+
84+
printLinkedList(llist->head);
85+
86+
return 0;
87+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ A collection of solutions for HackerRank data structures and algorithm problems
1717
| | [Sparse Arrays](https://www.hackerrank.com/challenges/sparse-arrays/problem) | Medium | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Arrays/sparse_arrays.cpp) | | |
1818
| | [Array Manipulation](https://www.hackerrank.com/challenges/crush/problem) | Hard | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Arrays/array_manipulation.cpp) | | |
1919
| Linked Lists | | | | | |
20-
| | [Print the Elements of a Linked List](https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list/problem) | Easy | | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Linked_Lists/print_elements.java) | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Linked_Lists/Print_the_Elements.py) |
20+
| | [Print the Elements of a Linked List](https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list/problem) | Easy | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Linked_Lists/solution.cpp) | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Linked_Lists/print_elements.java) | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Linked_Lists/Print_the_Elements.py) |
2121
| | [Insert a node at the head of a linked list](https://www.hackerrank.com/challenges/insert-a-node-at-the-head-of-a-linked-list/problem) | Easy | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Linked_Lists/Insert%20a%20node%20at%20the%20head%20of%20a%20linked%20list.cpp) | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Linked_Lists/Insert%20a%20node%20at%20the%20head%20of%20a%20linked%20list/solution.java)| |
2222
| | [Insert a Node at the Tail of a Linked List](https://www.hackerrank.com/challenges/insert-a-node-at-the-tail-of-a-linked-list/problem) | Easy | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Linked_Lists/Insert%20a%20Node%20at%20the%20Tail%20of%20a%20Linked%20List.cpp) | [View](https://github.com/adityabisoi/ds-algo-solutions/tree/main/Linked%20Lists/Insert%20a%20Node%20at%20the%20Tail%20of%20a%20Linked%20List/solution.java)| |
2323
| | [Insert a node at a specific position in a linked list](https://www.hackerrank.com/challenges/insert-a-node-at-a-specific-position-in-a-linked-list/problem) | Easy | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Linked_Lists/Insert%20a%20node%20at%20a%20specific%20position%20in%20a%20linked%20list.cpp) | [View](https://github.com/adityabisoi/ds-algo-solutions/blob/main/Linked%20Lists/Insert%20a%20node%20at%20a%20specific%20position%20in%20a%20linked%20list/solution.java)| |

0 commit comments

Comments
 (0)