#include "stdafx.h"
#include <iostream>
#include <string.h>
#include <conio.h>
#define OK 1
#define ERROR -1
#define OVERFLOW 0
typedef int ElemType;
typedef int Status;
typedef struct student
{
int data;
struct student *next;
}student,*Linkstu;
typedef struct linkqueue
{
Linkstu first,rear;
}queue,*Linkqueue;
Status insert_queue(Linkqueue &HQ,ElemType e)
{
Linkstu P;
P = (Linkstu)malloc(sizeof(student));
if(!P) return OVERFLOW;
P->data = e;
P->next = NULL;
if(HQ->rear == NULL)
{
HQ->first = P;
HQ->rear = P;
}
else
{
HQ->rear->next = P;
HQ->rear = P;
}
return OK;
}
Status del_queue(Linkqueue &HQ)
{
Linkstu P;
ElemType e;
if(HQ->first == NULL) // NULL queue
{
printf("\n yichu");
}
else
{
e = HQ->first->data;
P = HQ->first;
if(HQ->first == HQ->rear) // the only one data
{
HQ->first = NULL;
HQ->rear = NULL;
}
else
{
HQ->first = HQ->first->next;
free(P);
}
}
return OK;
}
void Travese(Linkqueue &HQ,void (*visit)(ElemType e))
{
Linkstu P;
P = HQ->first;
while(P)
{
visit(P->data);
P = P->next;
}
}
void print(ElemType e)
{
printf("%d\n",e);
}
Status init_queue(Linkqueue &HQ)
{
HQ = (Linkqueue)malloc(sizeof(student));
if(!HQ) return OVERFLOW;
HQ->first = NULL;
HQ->rear = NULL;
return OK;
}
int _tmain(int argc, _TCHAR* argv[])
{
Linkqueue headQueue;
init_queue(headQueue);
for(int i=1;i<8;i++)
insert_queue(headQueue,5*i);
Travese(headQueue,print);
// delete
del_queue(headQueue);
Travese(headQueue,print);
return 0;
}
#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
using namespace std;#define OK 1
#define ERROR -1
#define OVERFLOW 0
typedef int ElemType;
typedef int Status;
typedef struct student
{
int data;
struct student *next;
}student,*Linkstu;
typedef struct linkqueue
{
Linkstu first,rear;
}queue,*Linkqueue;
Status insert_queue(Linkqueue &HQ,ElemType e)
{
Linkstu P;
P = (Linkstu)malloc(sizeof(student));
if(!P) return OVERFLOW;
P->data = e;
P->next = NULL;
if(HQ->rear == NULL)
{
HQ->first = P;
HQ->rear = P;
}
else
{
HQ->rear->next = P;
HQ->rear = P;
}
return OK;
}
Status del_queue(Linkqueue &HQ)
{
Linkstu P;
ElemType e;
if(HQ->first == NULL) // NULL queue
{
printf("\n yichu");
}
else
{
e = HQ->first->data;
P = HQ->first;
if(HQ->first == HQ->rear) // the only one data
{
HQ->first = NULL;
HQ->rear = NULL;
}
else
{
HQ->first = HQ->first->next;
free(P);
}
}
return OK;
}
void Travese(Linkqueue &HQ,void (*visit)(ElemType e))
{
Linkstu P;
P = HQ->first;
while(P)
{
visit(P->data);
P = P->next;
}
}
void print(ElemType e)
{
printf("%d\n",e);
}
Status init_queue(Linkqueue &HQ)
{
HQ = (Linkqueue)malloc(sizeof(student));
if(!HQ) return OVERFLOW;
HQ->first = NULL;
HQ->rear = NULL;
return OK;
}
int _tmain(int argc, _TCHAR* argv[])
{
Linkqueue headQueue;
init_queue(headQueue);
for(int i=1;i<8;i++)
insert_queue(headQueue,5*i);
Travese(headQueue,print);
// delete
del_queue(headQueue);
Travese(headQueue,print);
return 0;
}