关于链表的创建、输出、释放问题,具体可以看本人之前的一篇文章:
c语言 创建链表,输出和释放
这里以将字符串‘I love Program’放入链表中为例:
- 尾插法
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define max 14
struct list
{
char data;
struct list *next;
};
typedef struct list node;
typedef node *link;
char str[max]={'I',' ','l','o','v','e',' ','P','r','o','g','r','a','m'};
//创建链表
link Create_list(link head)
{
int i;
link pointer;
link newpointer;
head=(link)malloc(sizeof(node));
if(head==NULL)
printf("error!!");
else
{
head->data=str[0];
head->next=NULL;
}
pointer=head;
for(i=1;i<max;i++)
{
newpointer=(link)malloc(sizeof(node));
if(newpointer==NULL)
pr