文档库 最新最全的文档下载
当前位置:文档库 › C语言实现队列的基本操作

C语言实现队列的基本操作

C语言实现队列的基本操作
C语言实现队列的基本操作

#define TRUE 1

#define FALSE 0

#define NULL 0

#define OK 1

#define OVERFLOW 0

#define ERROR 0

typedef int QElemType;

typedef int Status;

typedef struct QNode

{ QElemType data;

QNode *next;

}*QueuePtr;

struct LinkQueue

{ QueuePtr front,rear;//队头,队尾指针

};

//函数列表

void InitQueue(LinkQueue &Q);//构造一个空队列

void DestoryQueue(LinkQueue &Q);//销毁队列

void ClearQueue(LinkQueue &Q);//清空队列

Status QueueEmpty(LinkQueue Q);//判断队列是否为空

int QueueLength(LinkQueue Q);//求队列的长度

Status GetHead(LinkQueue Q,QElemType &e);//返回队头元素

void EnQueue(LinkQueue &Q,QElemType e);//插入元素为新的队尾元素Status DeQueue(LinkQueue &Q,QElemType &e);//删除队头元素

void QueueTraverse(LinkQueue Q,void(*visit)(QElemType));

void print(QElemType e);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////

基本函数操作bo3_2.cpp

#include

#include

#include

#include"c3_2.h"

void InitQueue(LinkQueue &Q)

{//初始化一个队列

Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));

if(!Q.front)//生成头结点失败

exit(OVERFLOW);

Q.front->next=NULL;

}

void DestoryQueue(LinkQueue &Q)

{ //销毁队列

while(Q.front)

{ Q.rear=Q.front->next;//Q.rear指向Q.front的下一个结点

free(Q.front);//释放Q.front所指结点

Q.front=Q.rear;//Q.front指向Q.front的下一个结点

}

}

void ClearQueue(LinkQueue &Q)

{ //将队列清为空

DestoryQueue(Q);//销毁队列

InitQueue(Q);//重新构造队列

}

Status QueueEmpty(LinkQueue Q)

{ //判断队列是否为空

if(Q.front->next==NULL)

return TRUE;

else return FALSE;

}

int QueueLength(LinkQueue Q)

{ //求队列的长度

int i=0;//计数器清0

QueuePtr p=Q.front;//p指向结点

while(Q.rear!=p)//p所指向的不是尾结点

{ i++;//计数器加1

p=p->next;

}

return i;

}

Status GetHead(LinkQueue Q,QElemType &e) { //若队列不空,则用e返回队头元素QueuePtr p;

if(Q.front==Q.rear) return ERROR;

p=Q.front->next;//p指向队头结点

e=p->data;//将队头元素的值赋给e

return OK;

}

void EnQueue(LinkQueue &Q,QElemType e)

{ //插入元素e为队列Q的新的队尾元素

QueuePtr p;

p=(QueuePtr)malloc(sizeof(QNode));

//动态生成新结点

if(!p)

exit(OVERFLOW);

p->data=e;//将e的值赋给新结点

p->next=NULL;//新结点的指针为空

Q.rear->next=p;//原队尾结点的指针域为指向新结点Q.rear=p;//尾指针指向新结点

}

Status DeQueue(LinkQueue &Q,QElemType &e) { //若队列不为空,删除Q的队头元素,用e返回其值QueuePtr p;

if(Q.front==Q.rear)//队列为空

return ERROR;

p=Q.front->next;//p指向队头结点

e=p->data;//队头元素赋给e

Q.front->next=p->next;//头结点指向下一个结点

if(Q.rear==p)//如果删除的队尾结点

Q.rear=Q.front;//修改队尾指针指向头结点

free(p);

return OK;

}

void QueueTraverse(LinkQueue Q,void(*visit)(QElemType))

{ //对队头到队尾依次对队列中每个元素调用函数visit()

QueuePtr p=Q.front->next;

while(p)

{ visit(p->data);//对p所指元素调用visit()

p=p->next;

}

printf("\n");

}

void print(QElemType e)

{ printf("%2d",e);

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////

主函数文件fun3_2.cpp

#include

#include"c3_2.h"

void main()

{ int i,k;

QElemType d;

LinkQueue q;

InitQueue(q);//构造一个空栈

for(i=1;i<=5;i++)

{ EnQueue(q,i);

}

printf("栈的元素为:");

QueueTraverse(q,print);

k=QueueEmpty(q);

printf("判断栈是否为空,k=%d(1:为空9;0:不为空)\n",k);

printf("将队头元素赋给d\n");

k=GetHead(q,d);

printf("队头元素为d=%d\n",d);

printf("删除队头元素:\n");

DeQueue(q,d);

k=GetHead(q,d);

printf("删除后新的队头元素为d=%d\n",d);

printf("此时队列的长度为%d\n",QueueLength(q));

ClearQueue(q);//清空队列

printf("清空队列后

q.front=%u,q.rear=%u,q.front->next=%u\n",q.front,q.rear,q.front->next); DestoryQueue(q);

printf("销毁队列后,q.front=%u,q.rear=%u\n",q.front,q.rear);

}

c++,链队列的基本操作(创建,销毁,查找,删除,插入等)

链队列的基本操作(创建,销毁,查找,删除,插入等)#include using namespace std; const bool TRUE=1 ; const bool FALSE=0; typedef int QElemType; typedef struct LNode { QElemType data; struct LNode *next; }LNode ,*LinkList; typedef LinkList QueuePtr; typedef struct{ QueuePtr front; QueuePtr rear; }LinkQueue; void InitQueue_L(LinkQueue &Q) {//构造一个只有头结点的空队列Q Q.front=Q.rear=new LNode; Q.front->next=NULL; }//InitQueue_L void DestroyQueue_L(LinkQueue &Q) { //销毁链队列结构Q while(Q.front){ Q.rear=Q.front->next; delete Q.front; Q.front=Q.rear; }//while }// DestroyQueue_L bool QueueEmpty_L(LinkQueue Q) {//判断队列是否为空,是则返回TRUE,否则返回FALSE if(Q.front==Q.rear)return TRUE; else return FALSE; }//QueueEmpty int QueueLength(LinkQueue Q)

数据结构(C语言)队列的基本操作

实验名称:实验四队列的基本操作 实验目的 掌握队列这种抽象数据类型的特点及实现方法。 实验内容 从键盘读入若干个整数,建一个顺序队列或链式队列,并完成下列操作: (1)初始化队列; (2)队列是否为空; (3)出队; (4)入队。 算法设计分析 (一)数据结构的定义 单链表存储结构定义为: struct Node; //链表单链表 typedef struct Node *PNode; int dui; dui =1; struct Node { int info; PNode link; }; struct LinkQueue { PNode f; PNode r; }; typedef struct LinkQueue *PLinkQueue; (二)总体设计 程序由主函数、创建队列函数、判断是否为空队列函数、入队函数、出队函数、取数函数、显示队列函数、菜单函数组成。其功能描述如下: (1)主函数:调用各个函数以实现相应功能 main() { PLinkQueue a; //定义链表a int b,c,e; //b 菜单选择c选择继续输入e输入元素 do { //菜单选择 mune(); scanf("%d",&b);

switch(b) { case 1://初始化 a=create(); //初始化队列 case 2: //入队 do { printf("\n请输入需要入队的数:"); if(e!=NULL) { scanf("%d",&e); enQueue(a,e); } printf("是否继续入队?(是:1 否:0)\n"); scanf("%d",&c); } while(c==1); break; case 3: //出队 c=frontQueue(a); deQueue(a); if(dui!=0) { printf("\n出队为:%d\n",c); } dui=1; break; case 4: //显示队中元素 showQueue(a); break; case 5: return; default: printf("输入错误,程序结束!\n"); return; } } while(a!=5); { return 0; } } (三)各函数的详细设计: Function1: PLinkQueue create(void)//创队

C语言数组编程题

实验4 数组 一.实验目的: 1.掌握一维数组的定义、赋值和输入输出的方法; 2.掌握字符数组定义、初始化、赋值的方法; 3.了解常见的字符串函数功能及其使用方法; 4.掌握二维数组的定义与引用。 二.实验内容: 1.编写程序,输入10个整数存入一维数组,统计输出其中的正数、负数和零的个数。 2.编写程序,输入10个整数存入一维数组,再按逆序重新存放后再输出。 3.编写程序,输入10个整数存入一维数组,对其进行升序排序后输出。 4.编写程序,求二维数组中元素的最大值和最小值。 5.编写程序,求一个4×4矩阵中所有元素之和。 6.编写程序:从键盘上输入一字符串,统计输出该字符串中的字母字符、数字字符、空格以及其他字符的个数。 7.编写程序:从键盘上输入一字符串,并判断是否形成回文(即正序和逆序一样,如“abcd dcba”)。 8. 产生一个由10个元素组成的一维数组并输出,数组元素由随机数(0-99)构成。 9. 产生一个由10个元素组成的一维数组,数组元素由随机数(0-99)构成。按照升序排列并输出。再输入一个数,按原来的规律将其插入并输出。 页脚内容1

10. 产生一个由10个元素组成的一维数组,数组元素由随机数(0-99)构成。按照升序排列并输出。再输入一个数,要求找出该数是数组中的第几个元素,如果不在数组中,则输出找不到。 11. 找出一个二维数组中的鞍点,即该位置上的元素在该行最大,在该列最小。可能没有鞍点。 12. 编程输出杨辉三角。(要求输出10行)(杨辉三角:每行端点与结尾的数为1.每个数等于它上方两数之和。每行数字左右对称,由1开始逐渐变大) 13. 输入一行字符,统计大写字母、小写字母、数字、空格以及其它字符个数。 14. 编写程序,将两个字符串连接起来,不用strcat。 15. 编写程序实现strcpy函数功能。 16. 编程实现strlen函数功能。 17. 编程求2-4+6-8…-100+102的值。 18. 假设某人有100,000现金。每经过一次路口需要进行一次交费。交费规则为当他现金大于50,000时每次需要交5%如果现金小于等于50,000时每次交5,000。请写一程序计算此人可以经过多少次这个路口。 19. 输入若干个正整数,以0结束,将其中大于平均值且个位为5的数排序后输出。(按由大到小的顺序排序) 20. 输入一个字符串,将其中ASCII码值为基数的字符排序后输出。(按由小到大的顺序) 21. 输入一个以回车结束的字符串(少于80个字符),滤去所有的非16进制字符后,组成一个新字符串(16进制形式),然后将其转换为10进制数后输出。 22. 读入一个正整数n(1<=n<=6),再读入n阶矩阵,计算该矩阵除副对角线、最后一行、最后一列 页脚内容2

队列的基本操作代码

队列的基本操作代码: #include #include #define MAXQSIZE 100 #define OVERFLOW 0 #define ERROR 0 #define OK 1 typedef int QElemType; typedef int Status; typedef struct { QElemType *base; int front; int rear; int tag; }SqQueue; Status InitQueue(SqQueue &Q) { Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType)); if(!Q.base) exit(OVERFLOW);//存储分配失败 Q.front=Q.rear=0; tag=0; return OK; } int QueueLength(SqQueue Q) { return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;//返回Q的元素个数,即队列的长度} Status EnQueue(SqQueue &Q,QElemType e) { if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;//队列满 Q.base[Q.rear]=e; Q.rear=(Q.rear+1)%MAXQSIZE; return OK; } Status DeQueue(SqQueue &Q,QElemType &e) { if(Q.front==Q.rear) return ERROR; e=Q.base[Q.front];

栈和队列的基本操作

《数据结构与算法》实验报告 专业班级学号 实验项目 实验二栈和队列的基本操作。 实验目的 1、掌握栈的基本操作:初始化栈、判栈为空、出栈、入栈等运算。 2、掌握队列的基本操作:初始化队列、判队列为空、出队列、入队列等运算。 实验容 题目1: 进制转换。利用栈的基本操作实现将任意一个十进制整数转化为R进制整数 算法提示: 1、定义栈的顺序存取结构 2、分别定义栈的基本操作(初始化栈、判栈为空、出栈、入栈等) 3、定义一个函数用来实现上面问题: 十进制整数X和R作为形参 初始化栈 只要X不为0重复做下列动作 将X%R入栈 X=X/R 只要栈不为空重复做下列动作 栈顶出栈输出栈顶元素 题目2: 利用队列的方式实现辉三角的输出。 算法设计分析 (一)数据结构的定义 1、栈的应用 实现十进制到其他进制的转换,该计算过程是从低位到高位顺序产生R进制数的各个位数,而打印输出一般从高位到低位进行,恰好与计算过程相反。因此,运用栈先进后出的性质,即可完成进制转换。 栈抽象数据结构描述 typedef struct SqStack /*定义顺序栈*/ { int *base; /*栈底指针*/ int *top; /*栈顶指针*/ int stacksize; /*当前已分配存储空间*/ } SqStack;

2、队列的应用 由于是要打印一个数列,并且由于队列先进先出的性质,肯定要利用已经进队的元素在其出队之前完成辉三角的递归性。即,利用要出队的元素来不断地构造新的进队的元素,即在第N行出队的同时,来构造辉三角的第N+1行,从而实现打印辉三角的目的。 队列抽象数据结构描述 typedef struct SeqQueue { int data[MAXSIZE]; int front; /*队头指针*/ int rear; /*队尾指针*/ }SeqQueue; (二)总体设计 1、栈 (1)主函数:统筹调用各个函数以实现相应功能 int main() (2)空栈建立函数:对栈进行初始化。 int StackInit(SqStack *s) (3)判断栈空函数:对栈进行判断,若栈中有元素则返回1,若栈为空,则返回0。 int stackempty(SqStack *s) (4)入栈函数:将元素逐个输入栈中。 int Push(SqStack *s,int x) (5)出栈函数:若栈不空,则删除栈顶元素,并用x返回其值。 int Pop(SqStack *s,int x) (6)进制转换函数:将十进制数转换为R进制数 int conversion(SqStack *s) 2、队列 (1)主函数:统筹调用各个函数以实现相应功能 void main() (2)空队列建立函数:对队列进行初始化。 SeqQueue *InitQueue() (3)返回队头函数:判断队是否为空,若不为空则返回队头元素。 int QueueEmpty(SeqQueue *q) (4)入队函数:将元素逐个输入队列中。 void EnQueue(SeqQueue *q,int x) (5)出队函数:若队列不空,则删除队列元素,并用x返回其值。 int DeQueue(SeqQueue *q) (6)计算队长函数:计算队列的长度。 int QueueEmpty(SeqQueue *q) (7)输出辉三角函数:按一定格式输出辉三角。 void YangHui(int n)

c语言编程有关数组的几道例题

实验四一维数组、二维数组 一、实验目的与要求 1、熟练掌握一维数组、二维数组的定义、赋值和输入输出的方法。 2、掌握与数组有关的算法。 二、实验内容 1、(1)输入N个整数,使用冒泡排序,将数据由大到小输出。 #include "stdafx.h" #include void swap2(int*,int*); void bubble(int a[],int n); int main(void) { int n,a[8]; int i; printf("Enter n(n<=8):"); scanf("%d",&n); printf("Enter a[%d]:", n); for(i=0;ia[j+1]) swap2(&a[j],&a[j+1]); /*交换*/ } void swap2(int *px,int *py) { int t; t=*px; *px=*py; *py=t; } 单向冒泡排序法: //输入10个整数,按从大到小输出// #include

队列的基本操作及其应用

广西工学院计算机学院 《数据结构》课程实验报告书实验四队列的基本操作及其应用 学生姓名:李四 学号:2012 班级:计Y124 指导老师:王日凤 专业:计算机学院软件学院 提交日期:2013年6月20日

1.实验目的 1)通过对队列特点的分析,掌握队列的存储结构及其基本操作,学会定义队列的顺序存储结构和链式存储结构,在实际问题中灵活运用。 2)掌握队列先进先出的特点,掌握队列的基本操作,如出队列、入队列、判队列空、判队列满等,熟悉各种操作的实现方法。 3)通过具体的应用实例,进一步熟悉和掌握队列的实际应用。 2.实验内容 (1)建立一个含n个数据的队列,实现队列的基本操作。包括: ?//1. 初始化,构造一个空队列 void initQueue(Queue &Q) ?//2. 判断队列空, 空则返回true bool QueueEmpty(seqQueue &Q) ?//3. 判断队列满, 满则返回true bool QueueFull(seqQueue &Q) ?//4. 取队头元素, 用x返回队头元素,返回true;空队列则返回false Bool QueueHead(seqQueue &Q, elementType &x) ?//5. 入队列,在队尾插入新元素x (流程图) bool pushQueue (seqQueue &Q, elementType x) ?//6. 出队列,用x带回队头元素,并在队头删除,返回true,队列空则返回false(流程图)bool popQueue (seqQueue &Q, elementType &x) ?//7. 输出队列,从队头到队尾依次输出 void printQueue(seqQueue Q) (2)队列应用:利用队列操作打印杨辉三角形的前n行(如n=7)。 3.实验要求 (1)上机前交实验源程序(纸质版),由学习委员统一收好交老师(附上不交同学名单)。 (2)用一切你能想到的办法解决遇到的问题,培养解决问题的能力。 (3)实验课上进行答辩。 (4)实验报告当场交。报告内容包括:实验目的、实验内容、实验代码、实验输入输出结果以及实验体会供五部分。

栈和队列的基本操作的实现

封面: 安徽大学 网络工程 栈和队列的基本操作的实现 ______2010\4\12

【实验目的】 1.理解并掌握栈和队列的逻辑结构和存储结构; 2.理解栈和队列的相关基本运算; 3.编程对相关算法进行验证。 【实验内容】 (一)分别在顺序和链式存储结构上实现栈的以下操作(含初始化,入栈,出栈,取栈顶元素等): 1.构造一个栈S,将构造好的栈输出; 2.在第1步所构造的栈S中将元素e 入栈,并将更新后的栈S输出; 3.在第2步更新后所得到的栈S中将栈顶元素出栈,用变量e返回该元素,并将更新后的栈S输出。(二)分别在链队列和循环队列上实现以下操作(初始化,入队,出队,取队头元素等): 1.构造一个队列Q,将构造好的队列输出; 2.在第1步所构造的队列Q中将元素e入队,并将更新后的队列Q输出; 3.在第2步更新后所得到的队列Q中将队头元素出队,用变量e返回该元素,并将更新后的队列Q输出。

【要求】 1.栈和队列中的元素要从终端输入; 2.具体的输入和输出格式不限; 3.算法要具有较好的健壮性,对运行过程中的错误 操作要做适当处理。 三、实验步骤 1.本实验用到的数据结构 (1)逻辑结构:线性结构 (2)存储结构:程序一、四(顺序存储结构); 程序二、三(链式存储结构); 2.各程序的功能和算法设计思想 程序一:顺序栈 # include # include # include #define STACKINITISIZE 100 # define STACKINCREMENT 10 # define OK 1 # define ERROR 0 # define OVERFLOW -2 typedef int SElemtype; typedef int status; typedef struct { SElemtype *base; SElemtype *top; int stacksize; }sqstack; void Initstack (sqstack *s) { (*s).base = (SElemtype *)malloc(STACKINITISIZE * sizeof (SElemtype)); if(!(*s).base) exit(OVERFLOW);

