c++ 编写一个线程安全的双向链表,要代码!

作者&投稿:盛谢 (若有异议请与网页底部的电邮联系)
C++线程安全型双向链表的实现~

#include
#include
using namespace std;


typedef struct Node{
int iVal;
Node *pUp,*pDown;
}DNode,*PNode;

class NodeList{
private:
PNode mHead;
char mName[1024];
CRITICAL_SECTION g_cs;
public:
NodeList(){
mHead=NULL;
mName[0]='\0';
}
~NodeList(){
Clean();
}
PNode Clone(){
PNode tmp=NULL , tmp1=mHead ,tmp2;
while(tmp1){
if(tmp){
tmp2->pDown=new DNode();
tmp2->pDown->iVal=tmp1->iVal;
tmp2->pDown->pDown=NULL;
tmp2->pDown->pUp=tmp2;
tmp2=tmp2->pDown;
}else{
tmp2=new DNode();
tmp2->iVal=tmp1->iVal;
tmp2->pUp=NULL;
tmp2->pDown=NULL;
tmp=tmp2;
}

tmp1=tmp1->pDown;
}
return tmp;
}
bool Append(PNode node){
EnterCriticalSection(&g_cs);
bool bRet=false;
PNode tmp=mHead;
if(node){
if (tmp){
while(tmp->pDown){
tmp=tmp->pDown;
}
tmp->pDown=node;
node->pUp=tmp;
}else{
mHead=node;
}
bRet=true;
}
LeaveCriticalSection(&g_cs);
return bRet;
}
void Append(int val){
PNode tmp=mHead , newNode;
EnterCriticalSection(&g_cs);
if(tmp)
while(tmp->pDown){
tmp=tmp->pDown;
}
newNode=new DNode();
newNode->iVal=val;
newNode->pDown=NULL;
if(tmp){
newNode->pUp=tmp;
tmp->pDown=newNode;
}else{
newNode->pUp=NULL;
mHead=newNode;
}
LeaveCriticalSection(&g_cs);
//cout<<endl<<"Append Item is "<<val<<" success!"<<endl;
}
bool Delete(int val){
EnterCriticalSection(&g_cs);
PNode tmp=mHead;
bool bRet=false;
while(tmp){
if ( tmp->iVal==val){
bRet=true;
break;
}else{
tmp=tmp->pDown;
}
}
if(bRet){
if(tmp==mHead){
mHead=tmp->pDown;
if ( tmp->pDown ){
mHead->pUp=NULL;
}
}else{
tmp->pUp->pDown=tmp->pDown;
if ( tmp->pDown){
tmp->pDown->pUp=tmp->pUp;
}
}
delete(tmp);
//cout操作:"<<" 删除值"<<val<<"成功!"<<endl;
}else{
//cout操作:"<<" 删除值"<<val<<"不成功!"<<endl;
}
LeaveCriticalSection(&g_cs);
return bRet;
}
void Print(){
PNode tmp=mHead;
int c=0;
cout操作:"<<" 显示 列表!"<<endl;
while(tmp){
coutiVal<<"";
tmp=tmp->pDown;
c++;
}
cout操作:"<<" 显示 元素有"<<c<<"个!"<<endl;
}
PNode Find(int val,PNode pStart){
PNode Ret=NULL;
PNode tmp=pStart;
while(tmp){
if ( tmp->iVal==val){
Ret=tmp;
break;
}else{
tmp=tmp->pDown;}
}
return tmp;
}
void BJ(){
cout操作:"<<"并集 开始!"<<endl;
PNode tmp=mHead;
while(tmp){
if( Find(tmp->iVal,tmp->pDown) ){
this->Delete(tmp->iVal);
tmp=mHead;
}else {
tmp=tmp->pDown;
}
}
cout操作:"<<"并集 结束!"<<endl;
}
void JJ(){
cout操作:"<<"交集 开始!"<<endl;
PNode tmp=mHead , tmp1=mHead , tmp2=NULL;
mHead=NULL;
while(tmp){
if( Find(tmp->iVal,tmp->pDown) ){
if( ! Find(tmp->iVal,mHead) )
Append(tmp->iVal);
}
tmp=tmp->pDown;
}
tmp2=mHead;
mHead=tmp1;
Clean();
mHead=tmp2;
cout操作:"<<"交集 结束!"<<endl;
}
void Clean(){
//cout操作:"<<"清除 开始!"<<endl;
while(mHead){
if ( mHead->pDown ){
mHead=mHead->pDown;
delete(mHead->pUp);
}else{
delete(mHead);
mHead=NULL;
}
}
mHead=NULL;
//cout操作:"<<"清除 结束!"<<endl;
}
void setName(char *name){
char *tmp=name;
int c=0;
while(*tmp){
this->mName[c]=*tmp;
tmp++;
c++;
}
mName[c]='\0';
}
};
int _tmain(int argc, _TCHAR* argv[])
{
/*(3,5,8,11), B=(2,6,8,9,11,15,20*/
/*NodeList tmp,tmp1,tmp2,tmp3;
tmp.setName("链表1");
tmp.Append(3);
tmp.Append(5);
tmp.Append(8);
tmp.Append(11);
tmp1.setName("链表2");
tmp1.Append(2);
tmp1.Append(6);
tmp1.Append(8);
tmp1.Append(9);
tmp1.Append(11);
tmp1.Append(15);
tmp1.Append(20);


tmp.Print();
tmp1.Print();
tmp2.setName("链表3");
tmp2.Append(tmp.Clone());
tmp2.Append(tmp1.Clone());
tmp2.Print();
tmp2.BJ();
tmp2.Print();
tmp2.Clean();
tmp2.Print();
tmp2.Append(tmp.Clone());
tmp2.Append(tmp1.Clone());
tmp2.JJ();
tmp2.Print();*/
int k;
cin>>k;
return 0;
}

