头插法和尾插法建立单链表(共6页).doc

上传人:飞****2 文档编号:12077346 上传时间:2022-04-23 格式:DOC 页数:6 大小:24KB
返回 下载 相关 举报
头插法和尾插法建立单链表(共6页).doc_第1页
第1页 / 共6页
头插法和尾插法建立单链表(共6页).doc_第2页
第2页 / 共6页
点击查看更多>>
资源描述

《头插法和尾插法建立单链表(共6页).doc》由会员分享,可在线阅读,更多相关《头插法和尾插法建立单链表(共6页).doc(6页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、精选优质文档-倾情为你奉上#include stdio.h#include stdlib.htypedef struct Listint data;struct List *next; /指针域List;void HeadCreatList (List *L) /头插法建立链表List *s;L-next=NULL;for (int i=0;idata=i;s-next=L-next; /将L指向的地址赋值给S;L-next=s;void TailCreatList(List *L) /尾插法建立链表List *s,*r;r=L;for (int i=0;idata=i;r-next=s;r=

2、s;r-next=NULL;void DisPlay(List *L)List *p=L-next;while(p!=NULL)printf (%d ,p-data);p=p-next;printf(n);int main ()List *L1,*L2;L1=(struct List*)malloc(sizeof(struct List);L2=(struct List*)malloc(sizeof(struct List);HeadCreatList(L1);DisPlay(L1);TailCreatList(L2);DisPlay(L2);/头插法创建链表#include #include

3、 struct node int data; struct node * next; ; /建立只含头结点的空链表 struct node * create_list() struct node * head = NULL; head = (struct node *)malloc(sizeof(struct node); if (NULL = head) printf(memory out of use/n); return NULL; head-next = NULL; head-data = 0; return head; /头插法建立链表 int insert_form_head(st

4、ruct node * head, int num) struct node * head_t = head-next; struct node * new_node = NULL; new_node = (struct node *)malloc(sizeof(struct node); if (NULL = new_node) printf(memory out of use/n); return -1; /将新结点插入到链表的最后 new_node-data = num; new_node-next = head_t; head-next = new_node; return 0; /打

5、印链表 int show_list(struct node * head) struct node * temp; temp = head-next; while(temp) printf(%d/n,temp-data); temp = temp-next; return 0; / 按值删除结点,头结点不被删除 int delete_node(struct node *head, int data) /head_t 保存要删除结点的上一个结点 struct node * head_t = head; struct node * temp = NULL; if (head = NULL) pri

6、ntf(delete node from empty list!/n); return -1; /查找删除的结点的前一个结点 /如果此处查找的是删除的结点,则需要另加一个指针保存删除结点的前一个指针 while(NULL != head_t-next) if (data = head_t-next-data) break; head_t = head_t-next; /如果要删除的结点不存在,直接返回 if (NULL=head_t-next) printf(node not found/n); return -1; /删除操作 temp = head_t-next; head_t-next

7、= head_t-next-next; free(temp); return 0; void main(int argc, char* argv) struct node * head; head = create_list(); if (NULL = head) printf(create_list error/n); insert_form_head(head,123); insert_form_head(head,456); show_list(head); printf(delete once!/n); delete_node(head,123); show_list(head); p

8、rintf(delete second!/n); delete_node(head,456); show_list(head); delete_node(head,0); show_list(head); /*/尾插法创建链表#include#includestruct Node int data; struct Node * next; ; /建立只含头结点的空链表 struct Node * create_list() struct Node * head = NULL; head = (struct Node *)malloc(sizeof(struct Node); if (NULL

9、= head) printf(memory out of use/n); return NULL; head-next = NULL; head-data = 0; return head; /尾插法建立链表 int insert_form_tail(struct Node * head, int num) struct Node * temp = head; struct Node * new_node = NULL; new_node = (struct Node *)malloc(sizeof(struct Node); if (NULL = new_node) printf(memor

10、y out of use/n); return -1; /寻找尾结点 while (temp-next != NULL) temp = temp-next; /将新结点插入到链表的最后 new_node-data = num; new_node-next = NULL; temp-next = new_node; return 0; /打印链表 int show_list(struct Node * head) struct Node * temp; temp = head-next; while(temp) printf(%d/n,temp-data); temp = temp-next; return 0; void main(int argc, char* argv) struct Node * head; head = create_list(); if (NULL = head) printf(create_list error/n); insert_form_tail(head,123); insert_form_tail(head,456); show_list(head); */ 专心-专注-专业

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com