Chinaunix首页 | 论坛 | 博客
  • 博客访问: 746279
  • 博文数量: 116
  • 博客积分: 923
  • 博客等级: 准尉
  • 技术积分: 1635
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-06 21:43
个人简介

一直帮老板搬运代码!!!

文章分类
文章存档

2013年(47)

2012年(69)

分类:

2012-08-15 13:54:55

原文地址:单链表 作者:yulianliu1218




1. 编程实现一个单链表的建立/测长/打印。

  1. #include<iostream>
  2. using namespace std;

  3. //单链表结构体
  4. typedef struct student
  5. {
  6.     int data;
  7.     struct student *next;
  8. }node;

  9. //建立单链表
  10. node *create()
  11. {
  12.     node *head,*p,*s;
  13.     int x,cycle=1;
  14.     head=(node*)malloc(sizeof(node)); //建立头节点
  15.     p=head;
  16.     while(cycle)
  17.     {
  18.         printf("\nPlease input the data:");
  19.         scanf("%d",&x);
  20.         if(x!=0)
  21.         {
  22.             s=(node*)malloc(sizeof(node));//每次新建一个节点
  23.             s->data=x;
  24.             printf("\n%d",s->data);
  25.             p->next=s;
  26.             p=s;
  27.         }
  28.         else
  29.         {
  30.             cycle=0;
  31.         }
  32.     }
  33.     head=head->next;
  34.     p->next=NULL;
  35.     printf("\n yyy %d",head->data);
  36.     return (head);
  37. }

  38. //单链表测长
  39. int length(node *head)
  40. {
  41.     int n=0;
  42.     node *p;
  43.     p=head;
  44.     while(p!=NULL)
  45.     {
  46.         p=p->next;
  47.         n++;
  48.     }
  49.     return (n);
  50. }

  51. //单链表打印
  52. void print(node *head)
  53. {
  54.     node *p;
  55.     int n;
  56.     n=length(head);
  57.     printf("\nNow,These %d records are :\n",n);
  58.     p=head;
  59.     if(head!=NULL)
  60.         p=p->next;
  61.     while(p!=NULL)
  62.     {
  63.         printf("\n uuu %d ",p->data);
  64.         p=p->next;
  65.     }
  66. }

2.编程实现单链表删除节点。

  1. //单链表删除节点
  2. node *remove(node *head ,int num)
  3. {
  4.     node *p1,*p2;
  5.     p1=head;
  6.     while(num!=p1->data && p1->next!=NULL)//查找data为num的节点
  7.     {
  8.         p2=p1;
  9.         p1=p1->next;
  10.     }
  11.     if(num==p1->data) //如果存在num节点,则删除
  12.     {
  13.         if(p1==head)
  14.         {
  15.             head=p1->next;
  16.             free(p1);
  17.         }
  18.         else
  19.         {
  20.             p2->next=p1->next;
  21.         }
  22.     }
  23.     else
  24.     {
  25.         printf("\n%d could not been found",num);
  26.     }
  27.     return (head);
  28. }

3.编写程序实现单链表的插入。

  1. //单链表插入节点
  2. node *insert(node *head,int num)
  3. {
  4.     node *p0,*p1,*p2;
  5.     p1=head;
  6.     p0=(node *)malloc(sizeof(node));
  7.     p0->data=num;
  8.     while(p0->data > p1->data && p1->next!=NULL)
  9.     {
  10.         p2==p1;
  11.         p1=p1->next;
  12.     }
  13.     if(p0->data<=p1->data)
  14.     {
  15.         if(head==p1)
  16.         {
  17.             p0->next=p1;
  18.             head=p0;
  19.         }
  20.         else
  21.         {
  22.             p2->next=p0;
  23.             p0->next=p1;
  24.         }
  25.     }
  26.     else
  27.     {
  28.         p1->next=p0;
  29.         p0->next=NULL;
  30.     }
  31.     return (head);
  32. }

4.编程实现单链表的排序。

  1. //单链表排序
  2. node *sort(node *head)
  3. {
  4.     node *p,*p2,*p3;
  5.     int n;
  6.     int temp;
  7.     n=length(head);
  8.     if(head==NULL ||head->next==NULL)//如果只有一个或者没有节点
  9.         return head;
  10.     p=head;
  11.     for(int j=1;j<n;++j)
  12.     {
  13.         p=head;
  14.         for(int i=0;i<n-j;++i)
  15.         {
  16.             if(p->data > p->next->data)
  17.             {
  18.                 temp=p->data;
  19.                 p->data=p->next->data;
  20.                 p->next->data=temp;
  21.             }
  22.             p=p->next;
  23.         }
  24.     }
  25.     return (head);
  26. }

5.编写实现单链表的逆置。

  1. //单链表逆置
  2. node *reverse(node *head)
  3. {
  4.     node *p1,*p2,*p3;
  5.     if(head==NULL || head->next==NULL)
  6.         return head;
  7.     p1=head;
  8.     p2=p1->next;
  9.     while(p2)
  10.     {
  11.         p3=p2->next;
  12.         p2->next=p1;
  13.         p1=p2;
  14.         p2=p3;
  15.     }
  16.     head->next=NULL;
  17.     head=p1;
  18.     return head;
  19. }
阅读(818) | 评论(0) | 转发(0) |
0

上一篇:链表

下一篇:内核链表

给主人留下些什么吧!~~