-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.cpp
executable file
·63 lines (57 loc) · 1.28 KB
/
struct.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
/**
* @brief this code is for to show about struct this code makes a linked list with struct
* @brief struct is a user defined data type which works like class but it doesn't have
* @brief security like class and don't have methods like class
*/
struct Node
{
int data;
Node *next;
};
Node *make_list(int arr[], int size)
{
Node *head = new Node();
Node *temp = head;
head->data = arr[0];
for (int i = 1; i < size; i++)
{
Node *newNode = new Node();
newNode->data = arr[i];
newNode->next = nullptr;
temp->next = newNode;
temp = newNode;
}
return head;
}
void append(Node *head, int value)
{
Node *temp = head;
while (temp->next != nullptr)
{
temp = temp->next;
}
Node *newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
temp->next = newNode;
}
void print_list(Node *head, int size)
{
Node *temp = head;
while (temp != nullptr)
{
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
}
int main()
{
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
Node *head = make_list(arr, size);
append(head, 100);
print_list(head, size);
return 0;
}