码迷,mamicode.com
首页 > 编程语言 > 详细

双向链表 C (两个无符号数unsinged int) 扩展为6个数组,添加时间显示。

时间:2015-01-15 18:07:20      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include "timers.h"


typedef struct ListNode ListNode;
typedef unsigned int uint;
#define DSP_NUMBERS 6
struct ListNode {
uint loop_count;
int number;
char time[25];
ListNode *next;
ListNode *last;
};
ListNode id_test[DSP_NUMBERS];
ListNode *p[DSP_NUMBERS], *q[DSP_NUMBERS], *end[DSP_NUMBERS];
ListNode *start[DSP_NUMBERS] = {NULL, NULL, NULL, NULL, NULL, NULL};

/* Insert function, according to the list in the order of insertion. */
void insert(int num, uint count, uint index)
{
ListNode *q[index];
time_t timep;
struct tm *ptime;
struct timespec timespec;

q[index]=(ListNode*) malloc(sizeof(struct ListNode));
if (start[index] == NULL) /* new */
{
start[index] = q[index];
end[index] = q[index];
p[index] = q[index];
start[index]->last = NULL;
end[index]->next = NULL;
} else {
if ( count <= start[index]->loop_count )/* Insert list head */
{
start[index]->last = q[index];
q[index]->next = start[index];
q[index]->last = NULL;
start[index] = q[index];

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

}

time(&timep);
ptime=gmtime(&timep);
memset(&timespec, 0, sizeof(timespec));
clock_gettime(CLOCK_REALTIME, &timespec);

/*
printf("%04d/%2d/%2d",(1900+ptime->tm_year), (1+ptime->tm_mon),ptime->tm_mday);
printf(" %02d:%02d:%02d.%03ld\n", ptime->tm_hour, ptime->tm_min, ptime->tm_sec,timespec.tv_nsec/1000000);
*/

memset(&q[index]->time,0x00,sizeof(q[index]->time));
sprintf(q[index]->time,"%04d/%2d/%2d %02d:%02d:%02d.%03ld",(1900+ptime->tm_year), (1+ptime->tm_mon),ptime->tm_mday, ptime->tm_hour, ptime->tm_min, ptime->tm_sec,timespec.tv_nsec/1000000);
/*
printf("time=%s\n",q[index]->time);
*/
q[index]->loop_count = count;
q[index]->number = num;

}

void del(int num, uint index) /* Delete by number */
{
for (p[index] = start[index]; p[index]->number != num; p[index] = p[index]->next)
;
if (p[index]->number == num) {
if (p[index]->next == NULL) {
if (p[index]->last == NULL) {
start[index] = NULL;
} else {
(p[index]->last)->next = NULL;
}
free( p[index] );
} else if (p[index]->last == NULL) {
(p[index]->next)->last = NULL;
start[index] = p[index]->next;
free( p[index] );
} else if (p[index]->last != NULL && p[index]->next != NULL) {
(p[index]->last)->next = p[index]->next;
(p[index]->next)->last = p[index]->last;
free( p[index] );
}
}

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

}


void print_list(uint index) {
printf("index\tnum\ttime\t\t\tloop_count\n");
for (p[index] = start[index];; p[index] = p[index]->next) {
printf("%d\t%d\t%s\t\t%d\n",index, p[index]->number, p[index]->time, p[index]->loop_count);
if (p[index]->next == NULL)
break;
}
}

void insert_node_test(void) {
int num;
uint loop_count;

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

num = 5;
loop_count = 5;
if ( (num > 4) && (loop_count > start[0]->loop_count) ) {
del(1, 0);
print_list(0);
} else {

}
insert(num, loop_count, 0);
print_list(0);
}

 

 

运行结果如下:

index   num   time               loop_count
0      1      2015/ 1/15 08:15:40.033      1
index   num   time               loop_count
0      1      2015/ 1/15 08:15:40.033      1
0      6      2015/ 1/15 08:15:40.033      6
index   num   time               loop_count
0      1      2015/ 1/15 08:15:40.033      1
0      4      2015/ 1/15 08:15:40.033      4
0      6      2015/ 1/15 08:15:40.033      6
index   num   time               loop_count
0      1      2015/ 1/15 08:15:40.033      1
0      2      2015/ 1/15 08:15:40.033      2
0      4      2015/ 1/15 08:15:40.033      4
0      6      2015/ 1/15 08:15:40.033      6
index   num   time               loop_count
0      2      2015/ 1/15 08:15:40.033      2
0      4      2015/ 1/15 08:15:40.033      4
0      6      2015/ 1/15 08:15:40.033      6
index   num   time               loop_count
0      2      2015/ 1/15 08:15:40.033      2
0      4      2015/ 1/15 08:15:40.033      4
0      5      2015/ 1/15 08:15:40.033      5
0      6      2015/ 1/15 08:15:40.033      6
->

双向链表 C (两个无符号数unsinged int) 扩展为6个数组,添加时间显示。

标签:

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

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