Data Structure in C

Sequential List

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

#define DATA_TYPE int

// Definition.
typedef struct Vector {
	DATA_TYPE *data;
	int size, length;
} Vector;

// Construct a vector whose size is n.
Vector *init(int n) {
	Vector *vec = (Vector *)malloc(sizeof(Vector));
	vec->data = (int *)malloc(sizeof(DATA_TYPE) * n);
	vec->size = n;
	vec->length = 0;
	return vec;
}

// Destruct a vector.
void clear(Vector *vec) {
	if (vec == NULL) return;
	free(vec->data);
	free(vec);
}

// Expand a vector.
// return 0: expand failure.
// return 1: expand success.
int expand(Vector *vec) {
	// Secure Ways to use realloc, avoid memory leaking.
	int new_size = vec->size * 2;
	DATA_TYPE *p = (DATA_TYPE *)realloc(vec->data, sizeof(vec->data) * new_size);
	if (p == NULL) return 0;
	vec->size = new_size;
	vec->data = p;
	return 1;
}

// Insert an item into vector.
// return 0: insert failure.
// return 1: insert success.
int insert(Vector *vec, int pos, DATA_TYPE val) {
	// 3 illegal inserting cases.
	if (vec == NULL) return 0;
	if (vec->length == vec->size) {
		if (!expand(vec)) return 0;
		printf("expand vector size to %d success\n", vec->size);
	}
	if (pos < 0 || pos > vec->length) return 0;

	for (int i = vec->length; i > pos; --i) {
		vec->data[i] = vec->data[i-1];
	}
	vec->data[pos] = val;
	vec->length += 1;
	return 1;
}

// Delete an item in vector.
// return 0: delete failure.
// return 1: delete success.
int del(Vector *vec, int pos) {
	// 3 illegal deleting cases.
	if (vec == NULL) return 0;
	if (vec->length == 0) return 0;
	if (pos < 0 || pos > vec->length-1) return 0;

	for (int i = pos; i < vec->length-1; ++i) {
		vec->data[i] = vec->data[i+1];
	}
	vec->length -= 1;
	return 1;
}

// Print out a vector.
void print(Vector *vec) {
	printf("Vector(%d) = [", vec->length);
	for (int i = 0; i < vec->length; ++i) {
		if (i != 0) printf(", ");
		printf("%d", vec->data[i]);
	}
	printf("]\n");
}

Link List

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

#define DATA_TYPE int

// Define data structure: link list node.
typedef struct ListNode
{
    DATA_TYPE data;
    struct ListNode *next;
} ListNode;

// Define data structure: link list.
typedef struct LinkList
{
    ListNode head;
    int length;
} LinkList;

// Initialize a link list node, returning a pointer to list node.
ListNode *init_listnode(DATA_TYPE val)
{
    ListNode *p = (ListNode *)malloc(sizeof(ListNode));
    if (p == NULL)
        return NULL;
    p->data = val;
    p->next = NULL;
    return p;
}

// Initialize a link list, returnning a link list.
LinkList *init_linklist()
{
    LinkList *l = (LinkList *)malloc(sizeof(LinkList));
    if (l == NULL)
        return NULL;
    l->head.next = NULL;
    l->length = 0;
    return l;
}

// Destruct link list node.
void clear_listnode(ListNode *p)
{
    if (p == NULL)
        return;
    free(p);
    return;
}

// Destruct link list.
void clear_linklist(LinkList *l)
{
    if (l == NULL)
        return;
    ListNode *p = l->head.next, *q;
    while (p)
    {
        q = p->next;
        clear_listnode(p);
        p = q;
    }
    free(l);
    return;
}

// Insert a link list node.
// return 0, inserting fail.
// return 1, inserting success.
int insert(LinkList *l, int ind, DATA_TYPE val)
{
    if (l == NULL)
        return 0;
    if (ind < 0 || ind > l->length)
        return 0;
    ListNode *p = &(l->head), *node = init_listnode(val);
    while (ind--)
        p = p->next;

    node->next = p->next;
    p->next = node;
    l->length += 1;
    return 1;
}

// Erase a link list node.
// return 0, erasing fail.
// return 1, erasing success.
int erase(LinkList *l, int ind)
{
    if (l == NULL)
        return 0;
    if (ind < 0 || ind > l->length - 1)
        return 0;
    ListNode *p = &(l->head), *q;
    while (ind--)
        p = p->next;

    q = p->next;
    p->next = p->next->next;
    clear_listnode(q);
    l->length -= 1;
    return 1;
}

// Print out link list.
void output(LinkList *l)
{
    printf("LinkList(%d): ", l->length);
    for (ListNode *p = l->head.next; p; p = p->next)
        printf("%d -> ", p->data);
    printf("NULL\n");
    return;
}

Queue

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

#define DATA_TYPE int

typedef struct Queue
{
    DATA_TYPE *data;
    int head, tail;
    int size, len;
} Queue;

Queue *init(int n) {
    Queue * que = (Queue *)malloc(sizeof(Queue));
    que->data = (DATA_TYPE *)malloc(sizeof(int) * n);
    que->len = que->head = que->tail = 0;
    que->size = n;
    return que;
}

void clear(Queue *que) {
    if (que == NULL) return;
    free(que->data);
    free(que);
    return;
}

int empty(Queue *que) {
    return que->len == 0;
}

DATA_TYPE front(Queue *que) {
    return que->data[que->head];
}

int push(Queue *que, DATA_TYPE val) {
    if (que == NULL) return 0;
    if (que->len == que->size) return 0;
    que->data[que->tail++] = val;
    que->tail %= que->size;
    que->len++;
    return 1;
}

int pop(Queue *que) {
    if (que == NULL) return 0;
    if (empty(que)) return 0;
    que->head %= (que->head + 1);
    que->len--;
    return 1;
}

void output(Queue *que) {
    printf("queue = [");
    for (int head = que->head, i = 0; i < que->len; ++i) {
        int ind = (head + i) % que->size;
        printf("%d", que->data[ind]);
        if (ind != que->tail-1) printf(", ");
        else printf("]\n");
    }
}

Stack

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

#define DATA_TYPE int

typedef struct Stack {
    DATA_TYPE *data;
    int top, size;
} Stack;

Stack *init(int n) {
    Stack *sta = (Stack *)malloc(sizeof(Stack));
    sta->data = (DATA_TYPE *)malloc(sizeof(DATA_TYPE) * n);
    sta->size = n;
    sta->top = -1;
    return sta;
}

void clear(Stack *sta) {
    if (sta == NULL) return;
    free(sta->data);
    free(sta);
    return;
}

int empty(Stack *sta) {
    if (sta == NULL) return NULL;
    return sta->top == -1;
}

int top(Stack *sta) {
    if (sta == NULL) return NULL;
    if (empty(sta)) return NULL;
    return sta->data[sta->top];
}

int push(Stack *sta, DATA_TYPE val) {
    if (sta == NULL) return 0;
    if (sta->top == sta->size - 1) return 0;
    sta->data[++sta->top] = val;
    return 1;
}

int pop(Stack *sta) {
    if (sta == NULL) return 0;
    if (empty(sta)) return 0;
    sta->top--;
    return 1;
}

void output(Stack *sta) {
    printf("stack(%d) = [", sta->top + 1);
    for (int i = 0; i < sta->top + 1; i++) {
        printf("%d", sta->data[i]);
        if (i != sta->top) printf(", ");
        else printf("]");
    }
}