Queue array and linked list implementation

Source: Internet
Author: User

1. the queue is the first-in-first-out data structure, which can be implemented using arrays or linked lists. When using an array to implement a linked list, you need to pre-allocate the size of the array. The front and rear subscripts are used to indicate the subscript of the element at the head and end of the team respectively. When inserting an element, add 1 to the subscript rear at the end of the team. When an element is deleted, add 1 to the front subscript to determine whether the queue is empty, as long as the front and rear are equal. The queue insertion operation can be expressed

# include 
 
  # include 
  
   # define Maxsize  100struct queue {int front;int rear;int q[Maxsize];};int main(){   void initialqueue(queue*);void push(queue *,int);int pop(queue*);int isempty(queue*);int isfull(queue*);queue p;initialqueue(&p);push(&p,1);push(&p,2);push(&p,3);    printf("%d\n",isempty(&p));printf("%d\n",pop(&p));system("pause");return 0;}void initialqueue(queue *p){p->front=p->rear=0;}int isfull(queue *p){return p->rear==p->front+Maxsize-1;}int isempty(queue *p){return p->front==p->rear;}void push(queue* p,int x){if(isfull(p)){printf("queue is full\n");exit(-1);}p->q[p->rear++]=x;}int pop(queue *p){if(isempty(p)){printf("queue is empty\n");exit(-1);}return p->q[p->front++];}
  
 

2. Implement queue through linked list

To implement a queue using a linked list, you need to define the value and pointer of the node struct storage node. In addition, you also need to define the header pointer and tail pointer, pointing to the head and end of the team respectively. The insert element operation can be expressed

# include 
 
  # include 
  
   struct node{int num;node *next;};typedef struct {node *front;node *rear;}link;int main(){void initialqueue(link p);link push(link,int);int pop(link);int isempty(link);int isfull(link);link p={0,0};initialqueue(p);p=push(p,1);p=push(p,2);p=push(p,3);printf("%d\n",pop(p));system("pause");return 0;}void initialqueue(link p){p.front=p.rear=0;}int isempty(link p){return p.front==NULL&&p.rear==NULL;}link push(link p,int x){node *t=(node *)malloc(sizeof(node));t->num=x;t->next=NULL;if(isempty(p)){p.front=p.rear=t;}else{p.rear->next=t;p.rear=t;}return p;}int pop(link p){int temp;node *t;if(isempty(p)){printf("queue is empty\n");exit(-1);}if(p.front!=p.rear){temp=p.front->num;t=p.front;p.front=p.front->next;free(t);}else{temp=p.front->num;t=p.front;p.front=p.rear=NULL;free(t);}return temp;}
  
 



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.