链表的定义及其简单解释 |
您所在的位置:网站首页 › 如何定义指针域 › 链表的定义及其简单解释 |
链表 1.存储方式: (1)顺序存储:以连续的存储单元进行存储 (2)链式存储:存储单元不联系 2.链式存储: (1)不可计算存储单元的地址,只能以存储的形式来完成 (2)结构: ①结点的结构: 数据域(data) 指针域(next) ②数据域:存储本结点的数据 ③指针域:存放下一个结点的地址 ④定义: struct node{ int data; struct node * next; };⑤类型重命名: a. struct node{ int data; struct node * next; }; typedef stuct node ElemSN; b. typedef struct node{ int data; struct node * next; }ElemSN;3.结点单元的分配: (1)设类型名为:ElemSN (2)ElemSN * p; p=(ElemSN *)malloc(sizeof(ElemSN));(3)引用成员:p->data,p->next; 4.链表的特点 5.术语: (1)链表中相邻的两个结点分别称为前驱结点和后继结点,前驱结点的指针域存放其后继结点的 地址 前驱节点的指针域指向后继结点 (2)头结点:单向链表中没有前驱结点的结点 (3)尾结点:单向链表中没有后继结点的结点 6.线性链表的特点: (1)有且仅有一个结点无前驱结点—头结点 (2)有且仅有一个结点无后继结点—尾结点 (3)除头、尾结点外,其余结点有且仅有一个前驱、后继 7.循环链表:尾结点的指针域指向头结点,即每一个结点都有它的前驱、后继 8.单向链表的创建、输出: #include #include #define N 10 typedef struct node { int data; struct node * next; }ElemSN; ElemSN * CreatLink(int arr[],int n);//创建链表 void PrintLink(ElemSN *head);//输出链表 int main(void){ int arr[N]; ElemSN * head; for(int i=0;inext=NULL; if(!head) head=tail=p; else tail=tail->next=p; } return head; } void PrintLink(ElemSN * head){ ElemSN * p; for(p=head;p;printf("%3d",p->data),p=p->next); } |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |