本文共 997 字,大约阅读时间需要 3 分钟。
自己尝试写了一个队列。
采用递归定义。#include#include #include #include using namespace std;struct queue{ int v; //队列元素存储的值 queue *next; //队列下一个元素}*front=NULL,*back=NULL; //一个指向队首,一个指向队尾void push(int v) //入队{ if(front==NULL) { back=(queue *)malloc(sizeof(queue)); back->v=v; back->next=NULL; front=back; } else { queue *temp=(queue *)malloc(sizeof(queue)); temp->v=v; temp->next=NULL; back->next=temp; back=temp; }//printf("%d\n",(*top).v);}void pop() //出队{ if(front==NULL) return; else { queue *temp=front->next; free(front); front=temp; }//printf("%d\n",(*top).v);}int size() //测量队列长度{ int num=0; queue *p,*temp; p=front; while(p!=NULL) { num++; temp=p->next; p=temp; }//cout<<'a'< next; free(front); front=temp; }}int main(){ int n,i,m; while((cin>>n)&&n) { for(i=1;i<=n;i++) { cin>>m; push(m); //测试push()函数 } cout< < v<<' '< v< >m; push(m); }clear(); //测试clear()函数 if(empty()) //测试empty()函数 cout<<"YES"<
转载地址:http://uddci.baihongyu.com/