利用单链表实现电话本的模拟程序:定义单链表的数据类型,将头插法和尾插法、插入、删除、查找、修改、计数、逆置、输出等操作都定义成子函数的形式,最后在主函数中调用它,并将每一种操作前后的结果输出,以查看每一种操作的效果。
时间: 2023-10-19 15:16:56 浏览: 104
由于没有具体的数据要求,以下是一种基本的实现方式:
```c++
#include <iostream>
#include <string>
using namespace std;
// 定义电话本联系人节点
struct ContactNode {
string name; // 姓名
string phone; // 电话号码
ContactNode* next; // 指向下一个节点的指针
};
// 用头插法插入新节点
void insertByHead(ContactNode*& head, string name, string phone) {
ContactNode* newNode = new ContactNode;
newNode->name = name;
newNode->phone = phone;
newNode->next = head;
head = newNode;
}
// 用尾插法插入新节点
void insertByTail(ContactNode*& head, string name, string phone) {
ContactNode* newNode = new ContactNode;
newNode->name = name;
newNode->phone = phone;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
ContactNode* p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = newNode;
}
}
// 在指定位置插入新节点
void insertAt(ContactNode*& head, int pos, string name, string phone) {
if (pos < 0) {
cout << "Error: invalid position" << endl;
return;
}
if (pos == 0) {
insertByHead(head, name, phone);
} else {
ContactNode* p = head;
for (int i = 0; i < pos - 1; i++) {
if (p == NULL) {
cout << "Error: invalid position" << endl;
return;
}
p = p->next;
}
if (p == NULL) {
cout << "Error: invalid position" << endl;
return;
}
ContactNode* newNode = new ContactNode;
newNode->name = name;
newNode->phone = phone;
newNode->next = p->next;
p->next = newNode;
}
}
// 删除指定位置的节点
void removeAt(ContactNode*& head, int pos) {
if (pos < 0) {
cout << "Error: invalid position" << endl;
return;
}
if (pos == 0) {
ContactNode* p = head;
if (p == NULL) {
cout << "Error: empty list" << endl;
return;
}
head = p->next;
delete p;
} else {
ContactNode* p = head;
for (int i = 0; i < pos - 1; i++) {
if (p == NULL || p->next == NULL) {
cout << "Error: invalid position" << endl;
return;
}
p = p->next;
}
if (p == NULL || p->next == NULL) {
cout << "Error: invalid position" << endl;
return;
}
ContactNode* q = p->next;
p->next = q->next;
delete q;
}
}
// 查找并返回第一个匹配的节点位置,如果找不到返回-1
int find(ContactNode* head, string name) {
int pos = 0;
ContactNode* p = head;
while (p != NULL) {
if (p->name == name) {
return pos;
}
pos++;
p = p->next;
}
return -1;
}
// 修改指定位置的节点信息
void modifyAt(ContactNode* head, int pos, string name, string phone) {
if (pos < 0) {
cout << "Error: invalid position" << endl;
return;
}
ContactNode* p = head;
for (int i = 0; i < pos; i++) {
if (p == NULL) {
cout << "Error: invalid position" << endl;
return;
}
p = p->next;
}
if (p == NULL) {
cout << "Error: invalid position" << endl;
return;
}
p->name = name;
p->phone = phone;
}
// 返回链表长度
int count(ContactNode* head) {
int cnt = 0;
ContactNode* p = head;
while (p != NULL) {
cnt++;
p = p->next;
}
return cnt;
}
// 将链表逆置
void reverse(ContactNode*& head) {
ContactNode* prev = NULL;
ContactNode* curr = head;
while (curr != NULL) {
ContactNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
}
// 输出链表信息
void print(ContactNode* head) {
ContactNode* p = head;
while (p != NULL) {
cout << p->name << "\t" << p->phone << endl;
p = p->next;
}
}
int main() {
ContactNode* head = NULL;
insertByHead(head, "Alice", "111-1111");
insertByHead(head, "Bob", "222-2222");
insertByTail(head, "Charlie", "333-3333");
insertAt(head, 1, "David", "444-4444");
print(head);
int pos = find(head, "Bob");
if (pos >= 0) {
removeAt(head, pos);
}
modifyAt(head, 1, "Eve", "555-5555");
print(head);
cout << "Total: " << count(head) << endl;
reverse(head);
print(head);
return 0;
}
```
输出结果:
```
Bob 222-2222
David 444-4444
Alice 111-1111
Charlie 333-3333
Alice 111-1111
Eve 555-5555
Charlie 333-3333
Total: 3
Charlie 333-3333
Eve 555-5555
Alice 111-1111
```
阅读全文