【练习】单链表基本操作
2013-04-24 18:34
281 查看
/* * szl_linkedlist.h */ #ifndef SZL_LINKEDLIST_H #define SZL_LINKEDLIST_H typedef int Element; typedef struct _tag_node_type Node, ListNode, * PNode, * List, * Position; void element_assign (Element *, Element *); void list_init (List * ); List list_append (List * , PNode ); List list_create (List * ); Position list_find (List , Element ); List list_remove (List *, Position ); List list_remove_element (List *, Element ); List list_remove_all_elements (List *, Element ); void list_make_empty (List * ); void list_traverse (List ); #endif
/* * szl_linkedlist.c */ #include "szl_linkedlist.h" #include <stdlib.h> static Element new_element (); static PNode new_node (); struct _tag_node_type{ Element element; struct _tag_node_type * link; }; static Element new_element (){ /* -1 is the end of input. */ Element ret; scanf("%d", &ret); return (ret); } static PNode new_node (){ /* return NULL when -1 is read */ Element e = new_element(); PNode pnode_ret = NULL; if(-1 != e){ pnode_ret = (PNode)malloc( sizeof(ListNode)); pnode_ret->link = NULL; element_assign (&(pnode_ret->element), &e); } return pnode_ret; } void element_assign (Element * e1, Element * e2){ (*e1) = (*e2); } void list_init (List * plist){ (*plist) = NULL; } List list_append (List * plist, PNode pnode){ List list = (*plist); PNode p = list; if(pnode){ pnode->link = NULL; if(!list){ (*plist) = pnode; } else{ while(p->link){ p = p->link; } p->link = pnode; } } return (*plist); } List list_create (List * plist){ PNode pnode = new_node(); while(pnode){ list_append (plist, pnode); pnode = new_node(); } return (*plist); } Position list_find (List list, Element value){ Position position = list; while(position){ if(position->element == value){ break; } position = position->link; } return position; } List list_remove_element (List * plist, Element e){ Position position = list_find (*plist, e); return (list_remove (plist, position)); } List list_remove_all_elements (List * plist, Element e){ Position position = list_find (*plist, e); while (position){ list_remove (plist, position); position = list_find (*plist, e); } return (*plist); } List list_remove (List * plist, Position position){ Position pnode_p, pnode_q; if (!(*plist)){ return (*plist); } else{ pnode_p = (*plist); pnode_q = pnode_p; while(pnode_p){ if (pnode_p == position){ break; } pnode_q = pnode_p; pnode_p = pnode_p->link; } if(pnode_p){ if(pnode_p == pnode_q){ (*plist) = (*plist)->link; free (pnode_p); pnode_p = pnode_q = NULL; } else{ pnode_q->link = pnode_p->link; free (pnode_p); pnode_p = NULL; } } return (*plist); } } void list_make_empty ( List * plist){ PNode pnode_p, pnode_q; pnode_p = (*plist); while (NULL != pnode_p){ pnode_q = pnode_p; pnode_p = pnode_p->link; free (pnode_q); } (*plist) = NULL; } void list_traverse (List list){ Position p = list; while(p){ printf("%d\n", p->element); p = p->link; } }
/* * szl_linkedlist_test.c */ #include <stdio.h> #include <stdlib.h> #include "szl_linkedlist.h" int main(int argc, char * argv[]){ List l; list_init (&l); list_create (&l); list_traverse (l); list_remove_all_elements(&l,2); list_traverse (l); list_make_empty (&l); return 0; }
相关文章推荐
- 链表解说和基本操作练习附代码
- 链表讲解和基本操作练习附代码
- 实验二 链表的基本操作
- 链表的基本操作
- 单链表的基本操作
- 不带头节点的双向循环链表的基本操作
- 静态单链表的基本操作
- 链表的基本操作
- Java中关于链表的基本操作
- 链表的一些基本操作
- 单链表的基本操作
- 双链表基本操作的实现
- 名单(两)——基本操作单向链表(创、删、印、节点统计数)
- 单链表基本操作总结
- 链表操作练习——学生信息链表
- 1326 Problem A 算法2-8~2-11:链表的基本操作
- 数据结构学习笔记(二) 链表之带表头结点的单循环链表基本操作
- 无头单链表基本操作的实现
- Java链表基础(3)-基本操作
- 重学数据结构(一):链表基本操作与一元多项式相加