#include <stdio.h> #define SIZE sizeof(LINKLIST) #define LINE_ITEM_COUNT (5) //每行显示的元素个数
#define PRINT_ITEM "%d " #define SCANF_ITEM "%d"
typedef int ITEM; typedef struct node //定义链表
{ ITEM data; struct node *next; }LINKLIST; //当调用GET方法是,如果出现异常时,进行下面的定义
const ITEM LINK_NULL = -9999; //定义当item不存在时为此值
const ITEM IDX_ERROR = -9998;//定义当index索引错误时返回链表data为该值
//单链表的操作方法
LINKLIST * INITIATE(); //初始化节点
LINKLIST * CREATE_NODE(ITEM); //创建节点
LINKLIST * INSERT_PRIOR_NODE(LINKLIST *, LINKLIST *);//在头指针的前面插入一个元素
LINKLIST * INSERT_BACK_NODE(LINKLIST *, LINKLIST *); //在头指针的后面插入一个元素
int LENGTH(LINKLIST *); //返回链表的节点个数
ITEM GET(LINKLIST *,int); //获取指定位置的值,-9999代表链表为NULL,-9998代表索引错误。
int LOCATE(LINKLIST *,ITEM);//获取值的索引;-1代表是空表,-2代表搜索不到;其它代表返回的索引出。
LINKLIST * DELETE(LINKLIST *,int);//删除指定索引编号的值
LINKLIST * INSERT_PRIOR_BY_INDEX(LINKLIST *,int,LINKLIST *);//在指定的索引前面插入一个元素
LINKLIST * INSERT_BACK_BY_INDEX(LINKLIST *,int,LINKLIST *);//在指定的索引后面插入一个元素
LINKLIST * DELETE_INDEX_BACK(LINKLIST *,int); //删除索引节点后面的元素0
void MENU();//菜单
void print_string(char *); void menu_method(char *,char *); //重构菜单方法
void print_string_info(char *);//重构菜单方法,打印相关信息
void print_tab(); //输出一个tab
void print_enter(); //输出一个回车
void print_length(LINKLIST *); //打印长度信息
void print_idx_is_error(); //打印索引错误的信息
void print_lnk_tbl_is_null();//打印链表为NULL错误信息
void print_info(LINKLIST *); int get_index_str(char *); int get_input_index(); int get_del_index(); LINKLIST * insert_single_node_prior(LINKLIST *); //在头指针的前面插入一个元素
LINKLIST * insert_single_node_back(LINKLIST *); //在链表的结尾出插入一个元素
LINKLIST * insert_multi_node_prior(LINKLIST *);//在头指针的前面插入多个个元素
LINKLIST * insert_multi_node_back(LINKLIST *);//在头指针的尾部插入多个个元素
LINKLIST * insert_method();//插入数据时,共同使用部分的提炼
void get_item_value(LINKLIST *); //获取指定索引出的值
void get_index(LINKLIST *); //获取链表中时候含有某个元素
LINKLIST * delete_node(LINKLIST *); //删除一个节点
LINKLIST * insert_prior_by_idx(LINKLIST *); //在指定的索引处的前面插入一个节点
LINKLIST * insert_back_by_idx(LINKLIST *); //在指定的索引处的后面插入一个节点
LINKLIST * del_idx_back(LINKLIST *);//删除指定索引后面的元素
LINKLIST * operate_method(LINKLIST *,LINKLIST *(* fun)(LINKLIST *)); //使用指向函数的指针,对程序进行重构
void operate_method_no_rtn(LINKLIST *,void (* fun)(LINKLIST *)); //使用指向函数的指针,对程序进行重构(无返回值)
int main(int argc,char *argv[]) { LINKLIST * head,*lnk = NULL; int sel_item_value; head = lnk; MENU(); do { print_tab();printf("you select menu:"); scanf("%d",&sel_item_value); switch(sel_item_value) { case -1: break; case 1: head = operate_method(head,insert_single_node_prior); break; case 2: head = operate_method(head,insert_single_node_back); break; case 3: head = operate_method(head,insert_multi_node_prior); break; case 4: head = operate_method(head,insert_multi_node_back); break; case 5: head = operate_method(head,insert_prior_by_idx); break; case 6: head = operate_method(head,insert_back_by_idx); break; case 7: operate_method_no_rtn(head,get_item_value); break; case 8: operate_method_no_rtn(head,get_index); break; case 9: head = operate_method(head,delete_node); break; case 10: head = operate_method(head,del_idx_back); break; case 11: operate_method_no_rtn(head,print_length); break; case 12: operate_method_no_rtn(head,print_info); break; case 0: MENU(); break; } }while(-1 != sel_item_value); return 0; }
LINKLIST * INITIATE() { LINKLIST *p; p = (LINKLIST *)malloc(SIZE); return p; }
LINKLIST * CREATE_NODE(ITEM data) { LINKLIST *p = INITIATE(); p->data = data; return p; }
LINKLIST * INSERT_PRIOR_NODE(LINKLIST * head, LINKLIST * data) { if (head != NULL) { data->next = head; } else { data->next = NULL; } head = data; return head; }
LINKLIST * INSERT_BACK_NODE(LINKLIST * head, LINKLIST * data) { LINKLIST *p; p = head; if(head != NULL) { while(p->next != NULL) { p = p->next; } p->next = data; data->next = NULL; } else { data->next = NULL; head = data; } return head; }
int LENGTH(LINKLIST * head) { LINKLIST *p; p = head; int result= 0; while (p != NULL) { result ++; p = p->next; } return result; }
ITEM GET(LINKLIST * head,int index) { ITEM result ; LINKLIST *p = head; int len = LENGTH(head); int i = 0; if (0 == len) { result = LINK_NULL; } else if (index < 0 || index > len - 1) { result = IDX_ERROR; } else { while (p->next != NULL && i != index) { p = p->next; i++; } result = p->data; } return result; }
int LOCATE(LINKLIST * head,ITEM data) { int idx = 0,len; LINKLIST *p; p = head; len = LENGTH(head); if (len < 1) { return -1; } else { while(p->next != NULL && p->data != data) { p = p->next; idx++; } if (p->data == data) { return idx; } else { return -2; } } }
LINKLIST * DELETE(LINKLIST * head,int index) { int i = 0,len = LENGTH(head); LINKLIST *del,*p1,*p2; p1 = p2 = head; if(len == 0) { return head; } else if (index < 0 || index > len - 1) { return head; } else { while (p1->next != NULL && i != index) { p2 = p1; p1 = p1->next; i++; } del = p1; if (p1 == head) { head = p1->next; } else if (p1->next == NULL) { p2->next = NULL; } else { p2->next = p1->next; } free(del); return head; } }
LINKLIST * INSERT_PRIOR_BY_INDEX(LINKLIST * head,int index,LINKLIST * data) { int i = 0,len = LENGTH(head); LINKLIST *p1,*p2; p1 = p2 = head; if (0 == len && index != 0) { ; } else if (0 == len && index == 0) { data->next = head; head = data; } else if ((len > 0) && (index < 0 || index > len)) { ; } else {
while (p1->next != NULL && i < index) { i++; p2 = p1; p1 = p1->next; } if (p2 == head && head == NULL) { data->next = head; head = data; } else if (i == index && p1->next != NULL) { data->next = p1; p2->next = data; } else if (p1->next == NULL) { p1->next = data; data->next = NULL; } else { ; } } return head; }
LINKLIST * INSERT_BACK_BY_INDEX(LINKLIST * head,int index,LINKLIST * data) { int i,len = LENGTH(head); LINKLIST *p = head; if (0 == len && index != 0) { ; } else if (0 == len && 0 == index) //当链表为空时,索引为0,则直接将此元素插入链表中
{ data->next = NULL; head = data; } else if(len > 0 && (index < 0 || index > len - 1)) { ; } else { while (p->next != NULL && i < index) { p = p->next; i++; } data->next = p->next; p->next = data; } return head; }
LINKLIST * DELETE_INDEX_BACK(LINKLIST * head,int index) { int i = 0,len = LENGTH(head); LINKLIST *p1,*p2,*del; p1 = p2 = head; if (0 == len) { ; } else if (len > 0 && (index < 0 || index > len - 1)) { ; } else { while(p1->next != NULL && i != index) { p2 = p1; p1 = p1->next; i++; } if (p1 == head) { del = p1; head = p1->next; free(del); } else if (p1->next != NULL) { p1 = p1->next; del = p1; p2->next = p1->next; free(del); } else { del = p1; p2->next = NULL; free(del); } } return head; }
void MENU() { print_enter(); print_tab();print_string_info("link table manage system"); print_enter(); menu_method("1","insert item before head point"); menu_method("2","insert item back head point"); menu_method("3","insert multi item before head point"); menu_method("4","insert multi item back head point"); menu_method("5","insert item by index before"); menu_method("6","insert item by index back"); menu_method("7","get index vlaue"); menu_method("8","locate element"); menu_method("9","delete element"); menu_method("10","dlete index back element"); menu_method("11","print length"); menu_method("12","print link table info"); menu_method("0","return menu"); menu_method("-1","exit system"); }
void menu_method(char * num,char * info) { print_tab(); print_string(num); print_tab(); print_string(info); print_enter(); }
void print_lnk_tbl_is_null() { return print_string_info("error:the link table is NULL."); }
void print_idx_is_error() { return print_string_info("error:the index is error."); } void print_string_info(char *str) { print_tab(); print_string(str); print_enter(); } void print_string(char * str) { char * p; p = str; while(*p) { printf("%c",*p++); } } void print_tab() { printf("\t"); }
void print_enter() { printf("\n"); }
void print_length(LINKLIST * head) { int len = LENGTH(head); print_tab(); printf("length : %d ",len); print_enter(); }
void print_info(LINKLIST * head) { int len = 0; LINKLIST *p; p = head; if (p != NULL) { print_string_info("the element is"); print_tab(); while(p != NULL) { printf(PRINT_ITEM,p->data); ++len; if (0 == len % LINE_ITEM_COUNT) { print_enter(); print_tab(); } p = p->next; } print_enter(); } print_tab();printf("all count: %d",len);print_enter(); }
LINKLIST * insert_single_node_prior(LINKLIST * head) { LINKLIST * item = insert_method(); head = INSERT_PRIOR_NODE(head,item); print_string_info("insert element sucess."); return head; }
LINKLIST * insert_single_node_back(LINKLIST * head) { LINKLIST * item = insert_method(); head = INSERT_BACK_NODE(head,item); print_string_info("insert element sucess."); return head; }
LINKLIST * insert_multi_node_prior(LINKLIST * head) { int i,count; print_tab(); printf("what count do you want:"); scanf("%d",&count); while(count < 1) { print_tab(); printf("input number must > 0,afresh number:"); scanf("%d",&count); } for (i = 0; i < count; i++) { head = insert_single_node_prior(head); } print_string_info("insert multi element sucess."); return head; }
LINKLIST * insert_multi_node_back(LINKLIST *head) { int i,count; print_tab(); printf("what count do you want:"); scanf("%d",&count); while(count < 1) { print_tab(); printf("input number must > 0,afresh number:"); scanf("%d",&count); } for (i = 0; i < count; i++) { head = insert_single_node_back(head); } print_string_info("insert multi element sucess."); return head; }
LINKLIST * insert_method() { print_tab(); printf("please input a number: "); ITEM element; scanf(SCANF_ITEM,&element); return CREATE_NODE(element); }
int get_input_index() { return get_index_str("please input index:"); }
int get_del_index() { return get_index_str("delete index:"); }
int get_index_str(char *p) { int idx; print_tab(); print_string(p); scanf("%d",&idx); return idx; }
void get_item_value(LINKLIST * head) { int idx = get_input_index(); ITEM item = GET(head,idx); if (item == LINK_NULL) { print_lnk_tbl_is_null(); } else if (item == IDX_ERROR) { void print_idx_is_error(); } else { print_tab(); printf("the %d index value is : ",idx); printf(PRINT_ITEM,item); print_enter(); } }
void get_index(LINKLIST * head) { int idx; ITEM element; print_tab(); printf("search item value:"); scanf(SCANF_ITEM,&element); idx = LOCATE(head,element); if (-1 == idx) { print_lnk_tbl_is_null(); } else if (-2 == idx) { print_tab(); printf("error:not this element."); print_enter(); } else { print_tab(); printf("the "); printf(PRINT_ITEM,element); printf(" index is %d",idx); print_enter(); } }
LINKLIST * delete_node(LINKLIST *head) { int len = LENGTH(head); int idx = get_del_index(); if (len == 0) { print_lnk_tbl_is_null(); } else if(idx < 0 || idx > len - 1) { print_idx_is_error(); } else { head = DELETE(head,idx); print_tab(); printf("delete index %d success.",idx); print_enter(); } return head; }
LINKLIST * insert_prior_by_idx(LINKLIST * head) { int len = LENGTH(head); LINKLIST *data = insert_method(); int idx = get_input_index(); if (0 == len && idx != 0) { print_lnk_tbl_is_null(); } else if ((len > 0) && (idx < 0 || idx > len)) { print_idx_is_error(); } else { head = INSERT_PRIOR_BY_INDEX(head,idx,data); print_string_info("insert data success"); } return head; }
LINKLIST * insert_back_by_idx(LINKLIST * head) { int len = LENGTH(head); LINKLIST *data = insert_method(); int idx = get_input_index(); if (0 == len && idx != 0) { print_lnk_tbl_is_null(); } else if ((len > 0) && (idx < 0 || idx > len - 1)) { print_idx_is_error(); } else { head = INSERT_BACK_BY_INDEX(head,idx,data); print_string_info("insert data success"); } return head; }
LINKLIST * del_idx_back(LINKLIST * head) { int len = LENGTH(head); int idx = get_del_index(); if (0 == len) { print_lnk_tbl_is_null(); } else if (len > 0 && (idx < 0 || idx > len - 1)) { print_idx_is_error(); } else { head = DELETE_INDEX_BACK(head,idx); print_string_info("del success."); } return head; }
LINKLIST * operate_method(LINKLIST * head,LINKLIST * (* fun)(LINKLIST * src_head)) { return (* fun)(head); }
void operate_method_no_rtn(LINKLIST * head,void (* fun)(LINKLIST *src_head)) { return (* fun)(head); }
|