数据结构——栈和队列的基本操作
// 栈的基本操作
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef char Chartype;
typedef struct
{
Chartype *top;
Chartype *base;
int stacksize;
}SqStack;
// 顺序栈的初始化
SqStack* InitStack()
{
SqStack *S = (SqStack*)malloc(sizeof(SqStack));
int size = STACK_INIT_SIZE * sizeof(Chartype);
S -> base = (Chartype*)malloc(size);
if (!S -> base)
exit(OVERFLOW);
S -> top = S -> base;
S -> stacksize = STACK_INIT_SIZE;
return S;
}
// 判断顺序栈是否为空
int StackEmpty(SqStack *S)
{
if (S -> top == S -> base)
return TRUE;
else
return FALSE;
}
// 求顺序栈的长度
int StackLength(SqStack *S)
{
return S -> top - S -> base;
}
// 清空顺序栈
int ClearStack(SqStack *S)
{
if (S -> base)
S -> top = S -> base;
return OK;
}
// 销毁顺序栈
int DestoryStack(SqStack *S)
{
if (S -> base)
{
free(S -> base);
S -> stacksize = 0;
S -> base = S -> top = NULL;
}
return OK;
}
// 顺序栈进栈
void Push(SqStack *S, Chartype e)
{
if (S -> top - S -> base >= S -> stacksize) // 栈满
{
int size = (S -> stacksize + STACKINCREMENT) * sizeof(Chartype);
S -> base = (Chartype*)realloc(S -> base, size);
if (!S -> base)
exit(OVERFLOW);
S -> top = S -> base + S -> stacksize;
S -> stacksize += STACKINCREMENT;
}
*S -> top = e;
S -> top++;
}
// 顺序栈出栈
void Pop(SqStack *S)
{
if (S -> top == S -> base)
exit(OVERFLOW);
--S -> top;
// e = *S -> top;
}
// 取顺序栈栈顶元素
Chartype GetTop(SqStack *S)
{
Chartype e;
if (S -> top == S -> base)
return 0;
e = *(S -> top - 1);
return e;
}
// 输入
void inputstack(SqStack *S)
{
int i = 0;
char str[100];
gets(str);
while (str[i] != '\0')
{
Push(S, str[i]);
i++;
}
}
// 打印
void printstack(SqStack *S)
{
SqStack *temp = S;
--temp -> top;
while (temp -> top != temp -> base)
{
printf("%c", *temp -> top);
--temp -> top;
}
printf("%c", *temp -> top);
}
int main(void)
{
......
return 0;
}
队列的基本操作
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct
{
node *front;
node *rear;
}queue;
//初始化结点
node *init_node(){
node *n = (node*)malloc(sizeof(node));
if(n == NULL)
exit(0); //建立失败,退出
return n;
}
//初始化队列
queue *init_queue(){
queue *q = (queue*)malloc(sizeof(queue));
if(q == NULL)
exit(0); //建立失败,退出
//头尾结点均赋值NULL
q -> front = NULL;
q -> rear = NULL;
return q;
}
// 判断是否为空
int empty(queue *q){
return q -> front == NULL;
}
//入队操作
void push(queue *q, int data){
node *n =init_node();
n -> data = data;
n -> next = NULL; //采用尾插入法
//if(q->rear==NULL){ //使用此方法也可以
if(empty(q))
{
q -> front = n;
q -> rear = n;
}
else
{
q -> rear -> next = n; //n成为当前尾结点的下一结点
q -> rear = n; //让尾指针指向n
}
}
//出队操作
int pop(queue *q){
node *n = q -> front;
int temp;
if(empty(q))
exit(0); //此时队列为空,直接返回函数结束
if(q -> front == q -> rear)
{
temp = q -> front -> data;
q -> front = NULL; //只有一个元素时直接将两端指向制空即可
q -> rear = NULL;
free(n); //记得归还内存空间
}
else
{
temp = q -> front -> data;
q -> front = q -> front -> next;
free(n);
}
return temp;
}
//打印队列元素
void print_queue(queue *q){
node *n = init_node();
n = q -> front;
if(empty(q))
exit(0); //此时队列为空,直接返回函数结束
while (n != NULL)
{
printf("%d ",n -> data);
n = n -> next;
}
printf("\n"); //记得换行
}
int main(void)
{
......
return 0;
}