資源簡介
多線程實(shí)現(xiàn)雙向鏈表的增刪改 其中的信號量設(shè)成一個(gè)也行- -
代碼片段和文件信息
#include
#include
#include?
#include?
HANDLE?sem;
HANDLE?tem;
HANDLE?uem;
HANDLE?vem;
typedef?int?Datatype;
typedef?struct?Point
{
struct?Point?*prior?*next;
Datatype?number;
}Point?*Pointlist;
typedef?struct?ProcParam
{
Pointlist?a;
};
Pointlist?InitList()
{
srand((unsigned)time(NULL));
Pointlist?head;
Datatype?data;
head?=?(Pointlist)malloc(sizeof(Point));
head->number?=?NULL;
head->prior?=?NULL;
head->next?=?NULL;
return?head;
}
Insert(Pointlist?head)
{
int?i?=?0;
WaitForSingleobject(sem?INFINITE);
srand((unsigned)time(NULL));
Pointlist?p1?p2;
p1?=?head;
if?(p1->number?==?NULL)
{
p1->number?=?(rand()?%?10)?+?1;
printf(“插入元素%d\n“?p1->number);
Sleep(500);
}
else
while?(p1->next?!=?NULL)
{
p1?=?p1->next;
}
for?(i?=?0;?i?3;?i++)
{
p2?=?(Pointlist)malloc(sizeof(Point));
p2->prior?=?p1;
p1->next?=?p2;
p2->number?=?(rand()?%?10?+?1);
p2->next?=?NULL;
p1?=?p2;
printf(“插入元素%d\n“?p2->number);
Sleep(500);
}
ReleaseSemaphore(sem?1?NULL);
return?head;
}
Pointlist?find(Pointlist?head)
{
WaitForSingleobject(vem?INFINITE);
srand((unsigned)time(NULL));
Pointlist?p;????
p?=?head;
int?i?=?1?c?=?(rand()?%?10)?+?2;
printf(“查找元素%d的位置\n“c);
while?(p?!=?NULL?&&?p->number?!=?c)
{???????????????????????????????
++i;????????????????????????
p?=?p->next;
}
if?(p?==?NULL)?????????????
{
printf(“無此節(jié)點(diǎn)\n“);
ReleaseSemaphore(vem?1?NULL);
return?0;
}
else??
printf(“在鏈表中的位置為:%d\n“?i);
ReleaseSemaphore(vem?1?NULL);
}
Pointlist?Insert2(Pointlist?head)
{
WaitForSingleobject(tem?INFINITE);
int?i?a;
srand((unsigned)time(NULL));
i?=?(rand()?%?10)?+?2;
a?=?(rand()?%?10);
printf(“在位置%d中插入元素%d\n“?i?a);
Sleep(500);
Pointlist?p?s;?????????????????????????
p?=?head;????????????????????????
int?tempi;
if?(i>10)
{
printf(“無此位置的節(jié)點(diǎn)\n“);
Sleep(500);
ReleaseSemaphore(tem?1?NULL);
return?head;
}
if?(i?==?1)
{
s?=?(Pointlist)malloc(sizeof(Point));
s->number?=?a;
s->prior?=?NULL;
s->next?=?p;
p->prior?=?s;
head?=?s;
print(head);
Sleep(500);
ReleaseSemaphore(tem?1?NULL);
return?head;
}
else
{
for?(tempi?=?1;?tempi? {
p?=?p->next;
}
s?=?(Pointlist)malloc(sizeof(Point));
s->number?=?a;?????????????????????????
s->next?=?p->next;????????????????????????
p->next->prior?=?s;
s->prior?=?p;
p->next?=?s;
print(head);
Sleep(500);
ReleaseSemaphore(tem?1?NULL);
return?head;
};
}
Pointlist Erase(Pointlist?head)
{
WaitForSingleobject(uem?INFINITE);
int?i;
srand((unsigned)time(NULL));
i?=?(rand()?%?10)?+?2;
printf(“刪除位置%d上的節(jié)點(diǎn)\n“?i);
Sleep(500);
int?tempi?=?1;
Pointlist?p;????????????????????????//p為查找結(jié)點(diǎn)。?
p?=?head;
while?(tempi?!=?i?&&?p?!=?NULL)
{
p?=?p->next;
tempi++;
}
if?(p?==?NUL
- 上一篇:中文郵件語料庫.docx
- 下一篇:fme 2012的licgen.exe
評論
共有 條評論