头插法和尾插法建立单链表.doc_第1页
头插法和尾插法建立单链表.doc_第2页
头插法和尾插法建立单链表.doc_第3页
头插法和尾插法建立单链表.doc_第4页
头插法和尾插法建立单链表.doc_第5页
免费预览已结束,剩余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=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 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(struct 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; /打印链表 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) printf(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 = 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); printf(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 = 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(memory 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) stru

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论