C语言链表逆置

本文介绍了使用C语言实现链表逆置的方法,包括链表的创建、逆置及显示等核心步骤,并提供了两种不同的逆置实现方式。

很久没有看C语言了

突然想到了链表逆置就写一个看看

//
//  main.c
//  list
//
//  Created by xiaoxiaobing on 13-12-16.
//  Copyright (c) 2013年 xiaoxiaobing All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int data;
    struct node *next;
}Node;


//创建链表
Node *CreatList(void)
{
    int val, i, n;
    Node *phead, *p, *q;
    
    phead = NULL;
    printf("请输入您要建立的链表长度:\n");
    scanf("%d", &n);
    printf("请输入您要输入的数据:\n");
    for(i=0; i<n; i++)
    {
        scanf("%d", &val);
        p = (Node *)malloc(sizeof(Node));
        p->data = val;
        //头结点刚开始的时候是空的
        if(NULL == phead)
            q = phead = p;
        else
            q->next = p;
        q = p;
    }
    // 最后一个节点指向空
    p->next = NULL;
    return phead;
}


//链表的逆置
Node *ReverseList(Node *phead)
{
    Node *p, *q, *r;
    
    p = phead;
    q=r=NULL;
    
    while(p)
    {
        //记录p后边的节点
        q = p->next;
        //让p的next指向p的上一个节点
        p->next = r;
        //r和p都向后移动一位
        r = p;
        p = q;
    }
    return r;
}


//输出链表
void ShowList(Node *phead)
{
    Node *p;
    
    p = phead;
    while(p)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}


int main(void)
{
    Node *phead;
    
    phead = CreatList();
    printf("链表逆置前的数据:\n");
    ShowList(phead);
    
    phead = ReverseList(phead);
    
    printf("链表逆置后的数据:\n");
    ShowList(phead);
    
    return 0;
}

当然上边的稍微好懂一点 下边有一个更简单的,直接传地址就行了,只是稍微难理解一点,和上边的对照一下应该不难理解的

//
//  main.c
//  list
//
//  Created by xiaoxiaobing on 13-12-16.
//  Copyright (c) 2013年 xiaoxiaobing All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int data;
    struct node *next;
}Node;


//创建链表
Node *CreatList(void)
{
    int val, i, n;
    Node *phead, *p, *q;
    
    phead = NULL;
    printf("请输入您要建立的链表长度:\n");
    scanf("%d", &n);
    printf("请输入您要输入的数据:\n");
    for(i=0; i<n; i++)
    {
        scanf("%d", &val);
        p = (Node *)malloc(sizeof(Node));
        p->data = val;
        //头结点刚开始的时候是空的
        if(NULL == phead)
            q = phead = p;
        else
            q->next = p;
        q = p;
    }
    // 最后一个节点指向空
    p->next = NULL;
    return phead;
}


//链表的逆置 这里没有返回值 直接把头指针的地址给改了
void  ReverseList(Node **phead)
{
    Node *p, *q, *r;
    
    p = *phead;
    q=r=NULL;
    
    while(p)
    {
        //记录p后边的节点
        q = p->next;
        //让p的next指向p的上一个节点
        p->next = r;
        //r和p都向后移动一位
        r = p;
        p = q;
    }
    (*phead) = r;
}


//输出链表
void ShowList(Node *phead)
{
    Node *p;
    
    p = phead;
    while(p)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}


int main(void)
{
    Node *phead;
    
    phead = CreatList();
    printf("链表逆置前的数据:\n");
    ShowList(phead);
    //把链表的头指针的地址传过去
    ReverseList(&phead);
    
    printf("链表逆置后的数据:\n");
    ShowList(phead);
    
    return 0;
}


三种不同的方法,挺不错的! #include<stdio.h> #include<stdlib.h> #include<string.h> #define N 100 typedef struct SList { char data[N]; //字符数组 struct SList *next; //定义链表头指针 }SList,*ListPointer; /*typedef struct List { SList *head; }List,* ListPointer; */ void initList(ListPointer &lp) { lp=(SList *)malloc(sizeof(SList));//初始化链表 lp->next=lp; //链表的头指针指向本身,实现链表循环 } void output(ListPointer lp) // 自定义输出函数 { SList *ep; ep=lp; while(ep->next!=lp) //判定条件 当指针重新指向头指针输出结束 { ep=ep->next; printf("%s ",ep->data); } } void revert(ListPointer lp) // 链表 { SList *p,*q; p=lp;q=NULL;lp=NULL; while(p) { q=p->next; p->next=lp; lp=p; p=q; } /*方法二 SList *p,*q,*end; p=lp->next; q=p->next; end=p; while(q!=lp) { lp->next=q; q=q->next; lp->next->next=p; p=lp->next; } end->next=lp; */ } void add_char(char *p,ListPointer lp) //将输入的字符传递给链表 { SList * ep; ep=lp; while(ep->next!=lp) //判定条件 当指针重新指向头指针输出结束 { ep=ep->next; } ep->next=(SList *)malloc(sizeof(SList)); //开辟空间存储 strcpy(ep->next->data,p); //字符的传递 ep->next->next=lp; } void main() { ListPointer L; char str[N]; initList(L); printf("输入#以结束\n");//确定输入终止条件 while(1) { scanf("%s",str); if(*str=='#') //判定条件 { break; } add_char(str,L); } printf("初始序列为:"); output(L); printf("\n"); revert(L); printf("后为:"); output(L); printf("\n"); }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值