Skip to content

Instantly share code, notes, and snippets.

@interval1066
Created October 26, 2019 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save interval1066/474b4f0d4e6c92b1263aa81574d4a4c9 to your computer and use it in GitHub Desktop.
Save interval1066/474b4f0d4e6c92b1263aa81574d4a4c9 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#include <memory>
using namespace std;
/* Link list node */
class Node
{
public:
int key;
Node* next;
};
class NodeUtils
{
public:
void
push(Node** head_ref, int new_key)
{
/* allocate node */
Node* new_node = new Node();
/* put in the key */
new_node->key = new_key;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
void
listall(Node* head)
{
Node* current = head;
while(current != nullptr) {
cout << current->key;
current = current->next;
if(current != nullptr) cout << ", ";
}
cout << endl;
}
/* searches for the value x in linked list */
Node*
search(Node* head, int x)
{
Node* current = head;
while(current != nullptr) {
if (current->key == x)
return current;
current = current->next;
}
return nullptr;
}
unsigned
count(Node* head)
{
Node* current = head;
unsigned count = 0;
while(current != nullptr) {
current = current->next;
count++;
}
return count;
}
void
sortasc(Node* head)
{
Node* temphead = head;
int tempkey;
unsigned counter = count(head);
for(unsigned n = 0; n < counter; n++) {
while(temphead->next) {
if(temphead->key > temphead->next->key) {
tempkey = temphead->key;
temphead->key = temphead->next->key;
temphead->next->key = tempkey;
}
temphead = temphead->next;
}
temphead = head;
}
}
void sortdsc(Node* head)
{
Node* temphead = head;
int tempkey;
unsigned counter = count(head);
for(unsigned n = 0; n < counter; n++) {
while(temphead->next) {
if(temphead->key < temphead->next->key) {
tempkey = temphead->key;
temphead->key = temphead->next->key;
temphead->next->key = tempkey;
}
temphead = temphead->next;
}
temphead = head;
}
}
Node*
deletefirst(Node* head)
{
Node* temp = new Node;
temp = head;
head = head->next;
delete temp;
return head;
}
void
deletelast(Node* head)
{
Node* current = new Node;
Node* previous = new Node;
current = head;
while(current->next != nullptr) {
previous = current;
current = current->next;
}
previous->next = nullptr;
delete current;
}
bool
deletevalue(Node* head, int val)
{
Node* temp = head;
if(head->key == val) {
head = head->next;
delete temp;
}
while(temp->next != nullptr && temp->next->key != val)
temp = temp->next;
if(temp->next == nullptr)
return false;
else {
if(temp->next->key == val) {
Node* remove = temp->next;
temp->next = temp->next->next;
delete remove;
}
}
return true;
}
};
int
main(int argc, char** argv)
{
/* Start with the empty list */
unique_ptr<NodeUtils> utl = make_unique<NodeUtils>();
Node* head = nullptr;
utl->push(&head, 10);
utl->push(&head, 30);
utl->push(&head, 11);
utl->push(&head, 21);
utl->push(&head, 14);
utl->push(&head, 22);
utl->push(&head, 56);
utl->push(&head, 57);
utl->push(&head, 104);
utl->search(head, 21)? cout << "Node found" << endl :
cout << "Node not found" << endl;
utl->listall(head);
cout << "number of elements: " << utl->count(head) << endl;
utl->sortasc(head);
cout << endl << "Sorted ascending:" << endl;
utl->listall(head);
utl->sortdsc(head);
cout << endl << "Sorted descending:" << endl;
utl->sortdsc(head);
utl->listall(head);
cout << endl << "Deleteing head node: " << head->key << endl;
head = utl->deletefirst(head);
utl->listall(head);
cout << endl << "Searching for 14" << endl;
(utl->search(head, 14) != nullptr)? cout << "Found" << endl :
cout << "Not found" << endl;
cout << endl << "Deleting tail" << endl;
utl->deletelast(head);
utl->listall(head);
cout << endl << "Deleting node valued 14" << endl;
utl->deletevalue(head, 14);
utl->listall(head);
return EXIT_SUCCESS;
}
@interval1066
Copy link
Author

Push down list I wrote for a prospective employer. They didn't hire me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment