#include <stdio.h>
#include <stdbool.h>
#include <string.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode, *LinkList;
bool InitLinkList(LinkList *l)
{
*l = (LNode*)malloc(sizeof(LNode));
if (*l == NULL) {
return false;
}
(*l)->next = NULL; // 注意这里一定要加(),不然 -> 的优先级比 * 的高
return true;
}
bool IsEmptyLinkList(LinkList l)
{
return (l->next == NULL);
}
bool InsertLNode(LinkList *l, int offset, int data)
{
if (offset < 1) {
return false;
}
LNode *p;
int j = 0;
p = *l;
while (p != NULL && j < offset - 1) {
p = p->next;
j++;
}
if (p == NULL) {
return false;
}
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = data;
s->next = p->next;
p->next = s;
return true;
}
bool DeleteLNode(LinkList *l, int offset)
{
if (offset < 1) {
return false;
}
LNode *s;
int j = 0;
s = *l;
while (s != NULL && j < offset - 1) {
s = s->next;
j++;
}
if (s == NULL || s->next == NULL) {
return false;
}
LNode *temp;
temp = s->next;
s->next = temp->next;
free(temp);
return true;
}
int main()
{
LinkList l;
printf("%d\n", InitLinkList(&l));
printf("%d\n", IsEmptyLinkList(l));
printf("%d\n", InsertLNode(&l, 1, 3));
printf("%d\n", InsertLNode(&l, 2, 4));
printf("%d\n", DeleteLNode(&l, 3));
while (l->next != NULL) {
printf("%d\n", l->next->data);
l = l->next;
}
}
Less Interests, More Interest.
少一些功利主义的追求,多一些不为什么的坚持。