编程实现线性表两种存储结构中的基本操作的实现(线性表的创建、 插入、删除和查找),并设计一个主菜单完成各个功能的调用。
时间: 2024-11-05 11:25:28 浏览: 46
在线性表的存储结构中,常见的有两种:顺序存储(数组)和链式存储(单链表)。以下是这两种结构的基本操作以及主菜单的设计:
**1. 顺序存储(数组)**
- **创建**:首先,你需要定义一个固定大小的数组,并初始化所有元素为默认值(如NULL或0)。
```c++
int linearArray[10]; // 创建一个长度为10的整数数组
```
- **插入**:如果需要添加元素,会涉及到移动其他元素。对于数组,通常会在末尾添加,然后将后续元素向右移动一位。
```c++
void insertArray(int index, int value) {
for (int i = length - 1; i >= index; i--) {
linearArray[i + 1] = linearArray[i];
}
linearArray[index] = value;
}
```
- **删除**:同理,删除指定位置的元素需要将后面的元素向前移一位。
```c++
void deleteArray(int index) {
for (int i = index; i < length - 1; i++) {
linearArray[i] = linearArray[i + 1];
}
}
```
- **查找**:通过索引来直接访问元素。
```c++
int searchArray(int index) {
if (index >= 0 && index < length) {
return linearArray[index];
} else {
return -1; // 表示未找到
}
}
```
**2. 链式存储(单链表)**
- **创建**:定义一个节点结构,包含数据和指向下一个节点的指针,然后创建首节点。
```c++
struct ListNode {
int data;
ListNode* next;
ListNode(int val) : data(val), next(nullptr) {}
};
ListNode* head = nullptr;
```
- **插入**:在链表头部或尾部插入新节点较为简单,只需改变头结点或相应节点的next指针。
```c++
void insertList(ListNode*& head, int value) {
ListNode* newNode = new ListNode(value);
newNode->next = head;
head = newNode;
}
```
- **删除**:遍历链表找到目标节点,更新前一个节点的next指针。
```c++
void deleteList(ListNode*& head, int value) {
ListNode* prev = nullptr;
ListNode* curr = head;
while (curr != nullptr) {
if (curr->data == value) {
if (prev == nullptr) {
head = curr->next;
} else {
prev->next = curr->next;
}
delete curr;
break;
}
prev = curr;
curr = curr->next;
}
}
```
- **查找**:遍历链表查找目标节点。
```c++
ListNode* searchList(ListNode* head, int value) {
ListNode* curr = head;
while (curr != nullptr) {
if (curr->data == value) {
return curr;
}
curr = curr->next;
}
return nullptr; // 表示未找到
}
```
**主菜单设计:**
```c++
void mainMenu() {
while (true) {
cout << "请选择操作:\n";
cout << "1. 创建线性表\n";
cout << "2. 插入元素\n";
cout << "3. 删除元素\n";
cout << "4. 查找元素\n";
cout << "5. 退出\n";
int choice;
cin >> choice;
switch (choice) {
case 1:
createLinearTable();
break;
case 2:
insertElement(linearStructureType, index);
break;
case 3:
deleteElement(linearStructureType, value);
break;
case 4:
searchElement(linearStructureType, value);
break;
case 5:
exit(0); // 退出程序
break;
default:
cout << "无效选择,请重试。\n";
}
}
}
```
其中,`linearStructureType`可以根据用户选择的不同而变化,例如数组或链表。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)