Review three data structures: stacks and queues

The following briefly describes the queue stack and
the stack (Stack) is a last in first out (LIFO) linear form, only the end of the table (i.e., top of the stack insertion or deletion), like the head into the tail of the train schedule.

Here Insert Picture Description
Queue (Queue) opposite to the stack, a first in first out (FIFO) linear form, insertion only at one end and at the other end of the deletion, insertion end allowing called the tail , allowing one end called the deleted as the team head , just line up daily life.

Here Insert Picture Description

Achieve representation and operation of the stack

Based on the knowledge already learned, we can use sequential storage structure and chain storage structure two storage structure to achieve stack, named sequence of stack, stack chain.

Stack order

Sequence of stack data field includes the stack, the stack pointer to the end of top, base and maximum capacity STACKSIZE stack, involving deletions order to find space on the change operation and the like, to be noted that the stack pointer move correctly; data out of the stack operation, data has not been deleted, but remained in the original space, etc. are covered under when incoming operation.
Specific algorithm slightly
below mainly to see the link stack:

Chain stack

Storage structure:

typedef struct StackNode{
    char data;    
    struct StackNode *next;
    }StackNode,*LinkStack;

Because the main stack operation is insertion and deletion in the stack, as the stack apparently linked list head is the most convenient, but not necessary the same as a single linked list for ease of operation and attaches a header node.

1, the initialization
configuration of a blank stack, the first node because there is no direct stack pointer blanking to
2, the stack
algorithm steps:
1. Drawing element e is allocated space, pointer p pointing to
2, the new node data field is set to E
. 3, the new node is inserted into the stack
4, stack pointer modify a p-
code implementation:

void Push(LinkStack &s,int e){
    LinkStack p=new StackNode;    
    p->data=e;
    p->next=s;    
    s=p;
    }

相当于把前面的元素都往栈里压
3、出栈
算法步骤:
1、判断是否栈空
2、将栈顶元素赋给e(不需要返回时可省略此步)
3、临时保存栈顶元素的空间,以备释放
4、修改栈顶指针,指向新的栈顶元素
5、释放原栈顶元素的空间
代码实现:

void Pop(LinkStack &s){
    if(!s){        
    cout<<"There's Nothing to Pop!\n";        
    return;        
    }    
    LinkStack p=s;    
    s=s->next;    
    delete p;
    }

这个比较好理解
4、取栈顶元素
直接返回栈顶指针s数据域,注意判断栈空

队列的表示和操作的实现

队列也有顺序和链式两种存储表示

顺序队列

存储结构是基地址base以及头尾指针front和rear(假装是指针,其实用作数组下标)
初始front=rear=0
进队rear++
出队front–
front始终指向队列头元素,rear始终指向队尾元素的下一个位置
头尾指针相同时队空
由于这种操作会造成“假溢出”(空间未占满,但rear已至base尾部,新元素无法入队)的问题,所以更提倡循环队列;用模运算来解决新增元素位置的问题。

如何区别循环队列满还是空?
1、牺牲一个元素空间,即队列空间大小为m时,有m-1个元素就认为队满,这样头尾指针相同时队空,而尾指针在循环意义上加1后等于头指针,则认为队满
此时队空条件:Q.front==Q.rear;
队满条件:(Q.rear+1)%maxqsize=Q.front
2、另设标志位区分队空还是队满

Code implements a circular queue Note:
1, the initial spatial distribution note leave space allocation failure outlet
2, when using the required length (Q.rear-Q.front + maxqsize)% maxqsize, plus prevent a negative maxqsize

Chain team

Storage structure:

typedef struct QNode{
    int data;    
    struct QNode *next;
    }QNode,*QueuePtr;
typedef struct{
    QueuePtr front;    
    QueuePtr rear;
}LinkQueue;

Initial front HOL queue tail rear common junction point, the pointer field is left blank, the subsequent elements enqueued, always points to the tail element rear, note the release of the original space occupied by the head elements dequeue.

------------------------------------
Note: The contents of this article are all from the "data structure (C language second Edition) "(Yan Wei Min teacher with)
pictures from Baidu

Released six original articles · won praise 0 · Views 137

Guess you like

Origin blog.csdn.net/Roy_F_M/article/details/103833289