Saturday, July 29, 2017

stack.cpp

#include"stack.h"

Stack::Stack()
{
  LinkedList* list = new LinkedList();
}

Stack::~Stack()
{
  removeAll();
}

int Stack::empty()
{
  return list->empty();
}

void Stack::push(int num)
{
  list->prepend(num);
}

int Stack::pop()
{
  return list->removeHead();
}

stack.h

#include"LinkedList.h"

class Stack{
private:
  LinkedList* list;
public:
  Stack();
  ~Stack();
  int empty();
  void push(int num);
  int pop();
};

Thursday, July 27, 2017

lel

int LLcycle(list_t* list)
{
  element_t* el1 = list->head;
  element_t* el2 = list->head->next;
  while(1){
    // NULL is detected - no cycle
    if(el1->next == NULL || el2->next == NULL){
      return 0;
    }
    if(el1 == el2){ //el2 is the same address as el1
      return 1;
    }
    el1 = el1->next;
    el2 = el2->next->next;
  }
}

Wednesday, July 26, 2017

cycle

void cycle(LL_t* list, int n, int m)
{
  element_t* elN = list->head;
  element_t* elM = list->head;
  unsigned int i= 0;
  unsigned int j =0;

  while(i < n){ //iterates through the list for n
    if(i == n-1){ // this is the index of the nth node that you want
      while(j < m){
        if(j == m-1){ // finds the mth node
          elN->next = elM; //the next pointer of the nth node is the address of m
        }
        j++;
        elM = elM->next;
      }
    }
    i++;
    elN = elN->next;
  }
}

reverse

void LLreverse(LL_t* list)
{
  //creates a new head
  LL_t* l = malloc (sizeof(LL_t));
  if(l){
    l->head = list->head;
  }
  if(l && list->head){
    list->head = list->head->next; //moves the list head to the next element
    l->head->next = NULL; //this is the last element of the new list

    //reverses the list
    node_t* el = list->head;
    while(el){
      list->head = list->head->next;
      el->next = l->head;
      l->head = el;
      el = list->head;
    }
    // makes the list->head point to new list
    list->head = l->head;
    //destroys the list
    l->head = NULL;
    free(l);
  }
}

part

#include<stdio.h>
#include"list.h"
#include<stdlib.h>

void LLreverse(list_t* list)
{
  //count the number of elements in list
  int count =0;
  for(element_t*el = list->head; el; el = el->next){
    count++;
  }
//  printf("%d",count );

}

int main(){
  list_t* list =list_create();
  for(int i =0; i<5; i++){
    list_append(list,i);
  }
  LLreverse(list);
  list_destroy(list);
  return 0;
}





partition

void swap(int a[], int i, int j) //swaps values using their indices
{
  int x = a[i];
  a[i] = a[j];
  a[j] = x;
}

int partition(int list[], int len)
{
  int pivot = list[0];
  int pi = 0; //pivot starting index
  int i = 0; int j = len-1;
  while(i!=j){
    if(pi == i){
      if(list[j] < pivot){
        swap(list,i,j);
        pi = j;
        i++;
      } else { //when the j->val is greater than pivot
        j--;
      }
    }else{//when the pivot index is at the j value instead
      if(list[i] > pivot){
        swap(list, i, j);
        pi = i;
        j--;
      } else {
        i++;
      }
    }
  }
  return pi;
}

int main ()
{
    int arr[8] = {6,1,7,5,8,9,3,2};
    int len = 8;
    int first = arr[0];
    int count = 0;
    partition(arr, len);

    for(int i=0; i<len; i++){
        printf("%d", arr[i]);
    }
    return 0;
}

Sunday, July 23, 2017

task 8

#include<stdio.h>
#include<stdlib.h>
//#include"list.h"

// List element: a list is a chain of these
typedef struct element
{
  int val;
  struct element* next;
} element_t;
// List header - keep track of the first and last list elements
typedef struct list
{
  element_t* head;
  element_t* tail;
} list_t;

