Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7551
  • 博文数量: 2
  • 博客积分: 86
  • 博客等级: 民兵
  • 技术积分: 35
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-25 20:12
文章分类

全部博文(2)

文章存档

2012年(2)

我的朋友

分类: C/C++

2012-04-26 14:11:03

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define LENGTH 5
  4. typedef struct node{
  5.     int data;
  6.     struct node *next;
  7. }pNode,* TNode;

  8. void Insert_sort(TNode &head,int num) //插入链表,并进行排序
  9. {
  10.     static int i=1;                        //第一个元素无需排序直接插入链表
  11.     pNode *p,*q,*temp;
  12.     p=(pNode*)malloc(sizeof(pNode));
  13.     p->data=num;
  14.     q=head->next;
  15.     if(i==1)
  16.     {
  17.         p->next=head->next;
  18.         head->next=p;
  19.     }
  20.     else
  21.     {
  22.         while (q!=NULL)
  23.         {
  24.             if(p->data<=q->data)
  25.             {
  26.                 p->next=q;
  27.                 if (q==head->next)
  28.                     head->next=p;
  29.                 else
  30.                     temp->next=p;
  31.                 break;
  32.             }
  33.             else
  34.             {
  35.                 temp=q;
  36.                 q=q->next;
  37.             }
  38.         }
  39.         if(q==NULL)
  40.         {    
  41.             p->next=temp->next;
  42.             temp->next=p;
  43.         }
  44.     }
  45.     i++;
  46. }

  47. void Disp(pNode *head)//显示链表
  48. {
  49.     pNode *p;
  50.     p=head->next;
  51.     while(p!=NULL)
  52.     {
  53.         printf("%4d",p->data);
  54.         p=p->next;
  55.     }
  56.     printf("\n");
  57. }

  58. void search(pNode *head,int num)//查找一个元素
  59. {
  60.     pNode *p;
  61.     int i=1;
  62.     p=head->next;
  63.     
  64.     while(p!=NULL)
  65.     {
  66.         if(p->data==num)
  67.             break;
  68.         else
  69.         {
  70.             p=p->next;
  71.             i++;
  72.         }
  73.     }
  74.     if(p==NULL)
  75.         printf("the num is not exit!\n");
  76.     else
  77.         printf("the %d 's position is:%d\n",num,i);
  78. }

  79. void del(TNode head,int num) //删除其中一个元素
  80. {
  81.     TNode temp,p;
  82.     p=head->next;
  83.     while (p!=NULL)
  84.     {
  85.         if (p->data==num)
  86.         {
  87.             if(head->next==p)
  88.                 head->next=p->next;
  89.             else
  90.             {
  91.                 temp->next=p->next;
  92.             }
  93.             break;
  94.         }
  95.         else
  96.         {
  97.             temp=p;
  98.             p=p->next;
  99.         }
  100.     }
  101.     if(p==NULL)
  102.         printf("the number is not exit!\n");
  103.     else
  104.     {
  105.         printf("delete success!\n");
  106.         free(p);
  107.     }
  108. }

  109. int main()
  110. {
  111.     pNode *head;
  112.     int i,num;
  113.     head=(pNode *)malloc(sizeof(pNode));            //创建一个头结点
  114.     head->next=NULL;
  115.     printf("    input a number:\n");
  116.     for (i=0;i<LENGTH;i++)
  117.     {
  118.         scanf("%d",&num);
  119.         Insert_sort(head,num);
  120.     }
  121.     Disp(head);
  122.     printf("please input a num to search:");
  123.     scanf("%d",&num);
  124.     search(head,num);
  125.     printf("please input a num to delete:");
  126.     scanf("%d",&num);
  127.     del(head,num);
  128.     Disp(head);
  129. }


 

阅读(2662) | 评论(0) | 转发(2) |
0

上一篇:没有了

下一篇:两个有序链表的合并

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