单链表的构建、查找(按位置查找以及按值查找)、插入、删除操作,并实现main程序。c语言
时间: 2023-05-02 07:04:40 浏览: 43
单链表是一种数据结构,由节点组成。每个节点包含一个数据项和指向下一个节点的指针。常用操作包括插入、删除和查找节点,可按位置和按值进行操作。在C语言中,可以定义节点结构体,使用malloc函数动态创建节点并连接起来。可以设计一个main函数来实现对链表的操作。
相关问题
单链表的构建、查找(按位置查找以及按值查找)、插入、删除操作,并实现main程序。
单链表是一种常见的数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。单链表的构建、查找、插入和删除操作如下:
1. 构建单链表:从头节点开始,依次插入新节点,直到链表末尾。
2. 按位置查找:从头节点开始,依次遍历链表,直到找到指定位置的节点。
3. 按值查找:从头节点开始,依次遍历链表,直到找到指定值的节点。
4. 插入节点:先找到要插入的位置,然后将新节点插入到该位置之前。
5. 删除节点:先找到要删除的节点,然后将该节点从链表中删除。
下面是一个简单的单链表实现及其main程序:
```c++
#include <iostream>
using namespace std;
// 定义单链表节点结构体
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
// 构建单链表
ListNode* createList(int n) {
ListNode* head = new ListNode(0);
ListNode* p = head;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
ListNode* node = new ListNode(x);
p->next = node;
p = p->next;
}
return head->next;
}
// 按位置查找
ListNode* findByIndex(ListNode* head, int index) {
if (index < 1) return NULL;
ListNode* p = head;
for (int i = 1; i < index && p != NULL; i++) {
p = p->next;
}
return p;
}
// 按值查找
ListNode* findByValue(ListNode* head, int value) {
ListNode* p = head;
while (p != NULL && p->val != value) {
p = p->next;
}
return p;
}
// 插入节点
void insertNode(ListNode* head, int index, int value) {
ListNode* p = findByIndex(head, index - 1);
if (p == NULL) return;
ListNode* node = new ListNode(value);
node->next = p->next;
p->next = node;
}
// 删除节点
void deleteNode(ListNode* head, int value) {
ListNode* p = head;
while (p->next != NULL && p->next->val != value) {
p = p->next;
}
if (p->next == NULL) return;
ListNode* temp = p->next;
p->next = p->next->next;
delete temp;
}
// 打印单链表
void printList(ListNode* head) {
ListNode* p = head;
while (p != NULL) {
cout << p->val << " ";
p = p->next;
}
cout << endl;
}
int main() {
int n;
cin >> n;
ListNode* head = createList(n);
printList(head);
int index, value;
cin >> index >> value;
insertNode(head, index, value);
printList(head);
cin >> value;
deleteNode(head, value);
printList(head);
return 0;
}
```
单链表按位置查找并返回找到的值,用c语言实现
以下是用C语言实现单链表按位置查找并返回找到的值的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义单链表结点结构体
struct Node {
int data;
struct Node* next;
};
// 定义单链表查找函数
int findNode(struct Node* head, int pos) {
struct Node* p = head;
int count = 1;
// 遍历链表,查找对应位置的结点
while (p != NULL && count < pos) {
p = p->next;
count++;
}
// 如果找到了对应位置的结点,返回该结点的值
if (p != NULL) {
return p->data;
}
// 如果没找到对应位置的结点,返回-1
return -1;
}
int main() {
// 创建单链表
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
struct Node* node1 = (struct Node*)malloc(sizeof(struct Node));
struct Node* node2 = (struct Node*)malloc(sizeof(struct Node));
struct Node* node3 = (struct Node*)malloc(sizeof(struct Node));
head->data = 0;
node1->data = 1;
node2->data = 2;
node3->data = 3;
head->next = node1;
node1->next = node2;
node2->next = node3;
node3->next = NULL;
// 按位置查找结点
int pos = 2;
int value = findNode(head, pos);
if (value != -1) {
printf("找到了第%d个结点,值为%d\n", pos, value);
} else {
printf("没有找到第%d个结点\n", pos);
}
// 释放内存
free(head);
free(node1);
free(node2);
free(node3);
return 0;
}
```
输出结果为:
```
找到了第2个结点,值为1
```