学习data structure的朋友们进来看一下啊

来源:百度知道 编辑:UC知道 时间:2024/05/19 20:35:25
这个问题我研究了很长时间也没弄明白
这个问题地说明就是 “A library is to be provided for the manipulation of a list of integer numbers. The library will implement
the list using an array. ”

以下是头文件

/* intList.h - list of integer numbers header */

#ifndef _intList_h
#define _intList_h

#include "boole.h"

boolean isEmpty(void);
/* pre: TRUE */
/* post: returns TRUE if the list is empty, FALSE otherwise */

void displayList(void);
/* pre: TRUE */
/* post: displays all the entries in the list */

boolean isFull(void);
/* pre: none */
/* post: returns TRUE if the list is full, FALSE otherwise */

void addItem(int item);
/* pre: list is not full */
/* post: adds the item to the end of the list */

boolean isPresent(int item);
/* pre: none */
/* post: returns TRUE if the item is in the list, FALSE otherwise */

void removeItem(int item);
/*

假定头文件里的函数是函数原型声明

int list[MAXITEMS], length = 0; 是全局量
实际函数写法如下

boolean isEmpty(void)
{
boolean v = TRUE; // pre TRUE
if (length > 0) v = FALSE;
return v;
}

void displayList(void)
{
int i;
if ( !isEmpty(void)){
for (i=0;i<length;i++) printf("%d ",list[i]);
printf("\n");
};
}

boolean isFull(void)
{
if (length >= MAXITEMS - 1) {return TRUE;}
else { return FALSE;};
}

void addItem(int item){
if ( !isFull(void)){
list[length] = item;
length = length + 1;
}
}

boolean isPresent(int item){
boolean v = FALSE;
int i;
for (i=0;i<length;i++){
if (list[i] == item) v = TRUE;
}
return v;
}

void removeItem(int item){
int i,j;
if ( isPresent(item) ){
for (i=0;i<length;i++) {
if (list[i] == item){
j = i;