单链表调整【C语言】

单链表调整【C语言】

题目:

单链表(a1,a2,a3,a4,…,an)
要求将该单链表调整成如下结构
(an,an-2,…,a4,a2,a1,a3, …,an-3,an-1)

时间复杂度为O(n)空间复杂度为O(1)
代码:
/*1-7*/
#include <stdio.h>
#include <stdlib.h>
#define elemtype int
int num=0;
typedef struct Node
{
    elemtype data;
    struct Node *next;
}Node;
typedef Node* Linklist;
void Initlist(Linklist *L)
{
    *L=(Linklist)malloc(sizeof(Node));
    (*L)->next=NULL;
}
void CreateFromTail(Linklist L)
{
    int i=0;
    Node *r=L,*s;
    elemtype e;
    while(i!=num)
    {
        scanf("%d",&e);
        s=(Node*)malloc(sizeof(Node));
        s->data=e;
        r->next=s;
        r=s;
        i++;
    }
    r->next=NULL;
}
void CreateFromHead(Linklist L)
{
    Node *s;
    int i=0;
    elemtype e;
    while(i!=num)
    {
        scanf("%d",&e);
        s=(Node*)malloc(sizeof(Node));
        s->data=e;
        s->next=L->next;
        L->next=s;
        i++;
    }
}
void recreate(Linklist L)
{
    int flag=1;
    Node *p,*pp,*t;
    t=L->next;
    p=L->next;
    pp=p->next;
    while(pp!=NULL)
    {
        if(flag%2==1)
        {
            L->next=pp;
            p->next=pp->next;
            pp->next=t;
            t=L->next;
            pp=p->next;
            flag++;
        }
        else
        {
            p=p->next;
            pp=pp->next;
            flag++;
        }
    }
}
void view(Linklist L)
{
    printf("此链表为:\n");
    Node *p;
    p=L->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}
int main()
{
    Linklist L;
    Initlist(&L);
    printf("请您输入--元素的总个数:\n");
    scanf("%d",&num);
    printf("若使用头插法请输入--'H'\n");
    printf("若使用尾插法请输入--‘T’\n");
    char I;scanf("%s",&I);
    printf("请您输入--元素:\n");
    if(I=='H')
    {
        CreateFromHead(L);
    }
    else
    {
        CreateFromTail(L);
    }
    view(L);
    recreate(L);
    view(L);
    printf("Hello World!\n");
    return 0;
}


运行情况:
请您输入--元素的总个数:
10
若使用头插法请输入--'H'
若使用尾插法请输入--‘T’
T
请您输入--元素:
1 2 3 4 5 6 7 8 9 10
此链表为:
1 2 3 4 5 6 7 8 9 10 
此链表为:
10 8 6 4 2 1 3 5 7 9 
Hello World!
Program ended with exit code: 0
Copyright © 2019 wyq. All rights reserved.
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表的就地逆置是指不引入新的节点,只调整原有节点的指针,将链表逆序排列。 实现链表的就地逆置可以使用三个指针:prev、curr和next。开始时,将prev指向NULL,curr指向链表的首节点,next指向curr的下一个节点。 接下来,通过循环遍历链表,每次将curr的指针指向prev,然后将prev、curr和next指针都向后移动一个节点。直到curr指向NULL,表示已经遍历到链表的末尾。 最后,将链表的头节点指向prev,完成就地逆置。 以下是C语言实现的代码示例: #include <stdio.h> struct Node { int data; struct Node* next; }; // 链表就地逆置函数 void reverseList(struct Node** head){ struct Node* prev = NULL; struct Node* curr = *head; struct Node* next = NULL; while(curr != NULL){ next = curr->next; curr->next = prev; prev = curr; curr = next; } *head = prev; } // 打印链表函数 void printList(struct Node* head){ struct Node* temp = head; while(temp != NULL){ printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main(){ // 创建一个示例链表: 1 -> 2 -> 3 -> 4 -> 5 struct Node* head = NULL; struct Node* second = NULL; struct Node* third = NULL; struct Node* fourth = NULL; struct Node* fifth = NULL; head = (struct Node*)malloc(sizeof(struct Node)); second = (struct Node*)malloc(sizeof(struct Node)); third = (struct Node*)malloc(sizeof(struct Node)); fourth = (struct Node*)malloc(sizeof(struct Node)); fifth = (struct Node*)malloc(sizeof(struct Node)); head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = fourth; fourth->data = 4; fourth->next = fifth; fifth->data = 5; fifth->next = NULL; printf("原始链表: "); printList(head); reverseList(&head); printf("就地逆置后的链表: "); printList(head); return 0; } 运行此程序,输出结果为: 原始链表: 1 2 3 4 5 就地逆置后的链表: 5 4 3 2 1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值