资源预览内容
第1页 / 共17页
第2页 / 共17页
第3页 / 共17页
第4页 / 共17页
第5页 / 共17页
第6页 / 共17页
第7页 / 共17页
第8页 / 共17页
第9页 / 共17页
第10页 / 共17页
亲,该文档总共17页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
堆栈、队列、字符串匹配相关算法 C+实现一、堆栈.cpp 部分#include#includestack.husing namespace std;int main ()int c;coutc;astack STA1(c);int t;coutt;STA1.push(t);int ch;coutch;while(ch!=-1)switch(ch)case 1:int re1;STA1.pop(re1);coutr;STA1.push(r);break;case 4:STA1.print();break;case 5:int s;couts;STA1.setsize(s); break;coutch;if(ch=-1)couttemplateclass astack /顺序堆栈/private:int size;T * stackarray;int top;int maxstacksize;public:astack(int s )maxstacksize=100;size=s;stackarray=new T maxstacksize;top=-1;astack()delete stackarray;bool push(const T& item)if(isfull() coutvoid astack:print()for(int i=0;i#includequeue.husing namespace std;int main () linkqueue que1;couts;coutc;que1.qinsert(c);int ch;coutch;while(ch!=-1)switch(ch)case 1:int re1;couttemp;que1.qinsert(temp);break;coutch;if(ch=-1)couttemplatestruct SLNodeT data;SLNode * next;SLNode( SLNode * nextnode=NULL)next=nextnode;SLNode(const T& item , SLNode * nextnode=NULL)data=item; next=nextnode;templateclass linkqueue/ 链式队列/private:SLNode * front, * rear;int count;public:linkqueue()front=NULL;rear=NULL;linkqueue()qclear();void qinsert(const T bool qdelete(T int isempty()constreturn front=NULL;void qclear();void print();templatevoid linkqueue:qinsert(const T&item)if(isempty()front=rear=new SLNode(item,NULL);count=1;else rear-next=new SLNode(item,NULL);rear=rear-next;count+;template bool linkqueue: qdelete(T& item)if(isempty()cout* temp=front;item=front-data;front=front-next;count-;delete temp;if(count=0)rear=NULL;return true;template bool linkqueue: qget(T& item)if(isempty()coutdata;return true;template void linkqueue:qclear()while(!isempty()rear=front;front=front-next;delete rear;count-;rear=NULL;templatevoid linkqueue:print()SLNode * p=front;while(p-next!=NULL)coutdatanext;coutdata#includeChoice.husing namespace std;int main ()char re;SLList list;coutre;while(re!=#)list.add(re);cinre;list.listout();check(list);return 0;.h 部分#includetemplatestruct SLNodeT data;SLNode * next;SLNode( SLNode * nextnode=NULL)next=nextnode;SLNode(const T next=nextnode;templateclass SLListprivate:SLNode * head, *tail ,*guard;int size;public:SLList();SLList(TSLList();bool isempty()return head-next=NULL;int lenth();void add(Tbool get(int k, Tvoid listout();void match();templateSLList:SLList()head=tail=guard=new SLNode();size=0;templateSLList:SLList(T& item)tail=guard=new SLNode(item,NULL);head=new SLNode(guard);size=1;templateSLList:SLList()while(!isempty()guard=head;head=guard-next;delete guard;delete head;templateint SLList:lenth()return size;templatevoid SLList:add(T& item)tail-next=new SLNode (item,tail-next);tail=tail-next; size+;templatevoid SLList:listout()if(isempty()coutnext;while(guard-next!=NULL)coutdatanext;i+;coutdatavoid SLList:match()char s150;char s250;int i=0, j=0;int rei ,rej;guard=head;while(guard-next!=NULL)if(guard-data=|guard-data=|guard-data=()s1i=guard-data;i+;guard=guard-next;else if(guard-data=|guard-data=|guard-data=)s2j=guard-data;j+;guard=guard-next;else guard=guard-next;if(guard-data=|guard-data=|guard-data=()s1i=guard-data;i+;if(guard-data=|guard-data=|guard-data=)s2j=guard-data;j+;rei=i;rej=j;int count=0;for(i=0,j=rej-1;i=0;)if(s1i=(&s2j=)|(s1i=&s2j=)|(s1i=&s2j=)i+;j-;count+;if(count=reielse coutvoid check(SLList & list)list.match();
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号