C++ Code:
#include<stdio.h> #include<conio.h> #include<iostream> using namespace std; //====================================================// typedef struct tagnode { int x; tagnode *pnext; }node; typedef struct stack { node *ptop; }stack; //Ham tao node node *taonode(int x) { node *p=new node; p->x=x; p->pnext=NULL; return p; } //Ham khoi tao stack rong void CreatStack(stack *k) { k->ptop=NULL; } //Ham push vao stack void PushStack(stack *k,node *p) { p->pnext=k->ptop; k->ptop=p; } //Ham pop ra stack int PopStack(stack *k) { int value; node *p=new node; p->pnext=k->ptop; k->ptop=k->ptop->pnext; value=p->x; p->pnext=NULL; delete(p); return value; } //Ham dao nguoc danh sach void DaonguocStack(stack *k) { while(k->ptop!=NULL) PopStack(k); } //============================================// void main() { node *p=new node; int i,x,n; stack *k; CreatStack(k); for(i=0;i<n;i++) { p=taonode(x); PushStack(k,p); } DaonguocStack(k); getch(); }
mình mới học stack, làm bài đảo ngựoc danh sách nhưng khi chạy lại báo lỗi, không biết sửa thế nào.mọi người xem qua với ạ
Hàm daochuoi đó để làm gì thế.
Ma chua in gi het sao biết đúng hay sai cho nào.