数据结构与算法-堆栈的操作
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《数据结构与算法分析》课程实验报告
【实验目的】
1. 理解堆栈的存储特性。
2. 掌握堆栈的常用操作算法。
【实验内容】
1. 利用堆栈实现对任意进制的数的转换;
2. 堆栈的应用及操作。
【实验方式】
个人实验。
【实验设备与环境】
PC机,Windows XP操作系统,VC++6.0开发环境。
【数据结构及函数定义】
(1)类的定义:类的数据成员,成员函数
……………………………………
(2)主函数main()实现初始化操作,完成对子函数的调用……………………………………
(3)子函数
…………………………………
【测试数据与实验结果】
(请用截图的方式展示实验结果,并辅以必要的文字说明)
【源程序清单】
(请附上源程序)
#include
#include
#include
struct stack
{
int data;
struct stack *next;
};
void main()
{
struct stack *creat();//创建链表函数
void output(struct stack *);
struct stack *insert(struct stack *,int);//在栈顶压入元素函数
struct stack *read(struct stack *);//读栈顶元素函数
struct stack *head;
head=creat();//创建链表函数
head->next=NULL;
int num,i,n,m;
cout<<"输入数据:";
cin>>num;
cout<<"输入要将输入数值转换成的进制:"; cin>>i;
while(num)
{n=num/i;
m=num%i;
head=insert(head,m);//在栈顶压入元素函数num=n;
}
output(head);
printf("数值经过转换后为:");
while(head->next!=NULL)
{head=read(head);}
getchar();
cout< } struct stack *creat()//创建链表函数 { struct stack *fresh,*head,*tail; int num=0; static int i=1; head=tail=(struct stack *)malloc(sizeof(struct stack)); cout<<"这是第"< i++; fresh=(struct stack *)malloc(sizeof(struct stack)); tail->next=fresh; tail=fresh; tail->next=NULL; return head; } struct stack *insert(struct stack *head,int m)//在栈顶压入元素函数 { struct stack *fresh,*p; p=head->next; fresh=(struct stack *)malloc(sizeof(struct stack)); fresh->data=m; head->next=fresh; fresh->next=p; return head; } struct stack *read(struct stack *head)//读栈顶元素函数{ struct stack *p; p=head->next; head=p; if(p!=NULL) {if(p->data>=10) printf("%c",p->data+55); else printf("%d",p->data);} else cout<<"->end"; return head; } void output(struct stack *head)//输出单链表函数{ int m=1; cout<<"栈表内的数据结构"; while(head->next!=NULL) { head=head->next; cout<<"->"< m=m+1; } cout< }……………………………………………………………………….