可重用的链表模块来实现命令行菜单小程序

21 篇文章 0 订阅


/**************************************************************************************************/
/* Copyright  (C) LS, 2014-2015                                                                   */
/* FILE NAME             :  linktable.h                                                           */
/* PRINCIPAL AUTHOR      :  ls                                                                    */
/* SUBSYSTEM NAME        :  menu                                                                  */
/* MODULE NAME           :  linktalbe                                                             */
/* LANGUAGE              :  C                                                                     */
/* TAGRGET ENVIRONMENT   :  ANY                                                                   */
/* DATE OF FIRST RELEASE :  2014/12/28                                                            */
/* DESCRIPTION           :  interface of Link Table                                               */
/**************************************************************************************************/

/*
 * Revision log:
 * 
 * Created by ls, 2014/12/28
 *
 */

#ifndef _LINK_TABLE_H_
#define _LINK_TABLE_H_

#define SUCCESS 0
#define FAILURE (-1)

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

/*
 * LinkTable Node Type
 */
typedef struct LinkTableNode
{
     struct LinkTableNode *pNext;
}tLinkTableNode;

/*
 * LinkTable Type
 */
typedef struct LinkTable
{
     tLinkTableNode *pHead;
     tLinkTableNode *pTail;
     int         SumOfNode;
}tLinkTable;

/*
 * Create a LinkTable
 */
tLinkTable * CreateLinkTable();

/*
 * Delete a LinkTalbe
 */
int DeleteLinkTable(tLinkTable *pLinkTable);

/*
 * Add a LinkTableNode to LinkTable
 */
int AddLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode);

/*
 * Delete a LinkTableNode from LinkTable
 */
int DelLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode);

/*
 * Get LinkTableHead
 */
tLinkTableNode * GetLinkTableHead(tLinkTable *pLinkTable);

/*
 * Get next LinkTableNode
 */
tLinkTableNode * GetNextLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode);

#endif /* _LINK_TABLE_H_ */



/**************************************************************************************************/
/* Copyright  (C) LS, 2014-2015                                                                   */
/* FILE NAME             :  linktable.c                                                           */
/* PRINCIPAL AUTHOR      :  ls                                                                    */
/* SUBSYSTEM NAME        :  menu                                                                  */
/* MODULE NAME           :  linktalbe                                                             */
/* LANGUAGE              :  C                                                                     */
/* TAGRGET ENVIRONMENT   :  ANY                                                                   */
/* DATE OF FIRST RELEASE :  2014/12/28                                                            */
/* DESCRIPTION           :  interface of Link Table                                               */
/**************************************************************************************************/

/*
 * Revision log:
 * 
 * Created by ls, 2014/12/28
 *
 */

#include "linktable.h"

/*
 * Create a LinkTable
 */
tLinkTable * CreateLinkTable()
{
     tLinkTable *pTable = (tLinkTable*)malloc(sizeof(tLinkTable));
     pTable->pHead = NULL;
     pTable->pTail = NULL;
     pTable->SumOfNode = 0;

     return pTable;
}

/*
 * Delete a LinkTable
 */
int DeleteLinkTable(tLinkTable *pLinkTable)
{
     free(pLinkTable);
     return 0; 
}

/*
 * Add LinkTableNode to LinkTable
 */
int AddLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode)
{
     if (pLinkTable == NULL) 
     {
          printf("The table is empty and cannot add this node!\n");
	  exit(0); 
     }

     if (pNode == NULL)
     {
          printf("The node is empty and cannot add this node!\n");
	  return 0;
     }

     if (pLinkTable->pHead == NULL)
     {
          pLinkTable->pHead = pNode;
	  pLinkTable->pTail = pNode;
	  pLinkTable->SumOfNode = 1;
     }else
     {
          pLinkTable->pTail->pNext = pNode;
	  pLinkTable->pTail = pNode;
	  pLinkTable->SumOfNode++; 
     }

     return 0;
}

/*
 * Delete a LinkTableNode from LinkTable
 */
int DelLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode)
{
    /* if (pLinkTable == NULL) 
     {
          printf("The table is empty and cannot delete this node!\n");
	  exit(0); 
     }

     if (pNode == NULL)
     {
          printf("The node is empty and cannot delete this node!\n");
	  return 0;
     }
    
     tLinkTableNode *p = pLinkTable->pHead;
     while */ 
}

/*
 * Get LinkTableHead
 */
tLinkTableNode * GetLinkTableHead(tLinkTable *pLinkTable)
{
     if (pLinkTable == NULL) 
     {
          printf("The table is empty!\n");
	  exit(0); 
     }

     return pLinkTable->pHead;
}

/*
 * Get Next LinkTableNode
 */
tLinkTableNode * GetNextLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode)
{
     if (pLinkTable == NULL) 
     {
          printf("The table is empty!\n");
	  exit(0); 
     }
    
     if (pNode == NULL) 
     {
          printf("The node is empty and cannot get next node!\n");
	  exit(0);
     }
     return pNode->pNext;
}
 