C语言中数组排序算法及函数调用

C语言中数组排序算法及函数调用 一、冒泡法(起泡法) 算法要求:用起泡法对10个整数按升序排序。 算法分析:如果有n个数,则要进行n-1趟比较。在第1趟比较中要进行n-1次相邻元素的两两比较,在第j趟比较中要进行n-j次两两比较。比较的顺序从前往后,经过一趟比较后,将最值沉底(换到最后一个元素位置),最大值沉底为升序,最小值沉底为降序。 算法源代码: # include main() { int a[10],i,j,t; printf("Please input 10 numbers: "); /*输入源数据*/ for(i=0;i<10;i++) scanf("%d",&a[i]); /*排序*/ for(j=0;j<9;j++) /*外循环控制排序趟数,n个数排n-1趟*/ for(i=0;i<9-j;i++) /*内循环每趟比较的次数,第j趟比较n-j次*/ if(a[i]>a[i+1]) /*相邻元素比较,逆序则交换*/ { t=a[i]; a[i]=a[i+1]; a[i+1]=t; } /*输出排序结果*/ printf("The sorted numbers: "); for(i=0;i<10;i++) printf("%d ",a[i]); printf("\n"); } 算法特点:相邻元素两两比较,每趟将最值沉底即可确定一个数在结果的位置,确定元素位置的顺序是从后往前,其余元素可能作相对位置的调整。可以进行升序或降序排序。 算法分析:定义n-1次循环,每个数字比较n-j次,比较前一个数和后一个数的大小。然后交换顺序。二、选择法 算法要求:用选择法对10个整数按降序排序。 算法分析:每趟选出一个最值和无序序列的第一个数交换,n个数共选n-1趟。第i趟假设i为最值下标,然后将最值和i+1至最后一个数比较,找出最值的下标,若最值下标不为初设值,则将最值元素和下标为i的元素交换。 算法源代码: # include main() { int a[10],i,j,k,t,n=10; printf("Please input 10 numbers:"); for(i=0;i<10;i++)

