#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; }

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;

}

以上代码讲解内容:该代码实现了顺序表和单链表的基本操作。

顺序表部分:

  • 定义了顺序表结构体SeqList,包含数据指针data、当前长度length和最大长度maxSize。
  • init函数用于初始化顺序表,动态分配内存并初始化相关变量。
  • insert函数用于在指定位置插入元素,先判断插入位置是否合法,再判断顺序表是否已满,如果满了则无法插入,否则将插入位置后的元素依次向后移动一个位置,然后将元素插入到指定位置,并更新长度。
  • removeElement函数用于删除指定位置的元素,先判断删除位置是否合法,然后将删除位置后的元素依次向前移动一个位置,然后更新长度。
  • getElement函数用于取指定位置的元素,先判断取元素位置是否合法,然后返回该位置上的元素。

单链表部分:

  • 定义了单链表结点结构体Node,包含数据data和指向下一个结点的指针next。
  • createLinkedList函数用于创建单链表,通过循环输入元素的值,创建新的结点并插入到链表的头部,直到输入-1结束。
  • printLinkedList函数用于输出单链表的所有元素,通过遍历链表中的结点,依次输出数据。
  • insertLinkedList函数用于在指定位置插入元素,先找到插入位置的前一个结点,然后创建新的结点并将其插入到链表中。
  • removeLinkedList函数用于删除指定位置的元素,先找到删除位置的前一个结点,然后将删除位置的结点从链表中删除,并释放内存。
  • getLinkedListElement函数用于取指定位置的元素,先找到指定位置的结点,然后返回其数据。

在main函数中,通过调用上述函数,展示了顺序表和单链表的基本操作。


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

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