用c语言创建一个存放了{1,2,3}3个元素的带头结点的单链表L2
时间: 2023-06-01 17:04:36 浏览: 68
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode {
int val;
struct ListNode* next;
} ListNode;
void insert(ListNode* head, int val) {
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->val = val;
node->next = head->next;
head->next = node;
}
void printList(ListNode* head) {
ListNode* p = head->next;
while(p != NULL) {
printf("%d ", p->val);
p = p->next;
}
printf("\n");
}
int main() {
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
head->next = NULL;
insert(head, 3);
insert(head, 2);
insert(head, 1);
printList(head);
return 0;
}
```
相关问题
用c语言创建带头节点单链表List L2并放入三个元素
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}Node;
typedef struct{
Node *head;
int length;
}List;
void initList(List *L){
L->head = (Node*)malloc(sizeof(Node));
L->head->next = NULL;
L->length = 0;
}
void insert(List *L, int index, int data){
if(index < 0 || index > L->length){
printf("插入位置不合法\n");
return;
}
Node *p = L->head;
for(int i = 0; i < index; i++){
p = p->next;
}
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = p->next;
p->next = newNode;
L->length++;
}
void printList(List *L){
Node *p = L->head->next;
while(p){
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main(){
List L2;
initList(&L2);
insert(&L2, 0, 1);
insert(&L2, 1, 2);
insert(&L2, 2, 3);
printList(&L2);
return 0;
}
请用C语言创建一个不带头结点的单链表
好的,以下是创建一个不带头结点的单链表的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int main() {
struct Node *head = NULL, *tail = NULL, *temp = NULL;
int num;
printf("请输入整数,输入 -1 结束:\n");
scanf("%d", &num);
while (num != -1) {
temp = (struct Node *) malloc(sizeof(struct Node));
temp -> data = num;
temp -> next = NULL;
if (head == NULL) {
head = temp;
tail = temp;
} else {
tail -> next = temp;
tail = temp;
}
scanf("%d", &num);
}
printf("链表的值为:\n");
temp = head;
while (temp != NULL) {
printf("%d ", temp -> data);
temp = temp -> next;
}
return 0;
}
```
希望这能够帮到您!
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)