C语言实现链表

来源:互联网 发布:数据挖掘工程师培训 编辑:程序博客网 时间:2024/05/16 05:33
# define Elem intstruct Node{    int num;    Elem elem;    struct Node *next;    struct Node *previous;};struct Link{    struct Node *head;    struct Node *end;    int length;};// 新建链表struct Link * creatLink(){    // 新建并初始化头节点    struct Node *head =(struct Node *) malloc (sizeof(struct Node));    head -> next = NULL;    head -> previous = NULL;    head -> num = 0;    head -> elem = 0;    // 新建并初始化链表    struct Link *link = (struct Link *)malloc(sizeof(struct Link ));    link -> head = head ;    link -> end = head;    link -> length = 0;        return link;}// 输出链表内的所有元素void showAll(struct Link *link){    // 新建节点    struct Node *p = link -> head;        while (p != NULL) {        printf("%d:%d\n", p -> num, p -> elem);        p = p -> next;    }    printf("共有%d个元素!\n",link -> length);}// 在链表最后添加新元素void add(struct Link *link,int elem){    // 得到最后的节点    struct Node *end = link -> end;    // 新建并初始化节点    struct Node *p = (struct Node *) malloc(sizeof(struct Node));    p -> num = (end -> num) + 1;    p -> elem = elem;    p -> previous = end;    p -> next = NULL;    // 将链表的尾节点修改为新建的节点,并将数量加1    // 在空链表上添加新元素    if( end ->num == 0){        link -> head -> next = p;        link -> end = p;        link -> length = (link -> length) + 1;    }    // 不是在空链表上添加元素    else{        end -> next = p;        link -> end = p;        link -> length = (link -> length) + 1;    }}// 删除链表第一个元素void removeFirst(struct Link *link){    if((link -> length) > 0){        struct Node *head = link -> head ;        struct Node *first = head -> next;        head -> next = first -> next;        first -> next -> previous = head ;        link -> length = (link -> length) - 1;    }    else{        printf("空链表!\n");    }}// 删除链表最后一个元素void removeLast(struct Link *link){    if((link -> length) > 0){        struct Node *end = link -> end;        struct Node *lastEnd = end -> previous;        lastEnd ->next =NULL;        link -> end = lastEnd;        link -> length = (link -> length) - 1;    }    else{        printf("空链表!\n");    }}// 获得链表里第一个元素Elem getFirst(struct Link *link){    if((link -> length) > 0){        struct Node *first = link -> head -> next;        Elem elem = first -> elem;                return  elem;    }    else{        printf("空链表!\n");        return FALSE;    }}// 获得链表里最后一个元素Elem getLast(struct Link *link){    if((link -> length) > 0){        Elem elem = link -> end -> elem;                return elem;    }    else{        printf("空链表!\n");        return FALSE;    }}int main(int argc, const char * argv[]) {        // 新建链表    struct Link *link ;    link = creatLink();        // 输出链表    showAll(link);        removeFirst(link);            // 添加新元素    printf("添加新元素!\n");    add(link, 2);    add(link, 2);    add(link, 2);    add(link, 2);    add(link, 2);    add(link, 3);    add(link, 4);    showAll(link);        // 获得第一个元素    printf("第一个元素是:%d\n",getFirst(link));        // 移除第一个元素    printf("移除第一个元素!\n");    removeFirst(link);    showAll(link);        // 获得最后一个元素    printf("最后一个元素是:%d\n",getLast(link));        // 移除最后一个元素    printf("移除最后一个元素!\n");    removeLast(link);    showAll(link);    printf("移除最后一个元素!\n");    removeLast(link);    showAll(link);    printf("移除最后一个元素!\n");    removeLast(link);    showAll(link);            // 获得最后一个元素    printf("最后一个元素是:%d\n",getLast(link));        // 获得第一个元素    printf("第一个元素是:%d\n",getFirst(link));        // 移除第一个元素    printf("移除第一个元素!\n");    removeFirst(link);    showAll(link);    return 0;}

0 0
原创粉丝点击