NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/*=========RUN LENGHT ENCODING TH=================*/

#include <stdio.h>

int main(){
int head = -1;
int length = 0;
while(1){
int c = getchar();
if (c == EOF){
if(length >= 3){
printf("%d", length);
}
if (length == 2){
printf("%c%c", head, head);
} else {
printf("%c",head);
}
}
if (head < 0){
head = c;
}

if ((c == head) && (length < 9)){
length += 1;
} else {
if(length >= 3){
printf("%d",length);
}

if (length == 2){
printf("%c%c", head, head);
} else {
printf("%c",head);
}

head = c;
length = 1;
}

if (c == 'n'){
printf("n");
}
}

return 0;
}



/*=========DECODING=================*/
#include<stdio.h>
#include<string.h>

char *decode_rle(char *a,int length, char *op)
{

int i,j,k=0,count=0;
for(i=0;i<length;i++)
{
if( (isalpha(a[i])) )
{
count = a[i+1] - '0';
for(j=0;j<count;j++)
{
op[k]=a[i];
k++;
}
}
}
op[k] = '';
printf("n DEKODOVANY STRING JE %sn",op);
return op;
}
int main()
{
int i=0,j,length,count;
char a[20],*output;
char op[50];


printf("n Zadaj string vo formate B2C1.... n ");
gets(a);
length = strlen(a);
output = decode_rle(a,length,op);
i=0;
while(output[i]!='')
{
// printf("%c",output[i]);
i++;
}
getchar();
return 0;
}




/*===============LINKED LIST================*/
#include "linked_list.h"
#include <stdio.h>

// struktura
struct list {
int data;
struct list* next;
}


// vytvorenie jednoprvkoveho linked listu
struct list* create_list(int value) {
struct list* new_list = (struct list*)malloc(sizeof(struct list));
new_list->data = value;
new_list->next = NULL;
return new_list;
}



//zmaze celz linked list
void destroy_list(struct list* list){
if (list!=NULL){
destroy_list(list->next);
free(list);
}
}


struct list* pop_list(struct list* list,int* value){
struct *list current = list;
if(list =! NULL){
while(current->next->next != NULL){
current = current -> next;
}

*value = current -> next -> value;
struct list* delete;
delete -> current->next;
current -> next = NULL;
free (delete);
return list;
}
else
return NULL;
}

struct list* push_list(struct list* list,int value){

struct list* add_end = (struct list*) malloc(sizeof(list));
struct list* current;
add_end -> value = value;
add_end -> next = NULL;

if(list = NULL){
list = add_end ;
}
else {
current = list;
while(current -> next != NULL){
current = current -> next;
}
current -> next = add_end;
}

return list;

}

// prida na zaciatok
struct list* shift_list(struct list* list,int value) {

struct list* add_first = (struct list*) malloc(sizeof(list));
add_first -> data = value;
add_first -> next = list ;
list = add_first;
return list;
}
// odoberie zo zaciatku
struct list* unshift_list(struct list* list,int* value){

if (list =! NULL){
struct list* delete_first;
struct list* delete_first = list ;
*value = list -> value ;
list = delete_first -> next;
free (delete_first);
return list;
}

else
return NULL;
}

/*============================================*/


/*==============WORD COUNT===============*/
#include <stdio.h>
#define OUT 0
#define IN 1
unsigned countWords(char *str)
{
int state = OUT;
unsigned wc = 0; // word count
while (*str)
{
if (*str == ' ' || *str == 'n' || *str == 't')
state = OUT;
else if (state == OUT)
{
state = IN;
++wc;
}
++str;
}
return wc;
}
int main(void)
{
char str[] = "marosko je kokot jak repa ";
printf("No of words : %un", countWords(str));
return 0;
}

/*======================READ LINE=================*/
int read_line(char* line, int size){
int i;
int c;
for(i = 0; i < size - 1; i++){
c = getchar();
if(c =='n'){
line[i]=c;
line[i+1]='';
return i;
}
else if(c == EOF){
line[i]='';
return EOF;
}

line[i]=c;

}

line[size]='';
return i;
}

/*======================HASH=================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct item {
char key[20];
int value;
struct item* next;
};

struct table {
struct item** slots;
int slot_count;
};

struct table* create_table(){
struct table* tab = malloc(sizeof(struct table));
tab->slot_count = 40;
tab->slots = calloc(tab->slot_count,sizeof(struct item *));
return tab;
}

void destroy_table(struct table* tab){
for (int i = 0; i < tab->slot_count; i++){
struct item* list = tab->slots[i];
while (list != NULL){
struct item* del = list;
list = del->next;
free(del);
}
}
free(tab->slots);
free(tab);
}

int hash_string(const char* word){
int hash = 0;
for (int counter = 0; word[counter]!=''; counter++){
hash = word[counter] + (hash << 6) + (hash << 16) - hash;
}
return hash;
}

int insert(struct table* tab,const char* key,int value){
int hash = hash_string(key);
int slot_index = hash % tab->slot_count;

struct item* slot = tab->slots[slot_index];
while (slot != NULL){
if (strcmp(slot->key,key) == 0) {
slot->value = value;
return 1;
}
slot = slot->next;
}
struct item* new = malloc(sizeof(struct item));
strcpy(new->key,key);
new->value = value;
new->next = slot;
tab->slots[slot_index] = new;
if (new->next){
return 1;
}
return 0;
}

int* find(struct table* tab,const char* key){
int hash = hash_string(key);
int slot_index = hash % tab->slot_count;

struct item* slot = tab->slots[slot_index];
while (slot != NULL){
if (strcmp(slot->key,key) == 0) {
return &(slot->value);
}
slot = slot->next;
}
return NULL;
}

int* delete(struct table* tab,const char* key){
int hash = hash_string(key);
int slot_index = hash % tab->slot_count;

struct item* slot = tab->slots[slot_index];

if(slot != NULL){
struct item* this = slot;
while (this->next != NULL){
if (strcmp(this->next->key,key) == 0) {
}
this = this->next;
}
}
return NULL;
}

int main(){
struct table* tab = create_table();
insert(tab,"ahoj",2);
insert(tab,"svet",4);

int* val = NULL;
val = find(tab,"kluc");

destroy_table(tab);
return 0;
}

/*======================STROM=================*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

struct node {
struct node* left;
struct node* right;
int height;
int value;
};

struct tree {
struct node* root;
size_t size;
};

struct tree* init_tree(){
return calloc(1,sizeof(struct tree));
}

struct node* create_node(int value){
struct node* node = malloc(sizeof(struct node));
node->left = NULL;
node->right = NULL;
node->value = value;
node->height = 0;
return node;
}

void destroy_node(struct node* node){
if(node != NULL){
destroy_node(node->left);
destroy_node(node->right);
free(node);
}
}

void destroy_tree(struct tree* tree){
destroy_node(tree->root);
free(tree);
}

int height(struct node* node){
int l = 0;
if (node == NULL){
return 0;
}
if (node->left){
l = node->left->height;
}
int r = 0;
if (node->right){
r = node->right->height;
}
if (l > r){
return l + 1;
}
return r + 1;
}

int balance_factor(struct node* node){
if (node == NULL){
return 0;
}
return height(node->right) - height(node->left);
}

//
// ROOT PIVOT
// / => /
// PIVOT C A ROOT
// / /
// A B B C
//
//
struct node* rotate_right(struct node* parent){
struct node* pivot = parent->left;
assert(pivot != NULL);
// printf("R %dn",pivot->value);
parent->left = pivot->right;
pivot->right = parent;
parent->height = height(parent);
pivot->height = height(pivot);
return pivot;
}

//
// ROOT PIVOT
// / => /
// A PIVOT ROOT C
// / /
// B C A B
//
//
struct node* rotate_left(struct node* parent){
struct node* pivot = parent->right;
assert(pivot != NULL);
// printf("L %dn",pivot->value);
parent->right = pivot->left;
pivot->left = parent;
parent->height = height(parent);
pivot->height = height(pivot);
return pivot;
}

struct node* balance_tree_node(struct node* node){
assert(node);
node->height = height(node);
// Left heavy
if (balance_factor(node) < -1){
if (balance_factor(node->left) > 1) {
node->left = rotate_left(node->left);
}
node = rotate_right(node);
}
// Right Heavy
else if (balance_factor(node) > 1){
if (balance_factor(node->right) < -1){
node->right = rotate_right(node->right);
}
node = rotate_left(node);
}
return node;
}

struct node* search(struct tree* tree, int value){
struct node* current = tree->root;
while( current != NULL ){
if (value < current->value ){
current = current->left;
}
else if (value > current->value){
current = current->right;
}
else {
break;
}
}
return current;
}

struct node* insert(struct node* node, int value){
if (node == NULL){
return create_node(value);
}
if (value < node->value){
node->left = insert(node->left,value);
}
else if (value > node->value){
node->right = insert(node->right,value);
}
node = balance_tree_node(node);
return node;
}

struct node* minimum(struct node* node){
assert(node);
while(node->left != NULL){
node = node->left;
}
return node;
}

struct node* delete(struct node* node,int value){
if (node == NULL){
return NULL;
}
struct node* res = node;
if (node->value == value){
if (node->left && node->right){
struct node* min = minimum(node->right);
node->value = min->value;
node->right = delete(node->right,min->value);
}
else if (node->left && node->right == NULL){
res = node->left;
free(node);
}
else if (node->right && node->left == NULL){
res = node->right;
free(node);
}
}
else if (node->value < value){
node->left = delete(node->left,value);
}
else {
node->right = delete(node->right,value);
}
return res;
}

void print_dot_node(struct node* node){
if (node == NULL){
return;
}
print_dot_node(node->left);
printf("%d [label="%d %d"];n",node->value,node->value, node->height);
if (node->left){
printf("%d -> %d;n",node->value,node->left->value);
}
if (node->right){
printf("%d -> %d;n",node->value,node->right->value);
}
print_dot_node(node->right);
}


void print_dot(struct node* node){
printf("digraph G {n");
print_dot_node(node);
printf("}n");
}


int main(){
time_t t;
srand((unsigned) time(&t));
struct node* tree = NULL;
for (int i = 0; i < 100; i++){
tree = insert(tree,rand() % 150);
}
print_dot(tree);
return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.