C++学习:第四章数据结构和算法 - (三)链表、栈、队列

链表

代码整理

List.h
#include <iostream>
using namespace std;

#ifndef LIST_H
#define LIST_H 1 //这个 1 可有可无

class List{

typedef int T;

struct Node{
		int data;
		Node* next;
		Node(const T& data = T(), Node* next = NULL);
	};

	Node* head;//头指针,用来保存头结点的地址
	int len;
public:
	List();
	List& push_front(const T& d);
	List& push_back(const T& d);
	Node*& getPtr(int pos);
	int size()const;
	void insert(const T& d, int pos);
	void erase(int pos);
	void remove(const T& d);
	int find(const T& d);
	void travel()const;
	void set(const T& d, int pos);
	bool empty()const;
	T front()const;
	T back()const;
	void clear();
	~List();
};


#endif
List.cpp
#include "List.h"

List::Node::Node(const T& data/* = T()*/, Node* next /*= NULL*/)
			:data(data),next(next){}

List::List():head(NULL),len(0){}

List& List::push_front(const T& d){//前插

		//等效写法
		//Node* p = new Node(d);
		//p->next = head;
		//head = p;	

		insert(d, 0);
		return *this;
	}

List& List::push_back(const T& d){//后插
		insert(d, size());
		return *this;
	}

List::Node*& List::getPtr(int pos){//找到链表中只想指定位置的指针
		if (pos<0 || pos > size())
			pos = 0;
		if (pos == 0)
			return head;
		Node* p = head;
		for(int i=1; i<pos; i++)
			p = p->next;
		return p->next;
	}

int List::size()const{//链表长度
		//int cnt = 0;
		//Node* p = head;
		//while(p->next){
		//	++cnt;
		//	p = p->next;
		//}
		//return cnt;
		return len;
	}

void List::insert(const T& d, int pos){//在任意位置插入
		//pn = p.next 地址
		Node*& pn = getPtr(pos);
		Node* p = new Node(d);
		//新元素的下一个元素指向原 p.next 指向的元素
		p->next = pn;
		//原来的 p.next 指向新元素
		pn = p;
		++len;
	}

void List::erase(int pos){//按位置擦除
		if(pos<0 || pos >len)
			return ;
		Node*& pn = getPtr(pos);
		Node* p = pn;
		pn = pn->next;
		delete p;
		-- len;
	}

void List::remove(const T& d){//按值擦除
		int pos;
		while((pos=find(d))!=-1)
			erase(pos);
	}

int List::find(const T& d){//按值查找
		int pos = 0;
		Node* p = head;
		while(p){
			if (p->data)
				return pos;
			p = p->next;
			++pos;
		}
		return -1;
	}

void List::travel()const{//遍历
		Node* p = head;
		while(p!=NULL){
			cout << p->data << ' ';
			p = p->next;
		}
		cout << endl << endl;
	}

void List::set(const T& d, int pos){//按位置修改
		if(pos<0 || pos >len)
			return ;
		getPtr(pos)->data = d;
	}

bool List::empty()const{//判断为空
		return head==NULL;
	}

 List::T List::front()const{//输出首值
		if(empty())
			throw "empty";
		return head->data;
	}

 List::T List::back()const{//输出末值
		if(empty())
			throw "empty";
		Node* p = head;
		while (p->next != NULL)	{
			p = p->next;
		}
		return p->data;
	}

void List::clear(){//清空
		while(head!=NULL){
			Node* p = head->next;
			delete head;
			head = p;
		}
	}

 List::~List(){
		clear();
	}
main.cpp
#include "List.h"

int main (){
	
	List l;
	cout << "sizeof(l1) = " << sizeof(l) << endl;
	
	List l_1;
	l_1.push_front(1);
	l_1.push_front(2);
	l_1.push_front(3);
	l_1.push_front(4);
	l_1.travel();

	List l_2;
	l_2.push_front(1).push_front(2).push_front(3).push_front(4);
	l_2.travel();


	getchar();
	return 0;
}

数组优势:随机访问读写
链表优势:频繁的插入和删除数据

#include <iostream>
#include <string>
using namespace std;

typedef string T;

class Stack{
	T a[5];
	int cur;
public:
	Stack():cur(0){}
	Stack& push(const T& d)throw(const char*);//数据入栈成为栈顶
	T pop()throw(const char*);//栈顶数据出栈,下一个数据成为栈顶
	const T&top()const;//取出栈顶数据
	bool empty()const{return cur==0;};//是否空栈
	bool full()const{return cur==5;};//是否已满
	void clear(){cur=0;};//栈清空(复位 )
	int size()const{return cur;};//栈中数据个数

};

Stack& Stack::push(const T& d)throw(const char*){
	if (full())	{
		throw "man le";	}
	a[cur++] = d;
	return *this;
}

T Stack::pop()throw(const char*){
	if (empty()){
		throw "kong le";}
	return a[--cur];
}

const T& Stack::top()const throw(const char*){
	if (empty()){
		throw "kong le";}
	return a[cur-1];
}



int main (){

	Stack s;
	s.push("1").push("2").push("3").push("4").push("5");

	while(!s.empty()){
		cout << s.pop() << endl;
	}

	getchar();
	return 0;
}

队列

#include <iostream>
#include <string>
using namespace std;

typedef int T;

class Quene{
	//队列:单边进出。
	T a[5];
	int b, n;//队首位置和有效元素个数
public:
	Quene():b(0),n(0){}
	Quene& push(const T& d)throw(const char *){
		if (full())	{
			throw "man le";	}
		a[(b+n++)%5] = d;
		return *this;
	}
	T pop()throw(const char *){
		if (empty())	{
			throw "kong le";	}
		--n;
		return a[(b++)%5];
	}
	const T& front(){
		return a[(b)%5];
	}
	const T& back(){
		return a[(b+n-1)%5];
	}
	int size(){
		return n;
	}
	void clear(){
		b=0;n=0;
	}
	bool empty(){
		return n==0;
	}
	bool full(){
		return n==5;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值