头插法和尾插法建立单链表(共6页).doc
《头插法和尾插法建立单链表(共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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 头插法 尾插法 建立 单链表
限制150内