|
发表于 2004-5-20 17:39:10
|
显示全部楼层
无法运行,修改了一下:
[code:1] #include <stdio.h>
typedef struct linked_queue
{
int data;
struct linked_queue *next;
} LqueueTp;
typedef struct queueptr
{
LqueueTp *front, *rear;
} QueptrTp;
int main()
{
QueptrTp *lq;
int input;
int i = 0;
int outnum;
//我认为这儿要加一条,因为你只要一个指针变量lq,并没有指向一个
//QueptrTp节点,你却在InitQueue中通过取地址想要使用这个
//QueptrTp节点。
lq=(QueptrTp*)malloc(sizeof(QueptrTp));
InitQueue(&lq);
printf("请输入一些数字:");
scanf("%d", &input);
while (input != 0)
{
EnQueue(&lq, input);
scanf("%d", &input);
}
GetHead(&lq, &input);
printf("当前链接列队的队头为:%d", input);
printf("\n请输入要出队的个数:");
scanf("%d", &outnum);
printf("依此出队的数为:");
while (i < outnum)
{
OutQueue(&lq, &input);
printf("%d ", input);
i++;
}
printf("\n当前列队排列:");
while (! EmptyQueue(&lq))
{
OutQueue(&lq, &input);
printf("%d ", input);
}
printf("\n\n");
free(lq);
}
int InitQueue (QueptrTp **lq)
{
LqueueTp *p;
p = (LqueueTp *) malloc (sizeof(LqueueTp));
(*lq) -> front = p;
(*lq) -> rear = p;
((*lq) -> front) -> next = NULL;
}
int EnQueue (QueptrTp **lq, int x)
{
LqueueTp *p;
p = (LqueueTp *) malloc (sizeof(LqueueTp));
p -> data = x;
p -> next = NULL;
((*lq) -> rear) -> next = p;
(*lq) -> rear = p;
}
int OutQueue (QueptrTp **lq, int *x)
{
LqueueTp *s;
if ((*lq) -> front == (*lq) -> rear)
{
printf("队空!\n");
return 0;
}
else
{
s = ((*lq) -> front) -> next;
*x = s -> data;
((*lq) -> front) -> next = s -> next;
if (s -> next == NULL)
(*lq) -> rear = (*lq) -> front;
free(s);
return 1;
}
}
int EmptyQueue (QueptrTp **lq)
{
if (((*lq) -> rear) == ((*lq) -> front))
return 1;
else
return 0;
}
int GetHead (QueptrTp **lq, int *x)
{
LqueueTp *p;
if ((*lq) -> rear == (*lq) -> front)
return 0;
else
{
p = ((*lq) -> front ) -> next;
*x = p -> data;
return 1;
}
} [/code:1]
另,加上-Wall参数有许多不合理的地方。 |
|