Last active
August 29, 2015 14:16
-
-
Save leftis/13d8c521008445f2d3ba to your computer and use it in GitHub Desktop.
Doubly linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sqlite3.h> | |
#include <stdarg.h> | |
#define ESCAPE 27 | |
#define CREATE_TABLE(table_name, ...) \ | |
"CREATE TABLE " #table_name " (" \ | |
#__VA_ARGS__ \ | |
");" | |
#define INSERT_INTO(tmp, table_name, columns, fmt, ...) \ | |
char *buffer = tmp; \ | |
asprintf(&(tmp), "INSERT INTO %s (%s) VALUES (" fmt ")", table_name, columns, ## __VA_ARGS__); \ | |
free(buffer); | |
struct Node | |
{ | |
int index; | |
struct Node *next; | |
struct Node *previous; | |
} *current = NULL; | |
typedef struct Node node; | |
node * | |
create_node() | |
{ | |
// create a new node | |
node *nd = (node *)malloc(sizeof(node)); | |
// if we dont have current node | |
if ( current == NULL ) | |
{ | |
// we don't have previous | |
nd->previous = NULL; | |
// Set index to 0 | |
nd->index = 0; | |
// it's the first | |
current = nd; | |
} else { | |
// Current's next node is the one we created | |
current->next = nd; | |
// Our new node will have as previous the current one | |
nd->previous = current; | |
// Create is always appending at the end | |
nd->index = ++(nd->previous->index); | |
// Set our current node to nd | |
current = nd; | |
} | |
return nd; | |
} | |
node * | |
create_nodes(int nodes_count) | |
{ | |
// Counter for our loop | |
int counter; | |
// Return the head of our struct | |
node *head; | |
// Create our nodes | |
for(counter = 0; counter < nodes_count; counter++) | |
{ | |
if ( counter == 0 ) | |
{ | |
// if it's the first store it as head | |
head = create_node(); | |
} else { | |
create_node(); | |
} | |
} | |
return head; | |
} | |
int main(int argc, char const *argv[]) { | |
sqlite3 *db; | |
int nodesCount = 0, c = 0; | |
char *zErrMsg = 0; | |
int rc; | |
scanf(" %d", &nodesCount); | |
node *head = create_nodes(nodesCount); | |
rc = sqlite3_open("nodes", &db); | |
sqlite3_exec(db, CREATE_TABLE("nodes", idx integer(4), test varchar(250)), NULL, 0, &zErrMsg); | |
if ( rc ) { | |
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); | |
sqlite3_close(db); | |
return(1); | |
} | |
while( c != ESCAPE ) { | |
char *tmp, *tmp2, *tmp3; | |
if( c == 'n' ) | |
head = head->next; | |
if( c == 'p' ) | |
head = head->previous; | |
tmp = (char *)malloc(sizeof(char)*4096); | |
tmp2 = (char *)malloc(sizeof(char)*4096); | |
tmp3 = (char *)malloc(sizeof(char)*4096); | |
if ( head->previous != NULL ) { | |
asprintf(&tmp, "Previous: %d", head->previous->index); | |
} | |
asprintf(&tmp2, "Current: %d", head->index); | |
if ( head->next != NULL ) { | |
asprintf(&tmp3, "Next: %d", head->next->index); | |
} | |
printf("%s - %s - %s\n", tmp, tmp2, tmp3); | |
free(tmp); | |
free(tmp2); | |
free(tmp3); | |
char *create_statement; | |
create_statement = (char *)malloc(sizeof(char)*4096); | |
INSERT_INTO(create_statement, "nodes", "idx, test", "%d, %d", head->index, c); | |
sqlite3_exec(db, create_statement, NULL, 0, &zErrMsg); | |
free(create_statement); | |
if ( rc ) { | |
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); | |
sqlite3_close(db); | |
return(1); | |
} | |
scanf(" %c", &c); | |
} | |
sqlite3_close(db); | |
free(head); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment