双向循环链表中结点的交换

交换双向循环链表中的指定结点及其前驱。

输入样例:
5
44 11 22 33 55
3
6
22 33 11 66 44 55
6
0

输出样例:
44 22 11 33 55
22 33 11 66 55 44

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

typedef struct LNode {
    int data;
    struct LNode *prior, *next;
} LNode, *LinkList;

LinkList ListInit();
void ListDestroy(LinkList *head);
void ListTailInsert(LinkList head, int x);
void ListSwap(LinkList head, int x);
void ListShow(LinkList head);

int main() {
    int m;

    while (scanf("%d%*c", &m) && m) {
        LinkList L = ListInit();
        int x;

        while (m--) {
            scanf("%d%*c", &x);
            ListTailInsert(L, x);
        }
        scanf("%d%*c", &x);
        ListSwap(L, x);
        ListShow(L);
        ListDestroy(&L);
    }
    return 0;
}

LinkList ListInit() {
    LinkList head = (LNode *) malloc(sizeof(LNode));

    head->data = 0;
    head->prior = head->next = head;
    return head;
}

void ListDestroy(LinkList *head) {
    LNode *tmp;

    (*head)->prior->next = NULL;
    while (*head) {
        tmp = *head;
        *head = (*head)->next;
        free(tmp);
    }
}

void ListTailInsert(LinkList head, int x) {
    LNode *newNode = (LNode *) malloc(sizeof(LNode));

    newNode->data = x;
    newNode->next = head;
    newNode->prior = head->prior;
    head->prior->next = newNode;
    head->prior = newNode;
}

void ListSwap(LinkList head, int x) {
    LNode *cursor = head, *previous;

    while (x--) cursor = cursor->next;
    previous = cursor->prior;
    previous->prior->next = cursor;
    cursor->prior = previous->prior;
    cursor->next->prior = previous;
    previous->next = cursor->next;
    cursor->next = previous;
    previous->prior = cursor;
}

void ListShow(LinkList head) {
    for (LNode *cursor = head->next; cursor != head; cursor = cursor->next) {
        printf("%d%c", cursor->data, cursor->next == head ? 10 : 32);
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现不带头结点双向循环链表的创建以及排序的数据结构代码: ```c++ #include <iostream> using namespace std; struct Node { int data; Node* prev; Node* next; }; Node* createList(int arr[], int n) { Node* head = NULL; Node* tail = NULL; for (int i = 0; i < n; i++) { Node* newNode = new Node; newNode->data = arr[i]; newNode->prev = tail; newNode->next = NULL; if (tail == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } if (head != NULL) { head->prev = tail; tail->next = head; } return head; } void printList(Node* head) { Node* cur = head; while (cur != NULL) { cout << cur->data << " "; cur = cur->next; if (cur == head) { break; } } cout << endl; } void sortList(Node* head) { if (head == NULL) { return; } Node* cur = head; while (cur->next != head) { Node* minNode = cur; Node* p = cur->next; while (p != head) { if (p->data < minNode->data) { minNode = p; } p = p->next; } if (minNode != cur) { int temp = cur->data; cur->data = minNode->data; minNode->data = temp; } cur = cur->next; } } int main() { int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; int n = sizeof(arr) / sizeof(arr[0]); Node* head = createList(arr, n); cout << "Original list: "; printList(head); sortList(head); cout << "Sorted list: "; printList(head); return 0; } ``` 该代码实现了不带头结点双向循环链表的创建和排序功能。在创建链表时,每个节点的前驱指针指向前一个节点,后继指针指向后一个节点,最后一个节点的后继指针指向头节点,头节点的前驱指针指向最后一个节点。在排序时,使用选择排序算法,每次找到未排序部分的最小值,与当前节点交换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值