---
# System prepended metadata

title: 資料結構(0411)程式碼

---

# 資料結構(0411)程式碼

**array_stack.c**

```c=
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int top = -1;

int isFull();
int isEmpty();
void pushStack(int *a, int item);
int popStack(int *a);

int main(){
	srand(time(NULL));
	int a[10] = {0};
	for (int i = 0; i < 15; i++){
		int x = rand();
		printf("%d\n", x);
		pushStack(a, x);
	}
	printf("===============\n");
	for (int i = 0; i < 10; i++){
		printf("%d\n", a[i]);
	}
	printf("===============\n");
	for (int i = 0; i < 5; i++){
		printf("%d\n", popStack(a));
	}
	return 0;
}
int isFull(){
	if (top == 9)
		return 1;
	else
		return 0;
}
int isEmpty(){
	if (top == -1)
		return 1;
	else
		return 0;
}
void pushStack(int *a, int item){
	if (isFull()) { 
		printf("Stack is full!\n");
	} else {
		a[++top] = item;
	}
}
int popStack(int *a){
	if (isEmpty()) { 
		printf("Stack is empty!\n");
	} else {
		int r = a[top];
		top--;
		return r;
	}
}
```

**linklist_stack.c**

```c=
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct node {
	int data;
	struct node *link;
} LNODE;

int isEmpty(LNODE *);
void pushStack(LNODE **,int);
int popStack(LNODE **);
void show_list(LNODE *);

int main() {
	srand(time(NULL));
	LNODE *h = NULL;
	for (int i = 0; i < 10; i++) {
		
		int x = rand() % 5;
		printf("i=%d x=%d ", i, x);
		if (x == 0)
			printf("Pop stack:%d\n", popStack(&h));
		else {
			int y = rand() % 100;
			printf("Push stack:%d\n", y);
			pushStack(&h, y);
		}
		show_list(h);
	}
	return 0;
}
int isEmpty(LNODE *a) {
	if (a == NULL)
		return 1;
	else
		return 0;
}
int popStack(LNODE **a) {
	LNODE *q = *a;
	if (isEmpty(q)) {
		printf("Stack is empty!\n");
	} else {
		int r = q->data;
		(*a) = q->link;
		return r;
	}
}
void pushStack(LNODE **a, int data) {
	LNODE *p;
	p = malloc(sizeof(LNODE));
	p->data = data;
	p->link = NULL;
	if ((*a) == NULL) {
		(*a) = p;
	} else {
		p->link = (*a);
		(*a) = p;
	}
}
void show_list(LNODE *a) {
	printf("List:");
	while (a != NULL) {
		printf("%4d", a->data);
		a = a->link;
	}
	printf("\n");
}
```

**array_queue.c**

```c=
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 10
int front = 0, rear = 0;

int isFull();
int isEmpty();
void enQueue(int *a, int item);
int deQueue(int *a);

int main() {
	srand(time(NULL));
	int a[N] = {0};
	for (int i = 0; i < 10; i++) {
		int c = rand() % 5;
		if (c <= 1) {
			printf("front=%d rear=%d dequeue...%3d\n", front, rear, deQueue(a));
			show_array(a);
		} else {
			int x = rand() % 101;
			printf("front=%d rear=%d enqueue...%3d\n", front, rear, x);
			enQueue(a, x);
			show_array(a);
		}
	}
	return 0;
}
void show_array(int *a) {
	printf("===============\n");
	for (int i = 0; i < N; i++) {
		printf("%4d", a[i]);
	}
	printf("\n");
}
int isFull() {
	if (rear == N)
		return 1;
	else
		return 0;
}
int isEmpty() {
	if (front == rear)
		return 1;
	else
		return 0;
}
void enQueue(int *a, int item) {
	if (isFull())
		printf("Queue is full!\n");
	else {
		a[rear] = item;
		rear++;
	}
}
int deQueue(int *a) {
	if (isEmpty())
		printf("Queue is empty!\n");
	else {
		int t = a[front];
		a[front] = 0;
		front++;
		return t;
	}
}
```

**linklist_queue.c**

```c=
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct node {
	int data;
	struct node *link;
} LNODE;

int isEmpty(LNODE *);
void enQueue(LNODE **,int);
int deQueue(LNODE **);
void show_list(LNODE *);

int main() {
	srand(time(NULL));
	LNODE *h = NULL;
	for (int i = 0; i < 10; i++) {
		int x = rand() % 5;
		if (x == 0) {
			deQueue(&h);
		} else {
			int y = rand() % 100;
			printf("enQueue:%d\n", y);
			enQueue(&h, y);
		}
		show_list(h);
	}
	return 0;
}
int isEmpty(LNODE *a) {
	if (a == NULL)
		return 1;
	else
		return 0;
}
int deQueue(LNODE **a) {
	LNODE *q = (*a);
	if (isEmpty(q)) {
		printf("Queue is empty!\n");
	} else {
		int r = q->data;
		printf("deQueue:%d\n", r);
		(*a) = q->link;
		return r;
	}
}
void enQueue(LNODE **a, int data) {
	LNODE *p;
	p = malloc(sizeof(LNODE));
	p->data = data;
	p->link = NULL;
	if ((*a) == NULL) {
		(*a) = p;
	} else {
		LNODE *t = (*a);
		while (t->link != NULL) {
			t = t->link;
		}
		t->link = p;
	}
}
void show_list(LNODE *a) {
	printf("===============\n");
	while (a != NULL) {
		printf("%4d", a->data);
		a = a->link;
	}
	printf("\n");
}
```