//CreateList_L.cpp
//To create a LinkList
#include
#include
#include
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2

typedef struct DuLNode
{ int data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode,*DuLinkList;
// 初始条件:L已存在。操作结果:返回L中数据元素个数
int ListLength(DuLinkList L)
{
int i=0;
DuLinkList p=L->next; // p指向第一个结点
while(p!=L) // p没到表头
{
i++;
p=p->next;
}
return i;
}

// 由双链循环线性表L的头结点出发,正序输出每个数据元素
void ListTraverse(DuLinkList L)
{
DuLinkList p=L->next;
while(p!=L)
{
coutdata<<"";
p=p->next;
}
cout<<endl;
}
// 由双链循环线性表L的头结点出发,逆序输出每个数据元素
void ListTraverseBack(DuLinkList L)
{
DuLinkList p=L->prior;
while(p!=L)
{
coutdata<<"";
p=p->prior;
}
cout<<endl;
}

//To Creatre a DuLinkList L with HeadNode
void CreateList_DuL(DuLinkList &L)
{
int n;
int i;
DuLNode *p;
L=(DuLinkList)malloc(sizeof(DuLNode));
L->next=L->prior=L;
cout<<"CreateList_L"<<endl<<"================"<<endl;
cout ";
cin>>n;

cout"<<endl;
for(i=n;i>0;--i)
{
p=(DuLinkList)malloc(sizeof(DuLNode));
cin>>p->data; //Reverse order inputing for Creating a LinkList
p->prior=L;
p->next=L->next;
L->next->prior=p;
L->next=p;
}
if(n)
{
cout<<endl;
cout<<"Success to Create a DuLinkList !"<<endl;
ListTraverse(L);
cout<<endl;
}
else cout<<"A NULL DuLinkList have been created !"<<endl;
}

//ListInsert_Dul()
int ListInsert_DuL(DuLinkList &L)
{
DuLNode *p=L,*s;
int j=0;
int i;
int e;
cout<<"======"<<"before insert:"<<"======"<<endl;
ListTraverse(L);
cout<<"input the location you want to insert:";
cin>>i;
while (iListLength(L)+1)
{
//i值不合法
cout<<"illeagle location,please input the correct location:";
cin>>i;
}

cout<<"input the number you want to insert:";
cin>>e;
while(j<i)
{ p=p->next;
++j;
}
if(!(s=(DuLinkList)malloc(sizeof(DuLNode))))
{
cout<<endl<<"Allocate space failure ! " ;
return (ERROR);
}
s->data=e;
s->prior=p->prior;
s->next=p;
if(i==1)
L->next=s;
p->prior->next=s;
p->prior=s;
cout<<"has insert:"<<e<<endl;
ListTraverse(L);
cout<<"======"<<"after insert:"<<"======"<<endl<<endl;
return (OK);
}

// 删除带头结点的双链循环线性表L的第i个元素,i的合法值为1≤i≤表长
int ListDelete(DuLinkList L)
{
DuLinkList p;
int j=1; // j为计数器
int e;
int i;

cout<<"input the location of the number you want to delete"<<endl;
cin>>i;
while(iListLength(L))
{
//i值不合法
cout<<"illeagle location,please input the correct location:";
cin>>i;
}

p=L->next; // p指向第一个结点
while(p!=L&&j<i) // 顺指针向后查找,直到p指向第i个元素或p指向头结点
{
p=p->next;
j++;
}
if(p==L||j>i) // 第i个元素不存在
{
cout<<"第i个元素不存在"<<endl;
return ERROR;
}
else
{
cout<<"======"<<"before delete:"<<"======"<<endl;
ListTraverse(L);
e=p->data; // 取第i个元素
if(!p) // p=NULL,即第i个元素不存在
return ERROR;
e=p->data;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
cout<<"has delete:"<<e<<endl;
ListTraverse(L);
cout<<"======"<<"after delete:"<<"======"<<endl<<endl;
return OK;
}

}
void menu(int c)
{
cout<<"================================================="<<endl;
cout<<"1----建立"<<endl;
cout<<"2----插入"<<endl;
cout<<"3----删除"<<endl;
cout<<"4----逆置"<<endl;
//cout<<"5----菜单"<<endl;
cout<<"0----退出"<<endl;
cout<<"================================================="<<endl;
cout <<endl<<"input you choice number:";
}
void main()
{
DuLinkList L;
int c=1;

while(c!=0)
{
switch(c)
{
case 0:
break;
case 1:
CreateList_DuL(L);
break;
case 2:
ListInsert_DuL(L);
break;
case 3:
ListDelete(L);
break;
case 4:
ListTraverseBack(L);
break;
default:
cout<<"infeasible choice,input again:"<<endl;
menu(c);
cin>>c;
}
if(c!=0)
{
menu(c);
cin>>c;
}
}
}

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

struct dllist {
int number;
struct dllist *next;
struct dllist *prev;
};

CCriticalSection mysec;

struct dllist *head, *tail;

void initial_node(struct dllist *lnode);
void append_node(struct dllist *lnode);
void insert_node(struct dllist *lnode, struct dllist *after);
void remove_node(struct dllist *lnode);

int main(void) {

struct dllist *lnode;
int i = 0;

/* add some numbers to the double linked list */
for(i = 0; i <= 5; i++) {
lnode = (struct dllist *)malloc(sizeof(struct dllist));
lnode->number = i;
append_node(lnode);
}

/* print the dll list */
for(lnode = head; lnode != NULL; lnode = lnode->next) {
printf("%d\n", lnode->number);
}

/* destroy the dll list */
while(head != NULL)
remove_node(head);

return 0;
}

void append_node(struct dllist *lnode) {
if(head == NULL) {
head = lnode;
lnode->prev = NULL;
} else {
tail->next = lnode;
lnode->prev = tail;
}

tail = lnode;
lnode->next = NULL;
}

void insert_node(struct dllist *lnode, struct dllist *after) {
lnode->next = after->next;
lnode->prev = after;

if(after->next != NULL)
after->next->prev = lnode;
else
tail = lnode;

after->next = lnode;
}

void remove_node(struct dllist *lnode) {
if(lnode->prev == NULL)
head = lnode->next;
else
lnode->prev->next = lnode->next;

if(lnode->next == NULL)
tail = lnode->prev;
else
lnode->next->prev = lnode->prev;
}

void initial_node(struct dllist *lnode) {

mysec.Lock();
//临界区锁定,进行数据处理
head = tail = NULL;

mysec.Unlock();//处理后接触临界区的锁定

}


微山县17249219726: c++ 编写一个线程安全的双向链表,要代码! -
何飘筋伤: #include <stdio.h>#include <stdlib.h>#include <afxmt.h> struct dllist { int number; struct dllist *next; struct dllist *prev; }; CCriticalSection mysec; struct dllist *head, *tail; void initial_node(struct dllist *lnode); void append_node(struct dllist *lnode); void ...

微山县17249219726: c++ 编写一个线程安全的双向链表,要代码!
何飘筋伤: #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;afxmt.h&gt; struct dllist { int number; struct dllist *next; struct dllist *prev; }; CCriticalSection mysec; struct dllist *head, *tail; void initial_node(struct dllist *lnode); void append_node(struct dllist ...

微山县17249219726: 用c++的类构造双向循环链表 -
何飘筋伤: struct Node { datatype date; Node * last, *next; // last指向上一个节点,next指向下一个节点,把多个节点连接成圈! }

微山县17249219726: 求一用C++写的线性表链式存储(双向链表)插入、删除运算的程序!!!急!!!谢谢!!! -
何飘筋伤: #include#include#include typedef struct node { int a;//data,懒得改了 struct node *prior,*next; }Lnode,*Linklist; Linklist creat(int node_number) { if(node_number return NULL; Linklist L=NULL;//Head of Linklist Linklist s,r=NULL; int x; printf("请输...

微山县17249219726: 数据结构与算法 C++ 双链表的创建 -
何飘筋伤: #include using namespace std; class node { public:int data; node *head; node *tail; public:node(){} }; class link { public:node * phead;//头指针 node * ptail;//尾指针 node * ptemp;//临时 public:void print(); link(); }; link::link()//构造循环双链表 { ...

微山县17249219726: C语言或C++实现一个双向有序链表类,可返回指定节点,可实现插入删除操作,且保持序列不变,现在要急急急 -
何飘筋伤: //CreateList_L.cpp//To create a LinkList#include <stdlib.h>#include <iostream.h>#include <conio.h># define TRUE 1# define FALSE 0# define OK 1# define ERROR 0# define INFEASIBLE -1# define OVERFLOW -2 typedef struct DuLNode { int ...

微山县17249219726: 用c++语言实现双向链表的排序 -
何飘筋伤: #include #include struct _Node { int data; struct _Node *next;};typedef struct _Node Node;// 交换两个结点的数据void SwapNodeData(Node *p1, Node *p2) { int temp = p1->data; p1->data = p2->data; p2->data= temp;}// 冒泡排序对链表进行排序...

微山县17249219726: c++ 双向链表 程序 -
何飘筋伤: #include <iostream>#include <string>using namespace std;template <class T>struct ListNode{ T value; struct ListNode<T> *next; struct ListNode<T> *prior;};template <class T>class List{private: struct ListNode<T> *head; int length;public: List() { ...

微山县17249219726: C++怎么实现list链表啊,双向指针的那种 -
何飘筋伤: 双链表结点类 struct _List_node_base { _List_node_base* _M_next; _List_node_base* _M_prev; }; template struct _List_node : public _List_node_base { _Tp _M_data; };

微山县17249219726: C++双向链表
何飘筋伤: #include <iostream> using namespace std; struct Data { Data() { no=0; } Data(int _no) { no=_no; } friend ostream & operator << (ostream & os,const Data & d){ os<<d.no; return os; } int no; }; struct Node { Node() { pNext=NULL; prior=NULL; } Data ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网