Created
March 24, 2022 08:36
-
-
Save JustinStitt/409b03ff58be5b61e7e0ae4aa20b0b16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
struct Node { | |
int data; | |
Node* next; | |
Node(int _data) : data(_data) {} | |
}; | |
class SinglyLinkedList { | |
private: | |
Node* head, *tail; | |
public: | |
SinglyLinkedList() : head(nullptr) {} | |
void push(int data) { // pushes to the back | |
Node* to_add = new Node(data); | |
if(head == nullptr) { | |
/* list is empty! */ | |
head = to_add; | |
tail = head; | |
return; | |
} | |
tail->next = to_add; | |
tail = tail->next; | |
} | |
void print() { | |
Node* tmp = head; | |
while(tmp != nullptr) { | |
std::cout << tmp->data << "->"; | |
tmp = tmp->next; | |
} | |
std::cout << "\n"; | |
} | |
bool isEmpty() { | |
return head == nullptr; | |
} | |
}; | |
int main() { | |
SinglyLinkedList sll; | |
sll.push(4); | |
sll.push(7); | |
sll.push(6); | |
sll.print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment