数据结构 关于栈队列串的编程.docx
- 文档编号:6182292
- 上传时间:2023-01-04
- 格式:DOCX
- 页数:43
- 大小:260KB
数据结构 关于栈队列串的编程.docx
《数据结构 关于栈队列串的编程.docx》由会员分享,可在线阅读,更多相关《数据结构 关于栈队列串的编程.docx(43页珍藏版)》请在冰豆网上搜索。
数据结构关于栈队列串的编程
顺序栈
#include
usingnamespacestd;
constintStackSize=100;
template
classSeqStack
{
private:
Tdata[StackSize];
inttop;
public:
SeqStack();
~SeqStack();
TGetTop();
boolEmpty();
voidPush(Tx);
TPop();
};
template
SeqStack
:
SeqStack()
{
top=-1;
}
template
SeqStack
:
~SeqStack()
{
top=-1;
}
template
TSeqStack
:
GetTop()
{
if(top==-1)throw"栈为空";
returndata[top];
}
template
boolSeqStack
:
Empty()
{
if(top==-1)
returntrue;
else
returnfalse;
}
template
voidSeqStack
:
Push(Tx)
{
if(top==StackSize-1)
throw"栈上溢";
top++;
data[top]=x;
}
template
TSeqStack
:
Pop()
{
if(top==-1)
throw"栈下溢";
Tvalue=data[top];
top--;
returnvalue;
}
voidmain()
{
cout<<"**********顺序栈的基本操作*********"< SeqStack for(inti=0;i<50;i++) S1.Push(i); cout< intcount=0; for(i=0;i<50;i++) { count++; cout< if(count%10==0) cout< } cout< } 两栈共享空间 #include usingnamespacestd; constintStackSize=100; template classBothStack { private: TData[StackSize]; inttop1; inttop2; public: BothStack(){top1=-1;top2=StackSize;} ~BothStack(){top1=-1;top2=StackSize;} TGetTop(inti); boolEmpty(inti); voidPush(inti,Tx); TPop(inti); }; template TBothStack : GetTop(inti) { if(i==1) { if(top1==-1) throw"栈1为空! "; returnData[top1]; } else { if(top2==StackSize) throw"栈2为空! "; returnData[top2]; } } template boolBothStack : Empty(inti) { if(i==1) { if(top1==-1) returntrue; else returnfalse; } else { if(top2==StackSize) returntrue; else returnfalse; } } template voidBothStack : Push(inti,Tx) { if(top2==top1+1) throw"栈上溢! "; if(i==1) { top1++; Data[top1]=x; } if(i==2) { top2--; Data[top2]=x; } } template TBothStack : Pop(inti) { if(i==1) { if(top1==-1) throw"栈1下溢"; returnData[top1--]; } if(i==2) { if(top2==StackSize) throw"栈2下溢"; returnData[top2++]; } } voidmain() { cout<<"**********两栈共享空间操作***********"< BothStack for(inti=-30;i<=-1;i++) Bs1.Push(1,i); for(intj=1;j<=30;j++) Bs2.Push(2,j); cout< (1)< intcount1=0; for(i=0;i<30;i++) { cout< (1)<<""; count1++; if(count1%10==0) cout< } cout< (1)< cout<<"************************"< cout< (2)< intcount2=0; for(i=0;i<30;i++) { cout< (2)<<""; count2++; if(count2%10==0) cout< } cout< (2)< } 链栈 #include usingnamespacestd; template structNode { Tdata; Node }; template classLinkStack { private: Node public: LinkStack() {top=NULL;} ~LinkStack(); TGetTop() { if(top! =NULL) returntop->data; else throw"链栈为空! "; } voidPush(Tx); TPop(); boolEmpty() { if(top==NULL) returntrue; else returnfalse; } }; template LinkStack : ~LinkStack() { Node while(top) { p=top->next; deletetop; top=p; } } template voidLinkStack : Push(Tx) { Node s=newNode s->data=x; s->next=top; top=s; } template TLinkStack : Pop() { if(top==NULL) throw"链栈为空! "; Tx=top->data; Node top=top->next; deletep; returnx; } voidmain() { cout<<"***********链栈基本操作************"< LinkStack for(inti=0;i<9;i++) L.Push(i); cout< for(i=0;i<9;i++) cout< cout< cout< } 循环队列 #include usingnamespacestd; constintQueueSize=10; template classCirQueue { private: Tdata[QueueSize]; intfront,rear; public: CirQueue(); ~CirQueue(){} TGetQueue(); boolEmpty(); boolFull(); intLength(); voidInsert(Tx); TDelete(); voidPrint(); }; template CirQueue : CirQueue() { front=rear=0; } template TCirQueue : GetQueue() { if(front==rear) throw"队列为空! "; inti=(front+1)%QueueSize; returnthis->data[i]; } template boolCirQueue : Empty() { if(rear==front) returntrue; else returnfalse; } template boolCirQueue : Full() { if((rear+1)%QueueSize==front) returntrue; else returnfalse; } template intCirQueue : Length() { return(this->rear-this->front+QueueSize)%QueueSize; } template voidCirQueue : Insert(Tx) { if((rear+1)%QueueSize==front) throw"队列上溢! "; rear=(rear+1)%QueueSize; data[rear]=x; } template TCirQueue : Delete() { if(rear==front) throw"队列下溢! "; front=(front+1)%QueueSize; returndata[front]; } template voidCirQueue : Print() { if(rear==front) cout<<"队列为空"< else { cout<<"***********************************"< intlength=(rear-front+QueueSize)%QueueSize; intcount=0; for(inti=(front+1)%QueueSize;;i=(i+1)%QueueSize) { if(count>=length) break; cout<<"data["< count++; if(count%5==0) cout< } cout< } } voidmain() { cout<<"************循环队列实验************"< CirQueue for(inti=1;i<=9;i++) Cq.Insert(i); Cq.Print(); Cq.Delete();Cq.Delete();Cq.Delete(); Cq.Insert(-1);Cq.Insert(-2);Cq.Insert(-3); Cq.Print(); cout< cout< cout< } 关于串的学习: #include usingnamespacestd; constintStrSize=100; classStr { public: //迫不得已,因为在类外不能引用类的私有成员 charData[StrSize]; public: Str(){} Str(chara[],intLength); ~Str(){}; intBF(charS[],charT[]); voidPrint(); }; Str: : Str(chara[],intLength) { Data[0]=Length+'0';//数字转化为字符 for(inti=1;i<=Length;i++) Data[i]=a[i-1]; } voidStr: : Print() { intLength=Data[0]-'0';//字符转化为整型数 for(inti=1;i<=Length;i++) cout< cout< } intStr: : BF(charS[],charT[]) { inti=1;intj=1; while((i<=S[0]-'0')&&(j<=T[0]-'0')) { if(S[i]==T[j]) { i++; j++; } else { i=(i-j+1)+1; j=1; } } if(j>(T[0]-'0')) return(i-j+1); else return-1; } voidmain() { Strmaster("abcdeidfdfafafa",15); master.Print(); Strsub("dfa",3); sub.Print(); cout< } 没有解决的问题: #include usingnamespacestd; voidmain() { intLen=5; constintSize=(const)Len; intnext[Size]; cout< } 错误: #include usingnamespacestd; constintStrSize=100; classStr { public: //迫不得已,因为在类外不能引用类的私有成员 charData[StrSize]; public: Str(){this->Data[0]=0+'0';} Str(chara[],intLength); ~Str(){}; intBF(charS[],charT[]); voidPrint(); voidInsert(charx);//每次在尾部插入一个字符 StrNext(chara[],intLen); }; Str: : Str(chara[],intLength) { Data[0]=Length+'0';//数字转化为字符 for(inti=1;i<=Length;i++) Data[i]=a[i-1]; } voidStr: : Print() { intLength=Data[0]-'0';//字符转化为整型数 for(inti=1;i<=Length;i++) cout< cout< } intStr: : BF(charS[],charT[]) { inti=1;intj=1; while((i<=S[0]-'0')&&(j<=T[0]-'0')) { if(S[i]==T[j]) { i++; j++; } else { i=(i-j+1)+1; j=1; } } if(j>(T[0]-'0')) return(i-j+1); else return-1; } voidStr: : Insert(charx) { intLen=(Data[0]-'0')+1; Data[0]=Len+'0'; Data[Len]=x; } StrStr: : Next(charT[],intLen) { Strnext("",Len); intk=0; for(intj=1;j<=T[0]-'0';j++) { if(j==1) next.Data[j]=0+'0'; if(j>=2) { next.Data[j]=1+'0'; for(k=2;k { Strsub1; for(intm=1;m<=k-1;m++) sub1.Insert(T[m]); Strsub2; for(intn=j-k+1;n<=j-1;n++) sub2.Insert(T[n]); //这是为了调试程序设置的 //cout< ";sub1.Print();cout<<"**";sub2.Print();cout<<"k="< //特别注意sub1.Data与sub2.Data的长度均为100,在这里只在存储 //了一些元素,后面很大一部分元素是未知的,故而二者永远不相等 /*if(sub1.Data==sub2.Data) if(k>next.Data[j]-'0')next.Data[j]=k+'0';*/ for(inti=1;i<=sub1.Data[0]-'0';i++) { if(sub1.Data[i]==sub2.Data[i]) continue; else break; } if(i>sub1.Data[0]-'0')//判断是否已经比较到两个字符串的结尾 if(k>next.Data[j]-'0')next.Data[j]=k+'0'; } } } returnnext; } voidmain() { cout<<"*************串的基本操作实验*****************"< Strmaster("abcdeidfdfafafa",15); cout<<"master: ";master.Print(); Strsub("dfa",3); cout<<"sub: ";sub.Print(); cout<<"location: "; cout< cout<<"*****求一个字符串的特征数组next[]: "< Strnext=sub.Next("5abcac",5);//特别注意此处传递子字符串的技巧 next.Print(); } #include usingnamespacestd; constintStrSize=100; classStr { public: //迫不得已,因为在类外不能引用类的私有成员 charData[StrSize]; public: Str(){this->Data[0]=0+'0';} Str(chara[],intLength); ~Str(){}; intBF(charS[],charT[]); voidPrint(); voidInsert(charx);//每次在尾部插入一个字符 StrNext(chara[],intLen); intKMP(charS[],charT[],charnext[]); }; Str: : Str(chara[],intLength) { Data[0]=Length+'0';//数字转化为字符 for(inti=1;i<=Length;i++) Data[i]=a[i-1]; } voidStr: : Print() { intLength=Data[0]-'0';//字符转化为整型数 for(inti=1;i<=Length;i++) cout< cout< } intStr: : BF(charS[],charT[]) { inti=1;intj=1; while((i<=S[0]-'0')&&(j<=T[0]-'0')) { if(S[i]==T[j]) { i++; j++; } else { i=(i-j+1)+1; j=1; } } if(j>(T[0]-
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 关于栈队列串的编程 关于 队列 编程