| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 656 人关注过本帖
标题:单链表应该怎么倒置啊
只看楼主 加入收藏
heqinwu8
Rank: 2
来 自:黄土高坡
等 级:论坛游民
帖 子:65
专家分:58
注 册:2009-7-11
结帖率:100%
收藏
 问题点数:0 回复次数:3 
单链表应该怎么倒置啊
struct tree
{int data;
struct tree *next;
};
void main()
{struct tree head,a,b,c,*p,*q,*i;
head.next=&a;a.data=1;
a.next=&b;b.data=2;
b.next=&c;c.data=3;
c.next=NULL;
p=head.next;
do
{printf("%d ",p->data);
p=p->next;
}while(p!=NULL);

q=&head;         //这里总是无限循环
while(q)
{i=q;
q=i->next;
q->next=head.next;
head.next=q;
}
应该怎么改啊,高手们帮帮忙
搜索更多相关主题的帖子: 倒置 单链 
2009-11-08 22:36
lzthadm
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2009-8-22
收藏
得分:0 
循环中head的值不变,所以循环中q始终在head和head.next之间来回循环,无法停止。
建议设两个指针,逻辑上比较简单呢。
    linklist *p,*q,*temp;
    p=head->next,q=head->next->next;
    head->next->next=NULL;
    while(q!=NULL)
    {
        temp=q->next;
        q->next=p;
        p=q;
        q=temp;
    }
    head->next=p;
2009-11-08 23:11
hyfl
Rank: 7Rank: 7Rank: 7
来 自:火星
等 级:黑侠
帖 子:113
专家分:552
注 册:2008-11-20
收藏
得分:0 
程序代码:
node *p, *q;
p = head->next;    
head->next = NULL;
while(p != NULL)
{
  q = p->next;
  p->next = head->next;
  head->next = p;
  p = q;
}

“一切高手都是从菜鸟炼成的!”1099285180@
2009-11-08 23:21
佳嘉
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:3
帖 子:534
专家分:1383
注 册:2009-11-8
收藏
得分:0 
#include<stdio.h>
#include<malloc.h>
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node *next;
}
linklist;
linklist *creatlist(int n)
{
int x,k;
linklist *head,*r,*p;
p=(linklist *)malloc(sizeof(linklist));
head=p;
p->next=NULL;
r=p;
for(k=1;k<=n;k++)
{
printf("input value :\n");
scanf("%d",&x);
p=(linklist *)malloc(sizeof(linklist));
p->data=x;
p->next=NULL;
r->next=p;
r=r->next;
}
return head;
 }
linklist *fun(linklist *head)
{
linklist *p,*r,*q;
p=head->next;
q=head;
q=p;
p=p->next;
q->next=NULL;
while(p)
 {
 r=q;
q=p;
p=p->next;
q->next=r;
 }
 head->next=q;
 return head;
 }
void main()
{
linklist *head,*p;
int n,i,x;
printf("input the length of the list :\n");
scanf("%d",&n);
head=creatlist(n);
head=fun(head);
printf("output the list :\n");
p=head->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
getch();
}
2009-11-09 14:54
快速回复:单链表应该怎么倒置啊
数据加载中...
 
   



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

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