Data Structure ---- linked list queue

Source: Internet
Author: User
Tags pop value

Using the C ++ one-way linked list to implement data structure queue is basically the same as the previous article. It is only inserted at the end of the linked list during insertion, and the elements are the same. They are all taken from the header.

 

# Pragma once # include "stdio. h" // use a linked list to implement queues. first-in-first-out class queue {public: queue (void); queue (int value );~ Queue (void); private: int m_value; queue * m_pnext; public: void push (int value); bool pop (int * value); bool top (int * value ); bool empty (); int size (); void output (); void destroy () ;};# include "stdafx. h "# include" queue. h "// construct an empty queue header pointer queue: queue (void) {m_value = 0x00; m_pnext = NULL;} // construct a queue node queue :: queue (int value) {m_value = value; m_pnext = NULL;} // output the deleted node queue ::~ Queue (void) {printf ("destroy node its value = % d \ n", m_value);} // element in queue void queue: push (int value) {queue * pnode = this; while (pnode-> m_pnext! = NULL) {pnode = pnode-> m_pnext;} queue * newnode = new queue (value); pnode-> m_pnext = newnode; m_value ++ ;} // element output queue bool queue: pop (int * value) {bool result = false; if (m_pnext! = NULL) {* value = m_pnext-> m_value; m_pnext = m_pnext-> m_pnext; result = true; m_value --;} return result ;} // obtain the bool queue: top (int * value) {bool result = false; if (m_pnext! = NULL) {* value = m_pnext-> m_value; result = true;} return result;} // determines whether the queue is empty bool queue: empty () {bool result = false; if (m_pnext = NULL) {result = true;} return result;} // get the queue size int queue: size () {return m_value ;} // element void queue in the output queue: output () {queue * pnode = this; while (pnode-> m_pnext! = NULL) {printf ("index = % d \ n", pnode-> m_pnext-> m_value); pnode = pnode-> m_pnext ;}// destroy the queue void queue:: destroy () {while (m_pnext! = NULL) {queue * pnode = m_pnext; m_pnext = m_pnext-> m_pnext; delete pnode ;}# pragma once # include "stdio. h "// use a linked list to implement queues. first-in-first-out class queue {public: queue (void); queue (int value );~ Queue (void); private: int m_value; queue * m_pnext; public: void push (int value); bool pop (int * value); bool top (int * value ); bool empty (); int size (); void output (); void destroy () ;};# include "stdafx. h "# include" queue. h "// construct an empty queue header pointer queue: queue (void) {m_value = 0x00; m_pnext = NULL;} // construct a queue node queue :: queue (int value) {m_value = value; m_pnext = NULL;} // output the deleted node queue ::~ Queue (void) {printf ("destroy node its value = % d \ n", m_value);} // element in queue void queue: push (int value) {queue * pnode = this; while (pnode-> m_pnext! = NULL) {pnode = pnode-> m_pnext;} queue * newnode = new queue (value); pnode-> m_pnext = newnode; m_value ++ ;} // element output queue bool queue: pop (int * value) {bool result = false; if (m_pnext! = NULL) {* value = m_pnext-> m_value; m_pnext = m_pnext-> m_pnext; result = true; m_value --;} return result ;} // obtain the bool queue: top (int * value) {bool result = false; if (m_pnext! = NULL) {* value = m_pnext-> m_value; result = true;} return result;} // determines whether the queue is empty bool queue: empty () {bool result = false; if (m_pnext = NULL) {result = true;} return result;} // get the queue size int queue: size () {return m_value ;} // element void queue in the output queue: output () {queue * pnode = this; while (pnode-> m_pnext! = NULL) {printf ("index = % d \ n", pnode-> m_pnext-> m_value); pnode = pnode-> m_pnext ;}// destroy the queue void queue:: destroy () {while (m_pnext! = NULL) {queue * pnode = m_pnext; m_pnext = m_pnext-> m_pnext; delete pnode ;}}


Main program test

 

// myqueue.cpp : Defines the entry point for the console application.  //   #include "stdafx.h"  #include "queue.h"   int _tmain(int argc, _TCHAR* argv[]) {     queue myqueue;     for(int i=0; i<10; i++)     {         myqueue.push(i * 10);     }      myqueue.output();     printf("queue size=%d\n", myqueue.size());      if (myqueue.empty())     {         printf("queue is empty\n");     }     else      {         printf("queue is not empty\n");     }      myqueue.destroy();      int value = 0;     for(int i=0; i<10; i++)     {         if (myqueue.pop(&value))         {             printf("pop value=%d successfully\n", value);         }         else         {             printf("pop unsuccessfully\n");         }     }      printf("queue size=%d\n", myqueue.size());      if (myqueue.empty())     {         printf("queue is empty\n");     }     else      {         printf("queue is not empty\n");     }      getchar();     return 0; } // myqueue.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "queue.h"int _tmain(int argc, _TCHAR* argv[]){ queue myqueue; for(int i=0; i<10; i++) {  myqueue.push(i * 10); } myqueue.output(); printf("queue size=%d\n", myqueue.size()); if (myqueue.empty()) {  printf("queue is empty\n"); } else {  printf("queue is not empty\n"); } myqueue.destroy(); int value = 0; for(int i=0; i<10; i++) {  if (myqueue.pop(&value))  {   printf("pop value=%d successfully\n", value);  }  else  {   printf("pop unsuccessfully\n");  } } printf("queue size=%d\n", myqueue.size()); if (myqueue.empty()) {  printf("queue is empty\n"); } else {  printf("queue is not empty\n"); } getchar(); return 0;} 

 

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.