#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct node
{
char data;
struct node* next;
};
struct node* tailinsert();
void destroy(struct node* head);
void traverse(struct node* head);
void traverse(struct node* head)
{
if (head == NULL)
{
return;
}
else
{
struct node* tmp = head->next;
while (tmp != NULL)
{
if (tmp->next != NULL)
{
printf("%c ", tmp->data);
}
else
{
printf("%c", tmp->data);
}
tmp = tmp->next;
}
printf("\n");
}
}
void destroy(struct node* head)
{
if (head == NULL)
{
return;
}
else
{
struct node* P;
struct node* tmp;
P = head->next;
head->next = NULL;
while (P != NULL)
{
tmp = P->next;
free(P);
P = tmp;
}
free(head);
head = NULL;
}
}
struct node* tailinsert()
{
char arr[5];
int i = 0;
scanf("%s",arr);
struct node* begin = (struct node*)malloc(sizeof(struct node));
if (begin == NULL)
{
return;
}
struct node* end = begin;
end->next = NULL;
for (i = 0; i < 5; i++)
{
struct node* new = (struct node*)malloc(sizeof(struct node));
if (new == NULL)
{
return;
}
new->data = arr[i];
new->next = NULL;
end->next = new;
end = new;
}
return begin;
}
void main()
{
struct node* head = NULL;
head = tailinsert();
traverse(head);
destroy(head);
}
如图:不明白的追问