赞
踩
带头结点的单链表的基本操作
带头结点的单链表的基本操作初始化、 插入(头插法,尾插法)、删除、逆置、合并(递归、非递归)、排序、遍历
- /*
- *
- * 带头结点的单链表的基本操作
- 初始化、 插入(头插法,尾插法)、删除、
- 逆置、合并(递归、非递归)、排序、遍历
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
-
- typedef struct node
- {
- int data;
- struct node *next;
- }Node, *Linklist;
-
- Linklist init()
- {
- Node *head;
- head = (Node *)malloc(sizeof(Node));
- if(NULL==head)
- {
- printf("malloc failed....\n");
- return head;
- }
- head->next = NULL;
- return head;
- }
-
- Linklist insert_head(Node *head, int num)
- {
- Node *p;
- p = (Node *)malloc(sizeof(Node));
- if(NULL==p)
- {
- printf("malloc failed...\n");
- return NULL;
- }
-
- p->data = num;
- p->next=head->next;
- head->next=p;
- return head;
- }
-
-
- Linklist insert_tail(Node *head, int num)
- {
- Node *p=NULL, *t=NULL;
- p = (Node *)malloc(sizeof(Node));
- if(NULL==p)
- {
- printf("malloc failed...\n");
- return NULL;
- }
- p->data = num;
- t=head;
-
- while(t->next!=NULL)
- {
- t = t->next;
- }
- t->next = p;
- p->next = NULL;
- return head;
- }
-
-
- Linklist delete(Node *head, int num)
- {
- Node *p=NULL, *t=NULL;
- if(NULL==head || NULL==head->next)
- {
- return head;
- }
- p=head;
- while(p->next!=NULL)
- {
- if(p->next->data==num)
- {
- // p->next = p->next->next;
- t = p->next;
- p->next = t->next;
- free(t);
- }
- p = p->next;
- }
- return head;
- }
-
-
- Linklist reverse(Node *head)
- {
- Node *p=NULL, *q=NULL;
- if(NULL==head || NULL==head->next)
- {
- return head;
- }
- p = head->next;
- q = p->next; //q=head->next->next;
- p->next = NULL;
-
- while(q!=NULL)
- {
- p = q->next;
- q->next = head->next;
- head->next = q;
- q = p;
- }
- return head;
- }
-
-
- Linklist merge(Node *head1, Node *head2)
- {
- if(NULL==head1)
- return head2;
- if(NULL==head2)
- return head1;
-
- Node *head = NULL;
- if(head1->data < head2->data)
- {
- head = head1;
- head->next = merge(head1->next, head2);
- }
- else
- {
- head = head2;
- head->next = merge(head1, head2->next);
- }
- return head;
- }
-
-
- Linklist merge_list(Node *head1, Node*head2)
- {
- if(NULL==head1)
- return head2;
- if(NULL==head2)
- return head1;
- Node *head = NULL, *p=NULL, *q=NULL;
-
- if(head1->data < head2->data)
- {
- head = head1;
- p = head1->next;
- q = head2;
- }
- else
- {
- head = head2;
- q = head2->next;
- p = head1;
- }
-
- Node *cur = head;
- while(p!=NULL && q!=NULL)
- {
- if(p->data <= q->data)
- {
- cur->next = p;
- cur = p;
- p = p->next;
- }
- else
- {
- cur->next = q;
- cur = q;
- q = q->next;
- }
- }
- if(p!=NULL)
- {
- cur->next = p;
- }
- if(q!=NULL)
- {
- cur->next = q;
- }
- return head;
- }
-
-
- Linklist sort(Node *head)
- {
- Node *p = NULL, *q = NULL;
- int i=0, j=0, t=0;
- int count = 0;
- if(NULL==head || NULL==head->next)
- {
- return head;
- }
-
- p = head->next;
- while(p!=NULL)
- {
- count++;
- p = p->next;
- }
-
- for(i=0; i<count-1; i++)
- {
- p = head->next;
- q = p->next;
- for(j=0; j<count-1-i; j++)
- {
- if(p->data > q->data)
- {
- t = p->data;
- p->data = q->data;
- q->data = t;
- }
- p = p->next;
- q = q->next;
- }
- }
- return head;
- }
-
-
- Linklist traverse(Node *head)
- {
- Node *p=NULL;
- if(NULL==head || NULL==head->next)
- {
- return head;
- }
- p=head->next;
- while(p!=NULL)
- {
- printf(" %d", p->data);
- p = p->next;
- }
- return head;
- }
-
-
- int main()
- {
- Node *head1, *head2, *t;
- int i=0;
-
- head1 = init();
- for(i=0; i<10; i=i+2)
- insert_tail(head1, i);
- traverse(head1);
- printf("\n");
-
- head2 = init();
- for(i=0; i<10; i=i+2)
- insert_head(head2, i);
- traverse(head2);
- printf("\n");
-
- sort(head2);
- traverse(head2);
- printf("\n");
-
- t = merge(head1->next, head2);
- //t = merge_list(head1->next, head2);
- traverse(t);
- printf("\n");
-
- delete(t, 8);
- traverse(t);
- printf("\n");
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。