试验 --循环队列的基本操作及应用

数据结构实验报告 ----试验三循环队列的基本操作及应用 一、问题描述: 熟悉并掌握循环队列的相关操作,自己设计程序,实现循环队列的构造、清空、销毁及队列元素的插入和删除等相关操作。 二、数据结构设计: #define MAXQSIZE 10 //最大队列长度 struct SqQueue { QElemType *base; //初始化动态分配存储空间 Int front; // 头指针,若队列不空,只想对列头元素 int rear; //尾指针,若队列不空,指向队列尾元素的 //下一个位置 }; 三、功能设计: 程序中所涉及到的函数如下: Status InitQueue(SqQueue &Q) //构造一个空队列Q Status DestroyQueue(SqQueue &Q) //销毁队列Q,Q不再存在 Status ClearQueue(SqQueue &Q) //将Q清为空队列 Status QueueEmpty(SqQueue Q) //若队列Q为空队列,则 //返回TRUE,否则返回FALSE int QueueLength(SqQueue Q) //返回Q的元素个数,即队列长度Status GetHead(SqQueue Q,QElemType &e)//若队列不空,则用e返回Q的对 //头元素,并返回OK,否则返回ERROR Status EnQueue(SqQueue &Q,QElemType e)//插入元素e为Q的新的队尾元素Status DeQueue(SqQueue &Q,QElemType &e)//若队列不空,则删除Q的队头 //元素,用e返回其值,并返回 //OK,否则返回ERROR Status QueueTraverse(SqQueue Q,void(*vi)(QElemType))//从队头到队尾依次 //对队列Q中每个元素调用函数 //vi()。一旦vi失败,则操作失败四、源程序: // c1.h (程序名) #include #include #include // malloc()等 #include // INT_MAX等 #include // EOF(=^Z或F6),NULL

