//尾删 void SListPopBack(SLTNode** pphead) { //1.空 if (*pphead == NULL) { return; } //2.一个结点 else if ((*pphead)->next == NULL) { free(*pphead); *pphead = NULL; } //3.多个节点,至少两个 else { SLTNode* prev = NULL; SLTNode* tail = *pphead; while (tail->next != NULL) { prev = tail; tail = tail->next; } free(tail); prev->next = NULL; } }
//查找 SLTNode* SListFind(SLTNode* phead, SLTDataType x) { SLTNode* cur = phead; //while (cur != NULL) while (cur) { if (cur->data == x) { return cur; } cur = cur->next; } return NULL; }
//在查找的那个数的前面插入x void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x) { SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode)); newnode->data = x; newnode->next = NULL; if (pos == *pphead) { SListPushFront(pphead, x); } else { SLTNode* prev = *pphead; while (prev->next != pos) { prev = prev->next; } prev->next = newnode; newnode->next = pos; } }
//删除pos位置的值 void SListErase(SLTNode** pphead, SLTNode* pos) { //1.为第一个结点或者为空 if (pos==*pphead) { SListPopFront(pphead); } else { SLTNode* prev = *pphead; while (prev->next != pos) { prev = prev->next; } prev->next = pos->next; free(pos); } }
好了今天就到这了,想必大家也学会到了许多啦!!!
我们继续加油哦!
下篇博客是双链表哦!