list_t* list_create( void )
{
  list_t* l =  malloc( sizeof(list_t) );
  if(l){
l->head = NULL;
l->tail = NULL;
  }
}

void list_destroy( list_t* list )
{
  element_t* el = list->head;
  while( el )
    {
      element_t* next = el->next;
      free( el );
      el = next;
    }

  free( list );
}
element_t* element_create( int i )
{
  element_t* el = malloc( sizeof(element_t) );
  if( el )
    {
      el->val = i;
      el->next = NULL;
    }
  return el;
}

int list_append( list_t* list, int i )
{
  element_t* el = element_create( i );
  if( el == NULL )
    return 1;

  if( list->head == NULL )
    list->head = el;

  if( list->tail )
    list->tail->next = el;

  list->tail = el;
  return 0;
}

void list_print( list_t* list )
{
  printf( "{" );

  for( element_t* el = list->head;
       el;
       el = el->next )
    printf( " %d", el->val );

  printf( " }\n" );
}

int arrLength(list_t* list)
{
  if(list->head == NULL) return 0;

  element_t* el = list->head;
  int num = 1;
  while(el->next != NULL){
    num++;
    el = el->next;
  }
  return num;
}

void swap(int a[], int i, int j) //swaps values using their indices
{
  int x = a[i];
  a[i] = a[j];
  a[j] = x;
}

/*int partition (int arr[], int low, int high)
{
    int pivot = arr[high];    // pivot
    int i = (low - 1);  // Index of smaller element

    for (int j = low; j <= high- 1; j++)
    {
        // If current element is smaller than or
        // equal to pivot
        if (arr[j] <= pivot)
        {
            i++;    // increment index of smaller element
            swap(arr, i, j);
        }
    }
    swap(arr, (i + 1), high);
    return (i + 1);
}*/

int partition(int list[], int low, int high)
{
  int pivot = list[low];
  int pi = low; //pivot starting index
  int i = low; int j = high;
  while(i!=j){
    if(pi == i){
      if(list[j] < pivot){
        swap(list,i,j);
        pi = j;
        i++;
      } else { //when the j->val is greater than pivot
        j--;
      }
    }else{//when the pivot index is at the j value instead
      if(list[i] > pivot){
        swap(list, i, j);
        pi = i;
        j--;
      } else {
        i++;
      }
    }
  }
  return pi;
}

void quicksort(int list[], int low, int high)
{
  if(low < high){
    int pivot = partition(list, low, high);
    quicksort(list, low, pivot-1);
    quicksort(list, pivot+1, high);
  }
}

void list_sort(list_t* list)
{
  int n = arrLength(list)-1;
  int* p = malloc(sizeof(int)*(n+1));
  //appends the elements the elements of the linked list to new array
  element_t* el = list->head;
  for(int i =0; i<=n; i++){
    p[i] = el->val;
    el = el->next;
  }
  //sorts the list
  quicksort(p, 0, n);
  //puts the elements back into linked list

  int k =0;
  //while(el)
  for(element_t* el1 = list->head; el1; (el1 = el1->next) && k++)
    el1->val = p[k];
    //k++;
}

int main()
{
  list_t* list = list_create();

  list_append(list, 3);
  list_append(list, 2);
  list_append(list, 8);
  list_append(list, 5);
  list_append(list, 7);

  list_sort(list);
  list_print(list);
  list_destroy(list);

  return 0;
}

Saturday, July 22, 2017

quicksort!!!

#include<stdio.h>
#include<stdlib.h>
#include"list.h"

int arrLength(list_t* list)
{
  if(list->head == NULL) return 0;

  element_t* el = list->head;
  int num = 1;
  while(el->next != NULL){
    num++;
    el = el->next;
  }
  return num;
}

void swap(list_t* list, int i, int j)
{
  int x;
  x = list_index(list, i)->val;
  list_index(list, i)->val = list_index(list, j)->val;
  list_index(list, j)->val = x;
}

