这个双向循环问题怎么做?
【问题描述】统计带头结点的双向循环链表中元素个数。【输入形式】输入一组整型数据,以空格隔开,遇字母结束输入。
【输出形式】统计链表中元素个数。
【样例输入】1 2 3 4 5 a
【样例输出】5
【样例说明】
【评分标准】要求链表是双向循环。使用C语言。
//double link node typedef struct _dlink { int value; struct _dlink* prev; struct _dlink* next; } dlink_t; //double link manager typedef struct _dlmgr { dlink_t* link; int count; } dlmgr_t; int insert_node(dlmgr_t* mgr, int value) { dlink_t* head = mgr->link; dlink_t* node = calloc(1, sizeof(dlink_t)); if (NULL == node) { return -1; } if (head) { dlink_t* last = head->prev; if (head != last) { last->next = node; node->next = head; } else {//only one node in link head->next = node; head->prev = node; node->prev = head; node->next = head; } } else { mgr->link = node; node->next = node; node->prev = node; } mgr->count++; return 0; } int delete_node(dlmgr_t* mgr, int value) { dlink_t* head = mgr->link; dlink_t* work = head; do { if (work->value == value) { dlink_t* last = work->prev; last->next = work->next; work->next->prev = last; free(work); mgr->count--; return 0; } } while (work != head); return -1; }