直接用数组模拟就行了吧
简单写了一下:
/******************************
author : delta
version: 1.0
date : 2010-8-31
*******************************/
#include <stdio.h>
#include <memory.h>
#define debug 1
#define MaxN 1024
struct queue {
int q[MaxN]
int front
int rear
// initial the queue
void initial() {
front = 0
rear = 0
memset(q, 0, sizeof(q))
}
// judge wether the queue is empty or not
bool empty() {
return front == rear
}
// insert an element
void push(int x) {
if (rear == MaxN) {
printf("The queue is fool !\n")
}
q[rear] = x
++rear
}
// delete an element
void pop() {
if (empty()) {
printf("There is no element in the queue !\n")
}
++front
}
// look up the first element
int top() {
if (empty()) {
printf("There is no element in the queue !\n")
}
return q[front]
}
}
int main()
{
#if debug
int x
struct queue q
q.initial()
q.push(1)
q.push(2)
x = q.top()
printf("q's first elememt is: %d\n", x)
q.pop()
printf("q is %s\n", q.empty()?"empty":"not empty")
q.push(3)
x = q.top()
printf("q's first element is: %d\n", x)
q.initial()
printf("q is %s\n", q.empty()?"empty":"not empty")
#endif
return 0
}
我的理解是 你想用数组模拟 队列?不是的话下面不用看,回复我给我再说一下题意,我重新给你写!首先输入一个操作,1入队,2出队,3退出
如果是1,再输入一个将要入队列的 数据,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 1000
int queue[LEN], fir, end
void printQueue()
{
int i = 0
for(i = firi <end++ i)
{
printf("%d ", queue[i])
}
printf("\n")
}
void insertQueue()
{
int value = 0, i = 0
printf("Enter the data which you want to insert to queue...\n")
scanf("%d", &value)
queue[end ++] = value
printQueue()
}
void deleteQueue()
{
printf("after delete the top data!\n")
fir ++
printQueue()
}
void demo()
{
int Number = 0
while(1)
{
printf("Enter the number:\n")
printf("1.insert...\n")
printf("2.delete...\n")
printf("3.exit!\n")
scanf("%d", &Number)
if(Number <1 || Number >4) return
switch(Number)
{
case 1: insertQueue()break
case 2: deleteQueue()break
case 3: exit(0)
default: return
}
}
}
void creatQueue()
{
int i = 0
fir = 0, end = 0
for(i = 0i <LEN++ i)
{
queue[i] = 0
}
}
int main()
{
creatQueue()
demo()
return 0
}
自行比对这两个函数吧
void insert_link(struct linkqueue *ps,int val)//完成队列的增加。{
struct node * pnew=(struct node*)malloc(sizeof(struct node))//申请一个节点
pnew->data=val//将要放入队列的值赋给节点的数据域
pnew->next=NULL
// pnew=ps->rear->next//将rear指向新的节点,并将新的节点的指针域置空。
ps->rear->next=pnew
ps->rear=ps->rear->next
// pnew->next=NULL
}
void traverse_link(struct linkqueue *ps)//完成队列遍历
{
struct node *p=ps->front->next//申请一个临时指针变量,以完成队列遍历。因为头节点没有存放数据所以让指针指向front下一个节点
while(/*p->next*/p!=NULL) { //当指针指向的节点的指针域不为空时就继续下移,并且输出本节点的数据
printf("%d ",p->data)
p=p->next
}
}