数据结构线性表的顺序表示和实现(C语言)概论

/* 线性结构,线性表的顺序表示和实现 */ # include # include # include //包含了exit()函数 //定义一个数组结构体 struct Arr { int * pBase; //保存数组的指针 int len; //保存数组的长度 int cnt; //数组元素的有效个数 }; //前置函数声明 void init_arr(struct Arr * pArr,int length); //初始化 bool append_arr(struct Arr * pArr,int val); //追加一个元素 bool insert_arr(struct Arr * pArr, int pos, int val); //插入一个元素 bool delete_arr(struct Arr * pArr, int pos, int * val); //删除数组中的一个元素int get(struct Arr * pArr, int pos); //获取某个元素的值 bool is_empty(struct Arr * pArr); //判断数组是否为空 bool is_full(struct Arr * pArr); //判断数组是否已满 void sort_arr(struct Arr * pArr); //为数组进行从小到大排序 void show_arr(struct Arr * pArr); //显示数组内容 void inversion_arr(struct Arr * pArr); //反转数组中的所有值 /* 创建一个数组,实现对这个数组的操作 1,追加一个元素 2,插入一个元素 3,对数组排序 4,反转数组元素 */ int main(void) { //定义一个结构体变量 struct Arr array; //获取一个被删除的元素的值 int val; //使用函数init_arr()初始化数组 init_arr(&array, 5);

栈和队列的基本操作实现及其应用

实验二栈和队列的基本操作实现及其应用 一_一、实验目的 1、熟练掌握栈和队列的基本操作在两种存储结构上的实现。 一_二、实验内容 题目一、试写一个算法,判断依次读入的一个以@为结束符的字符序列,是否为回文。所谓“回文“是指正向读和反向读都一样的一字符串,如“321123”或“ableelba”。 相关常量及结构定义: #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int SElemType; typedef struct SqStack { SElemType *base; SElemType *top; int stacksize; }SqStack; 设计相关函数声明: 判断函数:int IsReverse() 栈:int InitStack(SqStack &S )

int Push(SqStack &S, SElemType e ) int Pop(SqStack &S,SElemType &e) int StackEmpty(s) 一_三、数据结构与核心算法的设计描述 1、初始化栈 /* 函数功能:对栈进行初始化。参数:栈(SqStack S)。 成功初始化返回0,否则返回-1 */ int InitStack(SqStack &S) { S.base=(SElemType *)malloc(10*sizeof(SElemType)); if(!S.base) //判断有无申请到空间 return -1; //没有申请到内存,参数失败返回-1 S.top=S.base; S.stacksize=STACK_INIT_SIZE; S.base=new SElemType; return 0; } 2、判断栈是否是空 /*函数功能:判断栈是否为空。参数; 栈(SqStack S)。栈为空时返回-1,不为空返回0*/ int StackEmpty(SqStack S) { if(S.top==S.base) return -1; else return 0; } 3、入栈 /*函数功能:向栈中插入元素。参数; 栈(SqStack S),元素(SElemtype e)。成功插入返回0,否则返回-1 */ int Push(SqStack &S,SElemType e) { if(S.top-S.base>=S.stacksize) { S.base=(SElemType *)realloc(S.base,(S.stacksize+1) * sizeof(SElemType));

c语言 数组排序 字符串排序 函数实现

1.数组倒叙 #include void show(int *a,int len) { int i; for(i=0;i

2.字符串排序 #include #include int main() { char *a[]={"aaaaaaa","ddddddd","eeeee","cccccc"}; int i; int len=sizeof a/sizeof *a; for(i=0;i0) { a[y]=(char *)((int )a[y]^(int)a[y+1]); a[y+1]=(char *)((int )a[y]^(int)a[y+1]); a[y]=(char *)((int )a[y]^(int)a[y+1]); } for(i=0;i

数据结构 栈和队列的基本操作实现及其应用

实验二栈和队列的基本操作实现及其应用 一、实验目的 1、熟练掌握栈和队列的基本操作在两种存储结构上的实现。 2、会用栈和队列解决简单的实际问题。 二、实验内容(可任选或全做) 题目一、试写一个算法,判断依次读入的一个以@为结束符的字符序列, 是否为回文。所谓“回文“是指正向读和反向读都一样的一字符串,如“321123”或“ableelba”。 相关常量及结构定义: # define STACK_INIT_SIZE 100 # define STACKINCREMENT 10 # define OK 1 # define ERROR 0 typedef int SElemType; //栈类型定义 typedef struct SqStack { SElemType *base; SElemType *top; int stacksize; }SqStack; 设计相关函数声明: 判断函数:int IsReverse() 栈:int InitStack(SqStack &S ) int Push(SqStack &S, SElemType e ) int Pop(SqStack &S,SElemType &e) int StackEmpty(s) 题目二、编程模拟队列的管理,主要包括: 出队列、 入队、 统计队列的长度、 查找队列某个元素e、 及输出队列中元素。 [实现提示]:参考教材循环队列的有关算法,其中后两个算法参考顺序表的实现。 题目三、Rails

Description There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track. The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, ..., N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, ..., aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station. Input The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, ..., N. The last line of the block contains just 0. The last block consists of just one line containing 0. Output

队列的常见操作

数据结构面试之四——队列的常见操作 题注:《面试宝典》有相关习题,但思路相对不清晰,排版有错误,作者对此参考相关书籍和自己观点进行了重写,供大家参考。 四、队列的基本操作 1.用数组构造队列 队列即是满足先进先出的链表。用数组存储的话,同样需要满足队列头front出栈,队列末尾rear入栈。而对于数组来讲,rear和front可以代表数组头和尾。不能简单的固定rear 和front的大小为maxSize和0,因为可能出现中间元素为空的现象。所以,对于数组队列来讲,可以想象成环式存储,因为每一次入队后rear+1,每一次出队后front+1。这就需要控制front和rear的大小,每一次修改只要满足front=(front+1)%maxSize,rear=(rear+1)%maxSize即可满足要求。 同样需要注意:入队操作前先判定队列是否已经满;出队操作前先判定队列是否为空。 template class arrQueue { public: arrQueue(intnSize=100); ~arrQueue(); arrQueue(constarrQueue& copyQueue); arrQueue&operator=(const arrQueue& otherQueue); voidinitializeQueue(); void destroyQueue(); bool isQueueEmpty(); bool isQueueFull(); void addQueue(constType& item); void deQueue(Type&deletedItem); private: int maxSize;

c语言数组排序字符串排序函数实现

1.数组倒叙#include void show(int *a,int len) { int i; for(i=0;i

} return a; } int main() { int l,i; int a[]={1,2,3,4,5,6,7,8}; l=sizeof a/sizeof *a; show(a,l); int *b=reverse(a,l); show(b,l); } 2.字符串排序 #include #include int main() {

