下列程序用来生成一个带头结点的单链表,并将字符串str中的每一个字符存放到该单链表中去,要求单链表中的每个结点存放4个字符
时间: 2023-05-21 18:05:50 浏览: 125
以下是需要填写的程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node{
char data[4];
struct Node *next;
}Node, *LinkedList;
LinkedList createLinkedList(char *str){
LinkedList head, p, q;
head = (LinkedList)malloc(sizeof(Node));
head->next = NULL;
q = head;
int i, j = 1;
for(i=0;i<strlen(str);i++){
if(j == 1){
p = (LinkedList)malloc(sizeof(Node));
p->next = NULL;
q->next = p;
}
p->data[j-1] = str[i];
j = (j%4)+1;
}
return head;
}
int main(){
char str[100];
scanf("%s", str);
LinkedList head = createLinkedList(str);
return 0;
}
以上程序可以用来生成带头结点的单链表,并将字符串str中的每一个字符存放到该单链表中去,要求单链表中的每个结点存放4个字符。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.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)
![](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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)