设为首页 加入收藏

TOP

数据结构--链表(二)
2018-10-21 18:10:15 】 浏览:183
Tags:数据结构 链表
quot;
The List Length is %d\n",head -> length); /*输出节点内容*/ p = head -> next; while(p != NULL) { printf("%d\n",p -> data); p = p -> next; } return TRUE; } /*根据位置添加节点,大于链表长度的位置添加在链表末尾*/ bool AddNodeByLoc(ListHead* head, uint16 loc, datatype data) { /*合法性判断*/ if((head == NULL) || (loc == 0)) { return FALSE; } bool_val ret = FALSE; ListNode* node = head -> next; ListNode* tmp = NULL; /*初始化要创建的节点*/ ListNode* p = (ListNode*)malloc(sizeof(ListNode)); p -> data = data; p -> next = NULL; if(head -> length == 0)//对只有头节点的链表进行处理 { head -> next = p; p -> next = NULL; } else if(loc <= head -> length)//对1<loc<length的情况进行处理 { /*添加在头节点之后*/ if(loc == 1) { head -> next = p; p -> next = node; } else { /*寻找对应位置的前一个节点*/ while(loc > 2) { loc--; node = node -> next; } tmp = node -> next;//保存loc位置的节点地址 node -> next = p;//将要添加的节点放在loc位置 p -> next = tmp; } } else//对loc>length的情况进行处理 { while(node -> next != NULL) { node = node -> next; } node -> next = p; } head -> length++;//修改链表信息 ret = TRUE; return ret; } /*删除loc位置的节点*/ bool DelNodeByLoc(ListHead* head, uint16 loc) { /*进行合法性判断*/ if((head == NULL) || (loc == 0) || (loc > head -> length)) { return FALSE; } bool_val ret = FALSE; ListNode* tmp = head -> next; ListNode* freenode = NULL; if(loc == 1)//针对第一个节点的处理 { freenode = tmp; head -> next = tmp -> next; } else//对1<loc<length的情况进行处理 { while(loc > 2)//找到loc的前一个节点 { loc--; tmp = tmp -> next; } freenode = tmp -> next;//保存要释放的节点地址 tmp -> next = freenode -> next; } /*释放节点并修改链表信息*/ free(freenode); head -> length --; return ret; } /*修改loc位置的节点信息*/ bool ModNodeByLoc(ListHead* head, uint16 loc, datatype data) { /*合法性判断*/ if((head == NULL) || (loc == 0) || (loc > head -> length)) { return FALSE; } bool_val ret = FALSE; ListNode* tmp = head -> next; while(loc > 1)//找到loc节点 { tmp = tmp -> next; loc --; } tmp -> data = data;//修改节点数据 return ret; } /*返回loc节点的数据*/ datatype FindDataByLoc(ListHead* head, uint16 loc) { /*合法性判断*/ if((head == NULL) || (loc == 0) || (loc > head -> length)) { return FALSE; } datatype ret = 0; ListNode* tmp = head -> next; while(loc > 1)//找到loc节点 { tmp = tmp -> next; loc --; } ret = tmp -> data; return ret; } bool DestoryList(ListHead* head) { ListNode* p = NULL; ListNode* node = NULL; if(head == NULL) { return TRUE; } /*释放除头节点之外的节点*/ p = head -> next; while(p != NULL) { node = p; p = p -> next; free(node); } /*释放头节点*/ free(head); return TRUE; } chain.c

  以下为main.c文件

#include <stdio.h>
#include "chain.h"

int main()
{
    int i = 0;
    ListHead* head =  NULL;
    head = CreateList();//创建链表

    printf("Now we will Add four Nodes\n");
    for(i = 1; i < 5; i++)
    {
        AddNodeByLoc(head, i, i);//添加链表节点
    }
    ViewList(head);//遍历链表

    printf("Now we will Delete the third Node\n");
    DelNodeByLoc(head, 3);//删除链表的第三个节点
    ViewList(head);

    printf("Now we will modify the third Node to 5\n");
    ModNodeByLoc(head, 3, 5);//将第三个节点的信息修改为5
    ViewList(head);

    printf("Now we will view the second Node\n");
    printf("%d\n",FindDataByLoc(head, 2));//查看第二个节点的数据
    DestoryList(head);//销毁链表
}
main.c

  以下为MakeFile文件

main: chain.o main.o    #生成main依赖的文件
#执行命令cc chain.o main.o -o main生
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇c基础_笔记_1 下一篇C基础 如何让代码只执行一次

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目