读书人

课程设计的题目!望!关于堆栈操作的

发布时间: 2012-03-20 14:01:11 作者: rapoo

课程设计的题目!望高手指点!!!关于堆栈操作的
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();



for(int i=0; i <j;i++)
{
a1.push(a[i]);

cout < <a[i] < < '\t ';
}
cout < <a1.gettop() < <endl;


int m=0;
int sum=0;
while(a1.pop(m)) {

sum+=m;
m++;
}

cout < <sum < <endl;

}


[解决办法]
其实很简单
只要对
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();

for(int i=0; i <j;i++)
{
a1.push(a[i]);

cout < <a[i] < < '\t ';
}
cout < <a1.gettop() < <endl;

进行修改就行啦!!!
[解决办法]
栈的实现:
#include <stdio.h>
#include <malloc.h>
#include <conio.h>

#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define EQUAL 1
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef int Status ;

struct STU{
char name[20];
char stuno[10];
int age;
int score;
};
typedef struct STU SElemType;

struct STACK
{
SElemType *base;
SElemType *top;
int stacksize;
};

typedef struct STACK SqStack;
typedef struct STACK *pSqstack;

Status InitStack(SqStack **S);
Status DestroyStack(SqStack *S);
Status ClearStack(SqStack *S);
Status StackEmpty(SqStack S);
int StackLength(SqStack S);
Status GetTop(SqStack S,SElemType *e);
Status Push(SqStack *S,SElemType e);
Status Pop(SqStack *S,SElemType *e);
Status StackTraverse(SqStack S,Status (*visit)());

Status InitStack(SqStack **S)
{
(*S)=(SqStack *) malloc(sizeof(SqStack));
(*S)-> base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
if(!(*S)-> base)exit(OVERFLOW);
(*S)-> top=(*S)-> base;
(*S)-> stacksize=STACK_INIT_SIZE;
return OK;
}

Status DestroyStack(SqStack *S)
{
free(S-> base);
free(S);
}

Status ClearStack(SqStack *S)
{
S-> top=S-> base;
}

Status StackEmpty(SqStack S)
{
if(S.top==S.base) return TRUE;
else
return FALSE;
}

int StackLength(SqStack S)
{
int i;
SElemType *p;
i=0;
p=S.top;
while(p!=S.base)
{p++;
i++;
}
}

Status GetTop(SqStack S,SElemType *e)
{
if(S.top==S.base) return ERROR;
*e=*(S.top-1);
return OK;
}

Status Push(SqStack *S,SElemType e)
{
/*
if(S-> top - S-> base> =S-> stacksize)
{

S-> base=(SElemType *) realloc(S-> base,
(S-> stacksize + STACKINCREMENT) * sizeof(SElemType));
if(!S-> base)exit(OVERFLOW);
S-> top=S-> base+S-> stacksize;


S-> stacksize += STACKINCREMENT;
}
*/

*(S-> top++)=e;
return OK;
}

Status Pop(SqStack *S,SElemType *e)
{
if(S-> top==S-> base) return ERROR;
*e=*--S-> top;
return OK;
}

Status StackPrintElem(SElemType * e)
{
printf( "%s %s %d %d\n ",e-> name,e-> stuno,e-> age,e-> score);
}
Status StackTraverse(SqStack S,Status (*visit)())
{
while(S.top!=S.base)
visit(--S.top);
}

main()
{
SElemType e;
SqStack *Sa;

clrscr();

printf( "\n\n-------------------SqStack Demo is running...----------------\n\n ");
printf( "First is Push function.\n ");

InitStack(&Sa);

strcpy(e.name, "stu1 ");
strcpy(e.stuno, "100001 ");
e.age=80;
e.score=1000;

printf( " Now Stack is Empty.\n ");
StackTraverse(*Sa,StackPrintElem);

Push(Sa,e);

printf( " Now Stack has one element.\n ");
StackTraverse(*Sa,StackPrintElem);

strcpy(e.name, "stu3 ");
strcpy(e.stuno, "100002 ");
e.age=80;
e.score=1000;
Push(Sa,e);
printf( " Now Stack has another element.\n ");
StackTraverse(*Sa,StackPrintElem);

printf( " Now Pop Stack,the top elem put into variable e.\n ");
Pop(Sa,&e);
printf( "%s\n%s\n%d\n%d\n ",e.name,e.stuno,e.age,e.score);

printf( " Let 's see the left of Stack 's elem:\n ");
StackTraverse(*Sa,StackPrintElem);

getch();
printf( "\n\n\nWelcom to visit http://zmofun.topcool.net\n\n ");
}


读书人网 >C++

热点推荐