数组、链表、斐波那契数列的递归、Reverse数组、Reverse链表、约瑟夫环问题、合并两个排好序

stdafx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include "stdlib.h"

// TODO:  在此处引用程序需要的其他头文件
数组
#include "stdafx.h"

using namespace std;
/*int main()
{
	int array[] = { 5,4,10,1,7,9,2 };
	cout << array[4] << endl;
	cout << *(array+4) << endl;
	system("pause");
	return 0;
	
}*/
链表
#include "stdafx.h"
using namespace std;b
struct node
{
	int payload;
	node* next;
};
/*
int main() {
	node* head = nullptr;
	//insert 10 nodes
	for (int i = 0; i< 10; i++)
	{
		//链表头插入
		node* new_node = new node;
		new_node->payload = i * 10;
		new_node->next = head;
		head = new_node;
	}
	//output each node in the linked-list
	node* iterator = head;
	while (iterator) {
		cout << iterator->payload << endl;
		iterator = iterator->next;
	}
	system("pause");
	return 0;
}
*/
斐波那契数列的递归
#include "stdafx.h"
int fib(int n) {
	if (n < 2)
		return n;
	else
		return fib(n - 1) + fib(n - 2);
}
/*
int main() {
	for (int i = 0; i < 10; i++)
	{
		std::cout << fib(i) << std::endl;
	}
	system("pause");
	return 0;
}*/
Reverse数组
#include "stdafx.h"
//using namespace std;
void reverse(int[],int);
void output(int[],int);
/*
int main()
{
	int array[] = { 5,4,10,1,7,9,2 };
	reverse(array,7);
	output(array,7);
	system("pause");
	return 0;

}*/
void reverse(int array[],int length) {
	int left = 0;
	int right = length - 1;
	while (left < right) {
		int temp = array[left];
		array[left] = array[right];
		array[right] = temp;
		left++;
		right--;
	}
}
void output(int array[], int length) {
	for (int i = 0; i < length; i++) {
		std::cout << array[i] << std::endl;
	}
}
Reverse链表
#include "stdafx.h"
using namespace std;
struct node
{
	int payload;
	node* next;
};
//递归转置
node* reverse_recursive(node* head) {
	if (head == nullptr || head->next == nullptr)
		return head;
	node* second = head->next;
	node* new_head = reverse_recursive(second);
	second->next = head;
	head->next = nullptr;
	return new_head;

}
//非递归转置
node* reverse(node* head) {
	if (head == nullptr || head->next == nullptr)
		return head;
	node* p = head->next;
	node* p_previous = head;
	head->next = nullptr;
	while (p!=nullptr){
		node* p_next = p->next;
		p->next = p_previous;
		p_previous = p;
		p = p_next;
	}
	return p_previous;
}	

int main() {
node* head = nullptr;
//insert 10 nodes
for (int i = 0; i< 10; i++)
{
	//链表头插入
	node* new_node = new node;
	new_node->payload = i * 10;
	new_node->next = head;
	head = new_node;
	}
	node* iterator = head;
	while (iterator) {
		cout << iterator->payload << endl;
		iterator = iterator->next;
	}

	head = reverse_recursive(head);

	//output each node in the linked-list
	iterator = head;
	while (iterator) {
		cout << iterator->payload << endl;
		iterator = iterator->next;
	}
	///
	iterator = reverse(head);
	while (iterator) {
		cout << iterator->payload << endl;
		iterator = iterator->next;
	}
	system("pause");
	return 0;
}
约瑟夫环问题

#include "stdafx.h"
#include <iostream>
 struct node
 {
     int payload;
     node* next;
     node(int payload){this->payload=payload;}//构造函数,赋初值
 };

 class joseph_circle{
    node* tail;  //从尾处插入
    node* eliminate_ptr;
    public:
		joseph_circle() { tail = NULL; }//nullptr
        //add a new node to joseph circle
        void add(int value){
            if(tail == NULL){//if the joseph circle is empty,modify tail
                tail = new node(value);
                tail->next = tail;
            }else{//insert new node as the tail of the circle
                node* new_node = new node(value);
                new_node->next = tail->next;
                tail->next =new_node;
                tail = new_node;
            }
        }

        void eliminate(int step){
            node* p = tail;
            while(p!=NULL && p->next != p){
                //while more than one element in the circle
                for(int i=0;i<step-1;i++){
                    p=p->next;
                }
                node* eliminate_node = p->next;//eliminate the next one
                p->next=p->next->next;
                if(eliminate_node==tail)
                    tail = p;//update tail
                std::cout<<"deleting:"<<eliminate_node->payload << std::endl;
                delete eliminate_node;
                output();
            }
        }
        void output(){
            node* p =tail;
            while(p!=NULL){
                p=p->next;
                std::cout<<p->payload << " ";
                if(p == tail)
                    break;
            }
            std::cout << std::endl;
        }
 };
int main(int argc, char const *argv[])
 {
     joseph_circle circle;
     for (int i = 0; i < 6; ++i)
     {
         circle.add(i);
     }
     circle.eliminate(3);//eliminate using a step of 3
	 system("pause");
	 return 0;
 }
合并两个排好序的链表
//#include <iostream>
#include "stdafx.h"
struct node {
	int payload;
	node* next;
	node(int payload) { this->payload = payload; next = nullptr; };
};

class linkedlist {
	node *head, *tail;
public:
	//constructor,initialize head and tail to nullptr
	linkedlist() :head(nullptr), tail(nullptr) {};
	//push a new node at the end of the list
	void push_back(int value) {
		if (empty()) {
			head = tail = new node(value);
		}
		else {
			tail->next = new node(value);
			tail = tail->next;
		}
	}
	//return the value stored in the first node
	int front() {
		if (empty()) {
			throw "This list is empty.";
		}
		return head->payload;
	}
	//remove the first node
	void pop_front() {
		if (empty()) {
			throw "The list is empty.";
		}
		node* first_node = head;
		head = head->next;
		delete first_node;
	}

	bool empty() {
		return head == nullptr;
	}
	void output() {
		node* interator = head;
		while (interator) {
			std::cout << interator->payload << " ";
			interator = interator->next;
		}
		std::cout << std::endl;
	}
};

//merge to sorted linked list,return a new list
linkedlist merge(linkedlist a, linkedlist b) {
	linkedlist result;
	while (!a.empty() || !b.empty()) {
		if (a.empty()) {
			result.push_back(b.front());
			b.pop_front();
		}
		else if (b.empty()) {
			result.push_back(a.front());
			a.pop_front();
		}
		else if (a.front() > b.front()) {
			result.push_back(b.front());
			b.pop_front();
		}
		else {
			result.push_back(a.front());
			a.pop_front();
		}
	}
	return result;
}
int main() {
	linkedlist a, b;
	a.push_back(4); a.push_back(7); a.push_back(20);
	b.push_back(2); b.push_back(3); b.push_back(9); b.push_back(29);
	a.output();
	b.output();
	linkedlist result = merge(a, b);
	result.output();
	system("pause");
	return 0;
}

更多内容请关注微信公众号:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值