int partition(list_t* list, int low, int high)
{
  int pivot = list_index(list, low)->val;
  int i = low; int j = high;
  while(1){
    while(i<high){
      if(list_index(list, i)->val <= pivot){
        i++;
      }
    }
    do j--; while(list_index(list, j)->val > pivot);
    if( i>=j )break;
    swap(list, i, j);
  }
  swap(list, low, j);
  return j;
}

void quicksort(list_t* list, int low, int high)
{
  if(low<high){
    int pivot = partition(list, low, high);
    quicksort(list, low, pivot-1);
    quicksort(list, pivot+1, high);
  }
}

void list_sort(list_t* list)
{
  int n = arrLength(list)-1;
  quicksort(list, 0, n);
}

int main()
{
  list_t* list = list_create();

  list_append(list, 3);
  list_append(list, 2);
  list_append(list, 8);
  list_append(list, 5);
  list_append(list, 7);

  list_sort(list);

  return 0;
}

hellppppp

#include <stdio.h>
#include <stdlib.h>
// List element: a list is a chain of these
typedef struct element
{
  int val;
  struct element* next;
} element_t;
// List header - keep track of the first and last list elements
typedef struct list
{
  element_t* head;
  element_t* tail;
} list_t;

list_t* list_create( void )
{
  list_t* l =  malloc( sizeof(list_t) );
  if(l){
l->head = NULL;
l->tail = NULL;
  }
}

void list_destroy( list_t* list )
{
  element_t* el = list->head;
  while( el )
    {
      element_t* next = el->next;
      free( el );
      el = next;
    }    
 
  free( list );
}

element_t* element_create( int i )
{
  element_t* el = malloc( sizeof(element_t) );
  if( el )
    {
      el->val = i;
      el->next = NULL;
    }
  return el;
}

int list_append( list_t* list, int i )
{
  element_t* el = element_create( i );
  if( el == NULL )
    return 1;

  if( list->head == NULL )
    list->head = el;
 
  if( list->tail )
    list->tail->next = el;

  list->tail = el;
  return 0;
}

int list_prepend( list_t* list, int i )
{
  element_t* el = element_create( i );
  if( el == NULL )
    return 1;

  if( list->tail == NULL )
    list->tail = el;
 
  if( list->head )
    el->next = list->head;

  list->head = el;
 
  return 0;
}

element_t* list_index( list_t* list, unsigned int i )
{
  if( list->head == NULL )
    return NULL;
 
  element_t* el = list->head;
  unsigned int now = 0;
 
  while( now < i )
    {
      if( el->next == NULL )
return NULL;

      now++;
      el = el->next;
    }    
 
  return el;
}

int arrLength(list_t* list)
{
  if(list->head == NULL) return 0;

  element_t* el = list->head;
  int num = 1;
  while(el->next != NULL){
    num++;
    el = el->next;
  }
  return num;
}

void swap(list_t* list, int i, int j)
{
  int x;
  x = list_index(list, i)->val;
  list_index(list, i)->val = list_index(list, j)->val;
  list_index(list, j)->val = x;
}

int partition(list_t* list, int low, int high)
{
  int pivot = list_index(list, low)->val;
  int i = low; int j = high;
  while(1){
    do i++; while((i<high) &&(list_index(list, i)->val <= pivot) );
    do j--; while(list_index(list, j)->val > pivot);
    if( i>=j )break;
    swap(list, i, j);
  }
  swap(list, low, j);
  return j;
}

void quicksort(list_t* list, int low, int high)
{
  if(low<high){
    int pivot = partition(list, low, high);
    quicksort(list, low, pivot-1);
    quicksort(list, pivot+1, high);
  }
}

void list_sort(list_t* list)
{
  int n = arrLength(list)-1;
  quicksort(list, 0, n);
}

int main()
{
  list_t* list = list_create();

  list_append(list, 3);
  list_append(list, 2);
  list_append(list, 8);
  list_append(list, 5);
  list_append(list, 7);

  list_sort(list);

  return 0;
}

quicksort

#include<stdio.h>
#include<stdlib.h>
#include"list.h"

