#include <stdio.h>
#include <stdlib.h>
typedef char data_t;
typedef struct node
{
data_t data;
struct node *next;
} node_t, *node_p;
node_p create_list()
{
node_p p = (node_p)malloc(sizeof(node_t));
if (NULL == p)
{
printf("malloc error\n");
return NULL;
}
p->next = NULL;
return p;
}
int insert(node_p p, int post, data_t data)
{
node_p new = (node_p)malloc(sizeof(node_t));
if (NULL == new)
{
printf("malloc error\n");
return -1;
}
new->data = data;
new->next = NULL;
int i = 0;
while (i < post)
{
p = p->next;
i++;
}
new->next = p->next;
p->next = new;
return 0;
}
void show(node_p p)
{
if (NULL == p)
return;
show(p->next);
printf("%c ", p->data);
}
int main()
{
node_p p = create_list();
data_t ch='a';
int i = 0;
while ('a' <= ch && ch <= 'z')
{
insert(p, i, ch);
ch++;
i++;
}
show(p);
printf("\n");
return 0;
}
建立单向链表,把‘a‘--‘z‘26个字母插入到链表中,倒序输出。
最新推荐文章于 2024-11-02 20:41:42 发布