写一个抽象数据类型的圆的代码
时间: 2023-12-01 21:10:44 浏览: 41
下面是一个抽象数据类型的圆的代码示例:
```python
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * (self.radius ** 2)
def circumference(self):
return 2 * 3.14 * self.radius
def diameter(self):
return 2 * self.radius
```
这个代码定义了一个名为 `Circle` 的类,该类有三个方法: `area`、`circumference` 和 `diameter`。 `__init__` 方法用于初始化圆的半径。 `area` 方法返回圆的面积。 `circumference` 方法返回圆的周长。 `diameter` 方法返回圆的直径。这个抽象数据类型可以用来操作圆的相关信息。
相关问题
抽象数据类型的代码示例
以下是一个抽象数据类型的代码示例,实现了一个简单的栈:
```python
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
```
这个栈可以存储任意类型的数据,包括整数、字符串、列表等。可以使用 `push()` 方法将元素压入栈中,使用 `pop()` 方法弹出栈顶元素,使用 `peek()` 方法查看栈顶元素,使用 `size()` 方法获取栈中元素数量。使用 `is_empty()` 方法判断栈是否为空。
c语言中用数组定义一个抽象数据类型的list的代码
以下是用数组定义一个抽象数据类型的list的C语言代码示例:
```c
#define MAX_SIZE 100 // 定义最大容量为100
// 定义list结构体
typedef struct {
int data[MAX_SIZE];
int length;
} List;
// 初始化list
void init(List *list) {
list->length = 0;
}
// 判断list是否为空
int isEmpty(List *list) {
return list->length == 0;
}
// 判断list是否已满
int isFull(List *list) {
return list->length == MAX_SIZE;
}
// 在list末尾插入元素
void append(List *list, int value) {
if (isFull(list)) {
printf("List is full.\n");
return;
}
list->data[list->length++] = value;
}
// 在list指定位置插入元素
void insert(List *list, int value, int index) {
if (isFull(list)) {
printf("List is full.\n");
return;
}
if (index < 0 || index > list->length) {
printf("Invalid index.\n");
return;
}
for (int i = list->length; i > index; i--) {
list->data[i] = list->data[i - 1];
}
list->data[index] = value;
list->length++;
}
// 删除list指定位置的元素
void delete(List *list, int index) {
if (isEmpty(list)) {
printf("List is empty.\n");
return;
}
if (index < 0 || index >= list->length) {
printf("Invalid index.\n");
return;
}
for (int i = index; i < list->length - 1; i++) {
list->data[i] = list->data[i + 1];
}
list->length--;
}
// 获取list指定位置的元素
int get(List *list, int index) {
if (isEmpty(list)) {
printf("List is empty.\n");
return -1;
}
if (index < 0 || index >= list->length) {
printf("Invalid index.\n");
return -1;
}
return list->data[index];
}
// 修改list指定位置的元素
void set(List *list, int value, int index) {
if (isEmpty(list)) {
printf("List is empty.\n");
return;
}
if (index < 0 || index >= list->length) {
printf("Invalid index.\n");
return;
}
list->data[index] = value;
}
// 打印list中的元素
void print(List *list) {
if (isEmpty(list)) {
printf("List is empty.\n");
return;
}
printf("List: ");
for (int i = 0; i < list->length; i++) {
printf("%d ", list->data[i]);
}
printf("\n");
}
int main() {
List list;
init(&list);
append(&list, 1);
append(&list, 2);
insert(&list, 3, 1);
print(&list); // 输出:List: 1 3 2
delete(&list, 0);
set(&list, 4, 1);
print(&list); // 输出:List: 3 4
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)