《数据结构与算法分析》Chap3

3.1 PrintList   编译器vs2013    基于STL

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


int _tmain(int argc, _TCHAR* argv[])
{
	list<int> m_list;
	m_list.push_back(2);
	m_list.push_back(3);
	m_list.push_back(7);
	list<int>::iterator it;
	for (it = m_list.begin(); it != m_list.end(); it++)
		cout << *it << "  ";
	system("pause");
	return 0;
}

《数据结构与算法分析》Chap3 3.6

<pre name="code" class="cpp">/*《数据结构与算法分析》Chap3 3.6*/
#include "stdafx.h"
#include <iostream>
#include<list>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	struct poly{    //结构体定义多项式
		int coff;   //系数
		int power;  //指数
	};
	/*list1初始化,定义多项式1*/
	list<poly> list1;  
	list1.push_back({ 1, 1 });   list1.push_back({ 2, 5 }); list1.push_back({ 3, 9 });
	/*list2初始化,定义多项式2*/
	list<poly> list2;
	list2.push_back({ 1, 0 });  list2.push_back({ 3, 5 }); 
	list<poly> listadd;

	list<poly>::iterator p;  list<poly>::iterator q;
	p = list1.begin();   q = list2.begin();

	while(p!= list1.end()&&q!=list2.end()){
		if (p->power == q->power){ listadd.push_back({ p->coff + q->coff, p->power }); p++; q++; }
		else if (p->power < q->power)  { listadd.push_back({ p->coff, p->power });  p++; }
		else { listadd.push_back({ q->coff, q->power }); q++; }
	}
	if (p != list1.end()){
		for (; p != list1.end(); p++)    listadd.push_back({ p->coff, p->power });
	}
	else if (q != list2.end()){
		for (; q != list2.end(); q++)    listadd.push_back({ q->coff, q->power });
	}
	for (p = listadd.begin(); p != listadd.end(); p++)
		cout << p->coff << " " << p->power << endl;
	system("pause");    //等待输入
	return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值