一些PV操作的问题(C/C++),哪位大大好心解答一下呀!!

[复制链接]
查看11 | 回复1 | 2010-12-2 16:28:10 | 显示全部楼层 |阅读模式
我的程序运行不了。。。。。程序如下:
#include
#include
typedef struct Qnode//定义结点
{
int data;
struct Qnode *next;
}Qnode;
typedef struct linkqueue//定义链表队列
{
int data;
struct Qnode *front,*rear;
}linkqueue;
linkqueue *ready,*stop; //创建链表队列
int sem=2;
int x;
void linkqueue_init(linkqueue *q)//初始化链表队列
{
q->front=q->rear=0;
return;
}
void Insert_linkqueue(linkqueue *q,int e) //在链表队列尾部插入元素
{
Qnode *s;
s=(Qnode *)malloc(sizeof(Qnode));
s->data=e;
s->next=NULL;
q->rear= s;
q->rear->next = s;
}
void Delete_linkqueue(linkqueue *q,int e)//出队算法
{
Qnode *s;
if(q->front==q->rear)
printf("Queue is empty\n");
else
{
s=q->front->next;
e=s->data;
q->front->next=s->next;
free(s);
if(q->front->next==NULL) q->rear=q->front;
}
}
void block(int x)//阻塞
{
Insert_linkqueue(stop,x);
// printf();
// return;
}
void wait(int x)//P操作
{
sem=sem-1;
if (semnext;
else
Insert_linkqueue(ready,x);

}
void wakeup(int x)//唤醒
{
Delete_linkqueue(stop,x);
//x=*e;
Insert_linkqueue(ready,x);
return;
}
void signal(int x)//V操作
{
sem=sem+1;
if (semdata=e;
ready++;
printf("%d\t",e);
printf("\n");}
printf("stop:");
{
int e;
stop->data=e;
stop++;
printf("%d\t",e);
printf("\n");}
}
void main()
{
//linkqueue_init(*ready);
//linkqueue_init(*stop);
wait(1);
printf();
wait(2);
printf();
wait(3);
printf();
wait(4);
printf();
wait(5);
printf();
signal(1);
printf();
signal(2);
printf();
signal(3);
printf();
signal(4);
printf();
signal(5);
printf();
printf();
getchar();
}
我觉得是插入有问题,大概。。所以又编了一个小程序:
#include
#include
typedef struct Qnode//定义结点
{
int data;
struct Qnode *next;
}Qnode;
typedef struct linkqueue//定义链表队列
{
int data;
struct Qnode *front,*rear;
}linkqueue;
linkqueue *ready,*stop; //创建链表队列
int sem=2;
int x;
void linkqueue_init(linkqueue *q)//初始化链表队列
{
q->front=q->rear=0;
return;
}
void Insert_linkqueue(linkqueue *q,int e) //在链表队列尾部插入元素
{
Qnode *s;
if(q=NULL)
{
s=(Qnode *)malloc(sizeof(Qnode));
s->data=e;
s->next=NULL;
q->front= s;
}
else
{
s=(Qnode *)malloc(sizeof(Qnode));
s->data=e;
s->next=NULL;
q->rear->next=s;
q->rear=s;
}
}
int main ()
{
int a;
linkqueue_init(ready);
Insert_linkqueue(ready,1);
a=ready->data;
printf("%d\n",a);
}
我的思想是创建一个链表队列,用队列的头尾指针指向链表队列的头和尾,方便我尾部插入头部取出。不好意思麻烦了,我实在是找不到哪里出错。。。

回复

使用道具 举报

千问 | 2010-12-2 16:28:10 | 显示全部楼层
baidu一下...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行