c语言 for循环遍历链表
时间: 2023-08-06 10:14:15 浏览: 130
在C语言中,可以使用for循环遍历链表。假设我们有一个链表结构体如下:
```
struct node {
int data;
struct node *next;
};
```
其中,data表示节点存储的数据,next是指向下一个节点的指针。
现在假设我们已经创建了一个链表,并且头指针为head。我们可以使用for循环遍历链表,代码如下:
```
struct node *p;
for (p = head; p != NULL; p = p->next) {
printf("%d ", p->data);
}
```
上述代码中,我们定义了一个指针p,初始化为头指针head。然后在循环中,我们判断p是否为NULL,如果不是,则打印p节点的数据,并将指针p指向下一个节点p->next。这样就可以逐个遍历整个链表了。
相关问题
c语言创建双向循环链表
双向循环链表是一种特殊的链表,它的每个节点都有两个指针,一个指向前一个节点,一个指向后一个节点,而最后一个节点的后继指针指向头结点,头结点的前驱指针指向最后一个节点。下面是一个简单的 C 语言程序示例,用于创建双向循环链表:
```c
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
struct node *head = NULL;
struct node *tail = NULL;
void create_list(int n);
void display_list();
int main() {
int n;
printf("Enter the number of nodes: ");
scanf("%d", &n);
create_list(n);
printf("Data in the list:\n");
display_list();
return 0;
}
void create_list(int n) {
int i, data;
struct node *newNode;
if (n <= 0) {
printf("List size must be greater than zero.\n");
return;
}
head = (struct node*)malloc(sizeof(struct node));
if (head == NULL) {
printf("Unable to allocate memory.");
return;
}
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->prev = NULL;
head->next = NULL;
tail = head;
for (i = 2; i <= n; i++) {
newNode = (struct node*)malloc(sizeof(struct node));
if (newNode == NULL) {
printf("Unable to allocate memory.");
break;
}
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->prev = tail;
newNode->next = head;
tail->next = newNode;
tail = newNode;
head->prev = tail;
}
printf("List created successfully.\n");
}
void display_list() {
struct node *current;
if (head == NULL) {
printf("List is empty.\n");
return;
}
current = head;
printf("List is:\n");
do {
printf("%d\n", current->data);
current = current->next;
} while (current != head);
}
```
在这个程序中,我们定义了一个结构体 `node`,它包含三个成员:`data`,`prev` 和 `next`,分别表示节点的数据、前驱指针和后继指针。我们还定义了两个指针 `head` 和 `tail`,分别指向链表的头结点和尾结点。
在 `create_list` 函数中,我们首先创建头结点,并让 `head` 和 `tail` 指向头结点。然后通过 `for` 循环创建其他节点,并将它们插入到链表的尾部。每个节点的前驱指针指向上一个节点,后继指针指向下一个节点,最后一个节点的后继指针指向头结点,头结点的前驱指针指向最后一个节点。
在 `display_list` 函数中,我们首先判断链表是否为空,然后通过 `do-while` 循环遍历链表,输出每个节点的数据。由于这是一个双向循环链表,循环条件为 `current != head`。
c语言建立单循环链表
你可以使用以下代码来建立一个单循环链表的数据结构:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构
struct Node {
int data;
struct Node* next;
};
// 在链表末尾插入节点
void insertAtEnd(struct Node** head, int data) {
// 创建新节点
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
// 如果链表为空,将新节点设为头节点
*head = newNode;
newNode->next = *head;
} else {
// 找到链表的尾节点
struct Node* tail = *head;
while (tail->next != *head) {
tail = tail->next;
}
// 将新节点插入到尾节点后面
tail->next = newNode;
newNode->next = *head;
}
}
// 遍历并打印链表中的元素
void printList(struct Node* head) {
if (head == NULL) {
printf("链表为空。\n");
return;
}
struct Node* current = head;
printf("链表的元素:");
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
int main() {
struct Node* head = NULL; // 初始化头节点为空
// 在链表末尾插入节点
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
// 打印链表中的元素
printList(head);
return 0;
}
```
这段代码定义了一个 `Node` 结构来表示链表的节点,包含一个整数数据和一个指向下一个节点的指针。`insertAtEnd` 函数用于在链表末尾插入节点,`printList` 函数用于遍历并打印链表中的元素。在 `main` 函数中,我们创建一个头节点,并在链表末尾插入一些节点,然后打印链表中的元素。请注意,在插入第一个节点时,需要将新节点的 `next` 指针指向头节点,以形成循环链表。