这是一个利用类的继承和实现压栈及出栈,在VC 6中能过编译无语法错误,但不能显示栈链及出栈,这是很典型错误,请大家一起讨论
程序代码:
/* *********** node.h *************/ #include <iostream> using namespace std; class node { friend class linklist; friend class stack; public: node(); node(node &n); node(int i,char c='0'); node(int i,char c,node *p,node *n); ~node(); private: int idata; char cdata; node *prior; node *next; }; node::node() { cout <<"Node constructor is running..." <<endl; idata=0; cdata='0'; prior=NULL; next=NULL; } node::node(int i,char c) { cout <<"Node constructor is running..." <<endl; idata=i; cdata=c; prior=NULL; next=NULL; } node::node(int i,char c,node *p,node *n) { cout <<"Node constructor is running..." <<endl; idata=i; cdata=c; prior=p; next=n; } node::node(node &n) { idata=n.idata; cdata=n.cdata; prior=n.prior; next=n.next; } node::~node() { cout <<"Node destructor is running..." <<endl; }
程序代码:
/*************** linklist.h ****************/ #include "node.h" #include <iostream> using namespace std; class linklist { public: linklist(int i=0,char c='0'); linklist(linklist &l); ~linklist(); bool locate(int i); bool locate(char c); bool insert(int i=0,char c='0'); bool Delete(); void show(); void destroy(); protected: node head; node * pcurrent; }; linklist::linklist(int i,char c):head(i,c) { cout<<"Linklist constructor is running..."<<endl; pcurrent=&head; } linklist::linklist(linklist &l):head(l.head) { cout<<"Linklist Deep cloner running..." <<endl; pcurrent=&head; node * ptemp1=l.head.next; while(ptemp1!=NULL) { node * ptemp2=new node(ptemp1->idata,ptemp1->cdata,pcurrent,NULL); pcurrent->next=ptemp2; pcurrent=pcurrent->next; ptemp1=ptemp1->next; } } linklist::~linklist() { cout<<"Linklist destructor is running..."<<endl; destroy(); } bool linklist::locate(int i) { node * ptemp=&head; while(ptemp!=NULL) { if(ptemp->idata==i) { pcurrent=ptemp; return true; } ptemp=ptemp->next; } return false; } bool linklist::locate(char c) { node * ptemp=&head; while(ptemp!=NULL) { if(ptemp->cdata==c) { pcurrent=ptemp; return true; } ptemp=ptemp->next; } return false; } bool linklist::insert(int i,char c) { if(pcurrent!=NULL) { node * temp=new node(i,c,pcurrent,pcurrent->next); if (pcurrent->next!=NULL) { pcurrent->next->prior=temp; } pcurrent->next=temp; return true; } else { return false; } } bool linklist::Delete() { if(pcurrent!=NULL && pcurrent!=&head) { node * temp=pcurrent; if (temp->next!=NULL) { temp->next->prior=pcurrent->prior; } temp->prior->next=pcurrent->next; pcurrent=temp->prior; delete temp; return true; } else { return false; } } void linklist::show() { node * ptemp=&head; while (ptemp!=NULL) { cout <<ptemp->idata <<'\t' <<ptemp->cdata <<endl; ptemp=ptemp->next; } } void linklist::destroy() { node * ptemp1=head.next; while (ptemp1!=NULL) { node * ptemp2=ptemp1->next; delete ptemp1; ptemp1=ptemp2; } head.next=NULL; }
程序代码:
/*************** stack.h ****************/ #include "linklist.h" class stack:private linklist { public: bool push(int i,char c);// bool pop(int &i,char &c); // void show(); }; bool stack::push(int i,char c) { while(pcurrent->next != NULL) pcurrent = pcurrent->next; return insert(i,c); // } bool stack::pop(int &i,char &c) // { while (pcurrent->next != NULL) pcurrent = pcurrent ->next; i = pcurrent->idata; // c = pcurrent->cdata; // return Delete(); // } void stack::show() { show(); }
程序代码:
/***************** main.cppp ******************/ #include <iostream> #include "stack.h" int main() { stack ss; int i,j; char c; for (j=0;j<3;j++) { cout <<"请输入一个数字和一个字母:" <<endl; cin >>i >>c; if (ss.push(i,c)) { cout <<"压栈成功!" <<endl; } } //为何程序在这里 就结束了,不往下执行了!是那出错了!!请大家一起来讨论!!! ss.show(); while (ss.pop(i,c)) { cout <<"退栈数据为i=" <<i <<" c=" <<c <<endl; } return 0; }
[ 本帖最后由 fenjin1680 于 2013-7-5 07:55 编辑 ]