系统程序员成长计划001第一章双向链表

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef unsigned char    uint8_t;    //1
typedef unsigned short   uint16_t;   //2
typedef unsigned int     uint32_t;

/*
struct n{
    int a;
    struct n *b;
}x;

    x.a = 1;
    x.b = NULL;
*/

typedef struct _node{
    int data;
    struct _node* pf;
    struct _node* pn;
}node_t;

//}node_t, *pnode_t;
//相当于typedef node_t *pnode;


node_t* node_create(int val)
{
    node_t* p = (node_t*)malloc(sizeof(node_t));
    if(p){
        p->data = val;
        p->pf = NULL;
        p->pn = NULL;
    }
    return p;
}

int node_insert(node_t* q, node_t* pins)
{
    pins->pn = q->pn;
    pins->pf = q;
    
    if(q->pn != NULL){
        q->pn->pf = pins;
    }
    q->pn = pins;

    return 0;
}

void node_delet(node_t* p)
{
    if(NULL == p)
        return;

    if(p->pf){
        (p->pf)->pn = p->pn;
    }

    if(p->pn){
        (p->pn)->pf = p->pf;
    }

    free(p);
}


node_t* node_find(node_t* pHead, int val)
{
    node_t* q = pHead;

    for(;;){
        if(val == q->data){
            return q;
        }
        if(q->pn != NULL){
            q = q->pn;
        }else{
            break;
        }
    }
    return NULL;
}

int node_modify(node_t* pHead, int src, int dest)
{
    node_t* p;
    p = node_find(pHead, src);
    if(p){
        p->data = dest;
        return 0;
    }

    return 1;
}


void node_print(node_t* pHead)
{
    node_t* q = pHead;

    for(;;){
        printf("node_addr: %p\r\n", q);
        printf("node->dat: %d\r\n", q->data);
        printf("node->pf:  %p\r\n", q->pf);
        printf("node->pn:  %p\r\n", q->pn);
        printf("\r\n");
        if(q->pn != NULL){
            q = q->pn;
        }else{
            break;
        }
    }
}



void main(void)
{
    node_t* p = node_create(0);

    node_t* p1 = node_create(1);
    node_insert(p, p1);

    p1 = node_create(2);
    node_insert(p, p1);
    p1 = node_create(3);
    node_insert(p, p1);
    p1 = node_create(4);
    node_insert(p, p1);
    p1 = node_create(5);
    node_insert(p, p1);

    node_print(p);
    printf("==============================\n");

    p1 = node_find(p, 2);
    node_delet(p1);
    node_print(p);
    return;
}

首先按要求写了个双向链表。

后根据其代码风格、排版风格的建议,修改如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
    代码风格、排版风格

    结构名_node修改为_DListNode
    结构别名node_t修改为DListNode_t
    操作链表的函数,第一个参数如果不是head节点,用thiz这个名字
*/

typedef unsigned char    uint8_t;    //1
typedef unsigned short   uint16_t;   //2
typedef unsigned int     uint32_t;

/*
struct n{
    int a;
    struct n *b;
}x;

    x.a = 1;
    x.b = NULL;
*/


typedef struct _DListNode{
    int data;
    struct _DListNode* pf;
    struct _DListNode* pn;
}DListNode_t;

//}DListNode_t, *pDListNode_t;
//相当于typedef DListNode_t *pnode;

DListNode_t* node_create(int val)
{
    DListNode_t* p = (DListNode_t*)malloc(sizeof(DListNode_t));
    if(p){
        p->data = val;
        p->pf = NULL;
        p->pn = NULL;
    }

    return p;
}

int node_insert(DListNode_t* thiz, DListNode_t* pins)
{
    pins->pn = thiz->pn;
    pins->pf = thiz;
    
    if(thiz->pn != NULL){
        thiz->pn->pf = pins;
    }
    thiz->pn = pins;

    return 0;
}

void node_delet(DListNode_t* thiz)
{
    if(NULL == thiz)
        return;

    if(thiz->pf){
        (thiz->pf)->pn = thiz->pn;
    }

    if(thiz->pn){
        (thiz->pn)->pf = thiz->pf;
    }

    free(thiz);
}


DListNode_t* node_find(DListNode_t* head, int val)
{
    DListNode_t* q = head;

    for(;;){
        if(val == q->data){
            return q;
        }
        if(q->pn != NULL){
            q = q->pn;
        }else{
            break;
        }
    }

    return NULL;
}

int node_modify(DListNode_t* head, int src, int dest)
{
    DListNode_t* p;
    p = node_find(head, src);
    if(p){
        p->data = dest;
        return 0;
    }

    return 1;
}


void node_print(DListNode_t* head)
{
    DListNode_t* q = head;

    for(;;){
        printf("node_addr: %p\r\n", q);
        printf("node->dat: %d\r\n", q->data);
        printf("node->pf:  %p\r\n", q->pf);
        printf("node->pn:  %p\r\n", q->pn);
        printf("\r\n");
        if(q->pn != NULL){
            q = q->pn;
        }else{
            break;
        }
    }
}



void main(void)
{
    DListNode_t* p = node_create(0);

    DListNode_t* p1 = node_create(1);
    node_insert(p, p1);

    p1 = node_create(2);
    node_insert(p, p1);
    p1 = node_create(3);
    node_insert(p, p1);
    p1 = node_create(4);
    node_insert(p, p1);
    p1 = node_create(5);
    node_insert(p, p1);

    node_print(p);
    printf("==============================\n");

    p1 = node_find(p, 2);
    node_delet(p1);
    node_print(p);

    return;
}

 

posted @ 2017-10-27 15:24  为民除害  阅读(199)  评论(0)    收藏  举报