棧和隊列的基本實現

棧:一種特殊的線性表,其只允許在固定的一端進行插入和刪除元素操作。進行數據插入和刪除操作的一端 稱爲棧頂,另一端稱爲棧底。棧中的數據元素遵守後進先出的原則。 

棧的實現:一般可以使用數組和鏈表存儲的方式實現。

隊列:只允許在一端進行插入數據操作,在另一端進行刪除數據操作的特殊線性表,隊列具有先進先出的特性。隊列:進行插入操作的一端稱爲隊尾 出隊列:進行刪除操作的一端稱爲隊頭

隊列的實現:一般可以使用數組和鏈表存儲的方式實現。

 

一,棧順序存儲基本操作的實現:

    存儲結構的定義以及基本操作的接口:

//棧的順序存儲
#define STACK_SIZE 100
typedef struct Stack {
    int arr[STACK_SIZE];
    int top;
} Stack; 
void InitStack(Stack *s);
void StackPush(Stack *s, int v);
void StackPop(Stack *s);
int StackTop(Stack *s);
int StackSize(Stack *s);
bool StackEmpty(Stack *s);
void test_stack();

代碼實現:

#include"stack.h"
//棧的初始化
void InitStack(Stack *s) {
	s->top = 0;
}
//壓棧
void StackPush(Stack *s, int v) {
	if (s->top < STACK_SIZE) {
		s->arr[s->top] = v;
		s->top++;
	}
	else {
		printf("棧已滿,不能入棧!\n");
	}
}
//出棧
void StackPop(Stack *s) {
	s->top--;
}
//查看棧頂元素
int StackTop(Stack *s) {
	return s->arr[s->top - 1];
}
//查看元素個數
int StackSize(Stack *s) {
	return s->top;
}
//判斷棧是否爲空
bool StackEmpty(Stack *s) {
	if (s->top == 0) {
		return true;
	}
	return false;
}

二,棧鏈式存儲基本操作的實現:

    鏈式存儲結構的定義以及基本操作的接口:

//棧的鏈式存儲
typedef struct LStackNode {
	int value;
	struct LStackNode* next;
}	LStackNode;


void InitStack(LStackNode **top);
void StackPush(LStackNode *top, int v);
void StackPop(LStackNode *top);
int StackTop(LStackNode *top);
int StackSize(LStackNode *top);
bool StackEmpty(LStackNode *top);

基本操作的實現:

void InitStack(LStackNode **top) {
	(*top) = (LStackNode*)malloc(sizeof(LStackNode));
	if ((*top) != NULL) {
		(*top)->next = NULL;
	}
	return;
}

void StackPush(LStackNode *top, int v) {
	LStackNode* node = (LStackNode*)malloc(sizeof(LStackNode));
	node->value = v;
	node->next = top->next;
	top->next = node;
}

void StackPop(LStackNode *top) {
	if (top->next == NULL) {
		return;
	}
	LStackNode* next = top->next;
	top->next = top->next->next;
	free(next);
}
int StackTop(LStackNode *top) {
	if (top->next != NULL) {
		return top->next->value;
	}
	return -1;
}
int StackSize(LStackNode *top) {
	int size = 0;
	LStackNode* cur = top;
	while (cur->next != NULL) {
		size++;
		cur = cur->next;
	}
	return size;
}
bool StackEmpty(LStackNode *top) {
	if (top->next == NULL) {
		return true;
	}
	return false;
}

三,隊列鏈式存儲基本操作的實現:

    隊列鏈式存儲的定義以及借本操作的接口:

//鏈式存儲隊列
typedef struct Node {
	int value;
	struct Node *next;
}	Node;
typedef struct Queue {
	Node* head;
	Node* last;
} Queue;
void QueueInit(Queue *q);
void QueuePush(Queue *q, int v);
void QueuePop(Queue *q);
int QueueFront(Queue *q);
int QueueSize(Queue *q);
bool QueueEmpty(Queue *q);

基本操作代碼的實現:

void QueueInit(Queue *q) {
	q->head = q->last = NULL;
}
//入隊
void QueuePush(Queue *q, int v) {
	Node* node = (Node*)malloc(sizeof(Node));
	node->value = v;
	node->next = NULL;
	if (q->head == NULL) {
		q->head = node;
		q->last = node;
	}
	else {
		q->last->next = node;
		q->last = node;
	}
}
//出隊
void QueuePop(Queue *q) {
	if (q->head == NULL) {
		return;
	}
	Node* second = q->head->next;
	free(q->head);
	q->head = second;
	if (q->head == NULL) {
		q->last = NULL;
	}
}
//查看隊頭元素
int QueueFront(Queue *q) {
	return q->head->value;
}
//查看隊長
int QueueSize(Queue *q) {
	int size = 0;
	for (Node* cur = q->head; cur != NULL; cur = cur->next) {
		size++;
	}
	return size;
}
//判斷隊列是否爲空
bool QueueEmpty(Queue *q) {
	return q->head == NULL;
}

四,隊列順序存儲基本操作的實現:

    隊列順序存儲結構的定義以及基本操作接口:

#define QUEUE_SIZE 100
typedef struct Queue2 {
	int arr[QUEUE_SIZE];
	int front;//定義隊列的隊頭指針
	int rear;//隊尾
}	Queue2;

void Queue2Init(Queue2 *q);
void Queue2Push(Queue2 *q, int v);
void Queue2Pop(Queue2 *q);
int Queue2Front(Queue2 *q);
int Queue2Size(Queue2 *q);
bool Queue2Empty(Queue2 *q);

基本操作的代碼實現:

void Queue2Init(Queue2 *q) {
	q->front = q->rear = 0;
	for (int i = 0; i < QUEUE_SIZE; i++) {
		q->arr[i] = 0;
	}
}
void Queue2Push(Queue2 *q, int v) {
	if (q->rear == QUEUE_SIZE) {
		return;
	}
	q->arr[q->rear] = v;
	q->rear++;
}
void Queue2Pop(Queue2 *q) {
	if (q->front == q->rear) {
		return;
	}
	q->rear--;
	q->arr[q->rear] = 0;
}
int Queue2Front(Queue2 *q) {
	return q->arr[q->front];
}
int Queue2Size(Queue2 *q) {
	return q->rear;
}
bool Queue2Empty(Queue2 *q) {
	return q->arr[q->front] == q->arr[q->rear];
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章