您的位置:首页 > 其它

双向有头节点链表

2016-03-03 22:16 225 查看
#include<stdio.h>

#include<malloc.h>

#include<stdlib.h>

#include<stdbool.h>

#define MALLOC(name,type1,type2)\

    if(!((name) = (type1)malloc(sizeof(type2)))){\

        fprintf(stderr,"Insufficient_memmory");\

        exit(EXIT_FAILURE);\

    }

typedef enum {OK,ERROR}Status;

typedef int elemtype;

typedef struct Node* lInkedlist;

struct Node{

    elemtype data;

    struct Node* next;

    struct Node* prior;    

};

lInkedlist Listinit(lInkedlist head);

/*不带头节点链表初始化操作

*input:指向头节点的指针 head

 return:初始化后的head

*/

unsigned int Listlength(lInkedlist head);

/*计算链表长度

*input:指向头节点的指针 head

*返回链表的长度

*/

lInkedlist Listget(lInkedlist head,unsigned int i);

/*取链表第i个节点

*input:指向头节点的指针 head,位置 i

*返回指向链表第i个节点的指针,否则返回NULL

*/

unsigned int Listlocate(lInkedlist head,elemtype e);

/* 定位函数

*若链表中存在其值和e相等的元素,则返回该元素在链表中的次序,

*若有多个返回次序最小的一个,没有返回0

*/

Status Listinsert(lInkedlist head,lInkedlist N,elemtype e);

/*向链表N节点之后插入元素为e的新的节点

*节点N,元素e

return:插入成功返回OK;

*/

Status Listdelete(lInkedlist head,lInkedlist N);

/*删除链表中N节点

*input:指向头节点的指针 head,节点N

return:删除成功返回OK,若链表中没有该节点返回ERROR

*/

void Listtraverse(lInkedlist head);

/*链表遍历打印所有节点

*input:指向头节点的指针 head

*/

bool Listempty(lInkedlist head);

lInkedlist Listinit(lInkedlist head){

    MALLOC(head,lInkedlist,struct Node);

    head->next = NULL;

    return head;

}

    

unsigned int Listlength(lInkedlist head){

    unsigned int length = 0;

    head=head->next;

    for(;head;head=head->next){

        length++;

    }

    return length;

}

lInkedlist Listget(lInkedlist head,unsigned int i){

    head = head->next;

    if(i == 0 || i > Listlength(head)){

        return NULL;

    }

    for(;i>1;i--){

        head = head->next;

    }

    return head;

}

unsigned int Listlocate(lInkedlist head,elemtype e){

    unsigned int count = 0;

    head = head->next;

    for(;head;head = head->next){

        count++;

        if(e == head->data){

            return count;

        }

    }

    return 0;

}

Status Listinsert(lInkedlist head,lInkedlist N,elemtype e){

    lInkedlist temp;

    MALLOC(temp,lInkedlist,struct Node);

    temp->data = e;

    

     if(N == head){

         temp->next = head->next;

         head->next = temp;

         temp->prior = head;

     }

    else{

        temp->prior = N->prior;

        N->prior->next = temp;

        temp->next = N;

        N->prior = temp;

    }

        

    return OK;

}

Status Listdelete(lInkedlist head,lInkedlist N){

    unsigned int i = Listlocate(head,N->data);

    if(i == 0){

        printf("链表中不存在该节点无法删除");

        return ERROR;

    }

    

    N->prior->next = N->next;

    N->next->prior = N->prior;

    

    free(N);

    N = NULL;

    

    return OK;    

}

void Listtraverse(lInkedlist head){

    head = head->next;

    if(head == NULL){

        printf("链表为空");

    }

    for(;head;head = head->next){

        printf("%4d\n",head->data);

    }

}

bool Listempty(lInkedlist head){

    head = head->next;

    return !head;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: