顺序表、链表、栈和队列都是线性数据结构,但它们在管理和访问数据方面有不同的特点和用途。以下是它们之间的主要区别:
选择哪种数据结构取决于具体应用场景和对数据操作的频繁程度。
队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数 组头上出数据,效率会比较低。
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的 代价比较小。
顺序表
是一种线性表,在C语言中通常通过数组来实现。它具有以下特点:
在C语言中实现顺序表,需要定义以下几个基本操作:
下面是一个简单的顺序表在C语言中的
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *data;
int size;
} SequenceList;
SequenceList *init_sequence_list(int size) {
SequenceList *list = (SequenceList *)malloc(sizeof(SequenceList));
list->data = (int *)malloc(size * sizeof(int));
list->size = size;
return list;
}
void free_sequence_list(SequenceList *list) {
free(list->data);
free(list);
}
void sequence_list_insert(SequenceList *list, int index, int value) {
if (index < 0 || index > list->size) {
printf("Index out of bounds\n");
return;
}
for (int i = list->size; i > index; i--) {
list->data[i] = list->data[i - 1];
}
list->data[index] = value;
list->size++;
}
int sequence_list_get(SequenceList *list, int index) {
if (index < 0 || index >= list->size) {
printf("Index out of bounds\n");
return -1;
}
return list->data[index];
}
链表是由一系列节点组成,每个节点包含数据域和指向下一个节点的指针。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int value;
struct Node *next;
} LinkList;
LinkList *init_link_list() {
LinkList *head = (LinkList *)malloc(sizeof(LinkList));
head->next = NULL;
return head;
}
void free_link_list(LinkList *head) {
LinkList *temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
void link_list_insert(LinkList *head, int value) {
LinkList *new_node = (LinkList *)malloc(sizeof(LinkList));
new_node->value = value;
new_node->next = head->next;
head->next = new_node;
}
栈是一种后进先出(LIFO)的数据结构。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *data;
int top;
int size;
} Stack;
Stack *init_stack(int size) {
Stack *stk = (Stack *)malloc(sizeof(Stack));
stk->data = (int *)malloc(size * sizeof(int));
stk->top = -1;
stk->size = size;
return stk;
}
void free_stack(Stack *stk) {
free(stk->data);
free(stk);
}
void stack_push(Stack *stk, int value) {
if (stk->top >= stk->size - 1) {
printf("Stack overflow\n");
return;
}
stk->data[++stk->top] = value;
}
int stack_pop(Stack *stk) {
if (stk->top < 0) {
printf("Stack underflow\n");
return -1;
}
return stk->data[stk->top--];
}
队列是一种先进先出(FIFO)的数据结构。
#include <stdio.h>
#include <stdlib.h>
#define QUEUE_SIZE 100
typedef struct {
int data[QUEUE_SIZE];
int front;
int rear;
int count;
} Queue;
Queue *init_queue() {
Queue *q = (Queue *)malloc(sizeof(Queue));
if (q) {
q->front = q->rear = 0;
q->count = 0;
}
return q;
}
void free_queue(Queue *q) {
free(q);
}
int is_queue_empty(Queue *q) {
return q->count == 0;
}
int is_queue_full(Queue *q) {
return q->count == QUEUE_SIZE;
}
void enqueue(Queue *q, int value) {
if (is_queue_full(q)) {
printf("Queue is full. Cannot enqueue.\n");
return;
}
q->data[q->rear] = value;
q->rear = (q->rear + 1) % QUEUE_SIZE;
q->count++;
}
int dequeue(Queue *q) {
if (is_queue_empty(q)) {
printf("Queue is empty. Cannot dequeue.\n");
return -1;
}
int value = q->data[q->front];
q->front = (q->front + 1) % QUEUE_SIZE;
q->count--;
return value;
}
int main() {
Queue *q = init_queue();
enqueue(q, 1);
enqueue(q, 2);
enqueue(q, 3);
printf("Dequeue: %d\n", dequeue(q));
printf("Dequeue: %d\n", dequeue(q));
if (is_queue_empty(q)) {
printf("Queue is empty.\n");
} else {
printf("Queue is not empty.\n");
}
free_queue(q);
return 0;
}