这个程序主要用到的是C的链表知识。算属于数据结构中的吧,简单的单向链表。我很惭愧阿,还没搞懂二叉树。要是有牛人指点二三,不胜感激阿。废话少说,代码供上,牛人莫笑。

///
InBlock.gif //这是个简单的书本录入程序,实现的是从输入书本名,作者和价钱,保存到一结构体组成的内存空间里。退出时信息会丢失。 //
InBlock.gif //功能很简单,但用到了C中很多知识点,主要是学习结构体的使用和链表使用和函数的用法。我花了点时间用中文注释了下,供//
InBlock.gif //个人参考 //,程序编译成功运行,有爱好者可下载在UNIX平台编译运行。因水平实在有限,难免错误地方太多,望见谅。大家//
InBlock.gif //可以互相学习探讨,我的联系方世如下                                                                                                                                                               
InBlock.gif //MSN:[email]wu23qing45ying@163.com[/email]                                                                                                                                                                                //
InBlock.gif //QQ: 46499596                                                                                                     //
InBlock.gif //E-mail: [email]carywu@yahoo.cn[/email]  / [email]wu23qing45ying@163.com[/email]                                                            //
///
InBlock.gif
//
InBlock.gif //头文件                                        //
//
InBlock.gif#include <stdio.h>
InBlock.gif#include <stdlib.h>
InBlock.gif#include < string.h>
InBlock.gif#include <unistd.h>
InBlock.gif
#define NAME_SIZE 50
InBlock.gif#define MAXBOOKS  30
InBlock.gif

//
InBlock.gif //结构体定义                                    //
//
InBlock.giftypedef struct library
InBlock.gif{
InBlock.gif   char name[NAME_SIZE]; //书名,用的是数组,
InBlock.gif   char auther[NAME_SIZE]; //作者名。
InBlock.gif   float value; //价格
InBlock.gif   int number; //序号
InBlock.gif
   struct library *next; //链表指针。
InBlock.gif} library_t, *point;
InBlock.gif

//
InBlock.gif //结构体定义                                    //
//
InBlock.gif
InBlock.gif void delete_enter ( char *s);
InBlock.gif void print_list (point current);
InBlock.gif void book_append (point head);
InBlock.gif void book_delete (point current);
InBlock.gif int number = 0;  //全局定义序号,初始值为0.
InBlock.gif

