forked from ankit-kaushal/Lets-go-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayInsertionUsingLinkedList
More file actions
51 lines (43 loc) Β· 924 Bytes
/
arrayInsertionUsingLinkedList
File metadata and controls
51 lines (43 loc) Β· 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void insertelements(Node* root, int item)
{
Node* temp = new Node;
Node* ptr;
temp->data = item;
temp->next = NULL;
if (*root == NULL)
*root = temp;
else {
ptr = *root;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
void display(Node* root)
{
while (root != NULL) {
cout << root->data << " ";
root = root->next;
}
}
Node *arrayToList(int list[], int length)
{
Node *root = NULL;
for (int i = 0; i < length; i++)
insertelements(&root, list[i]);
return root;
}
int main()
{
int list[] = { 21, 32, 43, 54, 65 };
int length = sizeof(arr) / sizeof(arr[0]);
Node* root = arrayToList(list, length);
display(root);
return 0;
}