国内有什么网站浙江十大外贸公司
news/
2025/10/4 13:54:42/
文章来源:
国内有什么网站,浙江十大外贸公司,网站设计流程及制作流程,学做网站 软件线性表存储结构分为顺序存储、链式存储。 顺序存储的优点#xff1a; 顺序存储的缺点#xff1a; 链表就是典型的链式存储#xff0c;将线性表L #xff08;a0,a1,a2,........an-1#xff09;中个元素分布在存储器的不同存储块#xff0c;成为结点#xff08;Node… 线性表存储结构分为顺序存储、链式存储。 顺序存储的优点 顺序存储的缺点 链表就是典型的链式存储将线性表L a0,a1,a2,........an-1中个元素分布在存储器的不同存储块成为结点Node通过地址或指针建立他们之间的练习所得到的存储结构为链表结构。表中元素ai的结点形式如下 其中结点的data域存放数据元素ai,而next域是一个指针指向ai的直接后继a(i1)所在的结点。于是线性表La0,a1,......an-1的结构如图 一、节点类型描述 [cpp] view plaincopy typedef struct node_t { data_t data; //节点的数据域 struct node_t *next;//节点的后继指针域 }linknode_t,*linklist_t; 也可这样表示 [cpp] view plaincopy struct node_t { data_t data; struct node_t *next; } typedef struct node_t linknode_t; typedef struct node_t *linklist_t; 若说明 linknode_t A; linklist_t p A; 则结构变量A为所描述的节点而指针变量P为指向此类型节点的指针p的值为节点的地址 这样看来 linknode_t linklist_t 的作用是一样的那为什么我们要定义两个数据类型同一种呢主要为了代码的可读性我们要求标识符要望文识义便于理解 1、linknode_t *pnode 指向一个节点 2、linklist_t list 指向一个整体 二、头结点 head 我们在前篇提到的顺序存储线性表如何表达一个空表{ }是通过list-last -1来表现的所谓的空表就是数据域为NULL而我们的链表有数据域和指针域我们如何表现空链表呢这时就引入了头结点的概念头结点和其他节点数据类型一样只是数据域为NULLhead-next NULL下面我们看一个创建空链表的函数如何利用头结点来创建一个空链表 [cpp] view plaincopy linklist_t CreateEmptyLinklist() { linklist_t list; list (linklist_t)malloc(sizeof(linknode_t)); if (NULL ! list) { list-next NULL; } return list; } 只要头结点链表就还在 三、链表基本运算的相关算法 链表的运算除了上面的创建空链表还有数据的插入删除查找等函数链表的运算有各种实现方法如何写出一个高效的封装性较好的函数是我们要考虑的比如数据插入函数我们就要尽可能考虑所有能出现的结果比如1如果需插入数据的链表是个空表2所插入的位置超过了链表的长度如果我们的函数能包含所有能出现的情况不仅能大大提高我们的开发效率也会减少代码的错误率。下面我们来看看下面的这个链表的插入函数的实现 [cpp] view plaincopy int InsertLinklist(linklist_t list, int at, data_t x) { linknode_t *node_prev, *node_at, *node_new; int pos_at; int found 0; if (NULL list) return -1; /* at must 0 */ if (at 0) return -1; /*第一步、分配空间*/ node_new malloc(sizeof(linknode_t)); if (NULL node_new) { return -1; } node_new-data x; /* assigned value */ node_new-next NULL; /*节点如果插入超过链表长度的位置会接到尾节点后面这样node_new成了尾节点node_new-next NULL */ /*第二步、定位*/ node_prev list;//跟随指针帮助我们更好的定位 node_at list-next; //遍历指针 pos_at 0; while (NULL ! node_at) { if (pos_at at) { found 1; //找到正确的位置跳出循环 break; } /* move to the next pos_at */ node_prev node_at; //跟随指针先跳到遍历指针的位置 node_at node_at-next;//遍历指针跳到下一个节点的位置 pos_at; } /*第三步、插入*/ if (found) { /* found 1,找到正确的位置插入 */ node_new-next node_at;//插入的节点next指向node_at node_prev-next node_new;//插入节点的前一个节点 } else { /*若是没找到正确的位置即所插入位置超越了链表的长度则接到尾节点的后面同样这样适用于{ }即空链表这样我们可以建立一个空链表利用这个函数实现链表的初始化*/ node_prev-next node_new; } 这个插入函数可利用性就非常高。 下面讲一个完整链表代码贴出 listlink.h [cpp] view plaincopy #ifndef _LNK_LIST_H_ #define _LNK_LIST_H_ typedef int data_t; typedef struct node_t { data_t data; struct node_t *next; } linknode_t, *linklist_t; linklist_t CreateEmptyLinklist(); void DestroyLinklist(linklist_t list); void ClearLinklist(linklist_t list); int EmptyLinklist(linklist_t list); int LengthLinklist(linklist_t list); int GetLinklist(linklist_t list, int at, data_t *x); int SetLinklist(linklist_t list, int at, data_t x); int InsertLinklist(linklist_t list, int at, data_t x); int DeleteLinklist(linklist_t list, int at); linklist_t ReverseLinklist(linklist_t list); #endif /* _LNK_LIST_H_ */ linklist.c [cpp] view plaincopy #include stdio.h #include stdlib.h #include linklist.h linklist_t CreateEmptyLinklist() { linklist_t list; list (linklist_t)malloc(sizeof(linknode_t)); if (NULL ! list) { list-next NULL; } return list; } void DestroyLinklist(linklist_t list) { if (NULL ! list) { ClearLinklist(list); free(list); } } void ClearLinklist(linklist_t list) { linknode_t *node; /* pointer to the node to be removed */ if (NULL list) return; while (NULL ! list-next) { node list-next; list-next node-next; free(node); } return; } int LengthLinklist(linklist_t list) { int len 0; linknode_t *node; //iterate pointer if (NULL list) return -1; node list-next; // node points to the first data node while (NULL ! node) { len; node node-next; } return len; } int EmptyLinklist(linklist_t list) { if (NULL ! list) { if (NULL list-next) { return 1; } else { return 0; } } else { return -1; } } int GetLinklist(linklist_t list, int at, data_t *x) { linknode_t *node; /* used for iteration */ int pos; /* used for iteration and compare with */ if (NULL list) return -1; /* at must 0 */ if (at 0) return -1; /* start from the first element */ node list-next; pos 0; while (NULL ! node) { if (at pos) { if (NULL ! x) { *x node-data; } return 0; } /* move to the next */ node node-next; pos; } return -1; } int SetLinklist(linklist_t list, int at, data_t x) { linknode_t *node; /* used for iteration */ int pos; int found 0; if (!list) return -1; /* at must 0 */ if (at 0) return -1; /* start from the first element */ node list-next; pos 0; while (NULL ! node) { if (at pos) { found 1; /* found the position */ node-data x; break; } /* move to the next */ node node-next; pos; } if (1 found) { return 0; } else { return -1; } } int InsertLinklist(linklist_t list, int at, data_t x) { /* * node_at and pos_at are used to locate the position of node_at. * node_prev follows the node_at and always points to previous node * of node_at. * node_new is used to point to the new node to be inserted. */ linknode_t *node_prev, *node_at, *node_new; int pos_at; int found 0; if (NULL list) return -1; /* at must 0 */ if (at 0) return -1; node_new malloc(sizeof(linknode_t)); if (NULL node_new) { return -1; } node_new-data x; /* assigned value */ node_new-next NULL; node_prev list; node_at list-next; pos_at 0; while (NULL ! node_at) { if (pos_at at) { /* * found the node at */ found 1; break; } /* move to the next pos_at */ node_prev node_at; node_at node_at-next; pos_at; } if (found) { /* insert */ node_new-next node_at; node_prev-next node_new; } else { /* * If not found, means the provided at * exceeds the upper limit of the list, just * append the new node to the end of the list. */ node_prev-next node_new; } return 0; } int DeleteLinklist(linklist_t list, int at) { /* * node_at and pos_at are used to locate the position of node_at. * node_prev follows the node_at and always points to previous node * of node_at. */ linknode_t *node_prev, *node_at; int pos_at; int found 0; if (!list) return -1; /* at must 0 */ if (at 0) return -1; node_prev list; node_at list-next; pos_at 0; while (NULL ! node_at) { if (pos_at at) { /* * found the node at */ found 1; break; } /* move to the next pos_at */ node_prev node_at; node_at node_at-next; pos_at; } if (found) { /* remove */ node_prev-next node_at-next; free(node_at); return 0; } else { return -1; } } linklist_t ReverseLinklist(linklist_t list) { linknode_t *node; /* iterator */ linknode_t *node_prev; /* previous node of iterator */ linknode_t *node_next; /* next node of iterator, * used to backup next of iterator */ if (NULL list) return NULL; node_prev NULL; node list-next; while (NULL ! node) { /* * step1: backup node-next * due to the next of iterator will be * modified in step2 */ node_next node-next; /* * when iterator reaches the last node * of original list, make the list head * point to the last node, so the original * last one becomes the first one. */ if (NULL node_next) { list-next node; } /* * step2: reverse the linkage between nodes * make the node pointer to the previous node, * not the next node */ node-next node_prev; /* * step3: move forward */ node_prev node; node node_next; } return list; } main.c [cpp] view plaincopy #include stdio.h #include stdlib.h #include linklist.h int main() { int i; data_t x; linklist_t p; p CreateEmptyLinklist(); data_t a[10] {1,3,5,7,9,11,13,15,17,19}; for(i 0;i 10;i) { InsertLinklist(p,i,a[i]); } ReverseLinklist(p); printf(The length of the list is:%d\n,LengthLinklist(p)); GetLinklist(p,4,x); printf(The NO.4 of this list is:%d\n,x); SetLinklist(p,4,100); GetLinklist(p,4,x); printf(After updating!The No.4 0f this list is:%d\n,x); DeleteLinklist(p,4); printf(After updating!The length of the list is:%d\n,LengthLinklist(p)); GetLinklist(p,4,x); printf(After updating!The No.4 0f this list is:%d\n,x); ReverseLinklist(p); ClearLinklist(p); if(EmptyLinklist(p)) printf(This list is empty!\n); DestroyLinklist(p); printf(This list is destroyed!\n); return 0; } 执行结果如下 [cpp] view plaincopy fsubuntu:~/qiang/list/list2$ ./Test The length of the list is:10 The NO.4 of this list is:11 After updating!The No.4 0f this list is:100 After updating!The length of the list is:9 After updating!The No.4 0f this list is:9 This list is empty! This list is destroyed!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/927196.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!