数据结构实验报告 实验一:栈和队列 实验目的: 掌握栈和队列特点、逻辑结构和存储结构 熟悉对栈和队列的一些基本操作和具体的函数定义。 利用栈和队列的基本操作完成一定功能的程序。 实验任务 1.给出顺序栈的类定义和函数实现,利用栈的基本操作完成十进制数N 与其它d 进制数的转换。(如N=1357,d=8) 实验原理:将十进制数N 转换为八进制时,采用的是“除取余数法”,即每次用8 除 N 所得的余数作为八进制数的当前个位,将相除所得的商的整数部分作为新的N 值重复上述计算,直到 N 为0 为止。此时,将前面所得到的各余数反过来连接便得到最后的转换结果。 程序清单 #include #include using namespace std; typedef int DATA_TYPE; const int MAXLEN=100; enum error_code { success,overflow,underflow }; class stack { public: stack(); bool empty()const; error_code get_top(DATA_TYPE &x)const; error_code push(const DATA_TYPE x); error_code pop(); bool full()const; private: DATA_TYPE data[MAXLEN]; int count; }; stack::stack() { count=0; } bool stack::empty()const { return count==0; } error_code stack::get_top(DATA_TYPE &x)const { if(empty()) return underflow; else { x=data[count-1]; return success; } } error_code stack::push(const DATA_TYPE x) { if(full()) return overflow; else { data[count]=x; count++; } } error_code stack::pop() { if(empty()) return underflow; else { count--; return success; } } bool stack::full()const { return count==MAXLEN; } void main() { stack S; int N,d; cout<<"请输入一个十进制数 N 和所需转换的进制 d"<>N>>d; if(N==0) { cout<<"输出转换结果:"<