-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.h
61 lines (48 loc) · 976 Bytes
/
LinkedList.h
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
#include "Node.h"
#include <iostream>
using namespace std;
class LinkedList { //This is a linked list. It works simply by indexing the tail of the node
public:
node * Head = nullptr; //stores the head
node * Tail = nullptr; //stores the tail
LinkedList()
{
}
void Add(int val) //Method to add a node to the Linked List and maintain the order.
{
node * nd = new node(val, nullptr);
if (Head == nullptr) //If there is nothing in the Linked List
{
Head = nd; //Add a node
Tail = nd;
}
else //If there is something in the linked List
{
Tail->link = nd;
Tail = Tail->link;
}
}
node * deQueue()
{
node * nd = Head;
if(Head != nullptr)
Head = Head->link;
return nd;
}
void Print()//print values
{
if (Head == nullptr)
{
cout << "null\n";
}
else {
node * Traverse = Head;
while (Traverse != nullptr)
{
cout << "->" << Traverse->value;
Traverse = Traverse->link;
}
cout << "\n";
}
}
};