Skip to content

Instantly share code, notes, and snippets.

@M1nified
Created April 18, 2020 12:07
Show Gist options
  • Save M1nified/4f5844b0ae632fb295dded771b4b25aa to your computer and use it in GitHub Desktop.
Save M1nified/4f5844b0ae632fb295dded771b4b25aa to your computer and use it in GitHub Desktop.
#include <iostream>
struct node {
int val;
struct node *next;
};
void add (node*& head, int x)
{
node *new_node = new node;
new_node->val = x;
new_node->next=head;
head=new_node;
}
void print_list(node*head){
while(head != NULL){
std::cout<<head->val<<" ";
head=head->next;
}
std::cout<<std::endl;
}
void remove_even(node*&head){
node*current;
node*prev;
current=head;
prev=NULL;
while(current!=NULL){
if(current->val%2==0){
if(prev!=NULL){
prev->next=current->next;
delete current;
current=prev->next;
}else{
head=current->next;
delete current;
current=head;
}
}else{
prev=current;
current=current->next;
}
}
}
void find(node*head, int val_to_find, node*&curr, node*&prev){
curr=head;
prev=NULL;
while(curr!=NULL && curr->val != val_to_find){
prev=curr;
curr=prev->next;
}
}
void remove_between(node*head, int x, int y){
node *x_n, *x_prev, *y_n, *y_prev;
find(head, x, x_n, x_prev);
if(x_n == NULL){
return;
}
find(head, y, y_n, y_prev);
if(y_n == NULL){
return;
}
x_n->next=y_n;
// dodac kasowanie elementow (delete)
}
void move_between_to_tail(node*head, int x, int y){
node *x_n, *x_prev, *y_n, *y_prev, *last;
find(head, x, x_n, x_prev);
if(x_n == NULL){
return;
}
find(head, y, y_n, y_prev);
if(y_n == NULL || x_n==y_n){
return;
}
last=y_n;
while(last->next != NULL){
last=last->next;
}
last->next=x_n->next;
x_n->next=y_n;
y_prev->next=NULL;
}
void copy_to_tail_reversed(node*head){
node *rev_head, *curr, *prev;
rev_head=prev=NULL;
curr=head;
while(curr!=NULL){
add(rev_head, curr->val);
prev=curr;
curr=curr->next;
}
if(prev!=NULL){
prev->next=rev_head;
}
}
void max_to_head(node*&head){
node *max_node, *max_prev, *curr, *prev;
max_prev=prev=NULL;
curr=max_node=head;
while(curr!=NULL){
if(curr->val > max_node->val){
max_node=curr;
max_prev=prev;
}
prev=curr;
curr=curr->next;
}
if(max_prev!=NULL){
max_prev->next=max_node->next;
max_node->next=head;
head=max_node;
}
}
void swap_head_with_tail(node*&head){
node*tail,*tail_prev;
tail_prev=NULL;
tail=head;
if(head==NULL) return;
while(tail->next!=NULL){
tail_prev=tail;
tail=tail->next;
}
if(tail_prev!=NULL){
if(head->next!=tail){
tail->next=head->next;
head->next=NULL;
tail_prev->next=head;
head=tail;
}else{
tail->next=head;
head->next=NULL;
head=tail;
}
}
}
int main() {
node *h1, *h2, *h3, *h4, *h5, *h6, *h7;
h1=h2=h3=h4=h5=h6=h7=NULL;
add(h2, 5);
add(h3, 8); add(h3, 5);
add(h4, 22); add(h4, 12); add(h4, 8); add(h4, -5); add(h4, 18); add(h4, 5); add(h4, 8); add(h4, 6); add(h4, 3); add(h4, 5);
add(h5, 25); add(h5, 8); add(h5, 55);
add(h6, 25); add(h6, 55); add(h6, 8);
add(h7, 25); add(h7, 75); add(h7, 25); add(h7, 55); add(h7, 8);
print_list(h1);print_list(h2);print_list(h3);print_list(h4);print_list(h5);print_list(h6);print_list(h7);
// remove_between(h1, 6,12);
// remove_between(h2, 6,12);
// remove_between(h3, 6,12);
// remove_between(h4, 6,12);
// print_list(h1);print_list(h2);print_list(h3);print_list(h4);print_list(h5);print_list(h6);print_list(h7);
// move_between_to_tail(h1, 6,12);
// move_between_to_tail(h2, 6,12);
// move_between_to_tail(h3, 6,12);
// move_between_to_tail(h4, 6,12);
// print_list(h1);print_list(h2);print_list(h3);print_list(h4);print_list(h5);print_list(h6);print_list(h7);
// copy_to_tail_reversed(h1);
// copy_to_tail_reversed(h2);
// copy_to_tail_reversed(h3);
// copy_to_tail_reversed(h4);
// print_list(h1);print_list(h2);print_list(h3);print_list(h4);print_list(h5);print_list(h6);print_list(h7);
// max_to_head(h1);
// max_to_head(h2);
// max_to_head(h3);
// max_to_head(h4);
// max_to_head(h6);
// max_to_head(h7);
// print_list(h1);print_list(h2);print_list(h3);print_list(h4);print_list(h5);print_list(h6);print_list(h7);
swap_head_with_tail(h1);
swap_head_with_tail(h2);
swap_head_with_tail(h3);
swap_head_with_tail(h4);
print_list(h1);print_list(h2);print_list(h3);print_list(h4);print_list(h5);print_list(h6);print_list(h7);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment