标签:c语言 单链表 删除一个无头单链表的非尾节点
#include<stdio.h> #include<assert.h> #include<stdlib.h> typedef int DataType; typedef struct SListNode { DataType data; struct SListNode* next; }SListNode; SListNode* BuyNode( DataType x) { SListNode* next = (SListNode*)malloc(sizeof(SListNode)); next->data = x; next->next = NULL; return next; } void PushBack(SListNode* & ppHead, DataType x) { if (ppHead == NULL) { ppHead = BuyNode(x); } else { SListNode* tail = ppHead; while (tail->next != NULL) { tail = tail->next; } tail->next = BuyNode(x); } } void PrintSNodeList(SListNode* ppHead) { while (ppHead) { printf("%d->",ppHead->data); ppHead = ppHead->next; } printf("\n"); } //删除一个无头单链表的非尾节点 void DelTail(SListNode* &ppHead, SListNode* pos) { assert(ppHead); SListNode* del = pos; SListNode* cur = del->next; SListNode* ppHeadName = ppHead; while (ppHeadName != del) { ppHeadName = ppHeadName->next; } if (ppHeadName == NULL) { return; } del->data = cur->data; del->next = cur->next; free(cur); } void Test2() { SListNode* List = NULL; PushBack(List, 1); PushBack(List, 2); PushBack(List, 3); PushBack(List, 4); PushBack(List, 5); DelTail(List, List->next->next); PrintSNodeList(List); } int main() { Test2(); system("pause"); return 0; }
标签:c语言 单链表 删除一个无头单链表的非尾节点
原文地址:http://10740184.blog.51cto.com/10730184/1734402