//移除最后一个的节点 使用二级指针更好
int removeFromLast(node ** firstlocation) {
node *head = *firstlocation;
if (head == NULL) {
printf("have no number");
return 0;
}
if (head->next == NULL) {
*firstlocation = NULL;
free(head);
return 1;
}
node * current = head;
while (current->next->next != NULL) {
current = current->next;
}
free(current->next);
current->next = NULL;
return 0;
}
//打印所有的节点 通过头节点
void printList(node * head) {
node *current = head;
if (current == NULL) {
printf("have no value");
return;
}
while (current != NULL) {
printf("%d\n", current->value);
current = current->next;
}
}
//在结尾添加一个数字
void pushEnd(node * head, int val) {
node * current = head;
while (current->next != NULL) {
current = current->next;
}
node * va = malloc(sizeof(node));
va->next = NULL;
va->value = val;
current->next = va;
}
//添加到首个节点
void addFirst(int val, node ** head) {
node* current = malloc(sizeof(node));
current->value = val;
current->next = *(head);
*head = current;
}
/**
* 移除第一个节点,返回第一个值,修改head指针
*/
int removeFromFrist(node ** head) {
node * first = NULL;
if (*head == NULL) {
return -1;
}
int va = (*head)->value;
first = *head;
*head = (*head)->next;
free(first);
return va;
}
//移除最后一个元素
//条件是:首位为空情况下,第二位为空情况下,第三位为空?
//为啥并灭有实际改变这个值??
//通过下标移除节点
//从中间进行移除,需要从中间进行删除,所以,需要知道之前节点,然后知道后面节点 长度大于这个长度怎么办
void removeByIndex(int index, node**head) {
node *current = *head;
if (index == 0) {
*head = current->next;
free(current);
return;
}
int i;
node * previous;
for (i = 0; i < index; i++) {
if (current != NULL) {
previous = current;
current = current->next;
} else {
return;
}
}
//将节点放到接到后面,然后释放到当前节点内存
if (current == NULL) {
printf("链表中没有该节点\n");
} else {
previous->next = current->next;
free(current);
}
}
/**
*通过值进行删除,首先考虑删除一个,然后考虑如何进行删除多个操作
*删除多个可以通过多次循环执行这东西,所以需要有所返回
*/
int removeByValue(node**head, int val) {
node *current = *head;
if (current->value == val) {
int th = current->value;
*head = current->next;
free(current);
return th;
}
node * previous;
while (current != NULL && current->value != val) {
previous = current;
current = current->next;
}
if (current != NULL) {
int th = current->value;
previous->next = current->next;
free(current);
return th;
} else {
printf("don't have this value\n");
return -1;
}
}
链表合并:
//合并两个链表 获取的链表是有序的 被合并的两个链表也是有序的,这里都是升序
//改变两个链表,利用之前链表生成新的链表
int megeList(node *first, node * second) {
node * head;
node * one; //用于记录首个节点的地址
if (first->value <= second->value) {
head = first;
one = first;
first = first->next;
} else {
head = second;
one = second;
second = second->next;
}
while (first != NULL && second != NULL) {
//递增,就要找到最小的值
if (first->value <= second->value) {
head->next = first;
head = first;
first = first->next;
} else {
head->next = second;
head = second;
second = second->next;
}
}
head->next = first ? first : second;
return one;
}
//合并值,并不改变之前的两个链表
int mergeValue(node* fir, node* sec) {
node * head = malloc(sizeof(node));
node *one = head;
if (fir->value <= sec->value) {
head->value = fir->value;
fir = fir->next;
} else {
head->value = sec->value;
sec = sec->next;
}
while (fir!=NULL && sec!=NULL) {
head->next = malloc(sizeof(node));
head = head->next;
if (fir->value <= sec->value) {
head->value = fir->value;
fir = fir->next;
} else {
head->value = sec->value;
sec = sec->next;
}
}
while (fir) {
head->next = malloc(sizeof(node));
head = head->next;
head->value = fir->value;
fir = fir->next;
}
while (sec) {
head->next = malloc(sizeof(node));
head = head->next;
head->value = sec->value;
sec = sec->next;
}
head->next = NULL;
return one;
}
写着玩的,木有啥用