c语言增删改查
时间: 2023-06-21 09:06:32 浏览: 121
C语言中实现增删改查的方法不止一种,以下是几种常见的实现方式:
1. 数组实现
数组是一种简单的数据结构,可以用来存储一组相同类型的数据。在C语言中,我们可以使用数组来实现增删改查操作。
- 增加:将新元素插入到数组的末尾
- 删除:将指定位置的元素删除,并将后面的元素向前移动
- 修改:直接修改指定位置的元素的值
- 查找:遍历数组,逐一比较元素的值,找到目标元素
下面是一个示例代码:
```
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int arr[MAX_SIZE];
int n, pos, value, i;
// 添加元素
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// 删除元素
printf("Enter the position of the element to be deleted: ");
scanf("%d", &pos);
if (pos < 1 || pos > n) {
printf("Invalid position!\n");
} else {
for (i = pos - 1; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
printf("Element deleted successfully.\n");
}
// 修改元素
printf("Enter the position of the element to be modified: ");
scanf("%d", &pos);
printf("Enter the new value: ");
scanf("%d", &value);
if (pos < 1 || pos > n) {
printf("Invalid position!\n");
} else {
arr[pos - 1] = value;
printf("Element modified successfully.\n");
}
// 查找元素
printf("Enter the value to be searched: ");
scanf("%d", &value);
for (i = 0; i < n; i++) {
if (arr[i] == value) {
printf("Element found at position %d.\n", i + 1);
break;
}
}
if (i == n) {
printf("Element not found.\n");
}
return 0;
}
```
2. 链表实现
链表是一种动态数据结构,可以在运行时动态分配内存来存储数据。在C语言中,我们可以使用链表来实现增删改查操作。
- 增加:在链表的末尾插入新节点
- 删除:找到指定节点,并将其从链表中移除
- 修改:找到指定节点,并修改其值
- 查找:遍历链表,逐一比较节点的值,找到目标节点
下面是一个示例代码:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} Node;
Node *create_node(int data) {
Node *new_node = (Node *)malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
void insert_node(Node **head, int data) {
Node *new_node = create_node(data);
if (*head == NULL) {
*head = new_node;
} else {
Node *p = *head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_node;
}
}
void delete_node(Node **head, int data) {
Node *p = *head;
Node *prev = NULL;
while (p != NULL) {
if (p->data == data) {
if (prev == NULL) {
*head = p->next;
} else {
prev->next = p->next;
}
free(p);
return;
}
prev = p;
p = p->next;
}
}
void modify_node(Node *head, int pos, int data) {
Node *p = head;
int i = 1;
while (p != NULL && i < pos) {
p = p->next;
i++;
}
if (p != NULL) {
p->data = data;
}
}
void search_node(Node *head, int data) {
Node *p = head;
int i = 1;
while (p != NULL) {
if (p->data == data) {
printf("Element found at position %d.\n", i);
return;
}
p = p->next;
i++;
}
printf("Element not found.\n");
}
void print_list(Node *head) {
printf("List: ");
Node *p = head;
while (p != NULL) {
printf("%d -> ", p->data);
p = p->next;
}
printf("NULL\n");
}
int main() {
Node *head = NULL;
int n, data;
// 添加元素
printf("Enter the size of the list: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &data);
insert_node(&head, data);
}
print_list(head);
// 删除元素
printf("Enter the element to be deleted: ");
scanf("%d", &data);
delete_node(&head, data);
print_list(head);
// 修改元素
printf("Enter the position of the element to be modified: ");
scanf("%d", &n);
printf("Enter the new value: ");
scanf("%d", &data);
modify_node(head, n, data);
print_list(head);
// 查找元素
printf("Enter the value to be searched: ");
scanf("%d", &data);
search_node(head, data);
return 0;
}
```
以上是两种常见的实现方式,实际上还有其他数据结构和算法可以用来实现增删改查操作,具体实现要根据具体情况而定。
阅读全文