void LinkList(){ int n; do{ printf("\n"); printf("**************单链表的基本操作及应用***************\n"); printf("* 1 创建 *\n"); printf("* 2 插入 *\n"); printf("* 3 删除 *\n"); printf("* 4 查找 *\n"); printf("* 5 显示 *\n"); printf("* 6 通讯录(应用) *\n"); printf("* 7 退出 *\n"); printf("***************************************************\n"); printf("请选择:"); scanf("%d",&n); switch(n){ case 1: printf("--------创建单链表---------");break; case 2: printf("--------插入元素-------");break; case 3: printf("--------删除元素-------");break; case 4: printf("--------查找元素-------");break; case 5: printf("--------显示链表-------");break; case 6: printf("--------通讯录---------");break; case 7: break; default: printf("ERROR!");break; } }while(n!=7); }在里面插入具体实现函数
时间: 2023-07-05 22:24:29 浏览: 142
单链表的创建、插入、删除等基本操作
5星 · 资源好评率100%
好的,下面是单链表的具体实现函数:
1. 创建单链表
```cpp
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* createList() {
ListNode* head = new ListNode(0);
ListNode* p = head;
int n, val;
printf("请输入单链表的长度n:");
scanf("%d", &n);
for(int i = 0; i < n; i++) {
printf("请输入第%d个节点的值:", i+1);
scanf("%d", &val);
ListNode* node = new ListNode(val);
p->next = node;
p = p->next;
}
return head->next;
}
```
2. 插入元素
```cpp
void insertNode(ListNode* head, int pos, int val) {
ListNode* p = head;
for(int i = 0; i < pos - 1; i++) {
p = p->next;
if(p == NULL) {
printf("插入位置无效!\n");
return;
}
}
ListNode* node = new ListNode(val);
node->next = p->next;
p->next = node;
printf("插入成功!\n");
}
```
3. 删除元素
```cpp
void deleteNode(ListNode* head, int val) {
ListNode* p = head;
while(p->next != NULL) {
if(p->next->val == val) {
ListNode* q = p->next;
p->next = q->next;
delete q;
printf("删除成功!\n");
return;
}
p = p->next;
}
printf("未找到要删除的元素!\n");
}
```
4. 查找元素
```cpp
void searchNode(ListNode* head, int val) {
ListNode* p = head;
int pos = 0;
while(p != NULL) {
pos++;
if(p->val == val) {
printf("元素%d在链表中的位置是%d\n", val, pos);
return;
}
p = p->next;
}
printf("未找到要查找的元素!\n");
}
```
5. 显示链表
```cpp
void displayList(ListNode* head) {
ListNode* p = head;
printf("当前链表的元素为:");
while(p != NULL) {
printf("%d ", p->val);
p = p->next;
}
printf("\n");
}
```
6. 通讯录(应用)
```cpp
struct Contact {
string name;
string phone;
string email;
Contact* next;
Contact(string n, string p, string e) : name(n), phone(p), email(e), next(NULL) {}
};
Contact* createContactList() {
Contact* head = new Contact("0", "0", "0");
Contact* p = head;
int n;
string name, phone, email;
printf("请输入通讯录的长度n:");
scanf("%d", &n);
for(int i = 0; i < n; i++) {
printf("请输入第%d个联系人的信息:\n", i+1);
printf("姓名:");
cin >> name;
printf("电话:");
cin >> phone;
printf("邮箱:");
cin >> email;
Contact* contact = new Contact(name, phone, email);
p->next = contact;
p = p->next;
}
return head->next;
}
void displayContactList(Contact* head) {
Contact* p = head;
printf("当前通讯录的信息为:\n");
while(p != NULL) {
printf("姓名:%s,电话:%s,邮箱:%s\n", p->name.c_str(), p->phone.c_str(), p->email.c_str());
p = p->next;
}
}
void searchContact(Contact* head, string name) {
Contact* p = head;
while(p != NULL) {
if(p->name == name) {
printf("联系人%s的电话是%s,邮箱是%s\n", p->name.c_str(), p->phone.c_str(), p->email.c_str());
return;
}
p = p->next;
}
printf("未找到联系人%s的信息!\n", name.c_str());
}
void insertContact(Contact* head, int pos, string name, string phone, string email) {
Contact* p = head;
for(int i = 0; i < pos - 1; i++) {
p = p->next;
if(p == NULL) {
printf("插入位置无效!\n");
return;
}
}
Contact* contact = new Contact(name, phone, email);
contact->next = p->next;
p->next = contact;
printf("插入成功!\n");
}
void deleteContact(Contact* head, string name) {
Contact* p = head;
while(p->next != NULL) {
if(p->next->name == name) {
Contact* q = p->next;
p->next = q->next;
delete q;
printf("删除成功!\n");
return;
}
p = p->next;
}
printf("未找到要删除的联系人!\n");
}
```
阅读全文