summaryrefslogtreecommitdiffstats
path: root/inc/dslib.h
blob: 81ea5735c00b0825184e1b165187fcb9137b36fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define NUM_LEN 63

typedef struct data {
	char p[NUM_LEN];
	char unit;
} Data;

typedef struct stack {
	Data d;
	struct stack *link;
} stack;

typedef struct queue {
	Data d;
	struct queue *link;
} queue;

static void push(stack **top, Data d)
{
	stack *new = (stack *)malloc(sizeof(stack));

	new->d = d;
	new->link = NULL;

	if (*top == NULL)
		*top = new;
	else {
		new->link = *top;
		*top = new;
	}
}

static void pop(stack **top, Data *d)
{
	d->p[0] = '\0';
	d->unit = 0;

	if (*top != NULL) {
		stack *tmp = *top;
		*top = (*top)->link;
		*d = tmp->d;
		free(tmp);
	}
}

static void enqueue(queue **front, queue **rear, Data d)
{
	queue *new = (queue *)malloc(sizeof(queue));

	new->d = d;
	new->link = NULL;

	if (*front == NULL)
		*front = *rear = new;
	else {
		(*rear)->link = new;
		*rear = new;
	}
}

static void dequeue(queue **front, queue **rear, Data *d)
{
	d->p[0] = '\0';
	d->unit = 0;

	if (*front != NULL) {
		queue *tmp;

		if (*front == *rear) {
			tmp = *front;
			*d = tmp->d;
			free(tmp);
			*front = *rear = NULL;
		} else {
			tmp = *front;
			*front = (*front)->link;
			*d = tmp->d;
			free(tmp);
		}
	}
}

static int isempty(stack *top)
{
	if (top == NULL)
		return 1;

	return 0;
}

static char *top(stack *top)
{
	if (top == NULL)
		return NULL;

	return top->d.p;
}

static void emptystack(stack **top)
{
	stack *tmp;

	while (*top != NULL) {
		tmp = *top;
		*top = (*top)->link;
		free(tmp);
	}
}

static void cleanqueue(queue **front)
{
	queue *tmp;

	while (*front != NULL) {
		tmp = *front;
		*front = (*front)->link;
		free(tmp);
	}
}

/*
static void printstack(stack *top)
{
	stack *i;

	printf("\nStack: ");

	if (top == NULL)
		printf("Empty");

	for (i = top; i != NULL; i = i->link)
		printf(" %s ", i->d.p);

	printf("\n");
}

static void printqueue(queue *front)
{
	queue *i;

	printf("\nQueue: ");

	if (front == NULL)
		printf("Empty");

	for (i = front; i != NULL; i = i->link)
		printf(" %s ", i->d.p);

	printf("\n");
}
*/