2024/4/3 作业

1、单向循环链表头插,尾插,头删,尾删,遍历,按位置插入,按位置删除,按位置查询

main.c

#include <stdio.h>
#include "LinkedList.h"

int main() {
    Node* head = NULL;
    head = insertAtEnd(head, 1);
    head = insertAtEnd(head, 3);
    head = insertAtFront(head, 5);
    printList(head);

    printf("Insert 7 at position 1:\n");
    head = insertAtPosition(head, 7, 1);
    printList(head);

    printf("Delete at position 2:\n");
    head = deleteAtPosition(head, 2);
    printList(head);

    printf("Search data at position 2: %d\n", searchByPosition(head, 2));

    return 0;
}

LinkedList.c

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

Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// 头插法
Node* insertAtFront(Node* head, int data) {
    Node* newNode = createNode(data);
    if(head == NULL) {
        head = newNode;
        head->next = head; // 循环指向自身
    } else {
        newNode->next = head->next;
        head->next = newNode;
    }
    return head;
}

// 尾插法
Node* insertAtEnd(Node* head, int data) {
    Node* newNode = createNode(data);
    if(head == NULL) {
        head = newNode;
        head->next = head; // 循环指向自身
    } else {
        newNode->next = head->next;
        head->next = newNode;
        head = newNode;
    }
    return head;
}

// 头删
Node* deleteAtFront(Node* head) {
    if(head == NULL) {
        return NULL;
    }
    Node* temp = head->next;
    if(temp == head) { // 只有一个节点
        free(temp);
        return NULL;
    }
    head->next = temp->next;
    free(temp);
    return head;
}

// 尾删
Node* deleteAtEnd(Node* head) {
    if(head == NULL) {
        return NULL;
    }
    Node* temp = head->next;
    if(temp == head) { // 只有一个节点
        free(temp);
        return NULL;
    }
    Node* prev = head;
    while(temp->next != head) {
        prev = temp;
        temp = temp->next;
    }
    prev->next = temp->next;
    free(temp);
    return head;
}

// 遍历链表
void printList(Node* head) {
    if(head == NULL) {
        return;
    }
    Node* temp = head->next;
    while(temp != head) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

// 按位置插入
Node* insertAtPosition(Node* head, int data, int position) {
    if(position < 0) {
        return head;
    }
    if(position == 0) {
        return insertAtFront(head, data);
    }
    Node* temp = head->next;
    Node* newNode = createNode(data);
    for(int i = 0; i < position-1 && temp != head; i++) {
        temp = temp->next;
    }
    if(temp == head) {
        return head;
    }
    newNode->next = temp->next;
    temp->next = newNode;
    return head;
}

// 按位置删除
Node* deleteAtPosition(Node* head, int position) {
    if(head == NULL || position < 0) {
        return head;
    }
    if(position == 0) {
        return deleteAtFront(head);
    }
    Node* temp = head->next;
    Node* prev = head;
    for(int i = 0; i < position && temp != head; i++) {
        prev = temp;
        temp = temp->next;
    }
    if(temp == head) {
        return head;
    }
    prev->next = temp->next;
    free(temp);
    return head;
}

// 按位置查询
int searchByPosition(Node* head, int position) {
    if(head == NULL || position < 0) {
        return -1;
    }
    Node* temp = head->next;
    for(int i = 0; i < position && temp != head; i++) {
        temp = temp->next;
    }
    if(temp == head) {
        return -1;
    }
    return temp->data;
}

LinkedList.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

// 定义链表节点结构
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// 头插法
Node* insertAtFront(Node* head, int data);

// 尾插法
Node* insertAtEnd(Node* head, int data);

// 头删
Node* deleteAtFront(Node* head);

// 尾删
Node* deleteAtEnd(Node* head);

// 遍历链表
void printList(Node* head);

// 按位置插入
Node* insertAtPosition(Node* head, int data, int position);

// 按位置删除
Node* deleteAtPosition(Node* head, int position);

// 按位置查询
int searchByPosition(Node* head, int position);

#endif /* LINKEDLIST_H */

运行截图:

2、双向链表头插,尾插,头删,尾删,遍历,按位置插入,按位置删除,按位置查询

main.c

#include <stdio.h>
#include "DoublyLinkedList.h"

int main() {
    Node* head = NULL;

    // 测试头插
    head = insertAtFront(head, 1);
    head = insertAtFront(head, 2);
    head = insertAtFront(head, 3);
    printf("After inserting at front: ");
    printList(head);

    // 测试尾插
    head = insertAtEnd(head, 4);
    head = insertAtEnd(head, 5);
    printf("After inserting at end: ");
    printList(head);

    // 测试头删
    head = deleteAtFront(head);
    printf("After deleting at front: ");
    printList(head);

    // 测试尾删
    head = deleteAtEnd(head);
    printf("After deleting at end: ");
    printList(head);

    // 测试按位置插入
    head = insertAtPosition(head, 10, 2);
    printf("After inserting at position 2: ");
    printList(head);

    // 测试按位置删除
    head = deleteAtPosition(head, 1);
    printf("After deleting at position 1: ");
    printList(head);

    return 0;
}

DoublyLinkedList.c

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

Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode == NULL) {
        printf("Memory allocation failed!\n");
        exit(1);
    }
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

// 头插法
Node* insertAtFront(Node* head, int data) {
    Node* newNode = createNode(data);
    if (head == NULL) {
        head = newNode;
    } else {
        newNode->next = head;
        head->prev = newNode;
        head = newNode;
    }
    return head;
}

// 尾插法
Node* insertAtEnd(Node* head, int data) {
    Node* newNode = createNode(data);
    if (head == NULL) {
        head = newNode;
    } else {
        Node* temp = head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
        newNode->prev = temp;
    }
    return head;
}

// 头删
Node* deleteAtFront(Node* head) {
    if (head == NULL) {
        printf("List is empty!\n");
        return NULL;
    }
    Node* temp = head;
    head = head->next;
    if (head != NULL) {
        head->prev = NULL;
    }
    free(temp);
    return head;
}

// 尾删
Node* deleteAtEnd(Node* head) {
    if (head == NULL) {
        printf("List is empty!\n");
        return NULL;
    }
    Node* temp = head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    if (temp->prev != NULL) {
        temp->prev->next = NULL;
    }
    free(temp);
    return head;
}

// 遍历链表
void printList(Node* head) {
    if (head == NULL) {
        printf("List is empty!\n");
        return;
    }
    Node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

// 按位置插入
Node* insertAtPosition(Node* head, int data, int position) {
    if (position < 0) {
        printf("Invalid position!\n");
        return head;
    }
    if (position == 0) {
        return insertAtFront(head, data);
    }
    Node* newNode = createNode(data);
    Node* temp = head;
    int i;
    for (i = 0; temp != NULL && i < position - 1; i++) {
        temp = temp->next;
    }
    if (temp == NULL) {
        printf("Invalid position!\n");
        return head;
    }
    newNode->next = temp->next;
    newNode->prev = temp;
    if (temp->next != NULL) {
        temp->next->prev = newNode;
    }
    temp->next = newNode;
    return head;
}

// 按位置删除
Node* deleteAtPosition(Node* head, int position) {
    if (head == NULL || position < 0) {
        printf("List is empty or invalid position!\n");
        return head;
    }
    if (position == 0) {
        return deleteAtFront(head);
    }
    Node* temp = head;
    int i;
    for (i = 0; temp != NULL && i < position; i++) {
        temp = temp->next;
    }
    if (temp == NULL) {
        printf("Invalid position!\n");
        return head;
    }
    if (temp->next == NULL) {
        return deleteAtEnd(head);
    }
    temp->prev->next = temp->next;
    temp->next->prev = temp->prev;
    free(temp);
    return head;
}

DoublyLinkedList.h

#ifndef DOUBLYLINKEDLIST_H
#define DOUBLYLINKEDLIST_H

// 定义链表节点结构
typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

// 创建新节点
Node* createNode(int data);

// 头插法
Node* insertAtFront(Node* head, int data);

// 尾插法
Node* insertAtEnd(Node* head, int data);

// 头删
Node* deleteAtFront(Node* head);

// 尾删
Node* deleteAtEnd(Node* head);

// 遍历链表
void printList(Node* head);

// 按位置插入
Node* insertAtPosition(Node* head, int data, int position);

// 按位置删除
Node* deleteAtPosition(Node* head, int position);

#endif /* DOUBLYLINKEDLIST_H */

运行截图

3、约瑟夫问题

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值