Chinaunix首页 | 论坛 | 博客
  • 博客访问: 202531
  • 博文数量: 22
  • 博客积分: 641
  • 博客等级: 上士
  • 技术积分: 756
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-27 00:41
文章分类

全部博文(22)

文章存档

2014年(1)

2013年(1)

2012年(20)

分类: C/C++

2012-09-25 12:37:00

1.单链表创建、逆序、删除、排序代码


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. struct Node
  5. {
  6.  int data;
  7.  struct Node *next;
  8. };
  9. int main()
  10. {
  11.  struct Node *current,
  12.              *newNode,
  13.              *head,
  14.              *temp,
  15.              *NextCurrent;
  16.  int a[10]={1,3,2,4,5,6,7,8,9,0};
  17.  int i;
  18.  //create linklist
  19.  head=(struct Node*)malloc(sizeof(struct Node));
  20.  current=head;
  21.  head->next=NULL;
  22.  for(i=0;i<10;i++)
  23.   {
  24.    newNode=(struct Node*)malloc(sizeof(struct Node));
  25.    newNode->data=a[i];
  26.    newNode->next=NULL;
  27.    current->next=newNode;
  28.    current=current->next;
  29.   }
  30.  printf("Linklist:");
  31.  for(current=head->next;current!=NULL;current=current->next)
  32.     printf("%d...",current->data);
  33.  printf("\n");
  34.  //revert linklist
  35.  current=head->next;
  36.  NextCurrent=head->next->next;
  37.  while(NextCurrent!=NULL)
  38.     {
  39.      temp=NextCurrent->next;
  40.      NextCurrent->next=current;
  41.           current=NextCurrent;
  42.      NextCurrent=temp;
  43.     }
  44.  head->next->next=NULL;
  45.  head->next=current;
  46.  printf("LinkRevert:");
  47.  for(current=head->next;current!=NULL;current=current->next)
  48.     printf("%d...",current->data);
  49.  printf("\n");
  50.  //Linklist length
  51.  current=head->next;
  52.  int count=0;
  53.  printf("length of Linklist:");
  54.  while(current)
  55.      {
  56.        count++;
  57.        current=current->next;
  58.      }
  59.  printf("%d\n",count);
  60.  //delete node
  61.  int Number;
  62.  printf("delete Number:");
  63.  scanf("%d",&Number);
  64.  struct Node *pre;
  65.  pre=head;
  66.  current=head->next;
  67.  while(current!=NULL)
  68.      {
  69.        if(current->data==Number)
  70.          {
  71.            pre->next=current->next;
  72.          }
  73.        pre=current;
  74.        current=current->next;
  75.      }
  76.  current=head->next;
  77.  printf("deleted Linklist:");
  78.  while(current)
  79.      {
  80.        printf("%d..",current->data);
  81.                current=current->next;
  82.       }
  83.   printf("\n");
  84.   printf("sorted Linklist:");
  85.   current=head;
  86.   int ptemp,j;
  87.   for(i=0;i<9;i++)
  88.     {
  89.      current=head;
  90.      for(j=0;j<9-i;j++)
  91.        {
  92.         if(current->data>current->next->data)
  93.           {
  94.            ptemp=current->data;
  95.            current->data=current->next->data;
  96.            current->next->data=ptemp;
  97.           }
  98.         current=current->next;
  99.        }
  100.     }
  101.   current=head->next;
  102.   while(current)
  103.       {
  104.        printf("%d..",current->data);
  105.        current=current->next;
  106.       }
  107.   printf("\n");
  108.   return 0;
  109.  }
运行结果


  1. Linklist:1...3...2...4...5...6...7...8...9...0...
  2. LinkRevert:0...9...8...7...6...5...4...2...3...1...
  3. length of Linklist:10
  4. delete Number:9
  5. deleted Linklist:0..8..7..6..5..4..2..3..1..
  6. sorted Linklist:0..1..2..3..4..5..6..7..8..

阅读(2163) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~