数据结构----链表队列
利用C++ 单向链表实现数据结构队列,其实和上一篇基本内容相同,仅仅是插入的时候在链表的尾部插入,取元素都是一样的,都从头部取。
#pragma once #include "stdio.h" //利用链表来实现队列,先进先出 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" //构造一个空的队列头指针 queue::queue(void) { m_value = 0x00; m_pnext = NULL; } //构建一个队列结点 queue::queue(int value) { m_value = value; m_pnext = NULL; } //输出被删除掉的结点 queue::~queue(void) { printf("destroy node its value=%d\n", m_value); } //元素入队列 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++; } //元素出队列 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; } //得到队列顶部的元素 bool queue::top(int *value) { bool result = false; if (m_pnext != NULL) { *value = m_pnext->m_value; result = true; } return result; } //判断队列是否为空 bool queue::empty() { bool result = false; if (m_pnext == NULL) { result = true; } return result; } //得到队列大小 int queue::size() { return m_value; } //输出队列中的元素 void queue::output() { queue* pnode = this; while(pnode->m_pnext != NULL) { printf("index=%d\n", pnode->m_pnext->m_value); pnode = pnode->m_pnext; } } //销毁队列 void queue::destroy() { while(m_pnext != NULL) { queue* pnode = m_pnext; m_pnext = m_pnext->m_pnext; delete pnode; } } #pragma once #include "stdio.h" //利用链表来实现队列,先进先出 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" //构造一个空的队列头指针 queue::queue(void) { m_value = 0x00; m_pnext = NULL; } //构建一个队列结点 queue::queue(int value) { m_value = value; m_pnext = NULL; } //输出被删除掉的结点 queue::~queue(void) { printf("destroy node its value=%d\n", m_value); } //元素入队列 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++; } //元素出队列 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; } //得到队列顶部的元素 bool queue::top(int *value) { bool result = false; if (m_pnext != NULL) { *value = m_pnext->m_value; result = true; } return result; } //判断队列是否为空 bool queue::empty() { bool result = false; if (m_pnext == NULL) { result = true; } return result; } //得到队列大小 int queue::size() { return m_value; } //输出队列中的元素 void queue::output() { queue* pnode = this; while(pnode->m_pnext != NULL) { printf("index=%d\n", pnode->m_pnext->m_value); pnode = pnode->m_pnext; } } //销毁队列 void queue::destroy() { while(m_pnext != NULL) { queue* pnode = m_pnext; m_pnext = m_pnext->m_pnext; delete pnode; } }
主程序测试
// 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; }
补充:软件开发 , C++ ,