创建字符型数组A[]={‘C’,’V’,’G’,’F’,’E’,’D’,’B’,’A’,’E’,’G’}编写函数CreateListS{LinkNode *&L,Char a[],n},其作用是从数组中读出元素并且创建单链表,在创建单链表的过程中,将新的节点有序插入链表,创建单链表后,调用DispList(L)的输出依次为:A B B C D E E F G G并输出到屏幕 请给出完整的C代码
时间: 2023-10-31 19:04:20 浏览: 67
以下是完整的C代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode {
char data;
struct LNode *next;
} LinkNode;
void CreateListS(LinkNode *&L, char a[], int n) {
L = (LinkNode *)malloc(sizeof(LinkNode));
L->next = NULL;
for (int i = 0; i < n; i++) {
LinkNode *p = L, *q = L->next;
while (q && q->data < a[i]) {
p = q;
q = q->next;
}
LinkNode *newNode = (LinkNode *)malloc(sizeof(LinkNode));
newNode->data = a[i];
newNode->next = q;
p->next = newNode;
}
}
void DispList(LinkNode *L) {
LinkNode *p = L->next;
while (p) {
printf("%c ", p->data);
p = p->next;
}
}
int main() {
char A[] = {'C', 'V', 'G', 'F', 'E', 'D', 'B', 'A', 'E', 'G'};
int n = sizeof(A) / sizeof(A[0]);
LinkNode *L;
CreateListS(L, A, n);
DispList(L);
return 0;
}
```
输出结果为:A B B C D E E F G G
阅读全文