//
InBlock.gif //主函数                                        //
//
InBlock.gif int
InBlock.gifmain ( int argc, char **argv)
InBlock.gif{
InBlock.gif  point current = NULL, head = NULL, prev = NULL; //定义当前连表指针,头指针,前一个指针。
InBlock.gif
  fprintf (stdout, "Please input the book name:\n");
InBlock.gif
   char temp_storage[NAME_SIZE]; //定义一临时存储书名的数组。因为指针还为分配内存,不能用。
InBlock.gif
   while (fgets (temp_storage, NAME_SIZE, stdin) != NULL  //我用的是fgets,会将回车符存入,所以需要个删除回车做法。
InBlock.gif  && temp_storage[0] != '\n')
InBlock.gif    {
InBlock.gif      delete_enter (temp_storage); //删除回车符的函数,在下面有具体。
InBlock.gif
      current = (point) malloc ( sizeof (library_t));  //分配当前节点的内存,用于存储数据。
InBlock.gif      strcpy (current->name, temp_storage); //将临时中的字符串复制到结构题中的name数组中。
InBlock.gif      fprintf (stdout, "please input the auther name\n");
InBlock.gif      fgets (current->auther, NAME_SIZE, stdin); //同上,输入作者名。有个问题是没做错误检测。
InBlock.gif      delete_enter (current->auther); //同上。
InBlock.gif      fprintf (stdout, "please input the book value\n");
InBlock.gif      scanf ( "%f", &current->value); //输入价格。直接用scanf。
InBlock.gif      number++; //序号自加。
InBlock.gif      current->number += number;  //传给结构题中的number,实现每多一链表节点序号增加1。
InBlock.gif
       if (head == NULL)  //当头指针为空时,指向当前节点。
InBlock.gif    {
InBlock.gif   head = current;
InBlock.gif    }
InBlock.gif       else   //否则,前指针的下一跳指向当前节点。 这样三个指针都指向了具体的节点。
InBlock.gif    {
InBlock.gif   prev->next = current;
InBlock.gif    }
InBlock.gif
      current->next = NULL;  //当前指针的下一跳设为空。
InBlock.gif
      fprintf (stdout, "number      name      auther      value\n"); //输出信息。每次输完一个节点信息后,就输出查看。
InBlock.gif      fprintf (stdout, "%d    %10s    %10s       %.2f\n", current->number,
InBlock.gif        current->name, current->auther, current->value);
InBlock.gif
      prev = current;  //将前指针指向当前节点。
InBlock.gif

InBlock.gif      fprintf (stdout, "Please input the next name:\n");
InBlock.gif
       while (getchar () != '\n')
InBlock.gif     continue;
InBlock.gif    }
InBlock.gif
  fprintf (stdout, "below is the append program\n");
InBlock.gif  current = head;  //将当前指针指回到头节点,实现链表的遍历。
InBlock.gif  book_append (current);  //这是个按照序号选择来插入新节点信息的函数。
InBlock.gif
  fprintf (stdout, "show the total book list\n");
InBlock.gif  current = head; //同上。当前指针回归到头节点。
InBlock.gif  print_list (current);  //这是个打印整个链表信息的函数。
InBlock.gif
  fprintf(stdout, "below is the delete program\n");
InBlock.gif  current = head; //同上。
InBlock.gif  book_delete(current);  //这是个按照序号选择来删除节点的函数。
InBlock.gif
  fprintf (stdout, "show the total book list\n");
InBlock.gif  current = head;
InBlock.gif  print_list (current); //删除后再打印一次。我很苦恼似乎重复使用打印函数。
InBlock.gif
   return 0;
InBlock.gif}
InBlock.gif
void
InBlock.gifdelete_enter ( char *s)  //删除回车空行函数,很简单。
InBlock.gif{
InBlock.gif   int i;
InBlock.gif   while (s[i] != '\n')
InBlock.gif    ++i;
InBlock.gif   if (s[i] == '\n')
InBlock.gif    s[i] = '\0';
InBlock.gif
}
InBlock.gif
void
InBlock.gifprint_list (point current)
InBlock.gif{
InBlock.gif  fprintf (stdout,
InBlock.gif     "number %10s book_name %10s book_auther %10s book_value %10s\n",
InBlock.gif     " ", " ", " ", " ");
InBlock.gif   while (current != NULL)  //在当前指针指向空节点时结束。
InBlock.gif    {
InBlock.gif
      fprintf (stdout, "%d        %s           %10s        %10s  %.2f\n",
InBlock.gif        current->number, current->name, current->auther, " ",
InBlock.gif        current->value);
InBlock.gif      fprintf (stdout, "%20s\n", "--------------");
InBlock.gif      current = current->next;
InBlock.gif    }
InBlock.gif}
InBlock.gif
void
InBlock.gifbook_append (point current)
InBlock.gif{
InBlock.gif   int word_number;
InBlock.gif  point new_code = NULL;
InBlock.gif
  fprintf (stdout, "please input the number you want to add behind\n");
InBlock.gif  scanf ( "%d", &word_number);
InBlock.gif   for (; current != NULL; current = current->next)
InBlock.gif    {
InBlock.gif       if (word_number == current->number)
InBlock.gif    {
InBlock.gif   new_code = (point) malloc ( sizeof (library_t));
InBlock.gif
   fprintf (stdout, "please input the book name\n");
InBlock.gif   while (getchar () != '\n')
InBlock.gif     continue;
InBlock.gif   fgets (new_code->name, NAME_SIZE, stdin);
InBlock.gif   delete_enter (new_code->name);
InBlock.gif   fprintf (stdout, "please input the book auther\n");
InBlock.gif   fgets (new_code->auther, NAME_SIZE, stdin);
InBlock.gif   delete_enter (new_code->auther);
InBlock.gif   fprintf (stdout, "please input the book value\n");
InBlock.gif   scanf ( "%f", &new_code->value);
InBlock.gif   number++;
InBlock.gif   new_code->number += number;
InBlock.gif   new_code->next = current->next;
InBlock.gif   current->next = new_code;
InBlock.gif
   break;
InBlock.gif    }
InBlock.gif       else
InBlock.gif     continue;
InBlock.gif    }
InBlock.gif}
InBlock.gif
void
InBlock.gifbook_delete (point current)  //当前节点被指向了头节点。
InBlock.gif{
InBlock.gif   int word_number;
InBlock.gif  point prev;  //定义一个新的辅助前指针。
InBlock.gif
  fprintf (stdout, "please input the number you want to delete\n");
InBlock.gif  scanf ( "%d", &word_number);  //选择要删除的序号。
InBlock.gif
   while (current != NULL)
InBlock.gif    {
InBlock.gif      prev = current;  //删除单向链表其实很简单,前指针指向当前节点。
InBlock.gif      current = current->next;  //当前指针指向下一个节点。
InBlock.gif       if (word_number == current->number) //在序号相等情况下。
InBlock.gif    {
InBlock.gif   prev->next = current->next; //将前节点的下一跳指向当前节点的下一跳节点,也就是把当前节点孤立不链接。
InBlock.gif   free (current); //free掉当前节点。
InBlock.gif   break;  //退出。
InBlock.gif    }
InBlock.gif       else
InBlock.gif     continue;  //多此一举,不过写上好看些。
InBlock.gif    }
InBlock.gif}