Queue - stack

Màu nền
Font chữ
Font size
Chiều cao dòng

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

struct node{

int info;

node *next;

};

struct stack{

node *top;

};

void creatS(stack s)

{

s.top=NULL;

}

int emptyS(stack s)

{

if(s.top==NULL) return 1;

else return 0;

}

void push(int x, stack &s)

{

node *p= new node;

p->info=x;

p->next=s.top;

s.top=p;

}

int gettop(stack s)

{

if(s.top==NULL)

{

cout<<"Stack rong";

exit(0);

}

int a=s.top->info;

return a;

}

void pop(stack &s)

{

if(s.top==NULL)

{

cout<<"Stack rong";

exit(0);

}

node *r=s.top;

s.top=s.top->next;

delete (r);

}

void cut(stack &s1, stack &s2)

{

creatS(s2);

while(!emptyS(s1))

{

int x=gettop(s2);

pop(s1);

push(x,s2);

}

}

void tao(stack &s)

{

int n,x;

creatS(s);

cout<<"Nhap n";

cin>>n;

for(int i=1;i<=n;i++)

{

cout<<"Nhap x: ";cin>>x;

push(x,s);

}

}

void xuat(stack &s)

{

stack s1;

creatS(s1);

while(!emptyS(s))

{

int x=gettop(s);

cout<<x<<"\t";

pop(s);

push(x,s1);

}

cut(s1,s);

}

void main()

{

stack s;

tao(s);

xuat(s);

}

Bạn đang đọc truyện trên: Truyen2U.Pro