Skip to content

Instantly share code, notes, and snippets.

@honux77
Last active December 12, 2015 02:18
Show Gist options
  • Save honux77/4697082 to your computer and use it in GitHub Desktop.
Save honux77/4697082 to your computer and use it in GitHub Desktop.
linux style circular double linked list implementation. If you fully understand this code, you may pass CS 101 - basic computer programming. In fact it's c language code, but tested using VS2012.
/* heo-se.cpp : win32 console application
This program prints only one line.
Yes, that's all.
Main concept is from include/linux/list.c in linux kernel 2.6.xx.
Author: Hoyoung Jung, NHN NEXT
Date: 2012.02.01
license: anyone can freely use and modify this code.
*/
#include "stdafx.h"
// these headres are alreay included in stdafx.h
#include <stdio.h>
#include <string.h>
struct list {
list *prev, *next;
};
struct letter {
unsigned int seq;
char ch;
list node;
};
#define INIT_LL(name) \
struct list name = { &(name), &(name)}
inline void _add_list(struct list *node, struct list *head, struct list *next)
{
node-> next = next;
node-> prev = head;
head-> next = node;
next-> prev = node;
}
inline void add_first(struct list *node, struct list * head)
{
_add_list(node,head, head->next);
}
inline void add_last(struct list *node, struct list * head)
{
_add_list(node,head->prev,head);
}
#define for_each_node(list, head) \
for(list = (head)->next; list!=(head); list= list->next)
#define get_struct_from_list(list, type, member) \
(type *) ((char*)list - (unsigned long) &((type *) 0) ->member)
#define MAX 64
int _tmain(int argc, _TCHAR* argv[])
{
INIT_LL(head);
int i;
struct letter data[MAX];
//char *stmt = "Hello, NEXT";
char *stmt = "TXEN, olleH";
int len = strlen(stmt);
for (i=0; i < len; i++)
{
data[i].seq=i;
data[i].ch = stmt[i];
}
#if 0
//for test
for (i=0; i < len; i++)
{
if (data[i].seq==i)
printf("%c" ,data[i].ch);
}
printf("\n");
#endif
for(i=0; i < len; i++)
{
add_first( &data[i].node, &head);
//add_last(&data[i].node, &head);
}
struct list *n;
struct letter *itr;
for_each_node (n, &head)
{
itr = get_struct_from_list(n,struct letter, node);
printf("%c",itr->ch);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment