数据结构-DAY04

一、make file 工程管理工具
1.1 优点
        在.c文件非常多的情况下,当修改后,只编译被修改的.c文件,节省时间,提高效率

1.2 使用步骤
首先,使用vi Makefile进行代码编写
注意:无论版本1还是版本2,回车之后的,下一行使用Tab键打出空挡
clean 清除文件
敲下make :make会在当前目录下去寻找file,默认走第一条规则;使用make clean 走第二条规则, make clean删除预期文件和中间文件
指定make file的规则文件:make -f Makefile2
1.2.1 版本一

1.2.2 版本二

SRC += main.c
SRC += doulink.c
DST = app

CC = gcc
FLAG = -g 
LIB = -lm

$(DST):$(SRC)
	$(CC) $(SRC) $(FLAG) $(LIB) -o $(DST)

clean:
	rm $(DST)

二、练习和例题
1.创建双向链表

struct DouLinkList *CreateDouLinkList()
{
    struct DouLinkList *dl = malloc(sizeof(struct DouLinkList));
    if(NULL == dl)
    {
        fprintf(stderr, "CreatDouLinklist malloc");
        return NULL;
    }
    dl->head = NULL;
    dl->clen = 0;
    return dl;
}

2.双向链表-头插法

int  InsertHeadDouLinkList(struct DouLinkList *dl, struct DATATYPE *data)
{
    struct DouNode *newnode = malloc(sizeof(struct DouNode));
    if(NULL == newnode)
    {
        fprintf(stderr, "CreatDouLinklist malloc");
        return 1;
    }

    memcpy(newnode, data, sizeof(struct DATATYPE));
    newnode->next = NULL;

    newnode->next = dl->head;
    if(dl->head)
    {
        dl->head->prev = newnode;
    }
    dl->head = newnode;
    dl->clen++;
    return 0;
}

3.双向链表-遍历

int ShowDouLinkList(struct DouLinkList *dl, DIR dir)
{
    struct DouNode *tmp = dl->head;
    if(FORWARD == dir)
    {
        while(tmp)
        {
            printf("%s %c %d %d\n",tmp->data.name,tmp->data.sex,tmp->data.age,tmp->data.score);
            tmp = tmp->next;
        }
    }
    else if(BACKWARD == dir)
    {
        while(tmp->next)
        {
            tmp = tmp->next;
        }
        while(tmp)
        {
            printf("%s %c %d %d\n",tmp->data.name,tmp->data.sex,tmp->data.age,tmp->data.score);
            tmp = tmp->prev;
        }
    }
    return 0;
}

4.双向链表-尾插法

int InsertTailDouLinkList(struct DouLinkList *dl, struct DATATYPE *data)
{
    struct DouNode *tmp = dl->head;
    if(IsEmptyDouLinkList(dl))
    {
        return InsertHeadDouLinkList(dl, data);
    }
    else
    {
        while(tmp->next)
        {int IsEmptyDouLinkList(struct DouLinkList *dl);
            tmp = tmp->next;
        }
        struct DouNode *newnode = malloc(sizeof(struct DouNode));
        if(NULL == newnode)
        {
            fprintf(stderr, "CreatDouLinklist malloc");
            return 1;
        }
        memcpy(newnode, data, sizeof(struct DATATYPE));
        newnode->next = NULL;
        newnode->prev = NULL;
        
        newnode->prev = tmp;
        tmp->next = newnode;
        dl->clen++;
    }
    return 0;
}

5.双向链表-按位置插

int InsertPosLinkList(struct DouLinkList *dl, struct DATATYPE *data, int pos)
{
    int len = GetSizeofDouLinkList(dl);
    if(0 == pos)
    {
        return InsertHeadDouLinkList(dl, data);
    }
    else if(len == pos)
    {
        return InsertTailDouLinkList(dl, data);
    }
    else
    {
        struct DouNode *tmp = dl->head;
        struct DouNode *newnode = malloc(sizeof(struct DouNode));
        if(NULL == newnode)
        {
            fprintf(stderr, "CreatTailDouLinklist malloc");
            return 1;
        }
        memcpy(&newnode->data, data, sizeof(struct DATATYPE));
        newnode->next = NULL;
        newnode->prev = NULL;
        for(int i = 0; i < pos; ++i)
        {
            tmp = tmp->next;
        }
        newnode->next = tmp;
        newnode->prev = tmp->prev;
        tmp->prev = newnode;
        newnode->prev->next = newnode;
        dl->clen++;
    }
    return 0;
}

6.双向链表-查找

struct DouNode *FindDouLinkList(struct DouLinkList *dl, char *name)
{
    struct DouNode *tmp = dl->head;
    while(tmp)
    {
        if(0 == strcmp(tmp->data.name, name))
        {
            return tmp;
        }
        tmp = tmp->next;
    }
    return NULL;
}

7.修改-双向链表

int ModfiyDouLinkList(struct DouLinkList *dl, char *name, struct DATATYPE *data)
{
    struct DouNode *ret = FindDouLinkList(dl, name);
    if(NULL == ret)
    {
        return 1;
    }
    memcpy(&ret->data, data, sizeof(struct DATATYPE));
    return 0;
}

8.销毁-双向链表

int DestroyDouLinkList(struct DouLinkList** dl)
{
    while(1)
    {
        struct DouNode *tmp = (*dl)->head;
        if(NULL == tmp)
        {
            break;
        }
        (*dl)->head = (*dl)->head->next;
        free(tmp);
    }
    free(*dl);
    *dl = NULL;
    return 0;
}

9.删除-双向链表

int DeleteDouLinkList(struct DouLinkList* dl,char* name)
{
    struct DouNode *tmp = FindDouLinkList(dl, name);
    if(NULL == tmp)
    {
        return 1;
    }
    memcpy(&tmp->data.name, name, sizeof(struct DATATYPE));
    if(tmp == dl->head)
    {
        tmp->next->prev = NULL;
        dl->head = tmp->next;
        free(tmp);     
    }
    else if(tmp->next == NULL)
    {
        tmp->prev->next = NULL;
        free(tmp);
    }
    else
    {
        tmp->next->prev = tmp->prev;
        tmp->prev->next = tmp->next;
        free(tmp);
    }
    dl->clen--;
    return 0;
}

10.双向链表的逆序

int RevertDouLinkList(struct DouLinkList* dl)
{
    struct DouNode *Prev = NULL;
    struct DouNode *tmp = dl->head;
    struct DouNode *Next = tmp->next;
    int len = GetSizeofDouLinkList(dl);
    if(len < 2)
    {
        return 1;
    }
    while(1)
    {
        tmp->next = Prev;
        tmp->prev = Next;
        Prev = tmp;
        tmp = Next;
        if(NULL == tmp)
        {
            break;
        }
        Next = Next->next;
    }
    dl->head = Prev;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值