c语言中staloc是什么意思,c语言第一章.ppt

《c语言第一章.ppt》由会员分享,可在线阅读,更多相关《c语言第一章.ppt(105页珍藏版)》请在人人文库网上搜索。

1、数据结构算法 Visual C+ 6.0程序集 侯 识 忠 等编著 中国水利水电出版社,第一章 顺序存储结构的表、堆栈和队列,1、0 线性表的数组表示和实现 /线性表的类定义linelist1.h #define MaxListSize 20 #define EQUAL 1 typedef struct STU char name10; char stuno10; int age; int score; ElemType;,class List private: /线性表的数组表示 ElemType elemMaxListSize; int length; int MaxSize; publi。

2、c: /初始化顺序表 void init(List *L,int ms); /删除顺序表 void DestroyList(List ,/遍历顺序表 void ListTraverse(); /返回顺序表的长度 int ListLength(); / 获取顺序表中第i 个元素 void GetElem(int,ElemType *); / 判断顺序表两元素是否相等 bool EqualList(ElemType *,ElemType *); / 判断顺序表两元素是否不等 bool Less_EqualList(ElemType *,ElemType *); /顺序表的查找算法 bool Loc。

3、ateElem(ElemType,int); /更新线性表中的给定元素 bool UpdateList(ElemType,/线性表的实现linelist1.cpp,#include linelist1.h void List:init(List *L,int ms) *L=(List *)malloc(sizeof(List); (*L)-length=0; (*L)-MaxSize=ms; int List:ListLength() return length; ElemType List:PriorElem(ElemType cur_e,ElemType ,ElemType List:Ne。

4、xtElem(ElemType cur_e,ElemType i+),elemi-1=elemi; else /删除表尾元素 if(mark=length) return false; else e=elemi; for(j=i+1;jlength;j+) elemj-1=elemj; length-; return true; ,void List:ListTraverse() for(int i=0;ilength;i+) coutsetw(8)elemi.name; coutsetw(10)elemi.stuno; coutsetw(9)elemi.age; coutsetw(8)ele。

5、mi.scoreendl; void List:GetElem(int i,ElemType *e) *e=elemi;,bool List:EqualList(ElemType *e1,ElemType *e2) if (strcmp(e1-name,e2-name) return false; if (strcmp(e1-stuno,e2-stuno) return false; if (e1-age!=e2-age) return false; if (e1-score!=e2-score) return false; return true; ,bool List:Less_Equal。

6、List(ElemType *e1,ElemType *e2) if(strcmp(e1-name,e2-name)=0) return true; else return false; bool List:LocateElem(ElemType e,int type) int i; switch (type) case EQUAL: for(i=0;ilength;i+) if(EqualList( ,/更新线性表中的给定元素 bool List:UpdateList(ElemType,void List:MergeList(List *La,List *Lb) int i,j,La_len。

7、,Lb_len; La_len=La-ListLength();Lb_len=Lb-ListLength(); for(i=0;ielemi; for(j=0;jelemj; length=La_len+Lb_len; void List:UnionList(List *La, List *Lb) int i,La_len,Lb_len; ElemType e; La_len=La-ListLength();Lb_len=Lb-ListLength(); for(i=0;iGetElem(i,/对线性表按升序或降序输出 void List:printlist(int mark) int* b=。

8、new intlength; int i,k; cout 姓名 学号 年龄 成绩n; if(mark!=0) for(i=0; ilength;i+) bi=i; for(i=0; ilength;i+) k=i; for(int j=i+1;jlength;j+) if(mark=1i+),coutsetw(8)elembi.name; coutsetw(10)elembi.stuno; coutsetw(9)elembi.age; coutsetw(8)elembi.scoreendl; else for(i=0;ilength;i+) coutsetw(8)elemi.name; cou。

9、tsetw(10)elemi.stuno; coutsetw(9)elemi.age; coutsetw(8)elemi.scoreendl; ,/线性表的应用linelist1m.cpp #include #include #include #include #include linelist1.cpp void main() coutlinelist1m.cpp运行结果:n; ElemType e,e1,e2,e3,e4,e5,e6; List *La,*Lb,*Lc; int k; cout首先调用插入函数.n;,La-init(,strcpy(e3.stuno,100003); e3.。

10、age=19; e3.score=87; La-ListInsert(3,e3); La-printlist(0); coutListLength()init(,strcpy(e5.name,bobjin); strcpy(e5.stuno,100002); e5.age=23; e5.score=69; Lb-ListInsert(2,e5); strcpy(e6.name,stu1); strcpy(e6.stuno,100001); e6.age=22; e6.score=88; Lb-ListInsert(3,e6); Lb-printlist(0); coutListLength()。

11、endl; cin.get();,coutinit(,coutprintlist(0); cin.get(); strcpy(e.name,NoName); La-PriorElem(e2,e); if(strcmp(e.name,NoName)=0) coutNextElem(e3,e); if(strcmp(e.name,NoName)=0) coute3无后继!n; else coute3的后继e.name=e.nameendl; cin.get();,单击此处运行程序,coutprintlist(1);cin.get(); coutprintlist(-1);cin.get(); ,1。

12、、1 线性表的动态分配顺序表示和实现,/线性表的类定义linelist2.h #define EQUAL 1 #define OVERFLOW -1 #define LIST_INIT_SIZE 30 #define LISTINCREMENT 10 typedef struct STU char name10; char stuno8; int age; int score; ElemType;,/线性表的动态分配顺序存储结构 class List private: ElemType *elem;/存储空间基址 int length; /当前长度 int listsize; /当前分配的存储。

13、容量以一数据元素存储长度为单位 public: /初始化顺序表 void init(List *); /删除顺序表 void DestroyList(List ,/判断顺序表是否为空表 bool ListEmpty() if(length=0) return true; else return false; /判断顺序表是否为满 bool ListFull() return length=listsize; /决定返回表中元素pre_e的前驱 ElemType PriorElem(ElemType cur_e,ElemType ,/遍历顺序表 void ListTraverse(); /返回顺。

14、序表的长度 int ListLength(); / 获取顺序表中第i 个元素 void GetElem(int,ElemType *); / 判断顺序表两元素是否相等 bool EqualList(ElemType *,ElemType *); / 判断顺序表两元素是否不等 bool Less_EqualList(ElemType *,ElemType *); /顺序表的查找算法 bool LocateElem(ElemType,int); /更新线性表中的给定元素 bool UpdateList(ElemType,/顺序表的合并算法 void MergeList(List *,List *)。

15、; /顺序表的插入算法 bool ListInsert(int,ElemType); /顺序表的联合算法 void UnionList(List *,List *); /对线性表按升序或降序输出 void printlist(int); ; /线性表的操作linelist2.cpp void List:init(List *L) L-elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType); if(!L-elem) exit(OVERFLOW); L-length=0; L-listsize=LIST_INIT_SIZE;,int List。

16、:ListLength() return length; ElemType List:PriorElem(ElemType cur_e,ElemType ,ElemType List:NextElem(ElemType cur_e,ElemType ,length-; return true; void List:ListTraverse() for(int i=0;iname,e2-name)=0) return true; else return false; bool List:Less_EqualList(ElemType *e1,ElemType *e2) if(strcmp(e1-。

17、name,e2-name)=0) return true; else return false; ,bool List:LocateElem(ElemType e,int type) int i; switch (type) case EQUAL: for(i=0;ilength;i+) if(EqualList( ,void List:MergeList(List *La,List *Lb) ElemType *pa,*pb,*pc,*pa_last,*pb_last; pa=La-elem;pb=Lb-elem; listsize=length=La-length + Lb-length;。

18、 pc=elem=(ElemType *)malloc(listsize*sizeof(ElemType); if(!elem) exit(OVERFLOW); pa_last = La-elem + La-length - 1; pb_last = Lb-elem + Lb-length - 1; while(pa=pa_last ,bool List:ListInsert(int i,struct STU e) struct STU *p,*q; if (ilength+1) return false; q= ,void List:printlist(int mark) int* b=ne。

19、w intlength; int i,k; cout 姓名 学号 年龄 成绩n; if(mark!=0) for(i=0; ilength;i+) bi=i; for(i=0; ilength;i+) k=i; for(int j=i+1;jlength;j+) if(mark=1 ,/linelist2m.cpp #include #include #include #include #include #include linelist2.h #include linelist2.cpp void main() coutlinelist2m.cpp运行结果:n; ElemType e,e1,。

20、e2,e3,e4,e5,e6; List La,Lb,Lc; int k; cout首先调用插入函数.n;,La.init(,cout表La长:La.ListLength()endl; cin.get(); Lb.init(,e6.age=22; e6.score=88; Lb.ListInsert(3,e6); Lb.printlist(0); cout表Lb长:Lb.ListLength()endl; cin.get(); cout表La与Lb合并为表Lc:n; Lc.init(,单击此处运行程序,cout表La长:La.ListLength()endl; cin.get(); k=Lc.。

21、ListDelete(-1,e); if(k=0) cout删除失败!n; else cout删除成功!n; cout输出表Lc:n; Lc.printlist(0); cin.get(); strcpy(e.name,NoName); La.PriorElem(e2,e); if(strcmp(e.name,NoName)=0) coute2无前驱!n; else coute2的前驱e.name=e.nameendl; strcpy(e.name,NoName); La.NextElem(e3,e); if(strcmp(e.name,NoName)=0) coute3无后继!n; else。

22、 coute3的后继e.name=e.nameendl; cin.get(); cout按成绩升序输出表Lcn; Lc.printlist(1);cin.get(); cout按成绩降序输出表Lcn; Lc.printlist(-1);cin.get();,1、2 顺序堆栈的类定义(动态分配)和实现 /顺序栈的类定义stack1.h class Stack private: DataType *data; int maxSize; int top; public: /创建空栈 void SetStack(int n); /栈存在则栈被销毁 void FreeStack(); /栈存在则返回栈的。

23、元素个数,即栈的长度 int StackSize(); /判断栈是否为空 bool StackEmpty(); /判断栈是否为满 bool StackFull(); /栈存在且非空则返回栈的栈顶元素 DataType Peek();,/栈存在则插入元素item为新的栈顶元素 void Push(DataType item); /栈存在且非空则删除栈的栈顶元素并用e返回其值 DataType Pop(DataType e); /栈存在则清为空栈 void ClearStack(); / 栈的遍历 void StackTraverse(void (*visit)(DataType *); ; /顺。

24、序栈的实现stack1.cpp /#include stack1.h void Stack:SetStack(int n) data=new DataTypen; if(data=NULL) coutoverflow!n;exit(1); maxSize=n; top=-1; ,void Stack:FreeStack() free(data); int Stack:StackSize() return(top+1); bool Stack:StackEmpty() if(top=-1) return true; return false; bool Stack:StackFull() if(。

25、top=maxSize-1) return true; return false; DataType Stack:Peek() if(top=-1) cerr栈已空!n;exit(1);exit(1); return(datatop); ,void Stack:Push(DataType item) if(top=maxSize-1) cerr栈已满!n;exit(1); top+; datatop=item; DataType Stack:Pop(DataType e) if(top=-1) cerr栈已空!n;exit(1); top-; return datatop+1; void St。

26、ack:ClearStack() top=-1; void Stack:StackTraverse(void (*visit)(DataType *) while(top!=-1) visit(data);top-; ,/顺序栈的测试stack1M.cpp #include #include #include #include typedef struct STU char name10; char stuno10; int age; int score; DataType; #include stack1.h #includestack1.cpp void StackPrintElem(Da。

27、taType *e) coutnamestuno; coutage; coutscoreendl; ,void main() DataType e; Stack Sa;/clrscr(); int N=8; coutstack1M.cpp运行结果:n; cout首先调用创建空栈函数!n; Sa.SetStack(N); strcpy(e.name,stu1); strcpy(e.stuno,100001); e.age=20; e.score=87; Sa.Push(e); cout压栈后现在栈中有一个元素!n; StackPrintElem(,单击此处运行程序,cout现在栈中有二个元素!n。

28、; StackPrintElem(,1、3 顺序堆栈的类定义(动态分配)和实现 /顺序栈的类定义stack.h #define ERROR 0 #define EQUAL 1 #define OVERFLOW -1 #define STACKSIZE 100 #define STACKINCREMENT 10 class SqStack private: SElemType *base; SElemType *top; int stacksize; public: /构造一个空栈S Status InitStack(SqStack *S); /栈存在则栈被销毁 Status DestroySt。

29、ack(); /栈存在则清为空栈 Status ClearStack(); /栈存在则返回true,否则false bool StackEmpty();,/ 栈存在则返回栈的元素个数,即栈的长度 int StackLength(); /栈存在且非空则返回栈的栈顶元素 SElemType GetTop(); / 栈存在则插入元素e为新的栈顶元素 Status Push(SElemType e); / 栈存在且非空则删除栈的栈顶元素并用e返回其值 SElemType Pop(SElemType *e); / 栈的遍历 void StackTraverse(void (*visit)(SElemTy。

30、pe *); ; /顺序栈的实现stack.cpp #include stack.h Status SqStack:InitStack(SqStack *S) (*S)=(SqStack *) malloc(sizeof(SqStack); (*S)-base=(SElemType *)malloc(STACKSIZE *sizeof(SElemType); if(!(*S)-base) exit(OVERFLOW); (*S)-top=(*S)-base; (*S)-stacksize=0; return 1;,Status SqStack:DestroyStack() free(base)。

31、;return 1; Status SqStack:ClearStack() stacksize=0;return 1; bool SqStack:StackEmpty() if(stacksize=0) return true; else return false; int SqStack:StackLength() return stacksize; SElemType SqStack:GetTop() if(top=base) cerr空栈!n;exit(1); return *(top-1); Status SqStack:Push(SElemType e) *(top+)=e;sta。

32、cksize+; return 1; ,SElemType SqStack:Pop(SElemType *e) if(top=base) cerr #include #include #include #include typedef int Status ; typedef struct STU char name10; char stuno10; int age; int score; SElemType;,void StackPrintElem(SElemType *e) coutnamestuno; coutage; coutscoreInitStack(,单击此处运行程序,coutG。

33、etTop(); if(Sa-StackEmpty() coutPush(e); coutStackLength()Pop( ,1、4 顺序堆栈的类定义(数组表示)和实现 /顺序堆栈的类定义linearStack1.h const int LEN=40; class Stack private: ElemType stackLEN; int top; public: Stack()top=0; /构造函数 Stack() /析构函数 /创建有序或无序栈 void CreateStack(int,int m=LEN,int mark=0); void ClearStack(); /清空栈 boo。

34、l StackEmpty(); /检查栈是否为空 ElemType Peek(); /读取栈顶元素 /向栈中插入元素 void Push(const ElemType,/顺序堆栈的实现linearStack1.cpp #include #include linearStack1.h /创建有序或无序栈 void Stack:CreateStack(int n,int m,int mark) ElemType x,aLEN/2; int i,j; for(i=0;iaj) k=j; if(k!=i) x=ak;ak=ai;ai=x; for(i=0;im;i+) if(mark=1) Push(。

35、am-1-i);/升序 else if(mark=-1) Push(ai);/降序 else Push(rand()%100);/无序 ,/清空栈 void Stack:ClearStack() top=0; /检查栈是否为空 bool Stack:StackEmpty() return top=0; /读取栈顶元素 ElemType Stack:Peek() if(top=0) cerr栈为空!endl; exit(1); return stacktop; /向栈中插入元素 void Stack:Push(const ElemType ,/从栈中删除元素 ElemType Stack:Pop。

36、() if(top=0) cerr栈为空!endl;exit(1); top-; return stacktop+1; /检查栈是否已满 bool Stack:StackFull(int m) return top=m; /栈的输出 void Stack:StackPrint(int m) for(int i=0;im;i+) coutsetw(3)Pop(); ,/顺序堆栈的测试linearStack1m.cpp #include #include typedef int ElemType; #include linearStack1.cpp void main() coutlinearSt。

37、ack1m.cpp运行结果:n; int m=10,n=120; Stack q,p,w; q.CreateStack(n,m,1); coutn输出q栈元素(升序):n; q.StackPrint(m);coutendl; coutq栈:; if(q.StackFull(m)=1) cout已满!n; else cout未满!n; cout创建栈p(降序):n; p.CreateStack(n+10,m,-1); coutp栈:;,单击此处运行程序,if(p.StackFull(m)=1) cout已满!n; else cout未满!n; cout删除元素为:p.Pop()endl; cou。

38、tp栈:; if(p.StackEmpty()=1) cout为空!n; else cout为非空!n; cout输出p栈元素:n; p.StackPrint(m-1);coutendl; cout创建栈w(无序):n; w.CreateStack(2*n,m); cout输出w栈元素:n; w.StackPrint(m);coutendl; p.ClearStack(); cin.get();cin.get();,1、5 将中缀表达式转换为后缀表达式 /将中缀表达式转换为后缀表达式的文件zhuanhuan.cpp const int SM=40; #includelinearStack1.c。

39、pp int Precedence(char op); void zhuanhuan(char s1SM,char s2SM) /将字符串s1中的中缀表达式转换为存于字符串s2中的后缀表达式 Stack R; /定义用于暂存运算符的栈 R.Push(); /给栈底放入字符,它具有最低优先级0 int i,j; i=0; /用于指示扫描s1串中字符的位置,初值为0 j=0; /用于指示s2串中待存字符的位置,初值为0 char ch=s1i;/ch保存s1串中扫描到的字符,初值为第一个字符 while(ch!=) /顺序处理中缀表达式中的每个字符 if(ch= ) /对于空格字符不做任何处理,顺。

40、序读取下一个字符 ch=s1+i; else if(ch=() R.Push(ch);/对于左括号,直接进栈 ch=s1+i;,else if(ch=) /对于右括号,使括号内的仍停留在栈中的运算符依次 /出栈并写入到s2中 while(R.Peek()!=() s2j+=R.Pop(); R.Pop(); /删除栈顶的左括号 ch=s1+i; else if(ch=+|ch=-|ch=*|ch=/) /对于四则运算符,使暂存在栈中的不低于ch优先级 /的运算符依次出栈并写入到s2中 char w=R.Peek(); while(Precedence(w)=Precedence(ch) /Pr。

41、ecedence(w)函数返回运算符形参的优先级 s2j+=w;R.Pop();w=R.Peek(); R.Push(ch); /把ch运算符写入栈中 ch=s1+i; else /此处必然为数字或小数点字符 while(isdigit(ch)|ch=.) /把一个数值中的每一位依次写入到s2串中 s2j+=ch; ch=s1+i; /被转换后的每个数值后放入一个空格 s2j+= ; ,/把暂存在栈中的运算符依次出栈并写入到s2串中 ch=R.Pop(); while(ch!=) if(ch=() cerrexpression error!endl; exit(1); else s2j+=ch。

42、; ch=R.Pop(); /在后缀表达式的末尾放入表达式结束符和字符串结束符 s2j+=; s2j+=0; /求运算符优先级的Precedence函数为: int Precedence(char op) /返回运算符op所对应的优先级数值 switch(op) case +: case -:return 1;/定义加减运算的优先级为1 case *: case /:return 2;/定义乘除运算的优先级为2,单击此处运行程序,case (: case : /定义在栈中的左括号和栈底字符的优先级为0 default:return 0; /zhuanhuanm.cpp #include #in。

43、clude #include #include /含有int isdigit(int c)函数的原型 #include typedef char ElemType; #include zhuanhuan.cpp void main() printf(nzhuanhuanm.cpp运行结果:n); printf(输入中缀表达式:); char p140,p240; gets(p1); zhuanhuan(p1,p2); printf(n输出后缀表达式:); printf(%s,p2);cin.get(); ,1、6 十进制数转换成八进制数 /十进制数转换成八进制数convert.cpp #inc。

44、lude #include #include #include #include #include typedef int Status; typedef int SElemType; #include stack.cpp void conversion() SqStack *S; SElemType e; int n; S-InitStack(,单击此处运行程序,while(n) S-Push(n%8);n=n/8; printf(结果是:); while(!S-StackEmpty() S-Pop(,1、7 括号匹配的检验 /刮号匹配的检验kuohaopipei.cpp #include 。

45、#include #include #include typedef char Status; typedef char SElemType; #include stack.cpp void main() SqStack *p; int n,m=32,q=0; char ch,ca,cd; coutInitStack(n+),cinch; if(ch=) p-Push(ch); if(ch=() p-Push(ch); if(ch=) p-Push(ch); if(ch=) ca=p-Pop(,单击此处运行程序,if(p-StackEmpty() break; if(q=1) cout刮号序列。

46、不匹配!n; else cout刮号序列匹配!n; cin.get();cin.get();,1、8 行编辑程序 /单行编辑LINEEDIT.CPP #include #include #include #include #include #define EOFILE typedef char Status; typedef char SElemType; #include stack.cpp void LineEdit() SqStack *S,*T; char str100; int strlen=0; char e; char ch; S-InitStack(,while(ch!=EOF。

47、ILE) while(ch!=EOFILE,单击此处运行程序,while(!T-StackEmpty() T-Pop(,1、9 行编辑程序 /顺序栈的类定义stack2.h #define ERROR 0 #define EQUAL 1 #define OVERFLOW -1 #define STACKSIZE 100 #define STACKINCREMENT 10 class SqStack private: SElemType *base; SElemType *top; int stacksize; public: /构造函数 SqStack(); /栈存在则栈被销毁 Status 。

48、DestroyStack(); /栈存在则清为空栈 Status ClearStack();,/栈存在则返回true,否则false bool StackEmpty(); / 栈存在则返回栈的元素个数,即栈的长度 int StackLength(); /栈存在且非空则返回栈的栈顶元素 SElemType GetTop(); / 栈存在则插入元素e为新的栈顶元素 Status Push(SElemType e); / 栈存在且非空则删除栈的栈顶元素并用e返回其值 SElemType Pop(SElemType *e); / 栈的遍历 void StackTraverse(void (*visit。

49、)(SElemType *); ; /顺序栈的实现stack2.cpp #include stack2.h SqStack:SqStack() base=(SElemType *)malloc(STACKSIZE *sizeof(SElemType); if(!base) exit(OVERFLOW); top=base; stacksize=0; ,Status SqStack:DestroyStack() free(base);return 1; Status SqStack:ClearStack() stacksize=0;return 1; bool SqStack:StackEmpt。

50、y() if(stacksize=0) return true; else return false; int SqStack:StackLength() return stacksize; SElemType SqStack:GetTop() if(top=base) cerr空栈!n;exit(1); return *(top-1); Status SqStack:Push(SElemType e) *(top+)=e;stacksize+; return 1; ,SElemType SqStack:Pop(SElemType *e) if(top=base) cerr #include 。

51、#include #include #include #define EOFILE typedef char Status; typedef char SElemType;,#include stack2.cpp void LineEdit() SqStack S,T; char str100; int strlen=0; char e; char ch; ch=getchar(); while(ch!=EOFILE) while(ch!=EOFILE ,单击此处运行程序,if(ch=n) S.Push(ch); while(!S.StackEmpty() S.Pop(,1、10 表达式求值,。

52、/表达式求值expre.cpp #include #include #include #include #include #include #include #define EOFILE typedef char Status; typedef char SElemType; #include stack.cpp char OP10=+,-,*,/,(,),#; int precede77= 1,1,2,2,2,1,1, 1,1,2,2,2,1,1, 1,1,1,1,2,1,1, 1,1,1,1,2,1,1, 2,2,2,2,2,3,0, 1,1,1,1,0,1,1, 2,2,2,2,2,0,。

53、3;,int In(char c,char *op) int i=0; while(i; case 2: return ; case 3: return =; return A; ,char Operate(int a,char theta,int b) switch(theta) case +:return a+b-0; case -:return a-b+0; case *:return (a-0)*(b-0)+0; case /:return (a-0)/(b-0)+0; return A; char EvaluateExpression() SqStack *OPND,*OPTR; c。

54、har c,x,theta; char a,b; OPTR-InitStack( else,switch(Precede(OPTR-GetTop(),c) case Push(c); c=getchar();break; case =: OPTR-Pop( ,单击此处运行程序,void main() char i; printf(expre.cpp运行结果:n); printf(范围0,9,输入以#号结束:); i=EvaluateExpression(); coutn表达式的值是:; couti-0endl; getch(); ,1、11 顺序循环队列的类定义(数组表示)和实现,/顺序循环队。

55、列的类定义queue1.h #define QueueSize 10 typedef int datatype; class cirqueue private: int front; int rear; int count; datatype dataQueueSize; public: /构造函数 cirqueue(); / 判断队空 int queueempty(); / 判断队满 int queuefull(); / 入队(队列的插入) void enqueue(datatype); / 出队(队列的删除) datatype dequeue();,/ 取队列的头元素 datatype q。

56、ueuefront(); /求队列的长度(即队列的元素个数) int QueueLength(); ; /顺序循环队列的实现queue1.cpp #include #include queue1.h /构造函数 cirqueue:cirqueue() front=rear=count=0; / 判断队空 int cirqueue:queueempty() return count=0; / 判断队满 int cirqueue:queuefull() return count=QueueSize; / 入队(队列的插入) void cirqueue:enqueue(datatype x) /在队。

57、列中插入一元素e为Q的新队尾元素 if(queuefull() printf(队列满.n); count+;,datarear=x; rear=(rear+1)%QueueSize; /rear指针后移 / 出队(队列的删除) datatype cirqueue:dequeue() /若队列不空则删除队列头部元素并用e返回其值 datatype temp; if(queueempty() printf(队列空.n); temp=datafront; count-; /front指针后移 front=(front+1)%QueueSize; return temp; / 取队列的头元素 data。

58、type cirqueue:queuefront() if(queueempty() printf(队列空.n); return datafront;,/求队列的长度(即队列的元素个数) int cirqueue:QueueLength() return count; /顺序循环队列的应用queue1m.cpp #include #include #include #include #include queue1.cpp void main() printf(queue1m.cpp运行结果:n); cirqueue MyQueue; int i,x,m,a3; printf(输入产生随机数的种。

59、子数m:); scanf(%d,单击此处运行程序,printf(%dn,MyQueue.QueueLength(); printf(输出顺序循环队列MyQueue:n); printf(同时求任一个三位数是否为回文数.n); while(!MyQueue.queueempty() x=MyQueue.dequeue(); printf(%4d,x); a0=x%10; x=x/10;a1=x%10; x=x/10;a2=x; if(a0!=a2) printf(不是回文数!n); if(a0=a2) printf(是回文数!n); cin.get();cin.get();,1、12 顺序循环队。

60、列的类定义(动态分配)和实现,/顺序循环队列的类定义queue2.h /最大队列长度 #define QueueSize 10 typedef int datatype; class cirqueue private: /队列头指针,若队列不空则指向队列头元素 int front; /队列尾指针,若队列不空则指向队列尾元素的下一个位置 int rear; int count; /初始化的动态分配存储空间首地址 datatype *data; public: /构造函数 cirqueue(); /析构函数 cirqueue()delete data; / 判断队空 int queueempty(。

61、);,/ 判断队满 int queuefull(); / 入队(队列的插入) void enqueue(datatype); / 出队(队列的删除) datatype dequeue(); / 取队列的头元素 datatype queuefront(); /求队列的长度(即队列的元素个数) int QueueLength(); ; /顺序循环队列的实现queue2.cpp #include queue2.h /构造函数 cirqueue:cirqueue() data=new datatypeQueueSize; front=rear=count=0; / 判断队空 int cirqueue:。

62、queueempty() return count=0;,/ 判断队满 int cirqueue:queuefull() return count=QueueSize; / 入队(队列的插入) void cirqueue:enqueue(datatype x) /在队列中插入一元素e为Q的新队尾元素 if(queuefull() cerr队列满.n; count+; datarear=x; rear=(rear+1)%QueueSize; /rear指针后移 / 出队(队列的删除) datatype cirqueue:dequeue() /若队列不空则删除队列头部元素并用e返回其值 datat。

63、ype temp; if(queueempty() cerr队列空.n; temp=datafront; count-;,/front指针后移 front=(front+1)%QueueSize; return temp; / 取队列的头元素 datatype cirqueue:queuefront() if(queueempty() cerr #include #include #include #include queue2.cpp void main() coutqueue2m.cpp运行结果:n;,单击此处运行程序,cirqueue MyQueue; int i,m,x,a3; cou。

64、tm; srand(m); cout生成顺序循环队列MyQueue:n; for(i=0;iQueueSize;) x=rand()%1000; if(100=x,1、13 循环双端队列顺序表示和实现,/循环双端队列顺序表示duilie.cpp #include #include #include template class DoubleQueue int end1,end2;/end1和end2为端点 T *data; int size; public: DoubleQueue(int sz=10); DoubleQueue()delete data; void Enqueue(T item,int end); void Dequeue(int end); int IsEmpty()return end1=end2; int IsFull()retur。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值