您的位置:首页 > 理论基础 > 数据结构算法

数据结构第二讲笔记之栈、队列(2)

2018-03-22 21:59 253 查看
#数据结构 Record Two#    #Stack 、Queue #
#堆栈#
栈是一个先进后出的线性表,它仅允许在表的一端进行插入和删除操作(运算受限的线性表)!
下面首先来看一看顺序存储实现的堆栈:
/*堆栈的顺序存储实现*/
/*本测试所用数据类型为int型*/
#include <cstdio>
#include <cstdlib>

#define ERROR -1
typedef int Position;
typedef struct SNode *Stack;
struct SNode{
int * Data;
Position Top;
int MaxSize;
};

/*创建一个空栈*/
Stack CreateStack(int MaxSize){
Stack S;
S = (Stack)malloc(sizeof(struct SNode));
S->Data = (int*)malloc(MaxSize*sizeof(int));
S->Top = -1;
S->MaxSize = MaxSize;
return S;
}

/*判断栈是否满*/
bool isFull(Stack S){
return (S->Top == S->MaxSize-1);
}

/*入栈操作*/
bool Push(Stack S , int X){
if( isFull(S) ){
printf("堆栈满\n");
return false;
}
else{
S->Data[++(S->Top)] = X;
printf("元素%d入栈成功\n",X);
return true;
}
}

/*判断栈是否为空*/
bool isEmpty(Stack S){
return (S->Top == -1);
}

/*出栈操作*/
int Pop(Stack S){
if( isEmpty(S) ){
printf("堆栈空\n");
return ERROR;
}
else{
return (S->Data[(S->Top)--]);
}
}

int main(){
const int MAXSIZE = 5;
Stack S_stack = CreateStack(MAXSIZE);
Push(S_stack,1);
Push(S_stack,2);
Push(S_stack,3);
Push(S_stack,4);
Push(S_stack,5);

printf("\n");

while(!isEmpty(S_stack)){
int n;
n = Pop(S_stack);
printf("元素%d出栈成功\n",n);
}
return 0;
}
结果如图:



下面是堆栈的链式存储的实现:/*堆栈的链式存储实现*/
/*本测试所用数据类型为int型*/
#include <cstdio>
#include <cstdlib>

typedef struct SNode *PtrToSNode;
struct SNode{
int Data;
PtrToSNode Next;
};
typedef PtrToSNode Stack;

#define ERROR NULL

/*创建一个空栈*/
Stack CreateStack(){
Stack S;
S = (Stack)malloc(sizeof(struct SNode));
S->Next = NULL;
return S;
}

/*判断栈是否为空*/
bool isEmpty(Stack S){
return (S->Next == NULL);
}

/*入栈操作*/
bool Push(Stack S , int X){
PtrToSNode tmpCell
4000
;
tmpCell = (PtrToSNode)malloc(sizeof(struct SNode));
tmpCell->Data = X;
tmpCell->Next = S->Next;
S->Next = tmpCell;
printf("元素%d入栈成功\n",X);
return true;
}

/*出栈操作*/
int Pop(Stack S){
PtrToSNode firstCell;
int TopElem;

if(isEmpty(S)){
printf("堆栈为空,已无法进行出栈操作!\n");
return ERROR;
}
else{
firstCell = S->Next;
TopElem = firstCell->Data;
S->Next = firstCell->Next;
free(firstCell);
return TopElem;
}
}

//测试
int main(){
Stack S = CreateStack();
Push(S,1);
Push(S,2);
Push(S,3);

printf("\n");

while(!isEmpty(S)){
int n;
n = Pop(S);
printf("元素%d出栈成功\n",n);
}
return 0;
} 结果如图:

#队列#
和栈一样,队列也是一种操作受限制的特殊的线性表,特殊之处在于它们只允许在表的前端(队头)进行删除元素操作,而在表的后端(队尾)进行插入元素操作,因此队列的元素遵循先进先出的操作顺序。

实现顺序存储的循环队列要注意的问题:
1.如何解决队列中判空还是判满?
(1)使用额外标记:size(入队++ , 出队--)或者tag(入队置为1,出队置为0,当队头等于队尾时只要判断最后一次的tag情况即可知此时队列满还是空);
(2)仅使用n-1个数组空间
2.用求余的方法来维持循环队列。
下面是(循环)队列的顺序存储实现:
/*队列的顺序存储实现(测试)*/
/*本测试所用数据类型为int型*/
#include <cstdio>
#include <cstdlib>

typedef int Position;
struct QNode{
int *Data;
Position Front , Rear;//队列的头尾指针
int MaxSize;
};
typedef struct QNode *Queue;

/*创建一个队列*/
Queue CreateQueue(int MaxSize){
Queue Q;
Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (int*)malloc(MaxSize*sizeof(int));
Q->Front = Q->Rear = 0;
Q->MaxSize = MaxSize;
return Q;
}

/*判断队列是否为满*/
bool isFull(Queue Q){
return ((Q->Rear+1)%(Q->MaxSize) == Q->Front);
}

/*入队操作*/
bool AddQ(Queue Q , int X){
if(isFull(Q)){
printf("队列已满,无法进行入队操作!\n");
return false;
}
else{
Q->Rear = (Q->Rear+1)%(Q->MaxSize);
Q->Data[Q->Rear] = X;
printf("元素%d已入队成功\n",X);
return true;
}
}

/*判断队列是否为空*/
bool isEmpty(Queue Q){
return (Q->Rear == Q->Front);
}

#define ERROR -1
/*出队操作*/
int DeleteQ(Queue Q){
if(isEmpty(Q)){
printf("队列已空,无法进行出队操作!");
return ERROR;
}
else{
Q->Front = (Q->Front+1)%(Q->MaxSize);
return Q->Data[Q->Front];
}
}

int main(){
const int MaxSize = 5;
Queue Q;
Q = CreateQueue(MaxSize);
AddQ(Q,1);
AddQ(Q,2);
AddQ(Q,3);
AddQ(Q,4);
AddQ(Q,5);

printf("\n");

while(!isEmpty(Q)){
int q;
q = DeleteQ(Q);
printf("元素%d已出队!\n",q);
}
return 0;
}结过如图:

下面是(循环)队列的链式存储实现:
/*队列的链式存储实现(测试)*/
/*本测试所用数据类型为int型*/
#include <cstdio>
#include <cstdlib>

typedef struct SNode *PtrToSNode;
struct SNode{
int Data;
PtrToSNode Next;
};
typedef PtrToSNode Position;

struct QNode{
Position Front , Rear;/*队列的头尾指针*/
};
typedef struct QNode *Queue;

/*创建一个空队列*/
Queue CreateQueue(){
Queue Q = (QNode*)malloc(sizeof(QNode));
Q->Front = Q->Rear = (Position)malloc(sizeof(struct SNode));
Q->Front->Next = NULL;
return Q;
}

/*入队操作*/
bool AddQ(Queue Q , int X){
Position q;
q = (Position)malloc(sizeof(struct SNode));
q->Data = X;
Q->Rear->Next = q;
Q->Rear = q;
printf("元素%d入队成功!\n",X);
return true;
}

/*判断一个队列是否为空*/
bool isEmpty(Queue Q){
return (Q->Front == NULL);
}

#define ERROR -1
/*出队操作*/
int DeleteQ(Queue Q){
Position FrontCell;
int FrontElem;

if(isEmpty(Q)){
printf("队列已空,无法进行出队操作!\n");
return ERROR;
}

else{
FrontCell = Q->Front->Next;
if(Q->Front->Next == Q->Rear){
Q->Front = Q->Rear = NULL;
}
else{
Q->Front->Next = FrontCell->Next;
}
FrontElem = FrontCell->Data;
free(FrontCell);
return FrontElem;
}
}

int main(){

Queue Q;
Q = CreateQueue();
AddQ(Q,1);
AddQ(Q,2);
AddQ(Q,3);

printf("\n");

while(!isEmpty(Q)){
int n;
n = DeleteQ(Q);
printf("元素%d已成功出队!\n",n);
}
return 0;
}结果如图:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构