链表)将数组的内容,用建立有序(从小到大)一个带有头节点的单向链表。 #include "stdlib.h" #include "stdio.h" #define N 10 typedef struct list { int data; struct list *next; }NODE; NODE creat_list(int *a,int n) { } print(NODE *h) //打印链表内容 { NODE *p; p=h->next; while(p!=NUll) { printf("%d ",p->data); p=p->next; } printf("\n"); } int main() { int i,j,a[N]={1,13,25,37,9,911,133,415,147,19}; NODE *head; head=creat_list(a,10) print(head); }
时间: 2024-01-15 10:02:29 浏览: 23
好的,我可以帮你回答这个问题。
以下是将数组的内容用建立有序(从小到大)一个带有头节点的单向链表的代码:
```c
NODE *creat_list(int *a,int n) {
NODE *head,*p,*q;
int i;
head=(NODE*)malloc(sizeof(NODE));
head->next=NULL; //头节点指针域为空
for(i=0;i<n;i++) {
p=(NODE*)malloc(sizeof(NODE));
p->data=a[i];
q=head; //从头节点开始查找插入位置
while(q->next!=NULL && q->next->data<p->data)
q=q->next;
p->next=q->next; //将p结点插入到链表中
q->next=p;
}
return head;
}
```
这个函数与前一个问题的代码非常相似,唯一的区别是在插入节点时使用了小于号,这样就可以按照从小到大的顺序将元素插入到链表中。具体地,在循环中,首先创建一个新的节点p,将数组元素赋值给它。然后从头节点开始查找插入位置,直到找到第一个大于等于p的节点,将p插入到该节点之前。最后返回头节点指针head。
你可以将这段代码加入你的程序中,然后运行main函数,调用creat_list和print函数就可以输出链表内容了。
相关问题
1!(链表)先建立有序(从小到大)带有头节点的单向链表,再删除 个节点,并将链表输出。
好的,我来解答你的问题。
首先,我们需要定义链表节点的结构体:
```c
typedef struct Node {
int val;
struct Node *next;
} ListNode;
```
然后,我们可以编写一个函数,用于在有序链表中插入节点:
```c
ListNode* insertNode(ListNode *head, int val) {
ListNode *newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->val = val;
newNode->next = NULL;
if (head == NULL || val < head->val) {
newNode->next = head;
head = newNode;
} else {
ListNode *cur = head;
while (cur->next != NULL && cur->next->val < val) {
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
}
return head;
}
```
接下来,我们可以编写一个函数,用于删除指定节点:
```c
ListNode* deleteNode(ListNode *head, int val) {
if (head == NULL) {
return NULL;
}
if (head->val == val) {
ListNode *temp = head;
head = head->next;
free(temp);
return head;
}
ListNode *cur = head;
while (cur->next != NULL && cur->next->val != val) {
cur = cur->next;
}
if (cur->next != NULL) {
ListNode *temp = cur->next;
cur->next = cur->next->next;
free(temp);
}
return head;
}
```
最后,我们可以编写一个函数,用于输出整个链表:
```c
void printList(ListNode *head) {
ListNode *cur = head;
while (cur != NULL) {
printf("%d ", cur->val);
cur = cur->next;
}
printf("\n");
}
```
完整代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int val;
struct Node *next;
} ListNode;
ListNode* insertNode(ListNode *head, int val) {
ListNode *newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->val = val;
newNode->next = NULL;
if (head == NULL || val < head->val) {
newNode->next = head;
head = newNode;
} else {
ListNode *cur = head;
while (cur->next != NULL && cur->next->val < val) {
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
}
return head;
}
ListNode* deleteNode(ListNode *head, int val) {
if (head == NULL) {
return NULL;
}
if (head->val == val) {
ListNode *temp = head;
head = head->next;
free(temp);
return head;
}
ListNode *cur = head;
while (cur->next != NULL && cur->next->val != val) {
cur = cur->next;
}
if (cur->next != NULL) {
ListNode *temp = cur->next;
cur->next = cur->next->next;
free(temp);
}
return head;
}
void printList(ListNode *head) {
ListNode *cur = head;
while (cur != NULL) {
printf("%d ", cur->val);
cur = cur->next;
}
printf("\n");
}
int main() {
ListNode *head = NULL;
// 插入节点
head = insertNode(head, 4);
head = insertNode(head, 2);
head = insertNode(head, 1);
head = insertNode(head, 3);
head = insertNode(head, 5);
// 输出链表
printList(head);
// 删除节点
head = deleteNode(head, 3);
// 输出链表
printList(head);
return 0;
}
```
希望能够帮到你!
使用尾插法建立带头节点的单链表。 #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode
尾插法建立带头节点的单链表是指从链表的头节点开始,依次向后遍历链表,找到链表的尾节点,然后将新节点插入到尾节点之后的位置。这种方法可以保证新节点插入到链表的末尾,同时也不需要遍历整个链表来找到插入位置,因此效率比较高。在建立带头节点的单链表时,需要先创建一个头节点,并将其指针域指向NULL,然后再按照尾插法的方式插入新节点。