/**************************************************************************************************/
/* Copyright  (C) LS, 2014-2015                                                                   */
/* FILE NAME             :  linklist.h                                                            */
/* PRINCIPAL AUTHOR      :  ls                                                                    */
/* SUBSYSTEM NAME        :  menu                                                                  */
/* MODULE NAME           :  linklist                                                              */
/* LANGUAGE              :  C                                                                     */
/* TAGRGET ENVIRONMENT   :  ANY                                                                   */
/* DATE OF FIRST RELEASE :  2014/12/21                                                            */
/* DESCRIPTION           :  linklist for menu progrom                                             */
/**************************************************************************************************/

/*
 * Revision log:
 * 
 * Created by ls, 2014/12/28
 *
 */

#ifndef _LINK_LIST_H_
#define _LINK_LIST_H_
#include "linktable.h" 

/* data struct and its operations */

typedef struct DataNode
{
    tLinkTableNode *pNext;
    char*    cmd;
    char*    desc;
    int      (*handler)();
} tDataNode;

/* find a cmd in the linklist and return the data node pointer */
tDataNode * FindCmd(tLinkTable *head, char *cmd);
/* show all cmd in linklist */
int ShowAllCmd(tLinkTable *head);

#endif /*_Link_LIST_H_*/


/**************************************************************************************************/
/* Copyright  (C) LS, 2014-2015                                                                   */
/* FILE NAME             :  linklist.c                                                            */
/* PRINCIPAL AUTHOR      :  LISUN                                                                 */
/* SUBSYSTEM NAME        :  menu                                                                  */
/* MODULE NAME           :  linklist                                                                  */
/* LANGUAGE              :  C                                                                     */
/* TAGRGET ENVIRONMENT   :  ANY                                                                   */
/* DATE OF FIRST RELEASE :  2014/12/21                                                            */
/* DESCRIPTION           :  linklist for the  menu progrom                                                */
/**************************************************************************************************/

/*
 * Revision log:
 * 
 * Created by ls, 2014/12/28
 *
 */


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


tDataNode* FindCmd(tLinkTable * head, char* cmd)
{
     if (head == NULL || cmd == NULL)
     {
          return NULL;
     }
     tDataNode *p = (tDataNode*)GetLinkTableHead(head);
     while (p != NULL)
     {
          if (strcmp(p->cmd, cmd) == 0)
          {
               return p;
          }
          p = (tDataNode*)GetNextLinkTableNode(head, (tLinkTableNode*)p);
      }
      return NULL;
} 

/* Show all the cmd in Listlist */
int ShowAllCmd(tLinkTable * head)
{
     printf("Menu List:\n");
     tDataNode *p = (tDataNode*)GetLinkTableHead(head);
     while (p != NULL)
     {
          printf("%s - %s\n", p->cmd, p->desc);
	  p = (tDataNode*)GetNextLinkTableNode(head, (tLinkTableNode*)p);
     }

     return 0;

}


/**************************************************************************************************/
/* Copyright  (C) LS, 2014-2015                                                                   */
/* FILE NAME             :  menu.c                                                                */
/* PRINCIPAL AUTHOR      :  ls                                                                 */
/* SUBSYSTEM NAME        :  menu                                                                  */
/* MODULE NAME           :  menu                                                                  */
/* LANGUAGE              :  C                                                                     */
/* TAGRGET ENVIRONMENT   :  ANY                                                                   */
/* DATE OF FIRST RELEASE :  2014/12/21                                                            */
/* DESCRIPTION           :  This is a menu progrom                                                */
/**************************************************************************************************/

/*
 * Revision log:
 * 
 * Created by ls, 2014/12/28
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"
#include "linktable.h"


int Help();
int Quit();


#define CMD_MAX_LEN 128
#define DESC_LEN    1024
#define CMD_NUM     10

int InitMenuData(tLinkTable ** ppLinkTable)
{
     *ppLinkTable = CreateLinkTable();
     tDataNode * pNode = (tDataNode*)malloc(sizeof(tDataNode));
     pNode->cmd = "help";
     pNode->desc = "Menu List:";
     pNode->handler = Help;
     AddLinkTableNode(*ppLinkTable, (tLinkTableNode*)pNode);
     pNode = (tDataNode*)(tDataNode*)malloc(sizeof(tDataNode));
     pNode->cmd = "version";
     pNode->desc = "Menu Program V1.0";
     pNode->handler = NULL;
     AddLinkTableNode(*ppLinkTable, (tLinkTableNode*)pNode);
     pNode = (tDataNode*)malloc(sizeof(tDataNode));
     pNode->cmd = "quit";
     pNode->desc = "Quit from Menu Program V1.0";
     pNode->handler = Quit;
     AddLinkTableNode(*ppLinkTable, (tLinkTableNode*)pNode);
   
     return 0; 
}

tLinkTable * head = NULL;
int main ()
{
    InitMenuData(&head);
    /* cmd line begins */
    while (1)
    {
         char cmd[CMD_MAX_LEN];
	 printf("Input a cmd number > ");
	 scanf("%s", cmd);
	 tDataNode *p = FindCmd(head, cmd);
         if (p == NULL)
	 {
	      printf("This is a wrong cmd!\n");
	      continue;
	 }

	 printf("%s - %s\n", p->cmd, p->desc);
         if (p->handler != NULL)
	 {
	      p->handler();
	 }

    }
}

int Help()
{
     ShowAllCmd(head);
}

int Quit()
{
    exit(0);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值