C语言顺序表和单链表基本操作实现

这段代码实现了顺序表和单链表的基本操作,包括初始化、插入、删除、取元素等功能。

顺序表操作

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

// 定义顺序表结构体
typedef struct {
    int *data;  // 数据指针
    int length; // 当前长度
    int maxSize; // 最大长度
} SeqList;

// 初始化顺序表
void init(SeqList *list, int maxSize) {
    list->data = (int *)malloc(maxSize * sizeof(int));
    list->length = 0;
    list->maxSize = maxSize;
}

// 插入元素
int insert(SeqList *list, int index, int element) {
    if (index < 0 || index > list->length) {
        printf('插入位置错误\n');
        return 0;
    }
    if (list->length >= list->maxSize) {
        printf('顺序表已满,无法插入\n');
        return 0;
    }
    for (int i = list->length; i > index; i--) {
        list->data[i] = list->data[i-1];
    }
    list->data[index] = element;
    list->length++;
    return 1;
}

// 删除元素
int removeElement(SeqList *list, int index) {
    if (index < 0 || index >= list->length) {
        printf('删除位置错误\n');
        return 0;
    }
    for (int i = index; i < list->length - 1; i++) {
        list->data[i] = list->data[i+1];
    }
    list->length--;
    return 1;
}

// 取元素
int getElement(SeqList *list, int index) {
    if (index < 0 || index >= list->length) {
        printf('取元素位置错误\n');
        return -1;
    }
    return list->data[index];
}

单链表操作

// 单链表结点结构体
typedef struct Node {
    int data;
    struct Node *next;
} Node;

// 创建单链表
Node* createLinkedList() {
    Node *head = NULL;
    int value;
    printf('请输入元素的值,以-1结束输入:\n');
    scanf('%d', &value);
    while (value != -1) {
        Node *newNode = (Node *)malloc(sizeof(Node));
        newNode->data = value;
        newNode->next = head;
        head = newNode;
        scanf('%d', &value);
    }
    return head;
}

// 输出单链表
void printLinkedList(Node *head) {
    Node *p = head;
    while (p != NULL) {
        printf('%d ', p->data);
        p = p->next;
    }
    printf('\n');
}

// 在单链表的第index个位置插入元素
void insertLinkedList(Node *head, int index, int element) {
    Node *p = head;
    int count = 0;
    while (p != NULL && count < index - 1) {
        p = p->next;
        count++;
    }
    if (p == NULL || count > index - 1) {
        printf('插入位置错误\n');
        return;
    }
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->data = element;
    newNode->next = p->next;
    p->next = newNode;
    printf('插入成功\n');
}

// 删除单链表中的第index个元素
void removeLinkedList(Node *head, int index) {
    Node *p = head;
    int count = 0;
    while (p != NULL && count < index - 1) {
        p = p->next;
        count++;
    }
    if (p == NULL || count > index - 1 || p->next == NULL) {
        printf('删除位置错误\n');
        return;
    }
    Node *temp = p->next;
    p->next = temp->next;
    free(temp);
    printf('删除成功\n');
}

// 取单链表中的第index个元素
int getLinkedListElement(Node *head, int index) {
    Node *p = head;
    int count = 0;
    while (p != NULL && count < index) {
        p = p->next;
        count++;
    }
    if (p == NULL || count > index) {
        printf('取元素位置错误\n');
        return -1;
    }
    return p->data;
}

代码讲解

  1. 顺序表结构体 SeqList 包含数据指针 data、当前长度 length 和最大长度 maxSize
  2. init 函数用于初始化顺序表,分配内存并设置初始状态。
  3. insert 函数用于插入元素,判断插入位置和顺序表是否已满,并进行元素移动和更新长度。
  4. removeElement 函数用于删除元素,判断删除位置,并进行元素移动和更新长度。
  5. getElement 函数用于获取元素,判断取元素位置并返回对应元素。
  6. 单链表结点结构体 Node 包含数据 data 和指向下一个结点的指针 next
  7. createLinkedList 函数用于创建单链表,通过循环输入元素并创建新的结点,将新结点插入到链表头部。
  8. printLinkedList 函数用于输出单链表,遍历链表并输出每个结点的数据。
  9. insertLinkedList 函数用于在指定位置插入元素,找到插入位置的前一个结点,创建新结点并将其插入。
  10. removeLinkedList 函数用于删除指定位置的元素,找到删除位置的前一个结点,将要删除的结点从链表中移除并释放内存。
  11. getLinkedListElement 函数用于获取指定位置的元素,找到指定位置的结点并返回其数据。

示例代码

int main() {
    // 顺序表操作
    SeqList list;
    init(&list, 10);
    insert(&list, 0, 1);
    insert(&list, 1, 2);
    insert(&list, 2, 3);
    printf('顺序表中的元素:');
    for (int i = 0; i < list.length; i++) {
        printf('%d ', list.data[i]);
    }
    printf('\n');

    removeElement(&list, 1);
    printf('删除后顺序表中的元素:');
    for (int i = 0; i < list.length; i++) {
        printf('%d ', list.data[i]);
    }
    printf('\n');

    printf('顺序表中的第2个元素:%d\n', getElement(&list, 1));

    // 单链表操作
    Node *head = createLinkedList();
    printf('单链表中的元素:');
    printLinkedList(head);

    insertLinkedList(head, 3, 67);
    insertLinkedList(head, 9, 10);
    printf('插入后单链表中的元素:');
    printLinkedList(head);

    removeLinkedList(head, 6);
    removeLinkedList(head, 8);
    printf('删除后单链表中的元素:');
    printLinkedList(head);

    printf('单链表中的第5个元素:%d\n', getLinkedListElement(head, 5));
    printf('单链表中的第7个元素:%d\n', getLinkedListElement(head, 7));

    return 0;
}

总结

这段代码展示了顺序表和单链表的基本操作实现,可以作为学习数据结构和算法的参考。代码清晰易懂,便于理解和修改

C语言顺序表和单链表基本操作实现

原文地址: http://www.cveoy.top/t/topic/f3x3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录