C/C++ | 26-17 已知一个单向链表的头,删除其某一个结点的算法

来源:互联网 发布:微分销java源码 编辑:程序博客网 时间:2024/05/20 21:56

/*
已知一个单向链表的头,请写出删除其某一个结点的算法,
要求,先找到此结点,然后删除。
*/




#include <cstdio>#include <deque>#include <algorithm>#include <iterator>#include <stdio.h>  #include <stdlib.h>#include <iostream>#include <string.h>#include <assert.h>using namespace std;typedef struct LNode{int data;struct LNode *next;}LNode;LNode *CreatLink(int num)  // 构造{int i=1;LNode *head, *tail, *p;head = (LNode *)malloc(sizeof(LNode));tail = head;while (num--){p = (LNode *)malloc(sizeof(LNode));p->data = i;tail->next = p;tail = p;i++;}tail->next = NULL;return head;}LNode *FindLink(LNode *head,int X)  //查找{assert( head != NULL);LNode *chick=head->next;while (chick != NULL){if (chick->data == X)return chick;elsechick = chick->next;}return NULL;}void DeleteLink(LNode *head, int X){assert(head != NULL);LNode *p;LNode *q;p=FindLink(head, X);q = p->next;p->data = q->data;p->next = q->next;free(q);}int main(){LNode *head = CreatLink(50);DeleteLink(head, 5);system("pause");return 0;}


阅读全文
0 0
原创粉丝点击