C语言单链表逆置的代码实现 (简单易懂版)

  嗯,,这是自己写的第一篇博客哈,写的不好大家不要见怪,主要是想把自己的一些思想分享给大家。也欢迎大家指出错误,一同进步。

  话不多说,直接先说想法。要把一个单链表逆置,可以大致分为下列几步。先创建一个链表。然后要考虑到链表的逆置实现。最后是链表的输出。有了这样过几步大概的想法之后,我们便要来一步步的实现啦。嗯,,创建链表就不说了,大家都会。  然后呢就是链表的逆置,这里我是采用的就地逆置法,,嗯,反正我是这么叫的,大家可以参考一下。当然啦,你得考虑到函数的形参和返回值以及指针的交接,这里如果出了问题,编译器是不会报错的,所以大家务必多加注意。其余的小问题还是看代码吧。

  额,,之前画的草图不见了,,现在也没有办法给大家画个草图演示一下,很抱歉啊。如果大家看不懂链表逆置可以画个草图自己看看,应该就差不多了

  

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3
 4 struct student
 5 {
 6     int data;
 7     struct student *next;
 8 };
 9
10 int iCount;                                                        //定义全局变量保存代码长度
11
12 struct student *Create()
13 {
14     struct student *pHead = NULL;
15     struct student *pNew,*pEnd;
16     iCount = 0;
17     pEnd = pNew = (struct student*)malloc(sizeof(struct student));
18     printf("请输入数据:");
19     scanf("%d",&pNew->data);
20     while(pNew->data!=0)
21     {
22         iCount++;
23         if(iCount == 1)                                                 //从本条if语句开始就要多注意指针的交接了哦,比较容易错
24         {
25             pNew->next = NULL;
26             pEnd = pNew;
27             pHead = pNew;
28         }
29         else
30         {
31             pNew->next = NULL;
32             pEnd->next = pNew;
33             pEnd = pNew;
34         }
35         pNew = (struct student*)malloc(sizeof(struct student));
36         printf("请输入数据:");
37         scanf("%d",&pNew->data);
38     }
39     free(pNew);
40     return pHead;
41 }
42
43 struct student *reverse(struct student *pHead)                             //链表逆置函数
44 {
45     struct student *p,*q,*t;                                              //p为前置指针,q为后置指针,t为交换指针
46     q = pHead;
47     p = (q->next);
48     q->next = NULL;
49     while(t!=NULL)
50     {
51         t = p->next;
52         p->next = q;
53         q = p;
54         if(t!=NULL) p = t;
55         else;
56     }
57     return (p);
58 }
59
60 void showlist(struct student *pHead)                                //指针输出函数
61 {
62     struct student *temp;
63     temp = pHead;
64
65     while(temp)
66     {
67         printf(" %d ",temp->data);
68         temp = temp->next;
69     }
70     printf("\n");
71 }
72
73 int main()
74 {
75    struct student *first;
76
77     first = Create();
78     printf("链表逆置前的数据:");
79     showlist(first);
80
81     first = reverse(first);
82
83     printf("链表逆置后的数据:");
84     showlist(first);
85
86     return 0;
87 } 

原文地址:https://www.cnblogs.com/pasifar-31/p/9112805.html

时间: 2024-08-10 17:08:02

C语言单链表逆置的代码实现 (简单易懂版)的相关文章

单链表逆置

重写单链表逆置,熟能生巧- #include <iostream> #include <cstdlib> using namespace std; typedef struct List{ int num; struct List *next; }ListNode,*pListNode; void display(ListNode *pHead) { while(pHead) { cout<<pHead->num<<"--"; pH

03 单链表逆置

单链表逆置 方法: <1>断开第一个数据节点和第二个数据节点的链接 <2>将后面的节点通过头插法的思想插在头节点后面 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 // 数据结构 6 typedef struct node 7 { 8 int data; 9 struct node *next; 10 }linkList; 11 12 // 创建单链表,并

单链表逆置Java

package com.kpp; /** * 单链表逆置 * 将单链表从第一个结点和第二个节点之间断开,然后将第2,3...个结点依次插入第一个结点之前 * @author kpp * */ class LNode{ private String data; private LNode next; } public class LinkedListReverse { private static void reverse(LNode head){ if(head == null||head.ne

java实现单链表逆置

1 class LNode 2 { 3 public LNode next; 4 public int data; 5 } 6 /*逆置链表*/ 7 class Nizhi 8 { 9 private static LNode head = new LNode();; 10 private static LNode node; 11 private static LNode tail; 12 private static int index; 13 private static LNode ne

对于&quot;单链表逆置和递归&quot;的问题的理解.

一. 相关知识要点: 学习或了解基础数据结构和C语言, 对基础链表知识或相关知识有概况性认识. 例如: 本题目结构为: 1 #define elem_type int 2 3 typedef struct _single_list { 4 elem_type data; //所存的数据元素 5 _single_list *next; //所存的指针元素 6 }ListNode; 二. 问题的思考过程(本题以3种不同的方法解决): <1>类似于我们学习的C语言基础知识中的冒泡排序(参考C程序设计

C实现之单链表逆置问题

/*问题描述,如何在时间复杂度为O(n)的前提下,实现单链表翻转.并尽量减少内存消耗. 即1-2-4-5-6转化为6-5-4-2-1. */ 1 # include<stdio.h> 4 struct Slist{ 5 6 int size; 7 struct sl* head; 8 9 10 }; 11 struct sl{ 12 13 int k; 14 struct sl* next; 15 16 17 }; 18 typedef struct Slist Sl; 19 typedef

华为机试题-- 单链表逆序

[问题] 单链表逆序 [代码] #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct ListNode { int value; struct ListNode *next; }ListNode; typedef struct ListNode *List; List reverseList(List head) //列表逆序 { ListNode *rear, *curr,

(转载)C语言单链表实现19个功能完全详解

最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将要实现19个功能.到目前我只写了一半,先传上来,大家有兴趣的可以帮忙指正,谢谢 在vs2010上面编译运行无错误. 每天都会把我写的新代码添加到这个里面.直到此链表完成. #include "stdafx.h" #include "stdio.h" #include <stdlib.h> #in

PTA 链表逆置

6-3 链表逆置 (20 分) 本题要求实现一个函数,将给定单向链表逆置,即表头置为表尾,表尾置为表头.链表结点定义如下: struct ListNode { int data; struct ListNode *next; }; 函数接口定义: struct ListNode *reverse( struct ListNode *head ); 其中head是用户传入的链表的头指针:函数reverse将链表head逆置,并返回结果链表的头指针. 裁判测试程序样例: #include <stdi