赞
踩
线性表是计算机科学中常见的数据结构之一,它能够有效地存储和操作一组有序的数据元素。线性表可以基于顺序存储或链式存储来实现,下面我们将分别介绍这两种存储方式的定义、特点及其基本操作。
线性表是由n(n≥0)个数据元素组成的有限序列,这些元素排列在一个线性的序列中。每个元素最多只有一个直接前驱和一个直接后继。线性表可以为空表,即不包含任何元素。实现方式有顺序存储和链式存储。
顺序存储是将线性表的元素顺序地存储在计算机的一块连续的存储空间中。在C语言中,可以使用数组来实现顺序存储的线性表。顺序存储的特点包括:
以下是使用顺序存储实现线性表的基本操作示例:
#include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 // 假设线性表的最大容量 // 定义顺序存储的线性表结构体 typedef struct { int data[MAX_SIZE]; // 用数组存储数据元素 int length; // 当前线性表的长度 } SeqList; // 初始化顺序存储的线性表 void initSeqList(SeqList *list) { list->length = 0; // 初始时线性表长度为0 } // 在指定位置插入元素 int insert(SeqList *list, int index, int element) { if (index < 0 || index > list->length || list->length >= MAX_SIZE) { return 0; // 插入位置不合法或线性表已满 } // 将插入位置后的元素依次后移 for (int i = list->length - 1; i >= index; i--) { list->data[i + 1] = list->data[i]; } list->data[index] = element; list->length++; return 1; // 插入成功 } // 删除指定位置的元素 int delete(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 search(SeqList *list, int element) { for (int i = 0; i < list->length; i++) { if (list->data[i] == element) { return i; // 返回元素的位置 } } return -1; // 未找到元素 } // 获取指定位置的元素值 int get(SeqList *list, int index) { if (index < 0 || index >= list->length) { return -1; // 获取位置不合法 } return list->data[index]; } // 遍历输出线性表的所有元素 void traverse(SeqList *list) { printf("SeqList elements: "); for (int i = 0; i < list->length; i++) { printf("%d ", list->data[i]); } printf("\n"); } int main() { SeqList list; initSeqList(&list); insert(&list, 0, 10); insert(&list, 1, 20); insert(&list, 2, 30); printf("Inserted elements:\n"); traverse(&list); delete(&list, 1); printf("After deletion:\n"); traverse(&list); int index = search(&list, 30); if (index != -1) { printf("Element 30 found at index %d\n", index); } else { printf("Element 30 not found\n"); } return 0; }
链式存储通过节点之间的指针来连接各个元素,每个节点包含数据元素和指向下一个节点的指针。链式存储的特点包括:
以下是使用链式存储实现线性表的基本操作示例:
#include <stdio.h> #include <stdlib.h> // 定义链式存储的节点结构体 typedef struct Node { int data; // 数据域 struct Node *next; // 指针域,指向下一个节点 } ListNode; // 初始化链表,创建一个空表(头节点) ListNode* initLinkedList() { ListNode *head = (ListNode *)malloc(sizeof(ListNode)); head->next = NULL; return head; } // 在指定位置插入元素 int insert(ListNode *head, int index, int element) { ListNode *p = head; int currentPosition = 0; while (p != NULL && currentPosition < index) { p = p->next; currentPosition++; } if (p == NULL || currentPosition > index) { return 0; // 插入位置不合法 } ListNode *newNode = (ListNode *)malloc(sizeof(ListNode)); newNode->data = element; newNode->next = p->next; p->next = newNode; return 1; // 插入成功 } // 删除指定位置的元素 int delete(ListNode *head, int index) { ListNode *p = head; int currentPosition = 0; while (p->next != NULL && currentPosition < index) { p = p->next; currentPosition++; } if (p->next == NULL || currentPosition > index) { return 0; // 删除位置不合法 } ListNode *temp = p->next; p->next = temp->next; free(temp); return 1; // 删除成功 } // 查找元素的索引(第一次出现的位置) int search(ListNode *head, int element) { ListNode *p = head->next; int index = 0; while (p != NULL) { if (p->data == element) { return index; } p = p->next; index++; } return -1; // 未找到元素 } // 获取指定位置的元素值 int get(ListNode *head, int index) { ListNode *p = head->next; int currentPosition = 0; while (p != NULL && currentPosition < index) { p = p->next; currentPosition++; } if (p == NULL || currentPosition > index) { return -1; // 获取位置不合法 } return p->data; } // 遍历输出链表的所有元素 void traverse(ListNode *head) { ListNode *p = head->next; printf("LinkedList elements: "); while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { ListNode *head = initLinkedList(); insert(head, 0, 10); insert(head, 1, 20); insert(head, 2, 30); printf("Inserted elements:\n"); traverse(head); delete(head, 1); printf("After deletion:\n"); traverse(head); int index = search(head, 30); if (index != -1) { printf("Element 30 found at index %d\n", index); } else { printf("Element 30 not found\n"); } return 0; }
式存储相较于顺序存储,更加灵活,能够动态分配内存空间,适合需要频繁插入和删除操作的场景。
线性表是一种简单而实用的数据结构,它支持快速的插入、删除、查找和遍历操作,适用于各种应用场景。通过示例,我们深入理解了线性表的基本操作,并展示了如何实现和使用这些操作来管理数据。
希望本文能够帮助读者更好地理解和应用线性表这一基础数据结构!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。