栈的定义:
线性表的一种特殊具体形式,具有后进先出的特点,它要求只在表尾进行删除和插入操作
栈的插入和删除操作
栈的插入操作(Push),叫做进栈
栈的删除操作(Pop),叫做出栈
栈的顺序存储结构:
定义:
typedef struct
{
ElemType *base; //指向栈底的指针变量
ElemType *top; //指向栈顶的指针变量
int stackSize; //栈的当前可使用的最大容量
}sqStack;
创建一个栈:
#define STACK_INIT_SIZE 100
initStack(sqStack *s)
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->top = s->base; //最开始,栈顶就是栈底
s->stackSize = STACK_INIT_SIZE;
}
入栈操作:
#define SATCKINCREMENT 10
Push(sqStack *s, ElemType e)
{
//如果栈满,追加空间
if(s->top - s->base >= s->stackSize)
{
s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->top = s->base + s->stackSize; //设置栈顶
s->stackSize = s->stackSize + STACKINCREMENT; //设置栈的最大容量
}
*(s->top) = e;
s->top++;
}
出栈操作:
Pop(sqStack *s, ElemType *e)
{
if(s->top == s->base) //栈已空空如也
{
return;
}
*e = *--(s->top);
}
栈的其他操作
清空一个栈:
ClearStack(sqStack *s)
{
s->top = s->base; //数据存在,无法找到
}
销毁一个栈:
DestroyStack(sqStack *s)
{
int i, len;
len = s->stackSize;
for(i=0 ; i<len ; i++)
{
free(s->base);
s->base++;
}
s->base = s->top = NULL;
s->stackSize = 0;
}
计算栈的当前容量:
int StackLen(sqStack s)
{
return(s.top - s.base);
}
实例:将二进制数转化为十进制数
/*将二进制数转化为十进制数*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef char ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stackSize;
}sqStack;
/*初始化栈*/
void InitStack(sqStack *s)
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
/*入栈*/
void Push(sqStack *s, ElemType e)
{
if( s->top - s->base >= s->stackSize) //栈满,追加空间
{
s->base = (ElemType*)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->top = s->base + s->stackSize; //设置栈顶
s->stackSize = s->stackSize + STACKINCREMENT; //设置栈的最大容量
}
*(s->top) = e;
s->top++;
}
/*出栈*/
void Pop(sqStack *s, ElemType *e)
{
if(s->top == s->base)
{
return;
}
*e = *--(s->top);
}
/*计算栈的当前容量*/
int StackLen(sqStack s)
{
return(s.top - s.base);
}
int main(void)
{
ElemType c;
sqStack s;
int len, i, sum = 0;
InitStack(&s);
printf("Please enter binary number, enter # to end:\n");
scanf("%c", &c);
while( c != '#')
{
Push(&s, c);
scanf("%c", &c);
}
getchar(); //在使用char类型时,换行同样是字符,需要从缓冲区去掉
len = StackLen(s);
printf("The current capacity of the stack is %d\n", len);
for(i=0 ; i<len ; i++)
{
Pop(&s, &c);
sum = sum + (c-48) * pow(2, i);
}
printf("The result is %d\n", sum);
return 0;
}
将二进制转化为八进制数(用两个栈)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef char ElemType;
typedef struct
{
ElemType *top;
ElemType *base;
int stackSize;
}spStack;
/*初始化栈*/
void InitStack(spStack *s)
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
/*入栈*/
void Push(spStack *s, ElemType e)
{
if(s->top - s->base >= s->stackSize)
{
s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
s->top = s->base + s->stackSize;
s->stackSize = s->stackSize + STACKINCREMENT;
}
*(s->top) = e;
s->top++;
}
/*出栈*/
void Pop(spStack *s, ElemType *e)
{
if(s->top == s->base)
{
return;
}
*e = *--(s->top);
}
/*计算栈的当前容量*/
int Stacklen(spStack s)
{
return (s.top - s.base);
}
int main()
{
ElemType c;
spStack s1, s2;
int len, i, j, sum;
InitStack(&s1);
InitStack(&s2);
printf("Please enter binary number, enter # to end:\n");
scanf("%c", &c);
while( c != '#' )
{
Push(&s1, c);
scanf("%c", &c);
}
getchar();
len = Stacklen(s1);
printf("The current capacity of the stack is %d\n", len);
for(i=0 ; i<len ; i+=3)
{
sum = 0;
for(j=0 ; j<3 ; j++)
{
Pop(&s1, &c);
sum = sum + (c-48) * pow(2, j);
if(s1.base == s1.top)
{
break;
}
}
Push(&s2, sum+48);
}
printf("\nThe octal number of this number is:\n");
while(s2.base != s2.top)
{
Pop(&s2, &c);
printf("%c", c);
}
printf("(O)\n");
return 0;
}
将二进制转化为十六进制数(用两个栈)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef char ElemType;
typedef struct
{
ElemType *top;
ElemType *base;
int stackSize;
}sqStack;
void InitStack(sqStack *s)
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void Push(sqStack *s, ElemType e)
{
if(s->top - s->base >= s->stackSize)
{
s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
s->top = s->base + s->stackSize;
s->stackSize = s->stackSize + STACKINCREMENT;
}
*(s->top) = e;
s->top++;
}
void Pop(sqStack *s, ElemType *e)
{
if(s->top == s->base)
{
return;
}
*e = *--(s->top);
}
int StackLen(sqStack s)
{
return (s.top - s.base);
}
int main(void)
{
sqStack s1, s2;
ElemType c;
int len, i, j, sum;
InitStack(&s1);
InitStack(&s2);
printf("Please enter binary number, enter # to end:\n");
scanf("%c", &c);
while(c != '#')
{
Push(&s1, c);
scanf("%c", &c);
}
getchar();
len = StackLen(s1);
for(i=0 ; i<len ; i+=4)
{
sum = 0;
for(j=0 ; j<4 ; j++)
{
Pop(&s1, &c);
sum = sum + (c-48) * pow(2, j);
if(s1.top == s1.base)
{
break;
}
}
switch(sum)
{
case 10:
sum = 'A';
break;
case 11:
sum = 'B';
break;
case 12:
sum = 'C';
break;
case 13:
sum = 'D';
break;
case 14:
sum = 'E';
break;
case 15:
sum = 'F';
break;
default:
sum = sum + 48;
}
Push(&s2, sum);
}
printf("\nThe hexadecimal number of this number is:\n");
while( s2.base != s2.top )
{
Pop(&s2, &c);
printf("%c", c);
}
printf("(H)\n");
return 0;
}
栈的链式存储:
#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
typedef int Status;
typedef int ElemType;
/*栈的链式存储结构*/
typedef struct StackNode
{
ElemType data; //存放栈的数据
struct StackNode *next;
}StackNode, *LinkStackPtr;
typedef struct LinkStack
{
LinkStackPtr top; //top指针
int count; //栈元素计数器
}
/*进栈操作*/
Status Push(LinkStack *s, ElemType e)
{
LinkStackPtr p = (LinkStackPtr)malloc(sizeof(StackNode));
p->data = e;
p->next = s->top;
s->top = p;
s->count++;
return OK;
}
/*判断是否为空栈*/
Status StackEmpty(LinkStack *s)
{
if(s->count == 0)
{
return OK;
}
else
{
return ERROR;
}
}
/*出栈操作*/
Status Pop(LinkStack *s, ElemType *e)
{
LinkStackPtr p;
if(StackEmpty(*s))
{
return ERROR;
}
*e = s->top->data;
p = s->top;
s->top = s->top->next;
free(p);
s->count--;
return OK;
}
逆波兰计算器
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 10
#define MAXBUFFER 10
typedef double ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stackSize;
}sqStack;
/*初始化栈*/
void InitStack(sqStack *s)
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); //以base指针动态分配10个double类型的内存空间
if(!s->base)
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void Push(sqStack *s, ElemType e)
{
if( s->top - s->base >= s->stackSize )
{
s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->top = s->base + s->stackSize;
s->stackSize = s->stackSize + STACKINCREMENT;
}
*(s->top) = e;
s->top++;
}
void Pop(sqStack *s, ElemType *e)
{
if( s->top == s->base )
{
return;
}
*e = *--(s->top);
}
int StackLen(sqStack s)
{
return (s.top - s.base);
}
int main(void)
{
sqStack s;
char c;
int i = 0;
ElemType d, e;
char str[MAXBUFFER];
InitStack(&s);
printf("Please enter a value, each value is separated by a space, and enter # to end:\n");
scanf("%c", &c);
while( c != '#')
{
while( isdigit(c) || c == '.')
{
str[i] = c;
i++;
str[i] = '\0';
if(i >= 10)
{
printf("Error: The value entered is too large!\n");
return -1;
}
scanf("%c", &c);
if( c == ' ' )
{
d = atof(str);
Push(&s, d);
i = 0;
break;
}
}
switch( c )
{
case '+':
Pop(&s, &e);
Pop(&s, &d);
Push(&s, d+e);
break;
case '-':
Pop(&s, &e);
Pop(&s, &d);
Push(&s, d-e);
break;
case '*':
Pop(&s, &e);
Pop(&s, &d);
Push(&s, d*e);
break;
case '/':
Pop(&s, &e);
Pop(&s, &d);
if( e != 0)
{
Push(&s, d/e);
}
else
{
printf("Error: Divisor is 0!\n");
return -1;
}
break;
}
scanf("%c", &c);
}
Pop(&s, &d);
printf("The result is %f\n", d);
return 0;
}
计算器(包括中缀表达式转换成后缀表达式并进行计算):
/*主函数*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "calculate.h"
#include "polan.h"
#define MAXBUFFER 50
int main(void)
{
culStack s;
Stack r;
char c, d;
double e, f;
char str[MAXBUFFER];
char strg[MAXBUFFER];
int i = 0, j = 0;
CulInitStack(&s);
printf("Please enter an expression and enter # to end:\n");
scanf("%c", &c);
while('#' != c)
{
while(isdigit(c) || '.' == c)
{
str[i++] = c;
scanf("%c", &c);
}
if(')' == c)
{
CulPop(&s, &d);
while('(' != d)
{
str[i++] = ' ';
str[i++] = d;
CulPop(&s, &d);
}
}
else if('+' == c || '-' == c)
{
if(!CulStackLen(s))
{
CulPush(&s, c);
}
else
{
do
{
CulPop(&s, &d);
if('(' == d)
{
CulPush(&s, d);
}
else
{
str[i++] = ' ';
str[i++] = d;
}
}while(CulStackLen(s) && '(' != d);
CulPush(&s, c);
}
}
else if('*' == c || '/' == c || '(' == c)
{
CulPush(&s, c);
}
else if('#' == c)
{
break;
}
else
{
printf("Error: Illegal input!\n");
}
str[i++] = ' ';
scanf("%c", &c);
}
while(CulStackLen(s))
{
CulPop(&s, &d);
str[i++] = ' ';
str[i++] = d;
}
str[i] = '\0';
i = 0;
while('\0' != str[i])
{
printf("%c", str[i]);
i++;
}
printf("\n");
InitStack(&r);
i = 0;
while('\0' != str[i])
{
while(isdigit(str[i]) || '.' == str[i])
{
strg[j++] = str[i++];
strg[j] = '\0';
if( j >= MAXBUFFER)
{
printf("Error: The single data entered is too large!\n");
return -1;
}
if(' ' == str[i])
{
e = atof(strg);
Push(&r, e);
j = 0;
i++;
break;
}
}
switch( str[i] )
{
case '+':
Pop(&r, &f);
Pop(&r, &e);
Push(&r, e+f);
i++;
break;
case '-':
Pop(&r, &f);
Pop(&r, &e);
Push(&r, e-f);
i++;
break;
case '*':
Pop(&r, &f);
Pop(&r, &e);
Push(&r, e*f);
i++;
break;
case '/':
Pop(&r, &f);
Pop(&r, &e);
if( f != 0)
{
Push(&r, e/f);
i++;
}
else
{
printf("Error: Divisor is 0!\n");
return -1;
}
break;
case ' ':
i++;
break;
default:
break;
}
}
Pop(&r, &e);
printf("\nThe final calculation result is: %f\n", e);
return 0;
}
/*中缀转后缀用栈*/
//#include "calculate.h"
#include <stdio.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef struct
{
char *base;
char *top;
int stackSize;
}culStack;
void CulInitStack(culStack *s)
{
s->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
if(!s->base)
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void CulPush(culStack *s, char d)
{
if(s->top - s->base >= s->stackSize)
{
s->base = (char *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(char));
if(!s->base)
{
exit(0);
}
s->top = s->base + s->stackSize;
s->stackSize = s->stackSize + STACKINCREMENT;
}
*(s->top) = d;
s->top++;
}
void CulPop(culStack *s, char *d)
{
if(s->top == s->base)
{
return;
}
*d = *--(s->top);
}
int CulStackLen(culStack s)
{
return (s.top - s.base);
}
/*逆波兰计算器用栈*/
//#include "polan.h"
#include <stdio.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef struct
{
double *base;
double *top;
int stackSize;
}Stack;
void InitStack(Stack *s)
{
s->base = (double *)malloc(STACK_INIT_SIZE * sizeof(double)); //以base指针动态分配10个类型的内存空间
if(!s->base)
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void Push(Stack *s, double e)
{
if( s->top - s->base >= s->stackSize )
{
s->base = (double *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(double));
if(!s->base)
{
exit(0);
}
s->top = s->base + s->stackSize;
s->stackSize = s->stackSize + STACKINCREMENT;
}
*(s->top) = e;
s->top++;
}
void Pop(Stack *s, double *e)
{
if( s->top == s->base )
{
return;
}
*e = *--(s->top);
}
int StackLen(Stack s)
{
return (s.top - s.base);
}
队列:只允许在一端进行插入操作,而在另一端进行删除操作的线性表(输入缓冲区)
队列的链式存储结构:
typedef struct QNode
{
ElemType data;
struct QNode *next;
}QNode, *QueuePrt;
typedef struct
{
QueuePrt front, rear; //队头、队尾指针
}LinkQueue;
创建一个队列:
1.创建一个头结点
2.将队列的头指针和尾指针都指向生成的头结点
initQueue(LinkQueue *q)
{
q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));
if( !q->front )
{
exit(0);
}
q->front->next = NULL;
}
入队列操作:
InsertQueue(LinkQueue *q, ElemType e)
{
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if( p == NULL )
{
exit(0);
}
p->data = e;
p->next = q->rear->next;
q->rear->next = p;
q->rear = p;
}
出队列操作
void DeleteQueue(LinkQueue *q, ElemType *e)
{
QueuePtr p;
if(q->front == q->rear)
{
return;
}
p = q->front->next;
*e = p->data;
q->front->next = p->next;
if( q->rear == p )
{
q->rear = q->front;
}
free(p);
}
销毁队列操作
void DestroyQueue(LinkQueue *q)
{
while( q->front )
{
q->rear = q->front->next;
free(q->front);
q->front = q->rear;
}
}
输入字符并使之顺序输出:
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct QNode
{
ElemType data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{
QueuePtr front, rear; //队首、队尾指针
}LinkQueue;
void InitQueue(LinkQueue *q)
{
q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));
if( !q->front )
{
exit(0);
}
q->rear->next = NULL;
}
void InsertQueue(LinkQueue *q, ElemType e)
{
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if( p == NULL )
{
exit(0);
}
p->data = e;
p->next = q->rear->next;
q->rear->next = p;
q->rear = p;
}
void DeleteQueue(LinkQueue *q, ElemType *e)
{
QueuePtr p;
if(q->front == q->rear)
{
return;
}
p = q->front->next;
*e = p->data;
q->front->next = p->next;
if( q->rear == p )
{
q->rear = q->front;
}
free(p);
}
void DestroyQueue(LinkQueue *q)
{
while( q->front )
{
q->rear = q->front->next;
free(q->front);
q->front = q->rear;
}
}
void TreverseQueue(LinkQueue *q)
{
QueuePtr p;
p = q->front->next;
printf("\nThe queue:\n");
while( p->next != NULL )
{
printf("%c", p->data);
p = p->next;
}
printf("%c\n", p->data);
}
int main(void)
{
LinkQueue q;
int x;
char c;
printf("This is a program of a linked queue\n");
printf("\n1.InitQueue 2.InsertQueue 3.DeleteQueue 4.DestroyQueue 0.exit\n");
while( 1 )
{
printf("Please enter the number to choose the operation:\n");
scanf("%d", &x);
switch ( x )
{
case 1:
InitQueue(&q);
break;
case 2:
printf("Please enter the string, and enter # to end:\n");
fflush(stdin); //不清除缓存会将enter作为第一个字符录入
scanf("%c", &c);
while( '#' != c )
{
InsertQueue(&q, c);
scanf("%c", &c);
}
TreverseQueue(&q);
break;
case 3:
DeleteQueue(&q, &c);
printf("The deleted data is %c\n", c);
TreverseQueue(&q);
break;
case 4:
DestroyQueue(&q);
/*
if( !q->front )
{
printf("Operation success!\n");
}
else
{
printf("Operation failed!\n");
}
*/
break;
case 0:
exit(0);
}
}
return 0;
}
队列的顺序存储结构
#include <stdio.h>
#include <stdlib.h>
#define QueueSize 20
typedef char ElemType;
typedef struct
{
ElemType *base; //用于存放内存分配基地址
//ElemType qa[QueueSize]; //上述情况的数组存放形式
int front;
int rear;
}CycleQueue;
void InitArrayQueue(CycleQueue *q)
{
q->base = (ElemType *)malloc( QueueSize * sizeof(ElemType));
if( !q->base )
{
exit(0);
}
q->front = q->rear = 0;
}
void InsertArrayQueue(CycleQueue *q, ElemType e)
{
if( q->rear == q->front )
{
printf("The queue is full!\n");
return;
}
q->base[q->rear] = e;
q->rear = (q->rear+1) % QueueSize;
}
void DeleteArrayQueue(CycleQueue *q, ElemType *e)
{
if( q->front == q->rear )
{
printf("The queue is empty!\n");
return;
}
*e = q->base[q->front];
q->front = (q->front+1) % QueueSize;
}