《头插法和尾插法建立单链表(共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); */ 专心-专注-专业