대기 열 데이터 구조 작업 (2)

지루 한 숙제 를 시간 을 들 여 썼 으 니 마음대로 붙 여 라....................................................가장 간단 한 실현...
/**       :   :1.push 2.pop 3.traverse 4.free 5.rebuild -1   2     ,  pop   :1.push 2.pop 3.traverse 4.free 5.rebuild -1   1 input (type) a,a==-1 to end 23 input (type) a,a==-1 to end 345 input (type) a,a==-1 to end 56 input (type) a,a==-1 to end 34 input (type) a,a==-1 to end 45 input (type) a,a==-1 to end -1   :1.push 2.pop 3.traverse 4.free 5.rebuild -1   2 pop(23)   :1.push 2.pop 3.traverse 4.free 5.rebuild -1   2 pop(345)   :1.push 2.pop 3.traverse 4.free 5.rebuild -1   3 traverse: 56 34 45   :1.push 2.pop 3.traverse 4.free 5.rebuild -1   4 free:   :1.push 2.pop 3.traverse 4.free 5.rebuild -1   5 rebuild:   :1.push 2.pop 3.traverse 4.free 5.rebuild -1   1 input (type) a,a==-1 to end 23 input (type) a,a==-1 to end -1   :1.push 2.pop 3.traverse 4.free 5.rebuild -1   -1 */

#include<iostream>
#include<cstdlib>
using namespace std;
typedef int type;

struct Queue
{
    type data;
    Queue *next;
};

Queue *head;   //   
Queue *cur;    //     
void Init()
{
    head=(Queue *)malloc(sizeof(Queue));
    cur=head;
    cur->next=NULL;
}

void push(type a)
{
    cout<<"input (type) a,a==-1 to end"<<endl;
    Queue *add;    //    
    add=(Queue *)malloc(sizeof(Queue));
    add->data=a;
    cur->next=add;
    cur=add;
    cur->next=NULL;
}

void pop()
{
    if(head->next!=NULL)
    {
        cout<<"pop("<<head->next->data<<")"<<endl;
        head->next=head->next->next;
    }
    else
        cout<<"    ,  pop"<<endl;
}

void traverse()
{
    cout<<"traverse:"<<endl;
    Queue *p=head;
    while(p->next!=NULL)
    {
        p=p->next;
        cout<<p->data<<" "<<endl;
    }
}

void free_queue()
{
    Queue *p;
    while(head!=NULL)
    {
        p=head;
        head=head->next;
        free(p);
    }
}

int main()
{
    cout<<"  :1.push 2.pop 3.traverse 4.free 5.rebuild -1  "<<endl;
    int a;
    type b;
    Init();
    while(cin>>a&&a!=-1)
    {
        switch(a)
        {
            case 1:{cout<<"input (type) a,a==-1 to end"<<endl;while(cin>>b&&b!=-1){push(b);};break;};
            case 2:{pop();break;};
            case 3:{traverse();break;};
            case 4:{cout<<"free:"<<endl;free_queue();break;};
            case 5:{cout<<"rebuild:"<<endl;Init();break;}
            default:cout<<"please input 1 or 2 or 3 or -1"<<endl;
        }
        cout<<"  :1.push 2.pop 3.traverse 4.free 5.rebuild -1  "<<endl;
    }
    free_queue();
    return 0;
}

좋은 웹페이지 즐겨찾기