链表

来源:互联网 发布:正版windows价格 编辑:程序博客网 时间:2024/06/11 23:52

//////////////////////////////////////////////////

//LinkList

//////////////////////////////////////////////////

typedef void (*DISPLAY)(void*);

typedef int (*COMPARE)(void*,void*);


typedef struct _employee{

    char name[32];

    unsigned char age;

}Employee;


/*

 Node结构体定义一个节点,有两个指针,第一个是void指针,持有任意类型的数据,

 第二个是指向下一个结点指针

 */

typedef struct _node{

    void *data;

    struct _node *next;

}Node ;


/*

 LinkedList结构体表示链表,持有指向头节点和尾节点的指针,当前指针用来辅助遍历链表

 */

typedef struct _linkedList{

    Node *head;

    Node *tail;

    Node *current;

}LinkedList;



void initializeLinkList(LinkedList *);//初始化

void addHead(LinkedList*,void *);  //增加头节点数据

void addTail(LinkedList *,void *); //增加尾节点数据

void delete(LinkedList *,Node*);   //删除结点

Node *getNode(LinkedList *,COMPARE,void*);//返回指定节点的指针

void displayLinkedList(LinkedList *,DISPLAY);//打印链表



void initializeLinkList(LinkedList *list){

    list->head = NULL;

    list->tail = NULL;

    list->current = NULL;

    

}

/*

 1.查链表是否为空,如果空,把尾指针指向节点,然后把节点next字段赋值为NULL

 2.如果不空,将节点的next指向链表头

 3.无论哪种情况,链表头都指向节点

 */

void addHead(LinkedList *list,void *data){

    Node *node = (Node *)malloc(sizeof(Node));

    node->data = data;

    if (list->head ==NULL) {

        list->tail = node;

        node->next = NULL;

    }else{

        node->next = list->head;

    }

    list->head = node;

}


/*

 1.因总是将节点添加到末尾,即该节点的next字段被赋值NULL

 2.如果链表空,head指针就是NULL,把新节点赋给head

 2.如果不空,尾节点的next指针赋为新节点

 3.无论哪种情况,链表tail指针都赋为该节点

 */

void addTail(LinkedList *list,void *data){

    Node *node = (Node *)malloc(sizeof(Node));

    node->data = data;

    node->next = NULL;

    if (list->head ==NULL) {

        list->head = node;

    }else{

        list->tail->next = node;

    }

    list->tail = node;

}


/*

 为了保持函数简单,不检查链表内的空值和传入的节点

 1.第一个if处理删除头节点,如果头节点是唯一节点,那么将头节点和尾节点置为NULL,否则,

   头节点赋为原头节点的下一节点

 2.tmp指针从头到尾遍历链表,不论是将tmp赋值为NULL,还是tmp的下一节点就是要找的节点。

 3.tmp为空,表示要找的节点不在链表中

 4.函数末尾,将节点释放

 */

void delete(LinkedList *list,Node*node){

    if (node==list->head) {

        if (list->head->next ==NULL) {

            list->head=list->tail=NULL;

        }else{

            list->head=list->head->next;

        }

    }else{

        Node *tmp = list->head;

        while (tmp!=NULL && tmp->next !=node) {

            tmp = tmp->next;

        }

        if (tmp!=NULL) {

            tmp->next = node->next;

        }

    }

    free(node);

}


int compareEmployee(Employee *e1,Employee *e2){

    return strcmp(e1->name, e2->name);

}


void displayEmployee(Employee *employee){

    printf("%s\t%d\n",employee->name,employee->age);

}



Node *getNode(LinkedList *list,COMPARE compare,void *data){

    Node *node = list->head;

    while (node!=NULL) {

        if (compare(node->data,data)==0) {

            return node;

        }

        node=node->next;

    }

    return NULL;

}


void displayLinkedList(LinkedList *list,DISPLAY display){

    printf("\nLinked List\n");

    Node *current = list->head;

    while (current!=NULL) {

        display(current->data);

        current=current->next;

    }

}

int main(int argc,constchar * argv[]) {

    @autoreleasepool {

        //LinkList

        LinkedList linkedList;

        Employee *samuel = (Employee *)malloc(sizeof(Employee));

        strcpy(samuel->name,"samuel");

        samuel->age = 32;

        

       

        Employee *sally = (Employee *)malloc(sizeof(Employee));

        strcpy(sally->name,"sally");

        sally->age = 28;

        

        Employee *susan = (Employee *)malloc(sizeof(Employee));

        strcpy(susan->name,"susan");

        susan->age = 56;

        

        initializeLinkList(&linkedList);

        addHead(&linkedList, samuel);

        addHead(&linkedList, sally);

        addHead(&linkedList, susan);

        

        displayLinkedList(&linkedList, (DISPLAY)displayEmployee);

        

        Node *node = getNode(&linkedList, (int(*)(void*,void*))compareEmployee, susan);

        delete(&linkedList, node);

        displayLinkedList(&linkedList, (DISPLAY)displayEmployee);

        

        return 0;

}

}

//增加


//删除



0 0
原创粉丝点击