栈(顺序存储结构)
Stack.c
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 100
#define INCREMENT_SIZE 10
typedef unsigned short Bool;
typedef int Elemtype;
typedef struct
{
Elemtype *base;
int top;
int stackSize;
} Stack;
Bool push(Stack *stack, Elemtype elem)
{
if (stack->top > stack->stackSize)
{
Elemtype *newBase = (Elemtype *)realloc(stack->base, sizeof(Elemtype) * (stack->stackSize + INCREMENT_SIZE));
if (!newBase)
{
printf("重新分配内存失败!\n");
return FALSE;
}
stack->base = newBase;
}
stack->base[stack->top] = elem;
stack->top += 1;
return TRUE;
}
Bool pop(Stack *stack, Elemtype *elem)
{
if (stack->top == 0)
{
printf("栈为空!");
return FALSE;
}
stack->top -= 1;
if (elem)
{
*elem = stack->base[stack->top];
}
return TRUE;
}
Stack *createStack()
{
Stack *stack = (Stack *)calloc(1, sizeof(Stack));
if (!stack)
{
printf("内存分配失败!");
return NULL;
}
Elemtype *newBase = (Elemtype *)malloc(sizeof(Elemtype) * STACK_INIT_SIZE);
if (!newBase)
{
printf("内存分配失败!");
free(stack);
return NULL;
}
stack->base = newBase;
stack->stackSize = STACK_INIT_SIZE;
return stack;
}
void display(Stack *stack)
{
printf("Stack[@%x]:\n", stack);
if (stack->top == 0)
{
printf("[-空表-]\n");
return;
}
for (int i = 0; i < stack->top; i++)
{
if (i > 0)
{
printf("-> ");
}
printf("%d ", stack->base[i]);
}
printf("\n");
}
int main(int argc, char const *argv[])
{
int data[] = {5, 12, 45, 32, 1111, 33, 23, 455, 9, 0};
Stack *stack = createStack();
for (int i = 0; i < 10; i++)
{
push(stack, data[i]);
}
display(stack);
pop(stack, NULL);
pop(stack, NULL);
display(stack);
return 0;
}
DataStruct/Stack.java
package DataStruct;
import java.lang.reflect.Array;
import java.util.Arrays;
public class Stack<T> {
private static final int STACK_INIT_SIZE = 100;
private static final int INCREMENT_SIZE = 10;
private T[] base;
private int top;
private int size;
@SuppressWarnings("unchecked")
public Stack(Class<T> type) {
base = (T[]) Array.newInstance(type, STACK_INIT_SIZE);
top = 0;
size = STACK_INIT_SIZE;
}
public void push(T elem) {
if (top > size) {
base = Arrays.copyOf(base, size + INCREMENT_SIZE);
}
base[top] = elem;
top += 1;
}
public T pop() {
if (top <= 0) {
throw new IllegalStateException("栈为空!");
}
top -= 1;
return base[top];
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(super.toString());
sb.append(":\n");
if (top == 0) {
sb.append("[-空表-]");
return sb.toString();
}
for (int i = 0; i < top; i++) {
if (i > 0) {
sb.append("-> ");
}
sb.append(base[i]);
sb.append(' ');
}
return sb.toString();
}
public static void main(String[] args) {
Integer[] data = { 5, 12, 45, 32, 1111, 33, 23, 455, 9, 0 };
Stack<Integer> stack = new Stack<>(Integer.class);
System.out.println(stack);
for (Integer n : data) {
stack.push(n);
}
System.out.println(stack);
stack.pop();
stack.pop();
System.out.println(stack);
}
}
队列(顺序存储结构)
Queue.c
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define MAX_SIZE 100
typedef unsigned short Bool;
typedef int Elemtype;
typedef struct
{
Elemtype *data;
int front;
int rear;
int queueSize;
} Queue;
int getLength(Queue *queue)
{
if ((queue->rear + 1) % queue->queueSize == queue->front)
{
return 0;
}
if (queue->front <= queue->rear)
{
return queue->rear - queue->front + 1;
}
else
{
return queue->rear + queue->queueSize - queue->front + 1;
}
}
Bool addFirst(Queue *queue, Elemtype elem)
{
if ((queue->rear + 1) % queue->queueSize == queue->queueSize)
{
printf("队列已满!");
return FALSE;
}
int idx = queue->front - 1;
queue->front = idx < 0 ? queue->queueSize - 1 : idx;
queue->data[queue->front] = elem;
return TRUE;
}
Bool addLast(Queue *queue, Elemtype elem)
{
if ((queue->rear + 1) % queue->queueSize == queue->queueSize)
{
printf("队列已满!");
return FALSE;
}
int idx = (queue->rear + 1) % queue->queueSize;
queue->rear = idx;
queue->data[idx] = elem;
return TRUE;
}
Bool removeFirst(Queue *queue, Elemtype *elem)
{
if (getLength(queue) == 0)
{
printf("队列为空!");
return FALSE;
}
if (elem)
{
*elem = queue->data[queue->front];
}
queue->front = (queue->front + 1) % queue->queueSize;
return TRUE;
}
Bool removeLast(Queue *queue, Elemtype *elem)
{
if (getLength(queue) == 0)
{
printf("队列为空!");
return FALSE;
}
if (elem)
{
*elem = queue->data[queue->rear];
}
int idx = queue->rear - 1;
queue->rear = idx < 0 ? queue->queueSize - 1 : idx;
return TRUE;
}
Queue *createQueue()
{
Queue *queue = (Queue *)calloc(1, sizeof(Queue));
if (!queue)
{
printf("内存分配失败!");
return NULL;
}
Elemtype *newData = (Elemtype *)malloc(sizeof(Elemtype) * MAX_SIZE);
if (!newData)
{
printf("内存分配失败!");
free(queue);
return NULL;
}
queue->data = newData;
queue->queueSize = MAX_SIZE;
queue->front = 0;
queue->rear = -1;
return queue;
}
void display(Queue *queue)
{
printf("Stack[@%x]:\n", queue);
if (getLength(queue) == 0)
{
printf("[-空表-]\n");
return;
}
if (queue->front < queue->rear)
{
for (int i = queue->front; i <= queue->rear; i++)
{
if (i > queue->front)
{
printf("-> ");
}
printf("%d ", queue->data[i]);
}
}
else
{
for (int i = queue->front; i < queue->queueSize; i++)
{
if (i > queue->front)
{
printf("-> ");
}
printf("%d ", queue->data[i]);
}
for (int i = 0; i <= queue->rear; i++)
{
printf("-> ");
printf("%d ", queue->data[i]);
}
}
printf("\n");
}
int main(int argc, char const *argv[])
{
int dataFront[] = {1111, 32, 45, 12, 5};
int dataRear[] = {33, 23, 455, 9, 0};
Queue *queue = createQueue();
for (int i = 0; i < 5; i++)
{
addFirst(queue, dataFront[i]);
}
for (int i = 0; i < 5; i++)
{
addLast(queue, dataRear[i]);
}
printf("%d\n", getLength(queue));
display(queue);
removeFirst(queue, NULL);
removeLast(queue, NULL);
printf("%d\n", getLength(queue));
display(queue);
return 0;
}
DataStruct/Queue.java
package DataStruct;
import java.lang.reflect.Array;
public class Queue<T> {
private static final int QUEUE_INIT_SIZE = 100;
private T[] data;
private int front;
private int rear;
private int queueSize;
@SuppressWarnings("unchecked")
public Queue(Class<T> type) {
data = (T[]) Array.newInstance(type, QUEUE_INIT_SIZE);
front = 0;
rear = -1;
queueSize = QUEUE_INIT_SIZE;
}
public boolean addFirst(T elem) {
if ((rear + 1) % queueSize == queueSize) {
System.out.println("队列已满!");
return false;
}
int idx = front - 1;
front = idx < 0 ? queueSize - 1 : idx;
data[front] = elem;
return true;
}
public boolean addLast(T elem) {
if ((rear + 1) % queueSize == queueSize) {
System.out.println("队列已满!");
return false;
}
int idx = (rear + 1) % queueSize;
rear = idx;
data[idx] = elem;
return true;
}
public T removeFirst() {
if (getLength() == 0) {
throw new IllegalStateException("队列为空!");
}
T elem = data[front];
front = (front + 1) % queueSize;
return elem;
}
public T removeLast() {
if (getLength() == 0) {
throw new IllegalStateException("队列为空!");
}
T elem = data[rear];
int idx = rear - 1;
rear = idx < 0 ? queueSize - 1 : idx;
return elem;
}
int getLength() {
if ((rear + 1) % queueSize == front) {
return 0;
}
if (front <= rear) {
return rear - front + 1;
} else {
return rear + queueSize - front + 1;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(super.toString());
sb.append(":\n");
if (getLength() == 0) {
sb.append("[-空表-]");
return sb.toString();
}
if (front <= rear) {
for (int i = front; i <= rear; i++) {
if (i > front) {
sb.append("-> ");
}
sb.append(data[i]);
sb.append(' ');
}
} else {
for (int i = front; i < queueSize; i++) {
if (i > front) {
sb.append("-> ");
}
sb.append(data[i]);
sb.append(' ');
}
for (int i = 0; i <= rear; i++) {
sb.append("-> ");
sb.append(data[i]);
sb.append(' ');
}
}
return sb.toString();
}
public static void main(String[] args) {
Integer[] dataFront = { 1111, 32, 45, 12, 5 };
Integer[] dataRear = { 33, 23, 455, 9, 0 };
Queue<Integer> queue = new Queue<>(Integer.class);
for (Integer n : dataFront) {
queue.addFirst(n);
}
for (Integer n : dataRear) {
queue.addLast(n);
}
System.out.println(queue.getLength());
System.out.println(queue);
queue.removeFirst();
queue.removeLast();
System.out.println(queue);
System.out.println(queue.getLength());
queue.removeFirst();
queue.removeFirst();
queue.removeFirst();
queue.removeFirst();
queue.removeFirst();
queue.removeFirst();
System.out.println(queue.getLength());
queue.removeLast();
queue.removeLast();
System.out.println(queue.getLength());
}
}