用c语言编写一个完整可运行的代码实现顺序表的定义并创建顺序表。实现线性表顺序存储结构下元素的插入操作。实现线性表顺序存储结构下元素的删除操作。实现线性表顺序存储结构下取元素操作。单链表的存储定义从键盘上依次输入各元素的值逆序创建单链表并输出单链表中的各元素值。分别在单链表的第3个位置和第9个位置插入67和10给出插入成功或失败的信息并输出单链表中的各元素值。删除单链表中的第6个数据元素和第8个数据
#include <stdio.h> #include <stdlib.h>
// 定义顺序表结构体 typedef struct { int* data; // 数据指针 int length; // 当前长度 int capacity; // 总容量 } SeqList;
// 初始化顺序表 void initSeqList(SeqList* list, int capacity) { list->data = (int*)malloc(sizeof(int) * capacity); list->length = 0; list->capacity = capacity; }
// 插入元素 int insertElement(SeqList* list, int index, int element) { if (index < 0 || index > list->length) { return 0; // 插入失败 } if (list->length == list->capacity) { 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 deleteElement(SeqList* list, int index) { if (index < 0 || index >= list->length) { 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) { return -1; // 取元素失败 } return list->data[index]; // 返回元素值 }
// 定义单链表结点结构体 typedef struct ListNode { int data; // 数据 struct ListNode* next; // 指向下一个结点的指针 } ListNode;
// 创建单链表 ListNode* createLinkedList() { ListNode* head = NULL; // 头结点指针 ListNode* tail = NULL; // 尾结点指针 int element; printf("请输入元素的值(输入-1结束):"); while (1) { scanf("%d", &element); if (element == -1) { break; } ListNode* newNode = (ListNode*)malloc(sizeof(ListNode)); newNode->data = element; newNode->next = NULL; if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } return head; }
// 输出单链表中的元素 void printLinkedList(ListNode* head) { ListNode* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); }
// 在单链表的指定位置插入元素 int insertElementLinkedList(ListNode* head, int index, int element) { ListNode* current = head; int count = 0; while (current != NULL && count < index-1) { current = current->next; count++; } if (current == NULL || count > index-1) { return 0; // 插入失败 } ListNode* newNode = (ListNode*)malloc(sizeof(ListNode)); newNode->data = element; newNode->next = current->next; current->next = newNode; return 1; // 插入成功 }
// 删除单链表的指定位置的元素 int deleteElementLinkedList(ListNode* head, int index) { ListNode* current = head; int count = 0; while (current != NULL && count < index-1) { current = current->next; count++; } if (current == NULL || count > index-1 || current->next == NULL) { return 0; // 删除失败 } ListNode* temp = current->next; current->next = temp->next; free(temp); return 1; // 删除成功 }
// 取单链表中的指定位置的元素 int getElementLinkedList(ListNode* head, int index) { ListNode* current = head; int count = 0; while (current != NULL && count < index) { current = current->next; count++; } if (current == NULL || count > index) { return -1; // 取元素失败 } return current->data; // 返回元素值 }
int main() { // 顺序表操作 SeqList list; initSeqList(&list, 10); // 初始化顺序表,容量为10 insertElement(&list, 0, 1); // 在第0个位置插入元素1 insertElement(&list, 1, 2); // 在第1个位置插入元素2 insertElement(&list, 2, 3); // 在第2个位置插入元素3 printf("顺序表中的元素:"); for (int i = 0; i < list.length; i++) { printf("%d ", list.data[i]); } printf("\n"); deleteElement(&list, 1); // 删除第1个位置的元素 printf("删除后的顺序表中的元素:"); for (int i = 0; i < list.length; i++) { printf("%d ", list.data[i]); } printf("\n"); int element = getElement(&list, 0); // 取第0个位置的元素 printf("第0个位置的元素:%d\n", element);
// 单链表操作
ListNode* head = createLinkedList(); // 创建单链表
printf("单链表中的元素:");
printLinkedList(head);
insertElementLinkedList(head, 2, 67); // 在第2个位置插入元素67
insertElementLinkedList(head, 8, 10); // 在第8个位置插入元素10
printf("插入后的单链表中的元素:");
printLinkedList(head);
deleteElementLinkedList(head, 6); // 删除第6个位置的元素
deleteElementLinkedList(head, 8); // 删除第8个位置的元素
printf("删除后的单链表中的元素:");
printLinkedList(head);
int element1 = getElementLinkedList(head, 5); // 取第5个位置的元素
int element2 = getElementLinkedList(head, 7); // 取第7个位置的元素
printf("第5个位置的元素:%d\n", element1);
printf("第7个位置的元素:%d\n", element2);
return 0;
原文地址: https://www.cveoy.top/t/topic/hzPc 著作权归作者所有。请勿转载和采集!