#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 (×pec, 0, sizeof (TIMESPEC));
Clock_gettime (Clock_realtime, ×pec);
/*
printf ("%04d/%2d/%2d", (1900+ptime->tm_year), (1+ptime->tm_mon), ptime->tm_mday);
printf ("%02d:%02d:%02d.%0 3ld\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.%0 3ld ", (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);
}
The results of the operation are as follows:
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
-
The bidirectional list C (two unsigned unsinged int) expands to 6 arrays, adding a time display.