课程设计的题目!望!关于堆栈操作的
课程设计的题目!望高手指点!!!关于堆栈操作的
1实验目的
‐学习堆对象的分配与释放
‐学习静态数据成员和静态成员函数的使用
‐学习链表类的定义与使用
2实验内容
模拟栈操作。设计一个整数链表类,满足栈操作。即,总在链表首插入结点,总在链表首取出(删除)结点。
3实验分析
①类中需有记录结点个数的数据成员。
②如果链表为空,而要做取出结点的操作,则类必须给出出错显示
③编制应用程序,取 100次随机数(10∽200内),每取到比前一个随机数大时,放入链表中,否则,略去。然后逐个取出,求其和。
④用堆分配方法逐个产生满足条件的结点,插入链表中。每当从链表中取出一个结点时,要及时将结点删除。
⑤求和工作不要在链表类中完成,以使该链表具有通用性。
我已经写出代码,望高手改进
stack.h
#ifndef STACK_H
#define STACK_H
#include <stdio.h>
#include <iostream.h>
class stack {
int size;
int top;
int *stackptr;
public:
stack(int=100);
~stack() ;
int push(const int&);
int pop(int &);
int isEmpty() const ;
int isFull() const ;
int gettop() const;
};
#endif
8.cpp
#include <iostream.h>
#include "stack.h "
stack:: stack(int s)
{
size = s;
top = -1;
stackptr=new int [size];
}
stack:: ~stack()
{
delete [] stackptr;
}
int stack:: isEmpty() const
{
return top==-1;
}
int stack:: isFull() const
{
return top==size-1;
}
int stack:: push(const int &item)
{
if(!isFull()) {
stackptr[++top] =item;
return 1;
}
return 0;
}
int stack:: pop(int &popvalue)
{
if(!isEmpty()) {
popvalue = stackptr[top--];
return 1;
}
return 0;
}
int stack::gettop() const
{
return top;
}
main.cpp
#include <stdio.h>
#include <iostream.h>
#include "stack.h "
#include <assert.h>
#include <stdlib.h>
#include <math.h>
void swap(int v[], int i,int j)
{
int tmp;
tmp=v[i];
v[i]=v[j];
v[j]=tmp;
}
void main()
{
stack a1(150);
int a[100];
int k;
for(;;)
{
int i=0;
k=rand()%190+10;
if(k> a[i-1]&&i!=0)
{
a[i]=k;
i++;
}
else if(i> =100)
break ;
/* for(int j=0;j <i;j++)
{
if(a[j]> a[i])
swap(a,i,j);
}*/
}
int j;
j=a1.gettop();
1实验目的
‐学习堆对象的分配与释放
‐学习静态数据成员和静态成员函数的使用
‐学习链表类的定义与使用
2实验内容
模拟栈操作。设计一个整数链表类,满足栈操作。即,总在链表首插入结点,总在链表首取出(删除)结点。
3实验分析
①类中需有记录结点个数的数据成员。
②如果链表为空,而要做取出结点的操作,则类必须给出出错显示
③编制应用程序,取 100次随机数(10∽200内),每取到比前一个随机数大时,放入链表中,否则,略去。然后逐个取出,求其和。
④用堆分配方法逐个产生满足条件的结点,插入链表中。每当从链表中取出一个结点时,要及时将结点删除。
⑤求和工作不要在链表类中完成,以使该链表具有通用性。
我已经写出代码,望高手改进
stack.h
#ifndef STACK_H
#define STACK_H
#include <stdio.h>
#include <iostream.h>
class stack {
int size;
int top;
int *stackptr;
public:
stack(int=100);
~stack() ;
int push(const int&);
int pop(int &);
int isEmpty() const ;
int isFull() const ;
int gettop() const;
};
#endif
8.cpp
#include <iostream.h>
#include "stack.h "
stack:: stack(int s)
{
size = s;
top = -1;
stackptr=new int [size];
}
stack:: ~stack()
{
delete [] stackptr;
}
int stack:: isEmpty() const
{
return top==-1;
}
int stack:: isFull() const
{
return top==size-1;
}
int stack:: push(const int &item)
{
if(!isFull()) {
stackptr[++top] =item;
return 1;
}
return 0;
}
int stack:: pop(int &popvalue)
{
if(!isEmpty()) {
popvalue = stackptr[top--];
return 1;
}
return 0;
}
int stack::gettop() const
{
return top;
}
main.cpp
#include <stdio.h>
#include <iostream.h>
#include "stack.h "
#include <assert.h>
#include <stdlib.h>
#include <math.h>
void swap(int v[], int i,int j)
{
int tmp;
tmp=v[i];
v[i]=v[j];
v[j]=tmp;
}
void main()
{
stack a1(150);
int a[100];
int k;
for(;;)
{
int i=0;
k=rand()%190+10;
if(k> a[i-1]&&i!=0)
{
a[i]=k;
i++;
}
else if(i> =100)
break ;
/* for(int j=0;j <i;j++)
{
if(a[j]> a[i])
swap(a,i,j);
}*/
}
int j;
j=a1.gettop();