Practical C - Stack

Implement Stack in C

Guowei Lv

5 minute read

In this blog post, let’s implement a generic stack in C. We will do this by several iterations, from simple version to the full blown version step by step. An Integer Version Let’s start with implementing an integer stack. #include <assert.h>#include <stdio.h>#include <stdlib.h> typedef struct { // int array to store elements of the stack int *elems; // the actual length of the stack int logicalLen; // the allocated length of the array int allocLen; } stack; // Initialize a stack void StackNew(stack *s); // Dispose a stack void StackDispose(stack *s); // Push an element onto the stack void StackPush(stack *s, int value); // Pop an element from the stack int StackPop(stack *s); void StackNew(stack *s) { // allocate 4 elements for the array s->elems = (int *)malloc(4 * sizeof(int)); s->allocLen = 4; s->logicalLen = 0; } void StackDispose(stack *s) { free(s->elems); } void StackPush(stack *s, int value) { if (s->logicalLen == s->allocLen) { // double the size of the array if there is no free space in it s->elems = (int *)realloc(s->elems, 2 * s->allocLen * sizeof(int)); s->allocLen *= 2; } s->elems[s->logicalLen] = value; s->logicalLen++; } int StackPop(stack *s) { assert(s->logicalLen !

Practical C - Linear Search

Implement linear search in C

Guowei Lv

3 minute read

I start this blog series to show some of the trickier parts of C programming. In the first post, let’s implement the linear search in C. Integer Version Let’s imagine that we search on an integer array. Two things to notice here: The function needs the array size n as a separate parameter. Only the array’s address is passed in the function. Generic Basic Version Let’s write a more generic version that does not specify type.

OOP considered harmful ?

A dose of anti oop

Guowei Lv

1 minute read

I have no intention to start flame war here. Just over the years of exploring, I have collected some materials that are not supporting the mainstream OOP paradigm. I just list them here, in no particular order. Stop Writing Classes This is a great talk, I think people will agree most of what’s in this video. The Third Million Line Problem This is slightly off topic, but interesing to watch.

Clear Azure CosmosDB Documents

Clear Azure CosmosDB documents using NodeJS

1 minute read

It suprised me that one cannot clear all documents in a CosmosDB collection from the web portal. The only solution for now is to use its SDK, so I wrote a simple Node script. It now can list all the documents and delete them, it can be easily modified to suit your need. var docdb = require("documentdb"); var async = require("async"); var config = { host: "https://xxxx.documents.azure.com:443/", auth: { masterKey: "xxxx" } }; var client = new docdb.

1 minute read

Do you think Computer Science equals building websites and mobile apps? Are you feeling that you are doing repetitive and not so intelligent work? Are you feeling a bit sick about reading manuals and copy-pasting code and keep poking around until it works all day long? Do you want to understand the soul of Computer Science? If yes, read SICP!!! Writing recursive functions is not an easy task, often requires a different way of thinking.