char *a[]={"aaaaaaa","ddddddd","eeeee","cccccc"}; int i; int len=sizeof a/sizeof *a; for(i=0;i0) { a[y]=(char *)((int )a[y]^(int)a[y+1]); a[y+1]=(char *)((int )a[y]^(int)a[y+1]); a[y]=(char *)((int )a[y]^(int)a[y+1]); } for(i=0;i

链队列基本操作的C++实现

#include using namespace std; template class QueueLink { protected: Node *front,*rear; void Init(); public: LinkQueue(); //无参数的构造函数 virtual ~LinkQueue(); int Length() const; //求队列长度 bool QueueEmpty() const; void Clear(); void Traverse(void(*Visit)(ElemType &)); //遍历队列 StatusCode OutQueue(ElemType &e); //出队操作 StatusCode GetHead(ElemType &e) const; //取队头操作 StatusCode EnQueue(const ElemType &e); //入队操作 LinkQueue(const LinkQueue ©); //复制构造函数 LinkQueue&operator = (const LinkQueue ©); //赋值运算符重载 } template void QueueLink::Init() { rear = front = new Node; } template QueueLink::LinkQueue() { Init(); } template QueueLink::~LinkQueue() { Clear(); } template int QueueLink::Length()const { int count = 0; for(Node *tmpPtr = front->next;tmpPtr != NULL;tmpPtr = tmpPtr->next) count++;

顺序队列的基本操作

#include #include #include #include #define QueueSize 50 typedef char QueueData; typedef struct queue { QueueData data[QueueSize]; int rear,front; }SeqQueue; void Menu() { printf("\n"); printf("|…………………………………………|\n"); printf("| 1、建立|\n"); printf("| |\n"); printf("| 2、显示|\n"); printf("| |\n"); printf("| 3、入队|\n"); printf("| |\n"); printf("| 4、出队|\n"); printf("| |\n"); printf("| 5、取队头元素|\n"); printf("| |\n"); printf("| 6、退出|\n"); printf("|…………………………………………|\n"); printf("\n"); printf("请选择菜单项,按回车键完成选择:"); } //模块1 建立 void Set(SeqQueue *&Q) { Q=(SeqQueue*)malloc(sizeof(SeqQueue)); if(Q==NULL) { printf("存储空间分配失败!\n"); exit(1); } else { printf("存储空间分配成功!\n"); } Q->front=Q->rear=-1; //置空队列

相关文档
相关文档 最新文档