单链表的数据结构:包含一个数据域和指针(指向下一个结点)
typedef char ElemType; /* 定义结点数据类型 */
typedef struct LNode{ /* 定义数据结构 */
ElemType data;
struct LNode *next;
}LNode;
创建一个单链表
struct LNode *Creat_LinkList()
{
struct LNode *L = (LNode *)malloc(sizeof(LNode));
if(NULL == L){
return NULL;
}else{
L->next = NULL;
L->data = '\0';
}
return L;
}
对单链表赋值
int input(LNode *L,ElemType *elem,int count)
{
if(NULL == L){
return -1;
}
int i = 0;
int slen = count; /* 传入数据长度 */
struct LNode *tail = L; /* 尾结点 */
struct LNode *new_node = NULL; /* 新结点 */
/* 寻找尾结点 */
while(tail->next != NULL){
tail = tail->next;
}
/* 插入结点 */
for(i=0; i<slen; i++){
new_node = (LNode *)malloc(sizeof(LNode));
if(NULL == new_node){
return -1;
}else{
new_node->data = elem[i];
tail->next = new_node; /* 连接新结点 */
tail = new_node; /* 更新尾指针 */
}
tail->next = NULL;
}
return OK;
}