-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec1_2.cpp
More file actions
51 lines (44 loc) · 776 Bytes
/
lec1_2.cpp
File metadata and controls
51 lines (44 loc) · 776 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
// insertion at beginning and traversing
// copying elements of array to LL
//basic linked list structure
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int value)
{
data = value;
next = NULL;
}
};
int main()
{
Node *head = NULL;
int arr[] = {1,2,3,4,5};
for(int i = 0 ; i<5 ; i++)
{
//Linked list does not exist
if(head == NULL)
{
head = new Node(arr[i]);
}
//Linked list exists
else
{
Node *temp ;
temp = new Node(arr[i]);
temp->next = head;
head = temp;
}
}
// traverse
Node *temp = head;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}