Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created August 15, 2016 21:11
Show Gist options
  • Save dgodfrey206/2edf0da30ff83ca11e0719857b431c3a to your computer and use it in GitHub Desktop.
Save dgodfrey206/2edf0da30ff83ca11e0719857b431c3a to your computer and use it in GitHub Desktop.
Random linked list stuff
#include<iostream>
struct node{
int data;
node* next;
};
node* NewNode(int data){
node* n=new node;
n->data=data;
n->next=NULL;
return n;
}
void reverse(node** root) {
node* prev=NULL;
node* cur=*root;
node* next;
while (cur != NULL) {
next=cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
*root = prev;
}
void Push(node** head_ref, node* newNode) {
newNode->next = *head_ref;
*head_ref = newNode;
}
void Push(node** head_ref, int key) {
Push(head_ref, NewNode(key));
}
std::size_t Length(node* head) {
int i = 0;
for (; head != NULL; head = head->next) ++i;
return i;
}
node* BuildBounded(int l, int r) {
struct node dummy;
struct node* tail = &dummy;
int i;
dummy.next = NULL;
for (i = l; i <= r; ++i) {
Push(&tail->next, i);
tail = tail->next;
}
return dummy.next;
}
node* BuildOneTwoThree(){return BuildBounded(1,3);}
node* BuildN(int n){return BuildBounded(1,n);}
void Display(node*root){
std::size_t len = Length(root);
while(root){
std::cout<<root->data<<" ";
root=root->next;
}
std::cout<< "(len="<<len<<")\n";
}
int Count(struct node* cur, int key) {
int count;
for (count = 0; cur != NULL; cur = cur->next) {
if (cur->data == key) count++;
}
return count;
}
int GetNth(struct node* cur, int index) {
while (cur != NULL && index != 0) {
cur = cur->next;
index--;
}
return cur? cur->data: -1;
}
void DeleteList(struct node** head_ref) {
while (*head_ref) {
struct node* next = (*head_ref)->next;
delete *head_ref;
*head_ref = next;
}
}
void Pop(struct node** head_ref) {
struct node* head = *head_ref;
struct node* next;
if (!head)
return;
next = head->next;
delete head;
*head_ref = next;
}
void InsertNth(struct node** current, int index, int n) {
while (*current != NULL && index-- >= 0) {
current = &(*current)->next;
}
Push(current, n);
}
void SortedInsert(struct node** head_ref, struct node* newNode) {
while (*head_ref != NULL && (*head_ref)->data <= newNode->data)
head_ref = &(*head_ref)->next;
newNode->next = *head_ref;
*head_ref = newNode;
}
void InsertSort(struct node** head_ref) {
struct node* dummyHead = NULL;
struct node* current = *head_ref;
while (current != NULL) {
struct node* next = current->next;
SortedInsert(&dummyHead, current);
current = next;
}
*head_ref = dummyHead;
}
void Append(struct node** aRef, struct node** bRef) {
struct node dummy;
struct node* tail = &dummy;
dummy.next = *aRef;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = *bRef;
*aRef = dummy.next;
*bRef = NULL;
}
void FrontBackSplit(struct node** head_ref, struct node** front, struct node** back) {
std::size_t length = Length(*head_ref);
std::size_t pivot = (length + 1)/2;
std::size_t i;
struct node* newNode;
for (i = 0; *head_ref != NULL && i < pivot; ++i) {
newNode = NewNode((*head_ref)->data);
Push(front, newNode);
head_ref = &(*head_ref)->next;
front = &(*front)->next;
}
for (i = pivot; *head_ref != NULL && i < length; ++i) {
newNode = NewNode((*head_ref)->data);
Push(back, newNode);
head_ref = &(*head_ref)->next;
back = &(*back)->next;
}
}
int main() {
struct node* head = BuildN(9);
struct node* front = NULL, *back = NULL;
Display(head);
// do something with head
DeleteList(&head);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment