Skip to content

Instantly share code, notes, and snippets.

@PiotrWegrzyn
Created May 26, 2017 00:27
Show Gist options
  • Save PiotrWegrzyn/1b77b09142190c34e583ef95e7f1cb8a to your computer and use it in GitHub Desktop.
Save PiotrWegrzyn/1b77b09142190c34e583ef95e7f1cb8a to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
//https://gist.github.com/Brejw15/5aed87ba877a0c8cef8b02efc582e7d4
//https://codeshare.io/algorytmyOperacjeNaListach
struct node{
int val;
node * next;
};
bool isEmpty(node *& list){
if (list){
return false;
}
return true;
}
void show(node * list){
node *p = list;
cout << endl;
while (p) {
cout << p->val << " -> ";
p = p->next;
}
cout << "null" << endl;
}
void addLast( node * &list , int n){
node * nn = new node;
nn->val = n;
nn->next = NULL;
node * p = list;
if (isEmpty(list)){
list = nn;
}
else{
while (p->next){
p = p->next;
}
p->next = nn;
}
}
//a -> b -> c
//new -> a -> b -> c
void addFirst(node * &list, int n){
node * nn = new node;
nn->val = n;
nn->next = list;
list = nn;
}
void addAfterX(node *& list, int X, int n){
node * p = list;
while (p){
if (p->val == X){
node * nn = new node;
nn->val = n;
nn->next = p->next;
p->next = nn;
return ;
}
p = p->next;
}
}
//a -> b -> c - > null
// a -> b -> null
void removeLast(node *&list){
if (!isEmpty(list)){
node * p = list;
if (p->next == NULL){
list = NULL;
}
else{
while (p->next->next != NULL){
p = p->next;
}
p->next = NULL;
}
}
}
//before -> p1 -> p2 - > null
//before -> p2 -> p1 -> null
void swapNext(node * &before){
node * p1 = before -> next;
node * p2 = p1->next;
before->next = p2;
p1->next = p2->next;
p2->next = p1;
}
int getsize(node * &list){
node *p = list;
int size = 0;
while (p) {
size++;
p = p->next;
}
return size;
}
//pb,1,2,3,1,5,7,10
//
void boubbleSort(node *& list){
int size = getsize(list);
if (size > 1){
node * pb = new node; //pointer on before
pb->next = list;
node * p1 = list;
node * p2 = list->next;
for (int i = 0; i < size-1; i++){
if (p1->val > p2->val){
swapNext(pb);
}
p1 = pb->next->next;
p2 = p1->next;
pb = pb->next;
}
}
}
int main(){
node * list = nullptr;
addLast(list, 1);
addLast(list, 2);
addLast(list, 3);
addLast(list, 1);
addLast(list, 5);
addLast(list, 7);
addLast(list, 10);
node * list2 = nullptr;
/* OD KOMENTOWA W RAZIE PROGBELMÓW TO DZIAŁA ELO
node * p = new node;
p->val = 5;
p->next = NULL;
list = p;
if (!isEmpty(list))
cout << list->val <<endl;
if (list2!=NULL)
cout << list2->val << endl;
node * p2 = nullptr;
p2 = list;
int i = 1;
while (p2){
cout << i <<" : "<< p2->val<<endl;
i++;
p2 = p2->next;
}
addLast(list,5);
show(list);
addFirst(list, 2);
addFirst(list, 1);
show(list);
addAfterX(list, 2, 1);
show(list);
removeLast(list);
show(list);
*/
/*REMOVE TEST
node * head = NULL;
removeLast(head);
show(head);
node * head2 = NULL;
show(head2);
addFirst(head2, 1);
show(head2);
removeLast(head2);
show(head2);
*/
show(list);
swapNext(list);
show(list);
boubbleSort(list);
show(list);
system("PAUSE");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment