sdut 链表5

简介: sdut 链表5

7-5 sdut-C语言实验-链表的逆置

分数 20

全屏浏览

切换布局

作者 马新娟

单位 山东理工大学

输入多个整数,以-1作为结束标志,顺序建立一个带头结点的单链表,之后对该单链表的数据进行逆置,并输出逆置后的单链表数据。

输入格式:

输入多个整数,以-1作为结束标志。

输出格式:

输出逆置后的单链表数据。

输入样例:

12 56 4 6 55 15 33 62 -1

输出样例:

62 33 15 55 6 4 56 12

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int a;
    struct node *next;
};
void display(struct node *head);//链表输出函数;
void f(struct node *head);//链表逆置函数;
int main()
{
    int a;
    struct node *head,*p,*q;
    head = (struct node*)malloc(sizeof(struct node));
    head -> next = NULL;
    q = head;
    while(~scanf("%d",&a)&&a!=-1)
    //建立链表;
    {
        p = (struct node*)malloc(sizeof(struct node));
        p -> next = NULL;
        p -> a = a;
        q ->  next = p;
        q = p;
    }
    f(head);//链表逆置;
    display(head);//链表输出;
    return 0;
}
void f(struct node *head)
{
    struct node *p = head -> next,*q;
    q = p -> next;
    p ->next = NULL;
    p = q -> next;
    while(q)
    {
        q -> next = head -> next;
        head -> next = q;
        q = p;
        if(p)
        p = p -> next;
    }
 
}
void display(struct node *head)
{
    struct node *p = head -> next;
    while(p)
    {
        printf("%d%c",p->a,p->next?' ':'\n');
        p = p -> next;
    }
}

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

这两行代码包含了标准的输入输出库和标准库,后者提供了动态内存分配和一些其他的有用函数。

struct node { int a; struct node *next; };

定义了一个名为node的结构体,它包含一个整型数据a和一个指向同类型结构体的指针next。这个结构体用于表示链表中的每个节点。

void display(struct node *head); // 链表输出函数 void f(struct node *head); // 链表逆置函数

声明了两个函数:display用于输出链表中的元素,f用于逆置链表。

int main() { // ... }

定义了main函数,程序的执行从这里开始。

int a; struct node *head, *p, *q;

main函数中,声明了整型变量a用于存储输入的数值,以及指向node结构体的指针headpq

head = (struct node*)malloc(sizeof(struct node)); head -> next = NULL; q = head;

为链表的头节点分配内存,并初始化头节点的next指针为NULLq指针初始化为指向头节点。

while(~scanf("%d",&a)&&a!=-1) // 建立链表; { // ... }

使用while循环读取用户输入的整数,直到输入-1。~scanf是一种技巧,用于检查scanf是否成功读取了输入。

p = (struct node*)malloc(sizeof(struct node)); p -> next = NULL; p -> a = a; q -> next = p; q = p;

在循环内部,为新节点分配内存,设置新节点的next指针为NULL,并将用户输入的值赋给新节点的a。然后将新节点链接到链表的末尾,并更新q指针。

f(head); // 链表逆置; display(head); // 链表输出;

调用f函数逆置链表,然后调用display函数输出逆置后的链表。

return 0; }

main函数返回0,表示程序正常结束。

void f(struct node *head) { // ... }

定义了f函数,用于逆置链表。

struct node *p = head -> next, *q; q = p -> next; p ->next = NULL; p = q -> next;

f函数中,初始化两个指针pqp指向第一个数据节点,q指向第二个数据节点。然后,将pnext指针设置为NULL,准备逆置链表。

while(q) { // ... }

使用while循环逆置链表。

q -> next = head -> next; head -> next = q; q = p; if(p) p = p -> next;

在循环中,将q节点的next指针指向当前头节点的next,然后将头节点的next指针指向q节点,实现逆置。然后,q节点前移一位,如果p存在,p也前移一位。

void display(struct node *head) { // ... }

定义了display函数,用于输出链表中的元素。

struct node *p = head -> next;

display函数中,初始化指针p指向第一个数据节点。

while(p) { printf("%d%c",p->a,p->next?' ':'\n'); p = p -> next; }

使用while循环遍历链表,输出每个节点的值。如果节点后面还有元素,则输出一个空格,否则输出一个换行符。

这个程序演示了如何使用结构体和指针来创建和管理动态数据结构(链表),以及如何对链表进行基本操作(逆置和输出)。


目录
相关文章
|
1月前
sdut 链表4
sdut 链表4
23 1
|
1月前
sdut链表lab2
sdut链表lab2
27 1
|
1月前
|
存储
sdut 链表lab1
sdut 链表lab1
23 1
|
1月前
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
|
17天前
|
存储 SQL 算法
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
|
17天前
|
存储 SQL 算法
LeetCode 题目 86:分隔链表
LeetCode 题目 86:分隔链表
|
22天前
|
存储 算法 Java
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
10 2
|
1月前
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点.
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点
24 1
|
14天前
|
算法
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
|
17天前
|
存储 SQL 算法
LeetCode 83题:删除排序链表中的重复元素【面试】
LeetCode 83题:删除排序链表中的重复元素【面试】