Hi guys. Most of the people that I know don't care a ........
about programmation. I always liked this, and always
wanted to share knowledge with people who likes too.
So I am sharing my sort of technique to all of you.
If you all have any suggestion, please be free to do.
And here is the code.
Symbol.h
Code:
#ifndef SYMBOL_H
#define SYMBOL_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
Off, On
} STATUS;
typedef struct Symbol SYMBOL;
struct Symbol {
char symbol;
SYMBOL * preview, * next;
};
SYMBOL * symbol_set(char symbol);
STATUS symbol_next_front(SYMBOL ** to, SYMBOL * next);
STATUS symbol_next_back(SYMBOL ** to, SYMBOL * next);
STATUS symbol_destroy(SYMBOL * to);
typedef struct String STRING;
struct String{
SYMBOL * group;
STRING * preview,* next;
};
STRING * string_set(char * group);
STATUS string_next_front(STRING ** to,STRING * next);
STATUS string_next_back(STRING ** to,STRING * next);
STATUS string_destroy(STRING * to);
#ifdef __cplusplus
}
#endif
#endif /// SYMBOL_H
Symbol.c
Code:
#include "symbol.h"
SYMBOL * symbol_set(char symbol) {
SYMBOL * to = (SYMBOL *) malloc(sizeof (SYMBOL));
to->symbol = symbol;
to->preview = NULL;
to->next = NULL;
}
STATUS symbol_next_front(SYMBOL ** to, SYMBOL * next) {
if (next == NULL)return (Off);
next->next = *to;
if (*to != NULL) {
(*to)->preview = next;
}
*to = next;
return (On);
}
STATUS symbol_next_back(SYMBOL ** to, SYMBOL * next) {
if (next == NULL)return (Off);
if (*to != NULL) {
SYMBOL * update = *to, * preview = NULL;
while (update->next != NULL) {
preview = update;
update = update->next;
}
update->preview = preview;
update->next = next;
return (On);
}
*to = next;
return (On);
}
void symbol_show_console(SYMBOL * to) {
printf("%c", to->symbol);
}
STATUS symbol_destroy(SYMBOL * to) {
if (to != NULL) {
SYMBOL * symbol = to;
while (symbol != NULL) {
SYMBOL * next = symbol->next;
free(symbol), symbol = next;
}
return (On);
}
return (Off);
}
STRING * string_set(char * group){
STRING * to = (STRING *)malloc(sizeof(STRING));
to->next = to->preview = NULL;
to->group = NULL;
int lenght = strlen(group),up=lenght;
for(up=lenght-1;up>=0;up--)
symbol_next_front(&to->group,symbol_set(group[up]));
return(to);
}
STATUS string_next_front(STRING ** to,STRING * next){
if (next == NULL)return (Off);
next->next = *to;
if (*to != NULL) {
(*to)->preview = next;
}
*to = next;
return (On);
}
STATUS string_next_back(STRING ** to,STRING * next){
if (next == NULL)return (Off);
if (*to != NULL) {
STRING * update = *to, * preview = NULL;
while (update->next != NULL) {
preview = update;
update = update->next;
}
update->preview = preview;
update->next = next;
return (On);
}
*to = next;
return (On);
}
void string_show_console(STRING * to){
SYMBOL * group = NULL;
for(group = to->group;group != NULL;group=group->next)
printf("%c",group->symbol);
}
STATUS string_destroy(STRING * to){
if (to != NULL) {
STRING * symbol = to;
while (symbol != NULL) {
symbol_destroy(symbol->group);
STRING * next = symbol->next;
free(symbol), symbol = next;
}
return (On);
}
return (Off);
}
Main.c
Code:
/*
* File: main.c
* Author: Bean Dragon
*
* Created on 5 de Junho de 2015, 23:57
*/
#include "symbol.h"
int main(int argc, char** argv) {
STRING * array = NULL,* string;
string_next_back(&array,string_set("First"));
string_next_back(&array,string_set("Second"));
string_next_back(&array,string_set("Third"));
for(string=array;string!=NULL;string=string->next)
string_show_console(string),printf("\n");
string_destroy(array);
return (EXIT_SUCCESS);
}