单链表

来源:互联网 发布:淘宝退货步骤 编辑:程序博客网 时间:2024/06/03 21:08
#include<iostream>using namespace std;struct Node {    int data;    Node *next;};int add(Node * head, int data) {    Node *s = (Node *)new(Node);    if (!s)        return 0;    s->data = data;    s->next = head->next;    head->next = s;    return 1;}int deleteNode(Node * head,int c) {    Node *p = head;//找到这个节点    Node *s = p->next;    while (s != NULL){        if (s->data != c) {            p = p->next;            s = s->next;        }        else{            p->next = s->next;            free(s);            return 1;        }    }}void retrieve(Node * head, void (*visit)(int)) {//对每一个节点调用指定函数    Node *p = head;    while (p != NULL)    {        (*visit)(p->data);        p = p->next;    }}

0 0