#include<stdio.h> typedef int Elemtype ; typedef struct LNode{ Elemtype data ; struct LNode *next ;}LNode,LinkList ; void CreatList_H(LinkList &L, int n){ L=new LNode ; LinkList P ; L->data=NULL ; L->next=NULL ; int i ; for(i=1;i<=n;i++){ P=new LNode ; scanf("%d",&P->data) ; P->next=L->next ; L->next=P ; } } void ReverseList(LinkList &L,int n){ LinkList P,R ; P=L->next->next ; R=L->next ; int i ; for(i=1;i<n;i++){ R->next=P->next ; P->next=L->next ; L->next=P->next ; P=R->next ; } } void PrintList(LinkList L, int n) { int i ; for(i=1;i<=n;i++){ L=L->next ; printf("%d\n",L->data) ; } } int main(){ LinkList L; int n ; scanf("%d",&n); if(n==0) { printf("list is empty") ; break ; } void CreatList( L, n) ; void ReverseList( L, n); void PrintList( L, n) ; return 0 ; }
时间: 2024-01-15 19:05:25 浏览: 23
这段代码是一个单链表的创建、反转和输出程序,包括以下几个函数:
- CreatList_H():头插法创建单链表
- ReverseList():将单链表反转
- PrintList():输出单链表
在主函数中,首先输入要创建的单链表的长度n,然后通过调用CreatList_H()函数创建单链表,接着调用ReverseList()函数将单链表反转,最后调用PrintList()函数输出反转后的单链表。需要注意的是,这段代码中的LinkList是指向LNode结构体的指针类型,&符号用于传递指针的引用,其中break语句是错误的,应该改为return 0。
相关问题
#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode { ElemType da
`#include <stdio.h>`和`#include <stdlib.h>`是C语言中的两个头文件。`<stdio.h>`是标准输入输出头文件,提供了一些用于输入输出的函数和宏定义。`<stdlib.h>`是标准库头文件,提供了一些常用的函数和宏定义,如内存分配、随机数生成等。
在你的代码中,`typedef int ElemType;`定义了一个类型别名,将`int`类型命名为`ElemType`。
`typedef struct LNode`定义了一个结构体类型`LNode`,结构体是一种自定义的数据类型,可以包含多个不同类型的成员变量。在这里,`LNode`结构体中的成员变量还没有完整定义,因此代码截断了。
#include <stdio.h> #include <malloc.h> typedef char ElemType; typedef struct LNode
#include <stdio.h> 和 #include <malloc.h> 是 C 语言中的头文件,前者用于输入输出,后者用于内存动态分配和释放。
typedef char ElemType; 定义了一个名为 ElemType 的数据类型,实际上是 char 类型。
typedef struct LNode 定义了一个名为 LNode 的结构体类型。
该结构体类型中可以包含多个成员变量,具体定义方式如下:
typedef struct LNode {
int data;
struct LNode* next;
} LNode;
这里定义了一个 LNode 结构体类型,其中包含两个成员变量:data 和 next。data 是一个整型变量,next 是一个指向 LNode 类型的指针变量。这种结构体类型通常用于链表的实现。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)