-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
46 lines (36 loc) · 909 Bytes
/
Copy pathlinked_list.cpp
File metadata and controls
46 lines (36 loc) · 909 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
#include <iostream>
#include <cstdlib>
using namespace std;
struct Node { //Define a Node struct
int data;
Node* next; //Pointer to the next node
};
struct LinkedList { //Define a LinkedList struct
Node* head; //Pointer to first node
Node* tail; //Pointer to last node
};
void insertToList(LinkedList* list, int value) {
if (list -> head == 0) {
//empty list
Node* n = new Node;
n->data = value;
n->next = 0;
list->head = n;
list->tail = n;
}
else {
//Add new node onto the head of the list
Node* n = new Node;
n->data = value;
n->next = list->head;
list->head = n;
}
}
int main() {
LinkedList mylist; //Create a LinkedList struct
insertToList(&mylist, 10); //Insert 3 nodes into mylist
insertToList(&mylist, 15);
insertToList(&mylist, 20);
cout << mylist.head->next->next->data << endl; //Should print 10
return 0;
}