Chinaunix首页 | 论坛 | 博客
  • 博客访问: 408385
  • 博文数量: 51
  • 博客积分: 2030
  • 博客等级: 大尉
  • 技术积分: 1109
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-15 08:11
文章分类

全部博文(51)

文章存档

2022年(1)

2016年(2)

2015年(1)

2014年(2)

2013年(4)

2011年(9)

2010年(2)

2009年(5)

2008年(14)

2007年(11)

我的朋友

分类: LINUX

2010-04-20 14:42:18

本程序在LINUX下测序通过。编译命令: gcc -Wall -g link.c -o link
文件名为 link.c
 
#include
#include
typedef struct node{
 int data;
 struct node *next;
}Node;
void reverse(Node **head);
void free_link(Node **head);
int main()
{
   int i = 0;
   Node *head = NULL,*p = NULL,*q = NULL;
   for (i =0; i < 10; i++)
   {
      p = (Node *)malloc(sizeof(Node));
      p->data = i + 1;
      p->next = NULL;
      if (NULL == head)
      {
         head = p;
         q = head;
      }
      else
      {
         q->next = p;
         q = p;
      }
   }
   q = head;
   while(q)
   {
      printf("%5d",q->data);
      q = q->next;
   }
   printf("\n\n");
 
   reverse(&head);
 
   q = head;
   while(q)
   {
      printf("%5d",q->data);
      q = q->next;
   }
   puts("\n\n");
   free_link(&head);
   return 0;
}

void reverse(Node **head)
{
   Node *p = NULL,*q = NULL;
   p = *head;
   while(p)
   {
      if (NULL == q)
      {
         q = p;
         *head = q;
         p = p->next;
         (*head)->next = NULL;
      }
      else
      {
         q = p;
         p = p->next;
         q->next = *head;
         *head = q;
      }
   }
   return;
}
  
void free_link(Node **head)
{
   Node *p;
   int i = 0;
   while(*head)
   {
      p = *head;
      *head = (*head)->next;
      free(p);
      i++;
   }
   printf("%5d\n\n Link free successful\n",i);
   return;
}
阅读(969) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-04-25 17:15:37

代码原本是有缩进格式的,只是发布到博客里以后就没有格式了。原来的空格都被给过滤掉了,现在又重新缩进了一下

chinaunix网友2010-04-23 16:36:06

没有缩进的代码看起来好困难。