2009/07/19

Linked List (Single/Double)

=================== LINKED LIST DEMO ===================
[ ] [D]-[0xbfb6b5cc]-[11]-[Albert]
[ ] [D]-[0xbfb6b5f0]-[12]-[Benson]
[ ] [D]-[0xbfb6b614]-[13]-[Cano  ]
[ ] [D]-[0xbfb6b638]-[14]-[David ]
[ ] [D]-[0xbfb6b65c]-[15]-[Elvin ]
[ ] [D]-[0xbfb6b680]-[16]-[Fan   ]
[ ] [D]-[0xbfb6b6a4]-[17]-[Gary  ]
[ ] [D]-[0xbfb6b6c8]-[18]-[Helen ]
[ ] [D]-[0xbfb6b6ec]-[19]-[Ivan  ]
[ ] [D]-[0xbfb6b710]-[20]-[John  ]
[V] [D]-[0xbfb6b6a4]-[17]-[Gary  ]
[ ] [D]-[0xbfb6b734]-[21]-[Kevin ]
[ ] [D]-[0xbfb6b758]-[99]-[.Orz  ]
[ ] [D]-[0xbfb6b5cc]-[11]-[Albert]
[ ] [D]-[0xbfb6b5f0]-[12]-[Benson]
[ ] [D]-[0xbfb6b614]-[13]-[Cano  ]
[ ] [D]-[0xbfb6b638]-[14]-[David ]
[ ] [D]-[0xbfb6b65c]-[15]-[Elvin ]
[ ] [D]-[0xbfb6b680]-[16]-[Fan   ]
--------------------------------------------------------
[1] Insert [2] Delete [3]        [4]        [5]         
[6] Next   [7] Prev   [8] Head   [9] Tail   [0] Quit    
==> Your choice is : 6

Balanced Tree

AVL Balanced tree program written in C. Refer to http://en.wikipedia.org/wiki/AVL_tree. This program can display a tree structure as below. It also have a cursor which can be moved by numeric key (1-9). Cursor node is also highlight displayed :)

==================== Balance Tree (6)====================
[13](-1)----[38](-1)----[57](+1)----[90](+0)----[99](+0)
  |           |           |           +----[86](+0)
  |           |           +----[44](-1)----[54](+1)
  |           |                       |           +----[50](+0)
  |           |                       +----[39](+0)
  |           +----[23](+0)----[29](+0)----[35](+0)
  |                       |           +----[26](+0)
  |                       +----[19](+1)
  |                                   +----[14](+0)
  +----[10](+1)----[12](+1)
              |           +----[11](+0)
              +----[06](-1)----[08](+0)----[09](+0)
                          |           +----[07](+0)
                          +----[04](+0)
Cursor [29]
========== TREE OPERATION MENU ==========================
[7] Root     [8] Up      [9] Largest  [ ]      
[4] Up/Leftt [5] Left    [6] Right    [ ]      
[1] Smallest [2] LeftMost[3] RightMost[ ]      
        [0] INSERT  [.] DELETE   [q] QUIT 
==> Your Choice :


2009/07/12

getch() in Linux

A good reference from http://blog.csdn.net/t_larry/archive/2006/04/06/653124.aspx
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
/*------------------------------------------------*/
int getch(void) {
      int c=0;

      struct termios org_opts, new_opts;
      int res=0;
          //-----  store old settings -----------
      res=tcgetattr(STDIN_FILENO, &org_opts);
      assert(res==0);
          //---- set new terminal parms --------
      memcpy(&new_opts, &org_opts, sizeof(new_opts));
      new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
      tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
      c=getchar();
          //------  restore old settings ---------
      res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
      assert(res==0);
      return(c);
}