-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cc
More file actions
62 lines (55 loc) · 1.46 KB
/
Copy pathLinkedList.cc
File metadata and controls
62 lines (55 loc) · 1.46 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
52
53
54
55
56
57
58
59
60
61
62
/*=============================================================================
# Author: Hailin - https://fuhailin.github.io/
# Email: [email protected]
# Description: /Singly linked list in C++
=============================================================================*/
#include "LinkedList.h"
#include <iostream>
void LinkedList::printLinkedList(ListNode *head) {
ListNode *temp = head;
while (temp) {
std::cout << temp->val;
if (temp->next)
std::cout << "->";
temp = temp->next;
}
}
ListNode *LinkedList::createLinkedList(std::vector<int> nodes) {
ListNode *hair = new ListNode(0);
ListNode *cur = hair;
for (auto x : nodes) {
cur->next = new ListNode(x);
cur = cur->next;
}
return hair->next;
}
void myLinkedList::addNode(int value) {
ListNode *temp = new ListNode(value); // create new Node
if (head == NULL) {
head = temp;
tail = temp;
temp = NULL;
} else {
tail->next = temp;
tail = temp;
}
}
void myLinkedList::display() {
ListNode *temp; // = new ListNode;
temp = head;
while (temp) {
std::cout << temp->val;
if (temp->next)
std::cout << "->";
temp = temp->next;
}
}
// returns the first element in the list and deletes the Node.
// caution, no error-checking here!
int myLinkedList::popValue() {
ListNode *n = head;
int ret = n->val;
head = head->next;
delete n;
return ret;
}