数据结构-栈与队列

1、顺序队列

#include "stdio.h"    
#include "stdlib.h"   
//#include "io.h"  
//#include "math.h"  
//#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */

typedef int Status;
typedef int QElemType; /* QElemType类型根据实际情况而定,这里假设为int */

/* 循环队列的顺序存储结构 */
typedef struct
{
	QElemType data[MAXSIZE];
	int front;    	/* 头指针 */
	int rear;		/* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
}SqQueue;

Status visit(QElemType c)
{
	printf("%d ", c);
	return OK;
}

/* 初始化一个空队列Q */
Status InitQueue(SqQueue *Q)
{
	Q->front = 0;
	Q->rear = 0;
	return  OK;
}

/* 将Q清为空队列 */
Status ClearQueue(SqQueue *Q)
{
	Q->front = Q->rear = 0;
	return OK;
}

/* 若队列Q为空队列,则返回TRUE,否则返回FALSE */
Status QueueEmpty(SqQueue Q)
{
	if (Q.front == Q.rear) /* 队列空的标志 */
		return TRUE;
	else
		return FALSE;
}

/* 返回Q的元素个数,也就是队列的当前长度 */
int QueueLength(SqQueue Q)
{
	return  (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}

/* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
Status GetHead(SqQueue Q, QElemType *e)
{
	if (Q.front == Q.rear) /* 队列空 */
		return ERROR;
	*e = Q.data[Q.front];
	return OK;
}

/* 若队列未满,则插入元素e为Q新的队尾元素 */
Status EnQueue(SqQueue *Q, QElemType e)
{
	if ((Q->rear + 1) % MAXSIZE == Q->front)	/* 队列满的判断 */
		return ERROR;
	Q->data[Q->rear] = e;			/* 将元素e赋值给队尾 */
	Q->rear = (Q->rear + 1) % MAXSIZE;/* rear指针向后移一位置, */
	/* 若到最后则转到数组头部 */
	return  OK;
}

/* 若队列不空,则删除Q中队头元素,用e返回其值 */
Status DeQueue(SqQueue *Q, QElemType *e)
{
	if (Q->front == Q->rear)			/* 队列空的判断 */
		return ERROR;
	*e = Q->data[Q->front];				/* 将队头元素赋值给e */
	Q->front = (Q->front + 1) % MAXSIZE;	/* front指针向后移一位置, */
	/* 若到最后则转到数组头部 */
	return  OK;
}

/* 从队头到队尾依次对队列Q中每个元素输出 */
Status QueueTraverse(SqQueue Q)
{
	int i;
	i = Q.front;
	while ((i + Q.front) != Q.rear)
	{
		visit(Q.data[i]);
		i = (i + 1) % MAXSIZE;
	}
	printf("\n");
	return OK;
}

int main()
{
	Status j;
	int i = 0, l;
	QElemType d;
	SqQueue Q;
	InitQueue(&Q);
	printf("初始化队列后,队列空否?%u(1:空 0:否)\n", QueueEmpty(Q));

	printf("请输入整型队列元素(不超过%d个),-1为提前结束符: ", MAXSIZE - 1);
	do
	{
		/* scanf("%d",&d); */
		d = i + 100;
		if (d == -1)
			break;
		i++;
		EnQueue(&Q, d);
	} while (i<MAXSIZE - 1);

	printf("队列长度为: %d\n", QueueLength(Q));
	printf("现在队列空否?%u(1:空 0:否)\n", QueueEmpty(Q));
	printf("连续%d次由队头删除元素,队尾插入元素:\n", MAXSIZE);
	for (l = 1; l <= MAXSIZE; l++)
	{
		DeQueue(&Q, &d);
		printf("删除的元素是%d,插入的元素:%d \n", d, l + 1000);
		/* scanf("%d",&d); */
		d = l + 1000;
		EnQueue(&Q, d);
	}
	l = QueueLength(Q);

	printf("现在队列中的元素为: \n");
	QueueTraverse(Q);
	printf("共向队尾插入了%d个元素\n", i + MAXSIZE);
	if (l - 2>0)
		printf("现在由队头删除%d个元素:\n", l - 2);
	while (QueueLength(Q)>2)
	{
		DeQueue(&Q, &d);
		printf("删除的元素值为%d\n", d);
	}

	j = GetHead(Q, &d);
	if (j)
		printf("现在队头元素为: %d\n", d);
	ClearQueue(&Q);
	printf("清空队列后, 队列空否?%u(1:空 0:否)\n", QueueEmpty(Q));
	return 0;
}
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">#include<stdio.h>
#include<stdlib.h>

#define MaxSize 20
typedef int ElemType;

typedef struct
{
	ElemType data[MaxSize];
	int front;
	int rear;
}SqQueue;

void InitQueue(SqQueue *&Q);
bool ClearQueue(SqQueue *Q);
bool QueueEmpty(SqQueue *Q);
int QueueLength(SqQueue *Q);

bool EnQueue(SqQueue *Q, ElemType e);
bool DeQueue(SqQueue *Q, ElemType &e);
bool GetHead(SqQueue *Q, ElemType &e);

int main()
{
	int j;
	int i = 0, l;
	ElemType d;
	SqQueue *Q;
	InitQueue(Q);
	printf("初始化队列后,队列空否?%u(1:空 0:否)\n", QueueEmpty(Q));

	printf("请输入整型队列元素(不超过%d个),-1为提前结束符: ", MaxSize - 1);
	do
	{
		d = i + 100;
		if (d == -1)
			break;
		i++;
		EnQueue(Q, d);
	} while (i<MaxSize - 1);

	printf("队列长度为: %d\n", QueueLength(Q));
	printf("现在队列空否?%u(1:空 0:否)\n", QueueEmpty(Q));
	printf("连续%d次由队头删除元素,队尾插入元素:\n", MaxSize);
	for (l = 1; l <= MaxSize; l++)
	{
		DeQueue(Q, d);
		printf("删除的元素是%d,插入的元素:%d \n", d, l + 1000);
		
		d = l + 1000;
		EnQueue(Q, d);
	}
	l = QueueLength(Q);

	printf("共向队尾插入了%d个元素\n", i + MaxSize);
	if (l - 2>0)
		printf("现在由队头删除%d个元素:\n", l - 2);
	while (QueueLength(Q)>2)
	{
		DeQueue(Q, d);
		printf("删除的元素值为%d\n", d);
	}

	j = GetHead(Q, d);
	if (j)
		printf("现在队头元素为: %d\n", d);
	ClearQueue(Q);
	printf("清空队列后, 队列空否?%u(1:空 0:否)\n", QueueEmpty(Q));
	return 0;
}

void InitQueue(SqQueue *&Q)
{
	Q = (SqQueue *)malloc(sizeof(SqQueue));
	Q->front = Q->rear = 0;
}

bool ClearQueue(SqQueue *Q)
{
	free(Q);
	return true;
}


bool QueueEmpty(SqQueue *Q)
{
	if (Q->front==Q->rear)
		return true;
	else
		return false;
}

int QueueLength(SqQueue *Q)
{
	return (Q->rear+MaxSize-Q->front)%MaxSize;
}


bool EnQueue(SqQueue *Q, ElemType e)
{
	if ((Q->rear + 1) % MaxSize == Q->front)  //队满上溢出
		return false;
	Q->data[Q->rear] = e;
	Q->rear = (Q->rear + 1) % MaxSize;
	
	return true;
}

bool DeQueue(SqQueue *Q, ElemType &e)
{
	if (Q->front == Q->rear)      //队空下溢出
		return false;
	e = Q->data[Q->front];
	Q->front = (Q->front + 1) % MaxSize;
	
	return true;
}
bool GetHead(SqQueue *Q, ElemType &e)
{
	if (Q->front == Q->rear) 
		return false;
	e = Q->data[Q->front];
	return true;
}


 

2、链式队列

#include "stdio.h"    
#include "stdlib.h"   
//#include "io.h"  
#include "math.h"  
//#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */

typedef int Status;

typedef int QElemType; /* QElemType类型根据实际情况而定,这里假设为int */

typedef struct QNode	/* 结点结构 */
{
	QElemType data;
	struct QNode *next;
}QNode, *QueuePtr;

typedef struct			/* 队列的链表结构 */
{
	QueuePtr front, rear; /* 队头、队尾指针 */
}LinkQueue;

Status visit(QElemType c)
{
	printf("%d ", c);
	return OK;
}

/* 构造一个空队列Q */
Status InitQueue(LinkQueue *Q)
{
	Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
	if (!Q->front)
		exit(OVERFLOW);
	Q->front->next = NULL;
	return OK;
}

/* 销毁队列Q */
Status DestroyQueue(LinkQueue *Q)
{
	while (Q->front)
	{
		Q->rear = Q->front->next;
		free(Q->front);
		Q->front = Q->rear;
	}
	return OK;
}

/* 将Q清为空队列 */
Status ClearQueue(LinkQueue *Q)
{
	QueuePtr p, q;
	Q->rear = Q->front;
	p = Q->front->next;
	Q->front->next = NULL;
	while (p)
	{
		q = p;
		p = p->next;
		free(q);
	}
	return OK;
}

/* 若Q为空队列,则返回TRUE,否则返回FALSE */
Status QueueEmpty(LinkQueue Q)
{
	if (Q.front == Q.rear)
		return TRUE;
	else
		return FALSE;
}

/* 求队列的长度 */
int QueueLength(LinkQueue Q)
{
	int i = 0;
	QueuePtr p;
	p = Q.front;
	while (Q.rear != p)
	{
		i++;
		p = p->next;
	}
	return i;
}

/* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
Status GetHead(LinkQueue Q, QElemType *e)
{
	QueuePtr p;
	if (Q.front == Q.rear)
		return ERROR;
	p = Q.front->next;
	*e = p->data;
	return OK;
}


/* 插入元素e为Q的新的队尾元素 */
Status EnQueue(LinkQueue *Q, QElemType e)
{
	QueuePtr s = (QueuePtr)malloc(sizeof(QNode));
	if (!s) /* 存储分配失败 */
		exit(OVERFLOW);
	s->data = e;
	s->next = NULL;
	Q->rear->next = s;	/* 把拥有元素e的新结点s赋值给原队尾结点的后继,见图中① */
	Q->rear = s;		/* 把当前的s设置为队尾结点,rear指向s,见图中② */
	return OK;
}

/* 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR */
Status DeQueue(LinkQueue *Q, QElemType *e)
{
	QueuePtr p;
	if (Q->front == Q->rear)
		return ERROR;
	p = Q->front->next;		/* 将欲删除的队头结点暂存给p,见图中① */
	*e = p->data;				/* 将欲删除的队头结点的值赋值给e */
	Q->front->next = p->next;/* 将原队头结点的后继p->next赋值给头结点后继,见图中② */
	if (Q->rear == p)		/* 若队头就是队尾,则删除后将rear指向头结点,见图中③ */
		Q->rear = Q->front;
	free(p);
	return OK;
}

/* 从队头到队尾依次对队列Q中每个元素输出 */
Status QueueTraverse(LinkQueue Q)
{
	QueuePtr p;
	p = Q.front->next;
	while (p)
	{
		visit(p->data);
		p = p->next;
	}
	printf("\n");
	return OK;
}

int main()
{
	int i;
	QElemType d;
	LinkQueue q;
	i = InitQueue(&q);
	if (i)
		printf("成功地构造了一个空队列!\n");
	printf("是否空队列?%d(1:空 0:否)  ", QueueEmpty(q));
	printf("队列的长度为%d\n", QueueLength(q));
	EnQueue(&q, -5);
	EnQueue(&q, 5);
	EnQueue(&q, 10);
	printf("插入3个元素(-5,5,10)后,队列的长度为%d\n", QueueLength(q));
	printf("是否空队列?%d(1:空 0:否)  ", QueueEmpty(q));
	printf("队列的元素依次为:");
	QueueTraverse(q);
	i = GetHead(q, &d);
	if (i == OK)
		printf("队头元素是:%d\n", d);
	DeQueue(&q, &d);
	printf("删除了队头元素%d\n", d);
	i = GetHead(q, &d);
	if (i == OK)
		printf("新的队头元素是:%d\n", d);
	ClearQueue(&q);
	printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n", q.front, q.rear, q.front->next);
	DestroyQueue(&q);
	printf("销毁队列后,q.front=%u q.rear=%u\n", q.front, q.rear);

	return 0;
}
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">#include<stdio.h>
#include<stdlib.h>

typedef int ElemType;

typedef struct Node
{
	ElemType data;
	struct Node *next;
}Node;

typedef struct
{
	 Node *front;
	 Node *rear;
}LinkQueue;

void InitQueue(LinkQueue *&Q);
bool ClearQueue(LinkQueue *Q);
bool QueueEmpty(LinkQueue *Q);
int QueueLength(LinkQueue *Q);
bool GetHead(LinkQueue *Q, ElemType &e);
bool EnQueue(LinkQueue *Q, ElemType e);
bool DeQueue(LinkQueue *Q, ElemType &e);



int main()
{
	bool i;
	ElemType d;
	LinkQueue *q;
	InitQueue(q);
	printf("成功地构造了一个空队列!\n");
	printf("是否空队列?%d(1:空 0:否)  ", QueueEmpty(q));
	printf("队列的长度为%d\n", QueueLength(q));
	EnQueue(q, -5);
	EnQueue(q, 5);
	EnQueue(q, 10);
	printf("插入3个元素(-5,5,10)后,队列的长度为%d\n", QueueLength(q));
	printf("是否空队列?%d(1:空 0:否) \n ", QueueEmpty(q));
	i = GetHead(q, d);
	if (i == true)
		printf("队头元素是:%d\n", d);
	DeQueue(q, d);
	printf("删除了队头元素%d\n", d);
	i = GetHead(q, d);
	if (i == true)
		printf("新的队头元素是:%d\n", d);
	ClearQueue(q);
	printf("是否空队列?%d(1:空 0:否) \n ", QueueEmpty(q));
}


void InitQueue(LinkQueue *&Q)
{
	Q = (LinkQueue *)malloc(sizeof(LinkQueue));
	Q->front=Q->rear = NULL;
}
bool ClearQueue(LinkQueue *Q)
{
	Node *p = Q->front, *r;  
	if (p != NULL)            
	{
		r = p->next;
		while (r != NULL)
		{
			free(p);
			p = r;
			r = p->next;
		}
	}
	free(p);
	free(Q);
	return true;
}
bool QueueEmpty(LinkQueue *Q)
{
	return(Q->rear==Q->front);
}
int QueueLength(LinkQueue *Q)
{
	int n = 0;
	Node *p = Q->front;
	while (p != NULL)
	{
		n++;
		p = p->next;
	}
	return(n);
}
bool GetHead(LinkQueue *Q, ElemType &e)
{
	Node *p;
	if (Q->front == Q->rear)
		return false;
	p = Q->front;
	e = p->data;
	return true;
}
bool EnQueue(LinkQueue *Q, ElemType e)
{
	Node *p;
	p = (Node *)malloc(sizeof(Node));
	p->data = e;
	p->next = NULL;
	if (Q->rear == NULL)      
		Q->front = Q->rear = p;
	else
	{
		Q->rear->next = p;    
		Q->rear = p;
	}
	return true;
}
bool DeQueue(LinkQueue *Q, ElemType &e)
{
	Node *t;
	if (Q->rear == NULL)      //队列为空
		return false;
	t = Q->front;             //t指向第一个数据节点
	if (Q->front == Q->rear)  //队列中只有一个节点时
		Q->front = Q->rear = NULL;
	else                    //队列中有多个节点时
		Q->front = Q->front->next;
	e = t->data;
	free(t);
	return true;
}


 

3、顺序栈

#include "stdio.h"    
#include "stdlib.h"   
//#include "io.h"  
//#include "math.h"  
//#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */

typedef int Status;
typedef int SElemType; /* SElemType类型根据实际情况而定,这里假设为int */

/* 顺序栈结构 */
typedef struct
{
	SElemType data[MAXSIZE];
	int top; /* 用于栈顶指针 */
}SqStack;

Status visit(SElemType c)
{
	printf("%d ", c);
	return OK;
}

/*  构造一个空栈S */
Status InitStack(SqStack *S)
{
	/* S.data=(SElemType *)malloc(MAXSIZE*sizeof(SElemType)); */
	S->top = -1;
	return OK;
}

/* 把S置为空栈 */
Status ClearStack(SqStack *S)
{
	S->top = -1;
	return OK;
}

/* 若栈S为空栈,则返回TRUE,否则返回FALSE */
Status StackEmpty(SqStack S)
{
	if (S.top == -1)
		return TRUE;
	else
		return FALSE;
}

/* 返回S的元素个数,即栈的长度 */
int StackLength(SqStack S)
{
	return S.top + 1;
}

/* 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR */
Status GetTop(SqStack S, SElemType *e)
{
	if (S.top == -1)
		return ERROR;
	else
		*e = S.data[S.top];
	return OK;
}

/* 插入元素e为新的栈顶元素 */
Status Push(SqStack *S, SElemType e)
{
	if (S->top == MAXSIZE - 1) /* 栈满 */
	{
		return ERROR;
	}
	S->top++;				/* 栈顶指针增加一 */
	S->data[S->top] = e;  /* 将新插入元素赋值给栈顶空间 */
	return OK;
}

/* 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR */
Status Pop(SqStack *S, SElemType *e)
{
	if (S->top == -1)
		return ERROR;
	*e = S->data[S->top];	/* 将要删除的栈顶元素赋值给e */
	S->top--;				/* 栈顶指针减一 */
	return OK;
}

/* 从栈底到栈顶依次对栈中每个元素显示 */
Status StackTraverse(SqStack S)
{
	int i;
	i = 0;
	while (i <= S.top)
	{
		visit(S.data[i++]);
	}
	printf("\n");
	return OK;
}

int main()
{
	int j;
	SqStack s;
	int e;
	if (InitStack(&s) == OK)
	for (j = 1; j <= 10; j++)
		Push(&s, j);
	printf("栈中元素依次为:");
	StackTraverse(s);
	Pop(&s, &e);
	printf("弹出的栈顶元素 e=%d\n", e);
	printf("栈空否:%d(1:空 0:否)\n", StackEmpty(s));
	GetTop(s, &e);
	printf("栈顶元素 e=%d 栈的长度为%d\n", e, StackLength(s));
	ClearStack(&s);
	printf("清空栈后,栈空否:%d(1:空 0:否)\n", StackEmpty(s));

	return 0;
}

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

#define MaxSize 20
typedef int ElemType;

typedef struct
{
	ElemType data[MaxSize];
	int top;
}SqStack;

void InitStack(SqStack *&S);
bool ClearStack(SqStack *S);
bool StackEmpty(SqStack *S);
int StackLength(SqStack *S);
bool GetTop(SqStack *S, ElemType &e);
bool Push(SqStack *S, ElemType e);
bool Pop(SqStack *S, ElemType &e);
void DispStack(SqStack *S);


int main()
{
	int j;
	SqStack *s;
	int e;
	InitStack(s);
	for (j = 1; j <= 10; j++)
		Push(s, j);
	printf("栈中元素依次为:");
	DispStack(s);
	Pop(s, e);
	printf("弹出的栈顶元素 e=%d\n", e);
	printf("栈空否:%d(1:空 0:否)\n", StackEmpty(s));
	GetTop(s, e);
	printf("栈顶元素 e=%d 栈的长度为%d\n", e, StackLength(s));
	ClearStack(s);
	printf("清空栈后,栈空否:%d(1:空 0:否)\n", StackEmpty(s));
}


void InitStack(SqStack *&S)
{
	S = (SqStack *)malloc(sizeof(SqStack));
	S->top = -1;
}

bool ClearStack(SqStack *S)
{
	S->top=-1;
	return true;
}


bool StackEmpty(SqStack *S)
{
	if (S->top == -1)
		return true;
	else
		return false;
}

int StackLength(SqStack *S)
{
	return S->top+1;
}

bool GetTop(SqStack *S, ElemType &e)
{
	if (S->top == -1)
		return false;
	else
	{
		e = S->data[S->top];
		return true;
	}
}


bool Push(SqStack *S, ElemType e)
{
	if (S->top ==MaxSize-1)
		return false;
	S->top++;
	S->data[S->top] = e;
	return true;
}

bool Pop(SqStack *S, ElemType &e)
{
	if (S->top == -1)
		return false;
	e = S->data[S->top];
	S->top--;
	return true;
}

void DispStack(SqStack *S)
{
	int i;
	for (i = S->top; i >= 0; i--)
		printf("%d ", S->data[i]);
	printf("\n");
}


 4、链式栈 

#include "stdio.h"    
#include "stdlib.h"   
//#include "io.h"  
//#include "math.h"  
//#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */

typedef int Status;
typedef int SElemType; /* SElemType类型根据实际情况而定,这里假设为int */


/* 链栈结构 */
typedef struct StackNode
{
	SElemType data;
	struct StackNode *next;
}StackNode, *LinkStackPtr;


typedef struct
{
	LinkStackPtr top;
	int count;
}LinkStack;

Status visit(SElemType c)
{
	printf("%d ", c);
	return OK;
}

/*  构造一个空栈S */
Status InitStack(LinkStack *S)
{
	S->top = (LinkStackPtr)malloc(sizeof(StackNode));
	if (!S->top)
		return ERROR;
	S->top = NULL;
	S->count = 0;
	return OK;
}

/* 把S置为空栈 */
Status ClearStack(LinkStack *S)
{
	LinkStackPtr p, q;
	p = S->top;
	while (p)
	{
		q = p;
		p = p->next;
		free(q);
	}
	S->count = 0;
	return OK;
}

/* 若栈S为空栈,则返回TRUE,否则返回FALSE */
Status StackEmpty(LinkStack S)
{
	if (S.count == 0)
		return TRUE;
	else
		return FALSE;
}

/* 返回S的元素个数,即栈的长度 */
int StackLength(LinkStack S)
{
	return S.count;
}

/* 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR */
Status GetTop(LinkStack S, SElemType *e)
{
	if (S.top == NULL)
		return ERROR;
	else
		*e = S.top->data;
	return OK;
}

/* 插入元素e为新的栈顶元素 */
Status Push(LinkStack *S, SElemType e)
{
	LinkStackPtr s = (LinkStackPtr)malloc(sizeof(StackNode));
	s->data = e;
	s->next = S->top;	/* 把当前的栈顶元素赋值给新结点的直接后继,见图中① */
	S->top = s;         /* 将新的结点s赋值给栈顶指针,见图中② */
	S->count++;
	return OK;
}

/* 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR */
Status Pop(LinkStack *S, SElemType *e)
{
	LinkStackPtr p;
	if (StackEmpty(*S))
		return ERROR;
	*e = S->top->data;
	p = S->top;					/* 将栈顶结点赋值给p,见图中③ */
	S->top = S->top->next;    /* 使得栈顶指针下移一位,指向后一结点,见图中④ */
	free(p);                    /* 释放结点p */
	S->count--;
	return OK;
}

Status StackTraverse(LinkStack S)
{
	LinkStackPtr p;
	p = S.top;
	while (p)
	{
		visit(p->data);
		p = p->next;
	}
	printf("\n");
	return OK;
}

int main()
{
	int j;
	LinkStack s;
	int e;
	if (InitStack(&s) == OK)
	for (j = 1; j <= 10; j++)
		Push(&s, j);
	printf("栈中元素依次为:");
	StackTraverse(s);
	Pop(&s, &e);
	printf("弹出的栈顶元素 e=%d\n", e);
	printf("栈空否:%d(1:空 0:否)\n", StackEmpty(s));
	GetTop(s, &e);
	printf("栈顶元素 e=%d 栈的长度为%d\n", e, StackLength(s));
	ClearStack(&s);
	printf("清空栈后,栈空否:%d(1:空 0:否)\n", StackEmpty(s));
	return 0;
}

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

typedef int ElemType;

typedef struct Node
{
	ElemType data;
	struct Node *next;
}Node,LinkStack;

void InitStack(LinkStack *&S);
bool ClearStack(LinkStack *S);
bool StackEmpty(LinkStack *S);
int StackLength(LinkStack *S);
bool GetTop(LinkStack *S, ElemType &e);
bool Push(LinkStack *S, ElemType e);
bool Pop(LinkStack *S, ElemType &e);
void DispStack(LinkStack *S);


int main()
{
	int j;
	LinkStack *s;
	int e;
	InitStack(s);
	for (j = 1; j <= 10; j++)
		Push(s, j);
	printf("栈中元素依次为:");
	DispStack(s);
	Pop(s, e);
	printf("弹出的栈顶元素 e=%d\n", e);
	printf("栈空否:%d(1:空 0:否)\n", StackEmpty(s));
	GetTop(s, e);
	printf("栈顶元素 e=%d 栈的长度为%d\n", e, StackLength(s));
	ClearStack(s);
	printf("清空栈后,栈空否:%d(1:空 0:否)\n", StackEmpty(s));
	return 0;
}


void InitStack(LinkStack *&S)
{
	S = (LinkStack *)malloc(sizeof(Node));
	S->next=NULL;
}

bool ClearStack(LinkStack *S)
{
	S->next=NULL;
	return true;
}


bool StackEmpty(LinkStack *S)
{
	if (S->next==NULL)
		return true;
	else
		return false;
}

int StackLength(LinkStack *S)
{
	LinkStack *p;
	int i=0;
	p = S->next;
	while (p)
	{
		p = p->next;
		i++;
	}

	return i;
}

bool GetTop(LinkStack *S, ElemType &e)
{
	if (S->next == NULL)      //栈空的情况
		return false;
	e = S->next->data;
	return true;
}


bool Push(LinkStack *S, ElemType e)
{
	LinkStack *p;
	p = (LinkStack *)malloc(sizeof(Node));
	p->data = e;              //新建元素e对应的节点*p
	p->next = S->next;        //插入*p节点作为开始节点
	S->next = p;
	return true;
}

bool Pop(LinkStack *S, ElemType &e)
{
	LinkStack *p;
	if (S->next == NULL)      //栈空的情况
		return false;
	p = S->next;              //p指向开始节点
	e = p->data;
	S->next = p->next;        //删除*p节点
	free(p);                //释放*p节点
	return true;
}

void DispStack(LinkStack *S)
{
	LinkStack *p = S->next;
	while (p != NULL)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}


 

5、两栈共享空间

#include "stdio.h"    
#include "stdlib.h"   
//#include "io.h"  
//#include "math.h"  
//#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */

typedef int Status;

typedef int SElemType; /* SElemType类型根据实际情况而定,这里假设为int */


/* 两栈共享空间结构 */
typedef struct
{
	SElemType data[MAXSIZE];
	int top1;	/* 栈1栈顶指针 */
	int top2;	/* 栈2栈顶指针 */
}SqDoubleStack;


Status visit(SElemType c)
{
	printf("%d ", c);
	return OK;
}

/*  构造一个空栈S */
Status InitStack(SqDoubleStack *S)
{
	S->top1 = -1;
	S->top2 = MAXSIZE;
	return OK;
}

/* 把S置为空栈 */
Status ClearStack(SqDoubleStack *S)
{
	S->top1 = -1;
	S->top2 = MAXSIZE;
	return OK;
}

/* 若栈S为空栈,则返回TRUE,否则返回FALSE */
Status StackEmpty(SqDoubleStack S)
{
	if (S.top1 == -1 && S.top2 == MAXSIZE)
		return TRUE;
	else
		return FALSE;
}

/* 返回S的元素个数,即栈的长度 */
int StackLength(SqDoubleStack S)
{
	return (S.top1 + 1) + (MAXSIZE - S.top2);
}

/* 插入元素e为新的栈顶元素 */
Status Push(SqDoubleStack *S, SElemType e, int stackNumber)
{
	if (S->top1 + 1 == S->top2)	/* 栈已满,不能再push新元素了 */
		return ERROR;
	if (stackNumber == 1)			/* 栈1有元素进栈 */
		S->data[++S->top1] = e; /* 若是栈1则先top1+1后给数组元素赋值。 */
	else if (stackNumber == 2)	/* 栈2有元素进栈 */
		S->data[--S->top2] = e; /* 若是栈2则先top2-1后给数组元素赋值。 */
	return OK;
}

/* 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR */
Status Pop(SqDoubleStack *S, SElemType *e, int stackNumber)
{
	if (stackNumber == 1)
	{
		if (S->top1 == -1)
			return ERROR; /* 说明栈1已经是空栈,溢出 */
		*e = S->data[S->top1--]; /* 将栈1的栈顶元素出栈 */
	}
	else if (stackNumber == 2)
	{
		if (S->top2 == MAXSIZE)
			return ERROR; /* 说明栈2已经是空栈,溢出 */
		*e = S->data[S->top2++]; /* 将栈2的栈顶元素出栈 */
	}
	return OK;
}

Status StackTraverse(SqDoubleStack S)
{
	int i;
	i = 0;
	while (i <= S.top1)
	{
		visit(S.data[i++]);
	}
	i = S.top2;
	while (i<MAXSIZE)
	{
		visit(S.data[i++]);
	}
	printf("\n");
	return OK;
}

int main()
{
	int j;
	SqDoubleStack s;
	int e;
	if (InitStack(&s) == OK)
	{
		for (j = 1; j <= 5; j++)
			Push(&s, j, 1);
		for (j = MAXSIZE; j >= MAXSIZE - 2; j--)
			Push(&s, j, 2);
	}

	printf("栈中元素依次为:");
	StackTraverse(s);

	printf("当前栈中元素有:%d \n", StackLength(s));

	Pop(&s, &e, 2);
	printf("弹出的栈顶元素 e=%d\n", e);
	printf("栈空否:%d(1:空 0:否)\n", StackEmpty(s));

	for (j = 6; j <= MAXSIZE - 2; j++)
		Push(&s, j, 1);

	printf("栈中元素依次为:");
	StackTraverse(s);

	printf("栈满否:%d(1:否 0:满)\n", Push(&s, 100, 1));


	ClearStack(&s);
	printf("清空栈后,栈空否:%d(1:空 0:否)\n", StackEmpty(s));

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值