⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list.cpp

📁 一些C++的课件和实验源代码
💻 CPP
字号:
// list.cpp: implementation of the list class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "list.h"
#include <iostream>
using namespace std;

//////////////////////////////////////////////////////////////////////
// implementation of the list class.
//////////////////////////////////////////////////////////////////////

list::list()
{
	head = tail = NULL;
}

list::~list()
{
	// 释放资源
	while(head != NULL){
		Node* p = head;
		head = head->next;
		delete p;
		p = NULL;
	}
}

void list::clear()
{
	// 释放资源
	while(head != NULL){
		Node* p = head;
		head = head->next;
		delete p;
		p = NULL;
	}
	head = tail = NULL;
}

bool list::empty()
{
	return head == NULL;
}

void list::print()
{
	Node* p = head;
	while(p != NULL){
		cout << p->value << "  ";
		p = p->next;
	}
	cout << endl;
}

//////////////////////////////////////////////////////////////////////
// implementation of the stack class.
//////////////////////////////////////////////////////////////////////
int stack::pop()
{
	if(head == NULL){
		return 0;
	}
	else{
		int value = head->value;	// 获得栈顶元素
		Node* p = head;				// 栈顶节点指针
		head = head->next;			// 栈顶指向下一个
	
		delete p;					// 释放栈顶节点
		p = NULL;

		return value;
	}
}

void stack::push(int value)
{
	// 新分配节点
	Node *pnode = new Node;		
	pnode->value = value;	

	// 将节点插入到栈顶
	pnode->next = head;
	head = pnode;
}

//////////////////////////////////////////////////////////////////////
// implementation of the queue class.
//////////////////////////////////////////////////////////////////////
int queue::outqueue()
{
	if(head == NULL){
		return 0;
	}
	else{
		int value = head->value;	// 获得栈顶元素
		Node* p = head;				// 栈顶节点指针
		head = head->next;			// 栈顶指向下一个
	
		delete p;					// 释放栈顶节点
		p = NULL;

		return value;
	}	
}

void queue::inqueue(int value)
{
	// 新分配节点
	Node *pnode = new Node;		
	pnode->value = value;	
	pnode->next = NULL;
	
	if(head == NULL){	// 原队列为空
		head = tail = pnode;
	}
	else{	// 原队列不空,则添加到队列尾
		tail->next = pnode;
		tail = pnode;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -