Getting started with data structures-stacks and queues

Source: Internet
Author: User

Stack and queue are two important data structures, which have a wide range of applications, and they can be modified by restricting the function of the list. Stack is a kind of advanced post-out (FILO) data structure, can only be added at one end to delete, and the queue is a first-out (FIFO) data structure, can only join, the other end can only be deleted.


The implementation of the stack:

# include <stdio.h># include <malloc.h># include <stdlib.h>typedef struct node{int data;struct Node * PN ext;} NODE, * pnode;typedef struct stack{pnode ptop; Pnode Pbottom;}  STACK, * pstack;//function declaration void Init_stack (Pstack);  Create an empty stack void Push_stack (pstack, int);  pressure stack void Traverse_stack (const pstack);  Traverse output bool Empty_stack (pstack);  Determine if the stack is empty bool Pop_stack (pstack, int *);  Out of stack bool Clear_stack (pstack); empties the stack int main (void) {stack s;int val;init_stack (&s);p ush_stack (&s, 1);p Ush_stack (&s, 2);p Ush_stack ( &s,;p ush_stack (&s, 499);p ush_stack (&s, Ush_stack);p &s (134), Traverse_stack (&s);p Op_ Stack (&s, &val);/*if (Pop_stack (&s, &val))//If the IF statement is written here, if the judgment has been executed once, that is, the stack once, will cause a bug. {printf ("The stack succeeds, the stack's element value is:%d\n", Val);} Else{printf ("Out of Stack failed!") \ n ");} */traverse_stack (&s);p rintf ("Empty stack! \ n "); Clear_stack (&s); Traverse_stack (&s); return 0;} void Init_stack (Pstack pS) {ps->ptop = (pnode) malloc (sizeof (NODE)), if (NULL = = ps->ptop) {printf ("Memory allocation failed! \ n "); exit (-1);} Else{ps->pbottom = Ps->ptop;ps->ptop->pnext = NULL;} return;} void Push_stack (Pstack pS, int val) {Pnode pnew = (pnode) malloc (sizeof (NODE)); if (NULL = = pnew) {printf ("Stack memory allocation failed!") \ n "); return;} Pnew->data = Val;pnew->pnext = Ps->ptop;ps->ptop = Pnew;return;} void Traverse_stack (const pstack pS) {Pnode p = ps->ptop;if (Empty_stack (PS)) {printf ("Output failed, stack is empty! \ n ");} else{printf ("The element in the stack is: \ n"), while (P! = ps->pbottom) {printf ("%d\t", p->data);p = P->pnext;} printf ("\ n");} return;} BOOL Empty_stack (Pstack PS) {if (ps->ptop = = Ps->pbottom) return True;elsereturn false;} BOOL Pop_stack (pstack PS, int * pVal) {if (Empty_stack (PS)) {printf ("stack is empty, out of stack failed! \ n "); return false;} Else{pnode p = ps->ptop;*pval = P->data;ps->ptop = p->pnext;printf ("The stack succeeds, the element value of the stack is:%d\n", p->data); free (p );p = Null;return true;}} BOOL Clear_stack (Pstack PS) {if (Empty_stack (PS)) {printf ("stack is empty, cannot be emptied!") \ n "); return false;} Else{pnode p, q;p = Ps->ptop;while (ps->pbottom! = p) {q = P->pnext;free (p);p = q;} Ps->ptop = Ps->pbottom;} return true;}

Loop queue

The sequential queue has a congenital deficiency, that is, the space utilization is not high, will produce a "false overflow" phenomenon, namely: in fact, there is free space in the queue to store elements, but we determine whether the queue has space, the queue tells us that the queue is full, so this overflow is not a real overflow, There is still an empty position in the data array where the element can be placed, so this is a "false overflow";

So we introduced the concept of cyclic queue, figment the sequential queue into a ring of space, that is, the table of storage queue elements logically as a ring, called the Loop queue, which is as follows:


Note: As shown in, our loop queue for the convenience of implementation, there will be a position of idle, M_front (front) pointer always points to a position where the element value is empty, so (m_front+1)%capacity really point to the first element of the team, and M_rear ( The figure is rear) to point to a real presence of the team tail element;

Implementation of the cyclic queue:

# include <stdio.h># include <malloc.h>typedef struct queue{int * pbase;int front;int rear;}  Queue;void Init (QUEUE *);  Queue initialization bool En_queue (queue *, int);  queued void Traverse_queue (queue *);  Traverse queue bool Is_full (queue *);  Determines whether the queue is full bool Is_empty (queue *);  Determines whether the queue is empty bool Out_queue (queue *, int *); Out of line int main (void) {QUEUE q;int val;init (&q); En_queue (&q, 1); En_queue (&q, 2); En_queue (&q, 3); en_queue (&q, 4); En_queue (&q, 5); En_queue (&q, 6); En_queue (&q, 7); En_queue (&q, 8); Traverse_queue (&Q) ; Out_queue (&q, &val); Out_queue (&q, &val); Traverse_queue (&q); return 0;} void Init (QUEUE * pQ) {pq->pbase = (int *) malloc (sizeof (int) *6);p Q->front = 0;pq->rear = 0;} BOOL En_queue (queue * PQ, int val) {if (Is_full (PQ)) {printf ("Queued failed, queue is full!") \ n "); return false;} Else{pq->pbase[pq->rear] = Val;pq->rear = (pq->rear+1)%6;return true;}} BOOL Is_full (QUEUE * PQ) {if ((pq->rear+1)%6 = = Pq->front) return True;elsereturn false;} void Traverse_queue (queue *pq) {int i = pq->front;printf ("queue element: \ n"), while (i! = pq->rear) {printf ("%d", pq->pbase[i ]); i = (i+1)%6;} printf ("\ n");} BOOL Out_queue (queue * PQ, int * pVal) {int val;if (Is_empty (PQ)) {printf ("failed, queue is empty! \ n "); return false;} Else{*pval = Pq->pbase[pq->front];val = Pq->pbase[pq->front];p Q->front = (pQ->front + 1)% 6;printf (" The team succeeds, the element that is out of the team is:%d\n ", Val); return true;}} BOOL Is_empty (QUEUE * PQ) {if (Pq->front = = pq->rear) {return true;} Else{return false;}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Getting started with data structures-stacks and queues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.