| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 672 人关注过本帖
标题:[讨论]单链表逆序
只看楼主 加入收藏
jingjingyls
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-8-12
收藏
 问题点数:0 回复次数:2 
[讨论]单链表逆序
下面是我的程序:
#include "stdio.h"
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *rev(struct node * head)
{
struct node * p,* q,* temp;
p=head;
if(p==NULL||p->next==NULL)
return(p);/*链表为空或者一个节点*/
q=p->next;
while(q!=NULL)
{
temp=q->next;
q->next=p;
p=q;
q=temp;
}
head->next=NULL;/*将尾节点的next域置空*/
return(p);/*p指向表头*/
}
int main()
{
int n;
struct node * head,* new;
/*头插法建立链表*/
head=NULL;
do
{
printf("请输入整数,以0作为结束\n");
scanf("%d",&n);
new=(struct node *)malloc(sizeof(struct node));
new->data=n;
new->next=head;
head=new;
}while(n);
new=rev(head);
/*输出链表*/
while(new!=NULL)
{
printf("%d\t",new->data);
new=new->next;
}
return 0;
}
还可以用别的方法么?

[此贴子已经被作者于2007-9-7 13:38:47编辑过]

搜索更多相关主题的帖子: 逆序 单链 
2007-09-07 13:23
peswe
Rank: 1
等 级:新手上路
帖 子:197
专家分:0
注 册:2006-11-22
收藏
得分:0 

呵呵,其实你已经倒序了,因为你是前插输入数据的啊!~
看看我改之后,你就应该清楚了!~

#include "stdio.h"
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *rev(struct node * head)
{
struct node * p,* q,* temp;
p=head;
if(p==NULL||p->next==NULL)
return(p);/*链表为空或者一个节点*/
q=p->next;
while(q!=NULL)
{
temp=q->next;
q->next=p;
p=q;
q=temp;
}
head->next=NULL;/*将尾节点的next域置空*/
return(p);/*p指向表头*/
}
int main()
{
int n;
struct node * head,* t;
/*头插法建立链表*/
head=NULL;
do
{
printf("请输入整数,以0作为结束\n");
scanf("%d",&n);
t=(struct node *)malloc(sizeof(struct node));
t->data=n;
t->next=head;
head=t;
}while(n);

printf("在倒序之前数据的顺序:\t");//以下是修改部分
t=head;
while(t!=NULL)
{
printf("%d\t",t->data);
t=t->next;
}
printf("\n");

printf("在倒序之后数据的顺序:\t");//
t=rev(head);
/*输出链表*/
while(t!=NULL)
{
printf("%d\t",t->data);
t=t->next;
}
printf("\n");
return 0;
}

[此贴子已经被作者于2007-9-7 13:56:14编辑过]


C斗士~~~fighting!!!!
2007-09-07 13:54
jingjingyls
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-8-12
收藏
得分:0 
谢了!我已经发现了!

2007-09-07 14:51
快速回复:[讨论]单链表逆序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014068 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved