"Westward data structure" study notes three

Chapter 4 stacks and queues

Stack table is defined linear only insertion and deletion operations in the trailer.
Queue insertion operation is only allowed at one end and at the other end of the linear table delete operation.

Defined stack

Stack (Stack) is a table defining a linear only insertion and deletion operations in the trailer.

We stack allows insertion and deletion of one end called stack (top), and the other end is called the stack bottom (bottom), without any data elements called empty stack. Also known LIFO stack (Last In First Out) linear form, referred LIFO structure.

Stack insert operation, called into the stack, also known as push, push.
Stack delete operation, called a stack, and some called popping.

Push the popped variant

More than the number of elements, the order of the stack there are multiple possible.

Stack abstract data types

ADT 栈
Data
  同线性表。元素具有相同的类型,相邻元素具有前驱和后继关系。
Operation
  InitStack(*S):初始化操作,建立一个空栈S。
  DestroyStack(*S):若栈存在,则销毁它。
  ClearStack(*S):将栈清空。
  StackEmpty(S):若栈为空,返回 true ,否则返回 false。
  GetTop(S,*e):若栈存在且非空,用e返回S的栈顶元素。
  Push(*S,e):若栈S存在,插入新元素e到栈S中并成为栈顶元素。
  Pop(*S,e):删除栈S中的栈顶元素,并用e返回其值。
  StackLength(S):返回栈S的元素个数。
endADT

Sequential storage stack structure and implemented

Sequential storage structure stack

Stack structure definition

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

Sequentially storing stack structure - onto the stack.

Push operation push, which code is as follows:

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

Sequential storage structure stack - the stack operation

Pop operations pop, which code is as follows:

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

The push and pop without involving any loop, thus are time complexity O (1).

Stack two shared space

The use of such a data structure, usually when there are two stacks of space requirements opposite relationship, that is, another stack in the case of a shortened when the stack grows.

Storage Structure and realization of the stack

Stack of linked storage structure

Storage Structure of the stack, referred to as link stack.
Link stack structure of the code is as follows:

typedef struct StackNode
{
  SElemType data;
  struct StackNode *next;
}StackNode, *LinkStackPtr;

typedef struct LinkStack
{
  LinkStackPtr top;
  int count;
}

Storage Structure stack - onto the stack.

/* 插入元素 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;
}

Storage Structure stack - pop operation

Assume that the variable p is used to stack storage nodes to be removed, the stack pointer down one of the last to release p.

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;
}

The link stack and push the stack into the stack pop operation has no cycle of operation, are time complexity O (1).
If the stack during use of the element changes unpredictable, sometimes very small, sometimes very large, it is best to use the link stack, whereas, if it changes in the controllable range, use the sequence of stack would be better.

The role of the stack

The introduction of the stack simplifies the problem of programming, the division of the different levels of attention, so that narrow thinking, more focused on the core issues we have to solve.

Stack of applications - recursion

Fibonacci cut the number of columns to achieve

int Fbi (int i)
{
  if (i < 2)
    return i == 0 ? 0 : 1;
  return Fbi(i-1) + Fbi(i-2);
}
int main()
{
  int i;
  for (int i = 0;i < 40; i++)
    printf("%d ", Fbi(i));
  return 0;
}

Recursive definition

In high-level languages, different calls and other functions themselves are not essential. We own a direct call or call your own function indirectly by calling a series of statements, called recursive functions.
Each recursive definition must have at least one condition, no longer meet the recursive, that is no longer refer to themselves but to return value to exit.

Stack of applications - four arithmetic expression evaluation

  1. Infix expressions into postfix (out of the stack to signed operation).

  2. The postfix expression calculates the outcome (and out of the stack to the digital operation).

The definition of the queue

Queue (Queue) is allowed only in the insertion end and at the other end of the linear table delete operation.

A queue is a FIFO (First In First Out) linear form, referred to as FIFO. Permit insertion end is called the tail, allowed to delete called queue head end.

Queue abstract data types

ADT 队列(Queue)

Data
  同线性表。元素具有相同的类型,相邻元素具有前缀和后继关系。
Operation
  InitQueue(*Q):初始化操作,建立一个空队列Q。
  DestroyQueue(*Q):若队列Q存在,则销毁它。
  ClearQueue(*Q):将队列Q清空。
  QueueEmpty(*Q):若队列Q为空,返回 true,否则返回 false。
  GetHead(Q,*e):若队列Q存在且非空,用e返回队列Q的队头元素。
  EnQueue(*Q,e):若队列Q存在,插入新元素e到队列Q中并成为队尾元素。
  DeQueue(*Q,*e):删除队列Q中队头元素,并用e返回其值。
  QueueLength(Q):返回队列Q的元素个数。
endADT

Circular queue

Cyclic queue is defined

We call this the head and tail of the queue sequentially storing contact circular queue structure is referred to.

Storage Structure and the queue

Storage Structure queue, in fact, single-chain linear form, but it can only head out into the end of it, we call it the chain queue.
Chain configuration queue is:

typedef int QElemType;

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

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

Storage Structure queue - enqueue operation

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;
}

Storage Structure queue - dequeue

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;
}

In case you can determine the maximum queue length is recommended to use a circular queue, if you can not predict the length of the queue, the queue with the chain.

Published 10 original articles · won praise 1 · views 20000 +

Guess you like

Origin blog.csdn.net/pass7580/article/details/104544729