int arrLength(list_t* list)
{
  if(list->head == NULL) return 0;

  element_t* el = list->head;
  int num = 1;
  while(el->next != NULL){
    num++;
    el = el->next;
  }
  return num;
}

void swap(list_t* list, int i, int j)
{
  int x;
  x = list_index(list, i)->val;
  list_index(list, i)->val = list_index(list, j)->val;
  list_index(list, j)->val = x;
}

int partition(list_t* list, int low, int high)
{
  int pivot = list_index(list, low)->val;
  int i = low; int j = high+1;
  while(1){
    do i++; while(list_index(list, i)->val <= pivot && i<=high);
    do j--; while(list_index(list, j)->val > pivot);
    if( i>=j )break;
    swap(list, i, j);
  }
  swap(list, low, j);
  return j;
}

void quicksort(list_t* list, int low, int high)
{
  if(low<high){
    int pivot = partition(list, low, high);
    quicksort(list, low, pivot-1);
    quicksort(list, pivot+1, high);
  }
}

void list_sort(list_t* list)
{
  int n = arrLength(list)-1;
  quicksort(list, 0, n);
}

int main()
{
  list_t* list = list_create();

  list_append(list, 3);
  list_append(list, 2);
  list_append(list, 8);
  list_append(list, 5);
  list_append(list, 7);

  list_sort(list);

  return 0;
}

Friday, July 21, 2017

arrLength

#include<stdio.h>
#include<stdlib.h>
#include"list.h"

int arrLength(list_t* list)
{
  if(list->head == NULL) return 0;

  element_t* el = list->head;
  int num = 1;
  while(el->next != NULL){
    num++;
    el = el->next;
  }
  return num;
}

/*void list_sort(list_t* list)
{
  pivot =
}*/

int main()
{
  list_t* list = list_create();

  for( int i=0; i<5; i++ ){
    list_append( list, i );
    if(list->tail->next != NULL) return 1;
  }
  printf("%d",arrLength(list));
}

list_sort

#include<stdio.h>
#include<stdlib.h>
#include"list.h"

void list_sort(list_t* list)
{

}

Wednesday, July 5, 2017

circle pls

#include<math.h>

void draw_circle( uint8_t img[],
                  unsigned int cols,
                  unsigned int rows,
                  int x,
                  int y,
                  int r,
                  uint8_t color )
{
  int leftT, rightT, leftB, rightB;
  //vertical = radius
  if(x<cols && x>=0){
    for(int i=y-r; i<=y+r; i++){
      if(i>=0 && i<rows){
        img[x+i*cols]=color;
      }
    }
  }

  //horizontal = radius
  if(y>=0 && y<rows){
    for(int i=x-r; i<=x+r; i++){
      if(i>=0 && i<cols){
        img[i+y*cols]=color;
      }
    }
  }

  // y= y1 + (sqrt)(r^2-x^2)
  for(int xx =1; xx<=r; xx++){
    int yy= (int)(sqrt(r**2 - xx**2)+y);
    // top left to top right - indexes
    leftT = x-xx+(y-yy)*cols;
    rightT = x+xx+(y-yy)*cols;
    //for loop to draw one line across
    for(int i=leftT; i<=rightT; i++){
      if(i>=0 && i<cols*rows){
        img[i]=color;
      }
    }

    //bottom left to bottom right
    leftB = x-xx+(y+yy)*cols;
    rightB = x+xx+(y+yy)*cols;
    for(int i=leftB; i<=rightB; i++){
      if(i>=0 && i<cols*rows){
        img[i]=color;
      }
    }
  }
}

Sunday, July 2, 2017

mergesort

void mergesort( int arr[], int len)
{
  int low, mid, high, last;
  for(int i =2; i<len; i*=2){
    low =0; high =i-1;
    mid = (low+high)/2;
    while(high<len){
      merge(arr,low, mid, high);
      low += i;
      high += i;
      mid = (low+high)/2;
    }
    if( high != len-1){
    low -= i;
    mid = high-i;
    high = len-1;
    merge(arr, low, mid, high);
    }
  }

}