栈
顺序表实现
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack{
int * arr;
int capacity;
int top;
} * StackArr;
void initStackArr(StackArr stack){
stack->arr = malloc(sizeof (StackArr) * 10);
stack->capacity = 10;
stack->top = -1;
}
_Bool push(StackArr stack, int data) {
if(stack->top == stack->capacity - 1) {
int newCapacity = stack->capacity + (stack->capacity>>1);
int * newArr = realloc(stack, sizeof(StackArr) * newCapacity);
stack->arr = newArr;
stack->capacity = newCapacity;
}
stack->arr[++stack->top] = data;
return 1;
}
int pop(StackArr stack) {
if(stack->top == -1) {
return INT_MAX;
}
return stack->arr[stack->top--];
}
void printStack(StackArr stack) {
while(stack->top != -1) {
printf("%d\t", pop(stack));
}
}
int main() {
struct Stack stack;
initStackArr(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printStack(&stack);
}
链表实现
#include <stdio.h>
#include <stdlib.h>
typedef struct StackNode{
int data;
struct StackNode * next;
} * StackLinkList;
void initStackLinkList(StackLinkList stackHead){
stackHead->next = NULL;
}
_Bool push(StackLinkList stackHead, int data) {
struct StackNode * newNode = malloc(sizeof (StackLinkList));
newNode->data = data;
newNode->next = stackHead->next;
stackHead->next = newNode;
return 1;
}
int pop(StackLinkList stackHead) {
struct StackNode * temp = stackHead->next;
stackHead->next = temp->next;
int data = temp->data;
free(temp);
return data;
}
void printStack(StackLinkList stackHead) {
while(stackHead->next) {
stackHead = stackHead->next;
printf("%d\t", stackHead->data);
}
}
int main() {
struct StackNode stack;
initStackLinkList(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
pop(&stack);
printStack(&stack);
}
队列
顺序表实现
#include <stdio.h>
#include <stdlib.h>
typedef struct Queue{
int * arr;
int capacity;
int front, rear;
} * QueueArr;
void initQueue(QueueArr queueArr){
queueArr->arr = malloc(sizeof (int) * 10);
queueArr->capacity = 10;
queueArr->front = queueArr->rear = 0;
}
_Bool offerQueue(QueueArr queueArr, int data) {
int currentRear = (queueArr->rear + 1) % queueArr->capacity;
if(currentRear == queueArr->front) {
return 0;
}
queueArr->arr[currentRear] = data;
queueArr->rear++;
return 1;
}
int pollQueue(QueueArr queueArr) {
if(queueArr->front == queueArr->rear) {
return INT_MAX;
}
queueArr->front = (queueArr->front + 1) % queueArr->capacity;
return queueArr->arr[queueArr->front];
}
void printQueue(QueueArr queueArr){
printf("<<< ");
int i = queueArr->front;
do {
i = (i + 1) % queueArr->capacity;
printf("%d ", queueArr->arr[i]);
} while (i != queueArr->rear);
printf("<<<\n");
}
_Bool isEmpty(QueueArr queueArr) {
return queueArr->front == queueArr->rear;
}
int main() {
struct Queue queue;
initQueue(&queue);
for (int i = 0; i < 5; ++i) {
offerQueue(&queue, i * 100);
}
printQueue(&queue);
while (!isEmpty(&queue)) {
printf("%d ", pollQueue(&queue));
}
}
链表实现
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node * next;
} * LNode;
typedef struct Queue {
struct Node * front, * rear;
} * LQueue;
void initQueue(LQueue queue, LNode lNode){
lNode = malloc(sizeof (LNode));
queue->front = queue->rear = lNode;
}
_Bool offerQueue(LQueue queue, int data) {
LNode newNode = malloc(sizeof (LNode));
if(newNode == NULL) {
return 0;
}
newNode->data = data;
newNode->next = NULL;
queue->rear->next = newNode;
queue->rear = newNode;
return 1;
}
int pollQueue(LQueue queue) {
if(queue->front == queue->rear) {
return INT_MAX;
}
LNode tempNode = queue->front->next;
int data = tempNode->data;
if(tempNode->next == NULL) {
queue->rear = queue->front;
}
queue->front->next = tempNode->next;
free(tempNode);
return data;
}
void printQueue(LQueue queue){
printf("<<< ");
LNode node = queue->front->next;
while (node) {
printf("%d ", node->data);
if(node == queue->rear) break;
else node = node->next;
}
printf("<<<\n");
}
_Bool isEmpty(LQueue lQueue) {
return lQueue->front == lQueue->rear;
}
int main() {
struct Queue queue;
struct Node node;
initQueue(&queue, &node);
for (int i = 0; i < 5; ++i) {
offerQueue(&queue, i * 100);
}
printQueue(&queue);
while (!isEmpty(&queue)) {
printf("%d ", pollQueue(&queue));
}
}