接上篇带头结点的单链表操作,不带头节点真的比带头结点的麻烦好多,主要是我自己指针指的有点晕,搞了这几天的不带头结点的单链表,算是弄清楚指针的玩法了。。老样子上代码。
# include "stdlib.h"
# include "stdio.h"
# define OK 1
# define ERROR 0
typedef int Status;//自定义数据类型 相当于int仅用作装逼
/**
* 实现一个不带头结点的单链表
*/
//节点类
typedef struct LNode{
int data;//传说中的数据域 结构体A.next 指针 A->next
struct LNode *next;//传说中的指针域
}LNode;
//头插法建立没有头结点的单链表
Status headInitList(LNode **L,int A[]){
(*L) = NULL;
int len = 0;
while(*(A+(len++))!='\0');
len--;
//给首元节点赋值
(*L) = (LNode*)malloc(sizeof(LNode));
(*L)->data = A[--len];
(*L)->next = NULL;
LNode *p = *L;
for (int i = len; i > 0; i--)
{
//实际就是将数多次插入第一个位置
LNode* new = (LNode*)malloc(sizeof(LNode));
new->data = p->data;
new->next = p->next;
p->data = A[i-1];
p->next = new;
}
printf("头插法建立单链表成功\n");
return OK;
}
//尾插法建立没有头结点的单链表
Status tailInitList(LNode **L,int A[]){
(*L) = NULL;
int len = 0;
while(*(A+(len++))!='\0');
len--;<