#include<stdio.h>
#include<iostream>
using namespace std;
typedef char elemtype;
typedef struct node {
elemtype data;
struct node*next;
};
typedef struct LinkQueue {
node* front;
node* reat;
};
void initQueue(LinkQueue*q) //初始化队列
{
q->front = q->reat = (node*)malloc(sizeof(node));
if (!q->front)
cout << "initQueue error" << endl;
q->front->next = NULL;
}
void Enqueue(LinkQueue*q, elemtype e) //往队列里写入数据
{
node*p;
p = (node*)malloc(sizeof(node));
if (!q->front)
cout << "error" << endl;
p->data = e;
p->next = NULL;
q->reat->next = p;
q->reat = p;
}
int getLen(LinkQueue*q)
{
node* p;
int count = 0;
p = q->front;
while (p!=q->reat)
{
count++;
p = p->next;
}
return count;
}
int main()
{
LinkQueue q;
char e;
initQueue(&q);
cout << "input a string into a queue:" << endl;
cin >> e;
while (e != '#')
{
Enqueue(&q, e);
cin >> e;
}
cout << "hte length of the queue is:" << getLen(&q) << endl;
}