-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntroToLinkedList.cpp
More file actions
51 lines (38 loc) · 1.13 KB
/
IntroToLinkedList.cpp
File metadata and controls
51 lines (38 loc) · 1.13 KB
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
//LINKED LIST
#include<iostream>
#include<stdlib.h>
using namespace std;
struct Node {
int data;
struct Node* next; //struct Node type ka pointer hai and it points towads this kind ok structure
};
void linkedListTraversal(struct Node* ptr){
while(ptr != NULL){
cout<<(ptr->data)<<endl;
ptr = ptr->next;
}
}
int main(){
struct Node * head;
struct Node * second;
struct Node * third;
struct Node * fourth;
//Allocate memory for nodes in the linked list in HEAP
head = (struct Node *) malloc(sizeof(struct Node));
second = (struct Node *) malloc(sizeof(struct Node));
third = (struct Node *) malloc(sizeof(struct Node));
fourth = (struct Node *) malloc(sizeof(struct Node));
// Link first and second nodes
head->data = 42;
head->next = second;
// Link second and third nodes
second->data = 23;
second->next = third;
// Link third and fourth nodes
third->data = 18;
third->next = fourth;
// Terminate Linked List at the fourth node
fourth->data = 1;
fourth->next = NULL;
linkedListTraversal(head);
}