/* Author: ACb0y Date: 2010-6-11 Description: link list */ #include <stdio.h> #include <malloc.h> typedef int elemtype; struct node { elemtype data; node * pnext; }; typedef struct { node * head; int length; }link_list; /* FUNCTION link_list_create DESCRIPTION create link list PARAMETERS void RETURNS p_list: the pointer of link list */ link_list * link_list_create() { link_list * p_list = (link_list *)malloc(sizeof(link_list)); p_list->head = (node *)malloc(sizeof(node)); p_list->head->pnext = NULL; p_list->length = 0; return p_list; } /* FUNCTION link_list_length DESCRIPTION get the length of link list PARAMETERS p_list: the pointer of link list RETURNS p_list->length: the length of link list */ int link_list_length(link_list * p_list) { return p_list->length; } /* FUNCTION link_list_locate DESCRIPTION get the pointer of appoint element in the link list PARAMETERS p_list: the pointer of link list value: the appoint element RETURNS pnode: the pointer of appoint element */ node * link_list_locate(link_list * p_list, elemtype value) { node * pnode = p_list->head; int flag = 0; while (pnode->pnext != NULL) { if (pnode->data == value) { flag = 1; break; } pnode = pnode->pnext; } if (flag) { return pnode; } else { return NULL; } } /* FUNCTION link_list_get DESCRIPTION get the pointer of appoint position's element in the link list PARAMETERS p_list: the pointer of link list position: the appoint position RETURNS pnode: the pointer of appoint position's element in the link list */ node * link_list_get(link_list * p_list, int position) { if (position < 0 || position > p_list->length) { return NULL; } else { node * pnode = p_list->head; while (pnode->pnext != NULL && position != 0) { --position; pnode = pnode->pnext; } return pnode; } } /* FUNCTION link_list_insert DESCRIPTION insert the element to appoint position in the link list PARAMETERS p_list: the pointer of the link list value: the insert element positon: the appoint position RETURNS flag: insert operation whether successful */ int link_list_insert(link_list * p_list, elemtype value, int position) { int flag; if (position < 1 || position > link_list_length(p_list) + 1) { flag = 0; } else { node * pnode = link_list_get(p_list, position - 1); node * pnew = (node *)malloc(sizeof(node)); pnew->data = value; pnew->pnext = pnode->pnext; pnode->pnext = pnew; flag = 1; ++p_list->length; } return flag; } /* FUNCTION link_list_delete DESCRIPTION delete the element by appoint position PARAMETERS p_list: the pointer of link list position: appoint position RETURNS flag: flag whether the delete operation successful or not */ int link_list_delete(link_list * p_list, int position) { int length = link_list_length(p_list); int flag; if (length < 1 || length > length) { flag = 0; } else { node * pnode = link_list_get(p_list, position - 1); node * ptemp = pnode->pnext; pnode->pnext = pnode->pnext->pnext; free(ptemp); flag = 1; --p_list->length; } return flag; } /* FUNCTION link_list_print DESCRIPTION print the all element in the link list PARAMETERS p_list: the pointer of link list RETURNS void */ void link_list_print(link_list * p_list) { node * pnode = p_list->head->pnext; while (pnode != NULL) { printf("%d ", pnode->data); pnode = pnode->pnext; } printf("/n"); } /* FUNCTION link_list_reverse DESCRIPTION reverse the link list PARAMETERS p_list: the pointer of link list RETURNS void */ void link_list_reverse(link_list * p_list) { node * pnode = p_list->head->pnext; node * p; p_list->head->pnext = NULL; while (pnode != NULL) { p = pnode; pnode = pnode->pnext; p->pnext = p_list->head->pnext; p_list->head->pnext = p; } } int main() { link_list * p_list = link_list_create(); printf("The link list's length is %d/n", link_list_length(p_list)); if (link_list_locate(p_list, 5) == NULL) { printf("No find!/n"); } if (link_list_get(p_list, 0) == NULL) { printf("NULL pointer/n"); } int i; for (i = 0; i < 4; i++) { link_list_insert(p_list, i + 3, i + 1); } printf("address:0x%x the value is %d/n", link_list_locate(p_list, 4), *link_list_locate(p_list, 4)); link_list_print(p_list); link_list_reverse(p_list); link_list_print(p_list); printf("The length of link list is %d/n", link_list_length(p_list)); while (link_list_length(p_list) > 0) { link_list_delete(p_list, 1); } link_list_print(p_list); printf("The length of link list is %d/n", link_list_length(p_list)); return 0; }