//队列的链式存储结构(还没写遍历)==============

#include<iostream>
using namespace std;

typedef struct qnode
{
    int  data;
    struct qnode *next ;
}qnode,*queueptr;
typedef struct
{
    queueptr front;
    queueptr rear;
}linkqueue;
///////////////////
int initqueue(linkqueue &q)
{        
    q.front=q.rear=(queueptr)malloc (sizeof(qnode));
    if(!q.front)
        return 0
    q.front->next=NULL;
    return 1;

}
//////////////////////滚动回收指针
int destroyqueue(linkqueue &q)
{
    while(q.front)
    {
        q.rear=q.front->next;
        free(q.front);
        q.front=q.rear;
    }
    
    return 1;
}
///////////////////////在队尾插入新的元素e
int enqueue(linkqueue &q,int e)
{
    
    queueptr p=(queueptr)malloc(sizeof(qnode));
    if(!p)
        return 0;
    p->data=e;
    p->next=NULL;
    q.rear->next=p;
    q.rear =p;
    
    return 1;
}
//////////////////////////////在队头删除元素????????有问题??????????
int dequeue(linkqueue &q,int &e)
{   
 
         
     
    if(q.front==q.rear )
        return 0;
    
    queueptr p=q.front->next;    
    e=p->data;    
    q.front->next=p->next;
    if(q.rear==p)
        q.rear=q.front;//当只剩下一个时需修改尾指针
    free(p);
    return 1;
}
//////////////////////////////
int createqueue(linkqueue&q,int*array,int n)
{
    int i;
    for(i=0;i<n;i++)
        enqueue(q,array[i]);
    return 1;
}
 
 

posted on 2013-02-23 14:55  叶城宇  阅读(194)  评论(0编辑  收藏  举报