Class notes: stacks and queues

The logical structure of the stack

Stack: A linear table limited to insert and delete operations only at the end of the table.
Empty stack: a stack without any data elements.
The end that allows insertion and deletion is called the top of the stack, and the other end is called the bottom of the stack.
The operating characteristics of the stack: last in first out.
Note: The stack only restricts the location of table insert and delete operations, and does not limit the time for insert and delete operations.
Sequential storage structure and implementation
of the stack The sequential storage of the stack is realized using an array, which end of the array is used to indicate the bottom of the stack, and a pointer top is attached to indicate the position of the top element of the stack in the array.
Into the stack: top plus 1, out of the stack: top minus 1, stack empty: top = -1, full stack: top = MAX_SIZE-1.
Two-stack shared space (dual-end stack)
Two-stack shared space: use an array to store two stacks, let the bottom of one stack be the beginning of the array, the bottom of the other stack be the end of the array, two stacks Extend from each end to the middle.
Insert picture description here
Stack1 is empty when top1 = -1, stack2 is empty when top2 = Stack_Size, and stack is full when top1 + 1 = top2.
Stack link storage structure and implementation
Link stack: stack link storage structure.
The chain head is used as the top of the stack for easy operation, and the chain stack does not need to be attached with a head node.
The implementation of the chain stack-insert (stack)

template <class T>
void LinkStack<T>::Push(T x)
{
     s=new Node<T>;     
     s->data=x;      
     s->next=top;     
     top=s;
}

Chain stack implementation-delete (out of stack)

template <class T>
T LinkStack<T>::Pop( ) 
{
     if(top==NULL)
         throw "下溢";
     x=top->data;      
     p=top;      
     top=top->next;        
     delete p;     
     return x;
}

The realization of the chain stack-the destruction of the chain stack (the destruction of the chain stack)

template <class T>
LinkStack<T>::~LinkStack( ) 
{
     while(top)  
     {
         Node<T> *p;   
         p=top->next;           
         delete top;   
         top=p; 
     }
}

Comparison of sequential stack and chain stack
Time performance: the same, both are constant time O (1).
Space performance:
Sequential stack: There is a limit to the number of elements and the problem of wasted space.
Chain stack: There is no problem of stack full. The stack is full only when there is no available space in the memory, but each element requires a pointer field, which creates structural overhead.
Conclusion: When the number of elements changes greatly during the use of the stack, it is appropriate to use a chain stack, otherwise, a sequential stack should be used.

The logical structure of the queue

Queue: A linear table that allows only insert operations on one end and delete operations on the other end.
Empty queue: a queue that does not contain any data elements.
The end that allows insertion (also called enqueuing and entering the team) is called the tail of the team, and the end that allows deletion (also called out of the team) is called the head of the team.
Operational characteristics of the queue: First-in-first-out (FIFO, LILO)
sequential storage structure and implementation of the
queue. Sequential queue: the sequential storage structure of the queue
sets two pointers, the head of the queue and the tail of the queue. The queue head pointer points to the element before the first element in the queue, and the queue head pointer points to the last element in the queue. 2. The team head pointer points to the first element in the queue, and the team tail pointer points to the position after the last element in the queue.
False overflow: When the element is inserted into the position of the largest index in the array, the queue space is exhausted, although there is still free space at the lower end of the array at this time, this phenomenon is called false overflow.
The method to solve the false overflow: circular queue: connect the array of storage queue head to tail.
Realize circular queue: There
is no physical circular structure, and it is realized by software method.
Find the modulus: rear = (rear + 1)% MAXSIZE
front = (front + 1)% MAZSIZE

队空:front==rear
队满的条件:(rear+1) % QueueSize==front

The realization of circular queue-enqueue

template <class T> 
void CirQueue<T>::EnQueue(T x) 
{
     if ((rear+1) % QueueSize ==front) throw "上溢";
     rear=(rear+1) % QueueSize;        
     data[rear]=x;
}

The realization of circular queue-dequeue

template <class T>
T CirQueue<T>::DeQueue( ) 
{
     if (rear==front) throw "下溢";
     front=(front+1) % QueueSize;       
     return data[front]; 
} 

The realization of circular queue-read the head element

template <class T>
T CirQueue<T>::GetQueue( )
{
     if (rear==front) throw "下溢";      
     i=(front+1) % QueueSize;       
     return data[i]; 
}

The realization of circular queue-queue length

template <class T>
int CirQueue<T>::GetLength( )
{
     if (rear==front) throw "下溢";      
     len=(rear-front+ QueueSize) % QueueSize;       
     return len; 
}

Queue's linked storage structure and implementation
Chain Queue: Queue's linked storage structure
Chain Queue's implementation-constructor

template <class T>
LinkQueue<T>::LinkQueue( )
{
     front=new Node<T>;       
     front->next=NULL;        
     rear=front;
}

Implementation of Chain Queue-Enqueue

template <class T>
void LinkQueue<T>::EnQueue(T x)
{
     s=new Node<T>;      
     s->data=x;      
     s->next=NULL;     
     rear->next=s;      
     rear=s;
}

Implementation of Chain Queue-Dequeue

template <class T>
T LinkQueue<T>::DeQueue( )
{
       if (rear==front) throw "下溢";      
       p=front->next;       
       x=p->data;       
       front->next=p->next;              
       delete p;      
       if (front->next==NULL) rear=front;         
       return x;
}

Comparison of circular queue and chain queue
Time performance: The basic operations of circular queue and chain queue require constant time O (1).
Space performance:
circular queue: a fixed length must be determined in advance, so there is the problem of the limit of the number of storage elements and the waste of space.
Chain queue: There is no problem of queue full. The queue is full only when there is no available space in the memory, but each element requires a pointer field, which results in structural overhead.

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/102408419