数据结构奇偶链表c语言
时间: 2023-11-20 22:05:50 浏览: 67
以下是一个C语言实现的奇偶链表的例子:
```c
struct ListNode* oddEvenList(struct ListNode* head) {
if (head == NULL) {
return head;
}
struct ListNode* odd = head; // 奇数第一个结点
struct ListNode* evenHead = head->next; // 保存偶数头结点
struct ListNode* even = evenHead; // 偶数第一个结点
while (even != NULL && even->next != NULL) {
odd->next = even->next; // 奇偶指针的变换
odd = odd->next;
even->next = odd->next;
even = even->next;
}
odd->next = evenHead; // 奇数链表的尾巴指向偶数链表的头
return head;
}
```
相关问题
数据结构线性链表c语言代码
线性链表是一种常见的数据结构,它由一系列的节点组成,每个节点包含数据和指向下一个节点的指针。下面是一个用C语言实现的线性链表的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义节点结构
typedef struct Node {
int data;
struct Node* next;
} Node;
// 创建链表
Node* createLinkedList() {
Node* head = (Node*)malloc(sizeof(Node));
head->next = NULL;
return head;
}
// 在链表末尾插入节点
void insert(Node* head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
// 在链表指定位置插入节点
void insertAt(Node* head, int data, int position) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
Node* current = head;
int i = 0;
while (current->next != NULL && i < position - 1) {
current = current->next;
i++;
}
newNode->next = current->next;
current->next = newNode;
}
// 删除链表指定位置的节点
void deleteAt(Node* head, int position) {
Node* current = head;
int i = 0;
while (current->next != NULL && i < position - 1) {
current = current->next;
i++;
}
if (current->next != NULL) {
Node* temp = current->next;
current->next = temp->next;
free(temp);
}
}
// 输出链表中的所有节点
void display(Node* head) {
Node* current = head->next;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node* myList = createLinkedList();
// 插入节点
insert(myList, 1);
insert(myList, 3);
insert(myList, 5);
insert(myList, 7);
// 在指定位置插入节点
insertAt(myList, 9, 2);
// 删除指定位置的节点
deleteAt(myList, 3);
// 输出链表
display(myList);
return 0;
}
```
以上代码实现了线性链表的基本操作,包括创建链表、在链表末尾插入节点、在指定位置插入节点、删除指定位置的节点,以及输出链表中的所有节点。使用这些操作可以对线性链表进行增删改查等操作。
数据结构链表c语言
链表是一种数据结构,它由多个节点组成,每个节点包含了数据和指向下一个节点的指针。链表可以动态地增加或删除节点,因此非常适合需要频繁操作的场景。
下面是一个简单的链表的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
// 链表节点
struct Node {
int data; // 数据
struct Node* next; // 指向下一个节点的指针
};
// 创建节点
struct Node* createNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}
// 在链表末尾插入节点
void insertAtEnd(struct Node** head, int data) {
struct Node* node = createNode(data);
if (*head == NULL) {
*head = node;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = node;
}
}
// 在链表头部插入节点
void insertAtBeginning(struct Node** head, int data) {
struct Node* node = createNode(data);
node->next = *head;
*head = node;
}
// 删除链表中的节点
void deleteNode(struct Node** head, int data) {
struct Node* current = *head;
struct Node* prev = NULL;
while (current != NULL && current->data != data) {
prev = current;
current = current->next;
}
if (current == NULL) {
printf("Node with data %d not found\n", data);
return;
}
if (prev == NULL) {
*head = current->next;
} else {
prev->next = current->next;
}
free(current);
}
// 打印链表
void printList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtBeginning(&head, 0);
printList(head);
deleteNode(&head, 2);
printList(head);
return 0;
}
```
这个程序定义了一个`Node`结构体表示链表节点,包含了数据`data`和指向下一个节点的指针`next`。函数`createNode`用来创建节点,函数`insertAtEnd`用来在链表末尾插入节点,函数`insertAtBeginning`用来在链表头部插入节点,函数`deleteNode`用来删除节点,函数`printList`用来打印链表。
在`main`函数中,我们创建了一个空链表`head`,然后依次插入了四个节点。最后打印链表,删除节点2,再次打印链表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)