码迷,mamicode.com
首页 > 其他好文 > 详细

双向链表 C (两个无符号数unsinged int)

时间:2015-01-15 12:24:13      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

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

typedef struct ListNode ListNode;
typedef unsigned int uint;
struct ListNode {
uint loop_count;
int number;
ListNode *next;
ListNode *last;
};
ListNode *p, *q, *start = NULL, *end;
/* Insert function, according to the list in the order of insertion. */
void insert(int num, uint count)
{
ListNode *q;
q=(ListNode*) malloc(sizeof(struct ListNode));
if (start == NULL) /* new */
{
start = q;
end = q;
p = q;
start->last = NULL;
end->next = NULL;
} else {
if ( count <= start->loop_count )/* Insert list head */
{
start->last = q;
q->next = start;
q->last = NULL;
start = q;

} else if ( count > end->loop_count ) /* Insert list tail */
{
end->next = q;
q->last = end;
end = q;
q->next = NULL;
} else if ((count < end->loop_count) && ( count > start->loop_count))/* Inserted in the list */
{
for (p = start; count > p->loop_count; p = p->next)
;
q->next = p;
p->last = q;
for (p = start; p->next != q->next; p = p->next)
;
p->next = q;
q->last = p;
}

}
q->loop_count = count;
q->number = num;

}
void find(int num) /* Find by Serial Number */
{
if (start == NULL) {
printf("NULL\n");
} else {
for (p = start; p != NULL && p->number != num; p = p->next)
;
if (p == NULL) {
printf("erro num\n");

} else if (p->number == num) {
printf("num=%d\n", num);
printf("loop_count=%d", p->loop_count);
}

}
}
void del(int num) /* Delete by number */
{
for (p = start; p->number != num; p = p->next)
;
if (p->number == num) {
if (p->next == NULL) {
if (p->last == NULL) {
start = NULL;

} else {
(p->last)->next = NULL;
}
free( p );
} else if (p->last == NULL) {
(p->next)->last = NULL;
start = p->next;
free( p );
} else if (p->last != NULL && p->next != NULL) {
(p->last)->next = p->next;
(p->next)->last = p->last;
free( p );
}
}

else {
printf("Number does not exist!\n");
}

}
void print_list() {
printf("num\tloop_count\n");
for (p = start;; p = p->next) {
printf("%d\t%d\n", p->number, p->loop_count);
if (p->next == NULL)
break;
}
}
void insert_node_test(void) {
int num;
uint loop_count;

num = 1;
loop_count = 1;
insert(num, loop_count);
print_list();
num = 6;
loop_count = 6;
insert(num, loop_count);
print_list();
num = 4;
loop_count = 4;
insert(num, loop_count);
print_list();
num = 2;
loop_count = 2;
insert(num, loop_count);
print_list();

#if 0
del(num);
print_list();
find(num);
#endif
/* print_list(); */
}

 

 

运行结果如下:

num loop_count
1 1
num loop_count
1 1
6 6
num loop_count
1 1
4 4
6 6
num loop_count
1 1
2 2
4 4
6 6

双向链表 C (两个无符号数unsinged int)

标签:

原文地址:http://www.cnblogs.com/qunxuan/p/4225691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!