六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 35|回复: 0

单向链表翻转

[复制链接]

升级  0.65%

321

主题

321

主题

321

主题

探花

Rank: 6Rank: 6

积分
1013
 楼主| 发表于 2013-1-26 12:26:11 | 显示全部楼层 |阅读模式
临时笔记,创建一个链表
#include <stdlib.h>#include <stdio.h>typedef struct node* link;struct node {int item;link next;};int main(int argc,char * argv[]){    int i,N=atoi(argv[1]);    link t =malloc(sizeof *t),x=t;    t->item=1;t->next=t;    for(i=2;i<=N;i++){        x=(x->next=malloc(sizeof *x));        x->item=i;x->next=t;    }       x=t;//  for(i=1;i<=N;i++){    while(x->next!=t){        printf("%d\n",x->item);        x=x->next;    }}~   

单向链表翻转
#include <stdlib.h>#include <stdio.h>typedef struct node* link;struct node {int item;link next;};int main(int argc,char * argv[]){    int i,N=atoi(argv[1]);    struct node *t ,*x;    t=malloc(sizeof *t),x=t;    t->item=1;t->next=t;    for(i=2;i<=N;i++){        x=(x->next=malloc(sizeof *x));        x->item=i;x->next=t;    }       x=t;    for(i=1;i<=N;i++){        printf("%d\n",x->item);        x=x->next;    }//---------翻转开始--主要是temp节点存住了要变化的节点    struct node *s,*temp;    s=t;    for(i=1;i<=N;i++){        temp=s;        s=s->next;        temp->next=t;        t=temp;    }//---------翻转结束    x=t;    for(i=1;i<=N;i++){        printf("%d\n",x->item);        x=x->next;    }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表